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
29
30
31
32
33
34
35
import { fetch } from "https://esm.town/v/std/fetch";
/**
NOTE:
THIS CLIENT DOES NOT WORK AS EXPECTED. ALL VALS IN VAL TOWN
ARE JSON.stringify-ied SO RETURNED CLOSURES AREN'T SUPPORTED
USE THE INDIVIDUAL FUNCTIONS INSTEAD:
@claytn.replitGet
@claytn.replitSet
@claytn.replitDelete
@claytn.replitList
*/
/** Database client for replit's key-value store */
export function createReplitDBClient(REPLIT_DB_URL) {
return {
get: (key) => {
return fetch(`${REPLIT_DB_URL}/${key}`);
},
set: (key, value) => {
return fetch(`${REPLIT_DB_URL}/${key}=${encodeURIComponent(value)}`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
},
delete: (key) => {
return fetch(`${REPLIT_DB_URL}/${key}`, { method: "DELETE" });
},
list: (prefix) => {
return fetch(`${REPLIT_DB_URL}?prefix=${encodeURIComponent(prefix)}`);
},
};
}
👆 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.