Forked from jamiedubs/databin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 = oldData;
console.log("get", { key, data });
} 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);
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 29, 2024