diff options
| -rw-r--r-- | astro.config.mjs | 7 | ||||
| -rw-r--r-- | src/pages/api/counter.ts | 8 | ||||
| -rw-r--r-- | src/worker.ts | 8 | ||||
| -rw-r--r-- | worker-configuration.d.ts | 4 | ||||
| -rw-r--r-- | wrangler.jsonc | 10 |
5 files changed, 25 insertions, 12 deletions
diff --git a/astro.config.mjs b/astro.config.mjs index fc681bf..a66a98c 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -6,5 +6,10 @@ import cloudflare from '@astrojs/cloudflare'; // https://astro.build/config export default defineConfig({ output: 'server', - adapter: cloudflare() + adapter: cloudflare({ + workerEntryPoint: { + path: 'src/worker.ts', + namedExports: ['CounterDurableObject'] + } + }) });
\ No newline at end of file diff --git a/src/pages/api/counter.ts b/src/pages/api/counter.ts index 152dc54..1d3d0b7 100644 --- a/src/pages/api/counter.ts +++ b/src/pages/api/counter.ts @@ -4,8 +4,8 @@ interface Env { COUNTER_DO: DurableObjectNamespace; } -export const GET: APIRoute = async ({ request }) => { - const env = request.cf?.env as Env; +export const GET: APIRoute = async ({ locals }) => { + const env = locals.runtime.env as Env; if (!env?.COUNTER_DO) { return new Response(JSON.stringify({ error: 'Durable Object not available' }), { status: 500, @@ -24,8 +24,8 @@ export const GET: APIRoute = async ({ request }) => { }); }; -export const POST: APIRoute = async ({ request }) => { - const env = request.cf?.env as Env; +export const POST: APIRoute = async ({ request, locals }) => { + const env = locals.runtime.env as Env; if (!env?.COUNTER_DO) { return new Response(JSON.stringify({ error: 'Durable Object not available' }), { status: 500, diff --git a/src/worker.ts b/src/worker.ts index 0ad83a6..5eb9d5d 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -20,8 +20,8 @@ class CounterDurableObject extends DurableObject<Env> { const currentValue = (await this.ctx.storage.get<number>('counter')) || 0; const newValue = currentValue + 1; await this.ctx.storage.put('counter', newValue); - - return new Response(JSON.stringify({ + + return new Response(JSON.stringify({ counter: newValue, timestamp: new Date().toISOString() }), { @@ -31,7 +31,7 @@ class CounterDurableObject extends DurableObject<Env> { if (pathname === '/get') { const currentValue = (await this.ctx.storage.get<number>('counter')) || 0; - return new Response(JSON.stringify({ + return new Response(JSON.stringify({ counter: currentValue, timestamp: new Date().toISOString() }), { @@ -41,7 +41,7 @@ class CounterDurableObject extends DurableObject<Env> { if (pathname === '/reset') { await this.ctx.storage.put('counter', 0); - return new Response(JSON.stringify({ + return new Response(JSON.stringify({ counter: 0, message: 'Counter reset', timestamp: new Date().toISOString() diff --git a/worker-configuration.d.ts b/worker-configuration.d.ts index 42e4abb..e07cf0f 100644 --- a/worker-configuration.d.ts +++ b/worker-configuration.d.ts @@ -1,9 +1,9 @@ /* eslint-disable */ -// Generated by Wrangler by running `wrangler types` (hash: cd56b03d51e037593991fa9fdc687158) +// Generated by Wrangler by running `wrangler types` (hash: 34e15f1685f8c63782ad63fe3743b790) // Runtime types generated with workerd@1.20250709.0 2025-07-10 nodejs_compat declare namespace Cloudflare { interface Env { - COUNTER_DO: DurableObjectNamespace /* CounterDurableObject */; + COUNTER_DO: DurableObjectNamespace<import("./dist/_worker.js/index").CounterDurableObject>; ASSETS: Fetcher; } } diff --git a/wrangler.jsonc b/wrangler.jsonc index b86d45c..4252924 100644 --- a/wrangler.jsonc +++ b/wrangler.jsonc @@ -19,5 +19,13 @@ "class_name": "CounterDurableObject" } ] - } + }, + "migrations": [ + { + "tag": "v1", + "new_sqlite_classes": [ + "CounterDurableObject" + ] + } + ] }
\ No newline at end of file |
