diff --git a/examples/proxy-counter/README.md b/examples/proxy-counter/README.md new file mode 100644 index 0000000..cc58e96 --- /dev/null +++ b/examples/proxy-counter/README.md @@ -0,0 +1,8 @@ +``` +npm install +npm run dev +``` + +``` +npm run deploy +``` diff --git a/examples/proxy-counter/package.json b/examples/proxy-counter/package.json new file mode 100644 index 0000000..0c68d99 --- /dev/null +++ b/examples/proxy-counter/package.json @@ -0,0 +1,21 @@ +{ + "name": "hono-do-example-proxy-counter", + "private": true, + "version": "0.0.4", + "scripts": { + "lint": "eslint --fix --ext .ts,.tsx src", + "lint:check": "eslint --ext .ts,.tsx src", + "format": "prettier --write \"src/**/*.{ts,tsx}\"", + "format:check": "prettier --check \"src/**/*.{ts,tsx}\"", + "dev": "wrangler dev src/index.ts --port 3000", + "deploy": "wrangler deploy --minify src/index.ts" + }, + "dependencies": { + "hono": "^3.6.0", + "hono-do": "workspace:*" + }, + "devDependencies": { + "@cloudflare/workers-types": "^4.20230821.0", + "wrangler": "^3.7.0" + } +} diff --git a/examples/proxy-counter/src/counter.ts b/examples/proxy-counter/src/counter.ts new file mode 100644 index 0000000..ece4908 --- /dev/null +++ b/examples/proxy-counter/src/counter.ts @@ -0,0 +1,20 @@ +import { generateHonoObject } from "hono-do"; + +export const Counter = generateHonoObject("/counter", async (app, state) => { + const { storage } = state; + let value = (await storage.get("value")) ?? 0; + + app.post("/increment", (c) => { + storage.put("value", value++); + return c.text(value.toString()); + }); + + app.post("/decrement", (c) => { + storage.put("value", value--); + return c.text(value.toString()); + }); + + app.get("/", (c) => { + return c.text(value.toString()); + }); +}); diff --git a/examples/proxy-counter/src/index.ts b/examples/proxy-counter/src/index.ts new file mode 100644 index 0000000..815719d --- /dev/null +++ b/examples/proxy-counter/src/index.ts @@ -0,0 +1,15 @@ +import { Hono } from "hono"; + +import { Counter } from "./counter"; +import { Template } from "./template"; + +const app = new Hono(); + +app.use("/counter/*", Counter.byName("COUNTER", "counter")); + +app.get("/", (c) => { + return c.html(Template); +}); + +export default app; +export * from "./counter"; diff --git a/examples/proxy-counter/src/template.ts b/examples/proxy-counter/src/template.ts new file mode 100644 index 0000000..4c5ff54 --- /dev/null +++ b/examples/proxy-counter/src/template.ts @@ -0,0 +1,28 @@ +export const Template = /*html*/ ` + + +

Counter

+

Current value:

+ + + + + +`.trim(); diff --git a/examples/proxy-counter/tsconfig.json b/examples/proxy-counter/tsconfig.json new file mode 100644 index 0000000..9cd8489 --- /dev/null +++ b/examples/proxy-counter/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/examples/proxy-counter/wrangler.toml b/examples/proxy-counter/wrangler.toml new file mode 100644 index 0000000..247bcff --- /dev/null +++ b/examples/proxy-counter/wrangler.toml @@ -0,0 +1,9 @@ +name = "proxy-counter" +compatibility_date = "2023-01-01" + +[durable_objects] +bindings = [{ name = "COUNTER", class_name = "Counter" }] + +[[migrations]] +tag = "v1" +new_classes = ["Counter"] diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f3bee4e..71d5235 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -131,6 +131,22 @@ importers: specifier: ^3.7.0 version: 3.8.0 + examples/proxy-counter: + dependencies: + hono: + specifier: ^3.6.0 + version: 3.6.0 + hono-do: + specifier: workspace:* + version: link:../../packages/hono-do + devDependencies: + '@cloudflare/workers-types': + specifier: ^4.20230821.0 + version: 4.20230904.0 + wrangler: + specifier: ^3.7.0 + version: 3.8.0 + packages/hono-do: dependencies: hono: