Skip to content

Instantly share code, notes, and snippets.

@leaysgur
Created June 14, 2021 00:36
Show Gist options
  • Save leaysgur/a9e62afebb76ad0f2b6e6b27c17c5fc9 to your computer and use it in GitHub Desktop.
Save leaysgur/a9e62afebb76ad0f2b6e6b27c17c5fc9 to your computer and use it in GitHub Desktop.
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
let id = 0;
async function handleRequest(request) {
const params = new URL(request.url).searchParams;
const mode = params.get("mode") || "0";
if (mode === "list") {
const list = await KV.list();
console.log("LIST: %o", list.keys);
return new Response(JSON.stringify(list.keys));
}
if (mode === "put") {
await KV.put(id++, "OK");
console.log("PUT: %s", id);
return new Response();
}
const value = await KV.get(mode);
console.log("GET: %s => %s", mode, value);
return new Response(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment