Diffinery

homeflare/celldpublic

celld/examples/kv/index.js
21 lines · 601 BRaw History
1export default {
2 async fetch(request, env) {
3 const key = new URL(request.url).pathname.slice(1);
4 if (!key) return new Response("Use /KEY.", { status: 400 });
5
6 if (request.method === "PUT") {
7 await env.VALUES.put(key, request.body);
8 return new Response(null, { status: 204 });
9 }
10
11 if (request.method === "DELETE") {
12 await env.VALUES.delete(key);
13 return new Response(null, { status: 204 });
14 }
15
16 const value = await env.VALUES.get(key);
17 return value === null
18 ? new Response("Not found.", { status: 404 })
19 : new Response(value);
20 },
21};