diff options
| author | Yuval Adam <_@yuv.al> | 2025-07-10 13:55:01 +0200 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2025-07-10 13:55:23 +0200 |
| commit | 3f0837d920ba67496c5fc7f50485aed84b37d05e (patch) | |
| tree | 45548ab3defff3abfb34245e41db606caedf7e0b /src/worker.ts | |
| parent | c4284d7c2ab7b73af8424fcf379f44dee1682a1a (diff) | |
Simplify code'
Diffstat (limited to 'src/worker.ts')
| -rw-r--r-- | src/worker.ts | 43 |
1 files changed, 8 insertions, 35 deletions
diff --git a/src/worker.ts b/src/worker.ts index 5eb9d5d..52e9f5e 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -8,46 +8,19 @@ interface Env { } class CounterDurableObject extends DurableObject<Env> { - constructor(ctx: DurableObjectState, env: Env) { - super(ctx, env); - } - async fetch(request: Request): Promise<Response> { const url = new URL(request.url); - const pathname = url.pathname; - - if (pathname === '/increment') { - 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({ - counter: newValue, - timestamp: new Date().toISOString() - }), { - headers: { 'Content-Type': 'application/json' } - }); - } - if (pathname === '/get') { - const currentValue = (await this.ctx.storage.get<number>('counter')) || 0; - return new Response(JSON.stringify({ - counter: currentValue, - timestamp: new Date().toISOString() - }), { - headers: { 'Content-Type': 'application/json' } - }); + if (url.pathname === '/get') { + const count = (await this.ctx.storage.get<number>('count')) || 0; + return new Response(count.toString()); } - if (pathname === '/reset') { - await this.ctx.storage.put('counter', 0); - return new Response(JSON.stringify({ - counter: 0, - message: 'Counter reset', - timestamp: new Date().toISOString() - }), { - headers: { 'Content-Type': 'application/json' } - }); + if (url.pathname === '/increment') { + const count = (await this.ctx.storage.get<number>('count')) || 0; + const newCount = count + 1; + await this.ctx.storage.put('count', newCount); + return new Response(newCount.toString()); } return new Response('Not found', { status: 404 }); |
