jamiedubs-databin.web.val.run
Readme

a super simple JSON setter/getter using valtown blobs. use GET to fetch it, use POST to set it. expects the inbound data to be JSON for simplicity. use ?key=foobar to specify different storage locations.

set:

curl -s -X POST  -d '{"lol": "wut"}' https://jamiedubs-databin.web.val.run?key=foobar

get:

curl -s https://jamiedubs-databin.web.val.run?key=foobar
# {"lol":"wut"}
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!
v8
April 19, 2024