1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
};
πŸ‘† This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data β€” all from the browser.