|
| 1 | +import Counter from './counter'; |
| 2 | + |
| 3 | +function generateJSONResponse( |
| 4 | + body: Record<string, unknown>, |
| 5 | + status = 200, |
| 6 | + additionalHeaders: Record<string, unknown> = {} |
| 7 | +) { |
| 8 | + return new Response(JSON.stringify(body), { |
| 9 | + status, |
| 10 | + headers: { |
| 11 | + 'Content-Type': 'application/json', |
| 12 | + ...additionalHeaders, |
| 13 | + }, |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +function getCounterObjectStub( |
| 18 | + pathname: string, |
| 19 | + counterNamespace: DurableObjectNamespace<Counter> |
| 20 | +) { |
| 21 | + const counterID: DurableObjectId = counterNamespace.idFromName(pathname); |
| 22 | + return counterNamespace.get(counterID); |
| 23 | +} |
| 24 | + |
| 25 | +async function handleGetRequest( |
| 26 | + pathname: string, |
| 27 | + counterNamespace: DurableObjectNamespace<Counter> |
| 28 | +) { |
| 29 | + const counterObjectStub = getCounterObjectStub(pathname, counterNamespace); |
| 30 | + const newValue = await counterObjectStub.increment(); |
| 31 | + |
| 32 | + return generateJSONResponse({ newValue }); |
| 33 | +} |
| 34 | + |
| 35 | +async function handleDeleteRequest( |
| 36 | + pathname: string, |
| 37 | + counterNamespace: DurableObjectNamespace<Counter> |
| 38 | +) { |
| 39 | + const counterObjectStub = getCounterObjectStub(pathname, counterNamespace); |
| 40 | + await counterObjectStub.destroy(); |
| 41 | + |
| 42 | + return generateJSONResponse({ |
| 43 | + message: 'The counter is scheduled to be destroyed.', |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +async function handleRequest( |
| 48 | + pathname: string, |
| 49 | + method: string, |
| 50 | + counterNamespace: DurableObjectNamespace<Counter> |
| 51 | +) { |
| 52 | + switch (method) { |
| 53 | + case 'GET': |
| 54 | + return await handleGetRequest(pathname, counterNamespace); |
| 55 | + case 'DELETE': |
| 56 | + return await handleDeleteRequest(pathname, counterNamespace); |
| 57 | + default: |
| 58 | + return generateJSONResponse( |
| 59 | + { message: 'The method used in this request is not supported.' }, |
| 60 | + 405, |
| 61 | + { |
| 62 | + Allow: 'GET, DELETE', |
| 63 | + } |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export default { |
| 69 | + async fetch(request, env): Promise<Response> { |
| 70 | + const counterNamespace = env.COUNTER as DurableObjectNamespace<Counter>; |
| 71 | + |
| 72 | + const { url, method } = request; |
| 73 | + const { pathname } = new URL(url); |
| 74 | + |
| 75 | + try { |
| 76 | + return await handleRequest(pathname, method, counterNamespace); |
| 77 | + } catch (error) { |
| 78 | + if (error instanceof Error && error.message.startsWith('InvalidState')) { |
| 79 | + return generateJSONResponse({ message: error.message }, 400); |
| 80 | + } |
| 81 | + |
| 82 | + return generateJSONResponse( |
| 83 | + { message: 'Unknown internal server error occured.' }, |
| 84 | + 500 |
| 85 | + ); |
| 86 | + } |
| 87 | + }, |
| 88 | +} satisfies ExportedHandler<Env>; |
| 89 | + |
| 90 | +export { Counter }; |
0 commit comments