import { blob } from "https://esm.town/v/std/blob?v=10";
export const databin = async (req: Request) => {
const searchParams = new URL(req.url).searchParams;
const format = searchParams.get("format") ?? "json";
const key = searchParams.get("key");
if (!key) throw new Error("missing ?key=");
let data;
const oldData = await blob.getJSON(key);
if (req.method == "GET") {
data = { data: oldData };
} else if (req.method == "POST") {
const newData = await req.json();
await blob.setJSON(key, newData);
data = { oldData, data: newData };
console.log("set", { key, data });
}
else {
throw new Error("unsupported HTTP method");
}
return Response.json(data);
};