February 22, 2024
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { API_URL } from "https://esm.town/v/std/API_URL";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
import process from "node:process";
export async function set(key: string, value: any) {
let resp = await fetch(
`${API_URL}/v1/vals`,
{
method: "PUT",
body: JSON.stringify({
name: key,
code: `// set at ${new Date()}
export let ${key} = ${JSON.stringify(await value, null, 2)};`,
}),
headers: {
"content-type": "application/json",
authorization: "Bearer " + process.env.valtown,
},
},
);
if (!resp.ok) {
let { message } = await resp.json();
throw new Error("Set Error: " + message);
}
let val = await resp.json();
if (val.error) throw new Error("Set Error: " + val.error);
return val?.output?.json;
}
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!
February 22, 2024