From ba7aeeba5c12019fcc5afccc326b8167532b2f4a Mon Sep 17 00:00:00 2001
From: crazywoola
Date: Tue, 23 Jan 2024 10:07:46 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=9A=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 10 ++++++++
README.md | 8 ++++++
package.json | 14 ++++++++++
src/index.ts | 60 +++++++++++++++++++++++++++++++++++++++++++
tsconfig.json | 17 ++++++++++++
wrangler.toml.example | 20 +++++++++++++++
6 files changed, 129 insertions(+)
create mode 100644 .gitignore
create mode 100644 README.md
create mode 100644 package.json
create mode 100644 src/index.ts
create mode 100644 tsconfig.json
create mode 100644 wrangler.toml.example
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a19abe0
--- /dev/null
+++ b/.gitignore
@@ -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
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..cc58e96
--- /dev/null
+++ b/README.md
@@ -0,0 +1,8 @@
+```
+npm install
+npm run dev
+```
+
+```
+npm run deploy
+```
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..fb53f9f
--- /dev/null
+++ b/package.json
@@ -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"
+ }
+}
diff --git a/src/index.ts b/src/index.ts
new file mode 100644
index 0000000..b68a2b0
--- /dev/null
+++ b/src/index.ts
@@ -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;
diff --git a/tsconfig.json b/tsconfig.json
new file mode 100644
index 0000000..9cd8489
--- /dev/null
+++ b/tsconfig.json
@@ -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"
+ },
+}
\ No newline at end of file
diff --git a/wrangler.toml.example b/wrangler.toml.example
new file mode 100644
index 0000000..fe5a3ce
--- /dev/null
+++ b/wrangler.toml.example
@@ -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 = ""