From 3f0837d920ba67496c5fc7f50485aed84b37d05e Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Thu, 10 Jul 2025 13:55:01 +0200 Subject: Simplify code' --- src/worker.ts | 43 ++++++++----------------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) (limited to 'src/worker.ts') 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 { - constructor(ctx: DurableObjectState, env: Env) { - super(ctx, env); - } - async fetch(request: Request): Promise { const url = new URL(request.url); - const pathname = url.pathname; - - if (pathname === '/increment') { - const currentValue = (await this.ctx.storage.get('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('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('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('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 }); -- cgit v1.3.1