import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
const kv = await Deno.openKv();
serve(async (req: Request) => {
const pathname = new URL(req.url).pathname
if (req.method === 'GET') {
for await (const { key, value } of kv.list({ prefix: [pathname] }, { limit: 1 })) {
kv.delete(key)
return new Response(typeof value === 'object' ? JSON.stringify(value) : value, { headers: { "Access-Control-Allow-Origin": "*" } })
}
} else if (req.method === 'POST') {
const now = Date.now()
const entries = await req.json()
const id = Math.random()
for(var i=0; i<entries.length; i++) {
kv.set([pathname, now, i, id], entries[i])
}
return new Response('201 Created', { status: 201, headers: { "Access-Control-Allow-Origin": "*" } })
}
return new Response('', { status: 404, headers: { "Access-Control-Allow-Origin": "*" } })
});