Skip to content

Commit

Permalink
feat: 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywoolala committed Jan 23, 2024
0 parents commit ba7aeeb
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules
dist
.wrangler
.dev.vars

# Change them to your taste:
wrangler.toml
package-lock.json
yarn.lock
pnpm-lock.yaml
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
```
npm install
npm run dev
```

```
npm run deploy
```
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"scripts": {
"dev": "wrangler dev src/index.ts",
"deploy": "wrangler deploy --minify src/index.ts"
},
"dependencies": {
"@hono/zod-openapi": "^0.9.5",
"hono": "^3.12.6"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231218.0",
"wrangler": "^3.22.0"
}
}
60 changes: 60 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";

type Bindings = {
OPENAPI_VERSION: string;
TOOL_VERSION: string;
TOOL_NAME: string;
TOOL_DESCRIPTION: string;
};

const app = new OpenAPIHono<{ Bindings: Bindings }>();

app.doc31("/doc", (c) => ({
openapi: c.env.OPENAPI_VERSION,
info: {
version: c.env.TOOL_VERSION,
title: c.env.TOOL_NAME,
description: c.env.TOOL_DESCRIPTION,
},
servers: [{ url: new URL(c.req.url).origin }],
}));

const reqSchema = z.object({
count: z.number().openapi({ example: 1, description: "Number of quotes to get" }),
});

const resSchema = z.object({
result: z.string().openapi({ example: "ok" }),
});

const quoteRoute = createRoute({
method: "post",
path: "/quotes",
summary: "Get quotes from Breaking Bad",
description: "Retrieve quotes from the Breaking Bad series",
operationId: "GetQuotesFromBreakingBad",
request: {
body: {
content: {
"application/json": { schema: reqSchema },
},
},
},
responses: {
200: {
content: {
"application/json": { schema: resSchema },
},
description: "OK",
},
},
});

app.openapi(quoteRoute, async (c) => {
const { count } = c.req.valid("json");
const url = `https://api.breakingbadquotes.xyz/v1/quotes/${count}`;
const result = await fetch(url).then((res) => res.text());
return c.json({ result });
});

export default app;
17 changes: 17 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"lib": [
"esnext"
],
"types": [
"@cloudflare/workers-types"
],
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
},
}
20 changes: 20 additions & 0 deletions wrangler.toml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name = "dify-custom-tool"
compatibility_date = "2023-12-01"

# [vars]
OPENAPI_VERSION = "3.1.0"
TOOL_VERSION = "1.0.0"
TOOL_NAME = "Dify Custom Tool"
TOOL_DESCRIPTION = "Dify Custom Tool"

# [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# [[r2_buckets]]
# binding = "MY_BUCKET"
# bucket_name = "my-bucket"

# [[d1_databases]]
# binding = "DB"
# database_name = "my-database"
# database_id = ""

0 comments on commit ba7aeeb

Please sign in to comment.