Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { z } from "https://esm.run/zod";
import { serveTools } from "https://esm.town/v/romannurik/toolsLib";
import { sqlite } from "https://esm.town/v/std/sqlite";
await sqlite.execute(`create table if not exists todos(
id integer primary key autoincrement,
body text
)`);
export default async function(request: Request) {
return await serveTools(request, {
ListTodos: {
description: "Lists the user's stored TODOs",
input: z.object({
query: z.string().optional(),
}),
async handler({ query }) {
const data = await sqlite.execute("select id, body from todos");
return data.rows.map(s => `[${s[0]}]: ${s[1]}`).join("\n");
},
},
CreateTodo: {
description: "Creates a TODO",
input: z.object({
body: z.string(),
}),
async handler({ body }) {
await sqlite.execute({
sql: `insert into todos (body) values (:body)`,
args: { body },
});
return "Success";
},
},
DeleteTodo: {
description: "Deletes TODO",
input: z.object({
id: z.string(),
}),
async handler({ id }) {
await sqlite.execute({
sql: `delete from todos where id = :id`,
args: { id },
});
return "Success";
},
},
});
}
romannurik-todotools.web.val.run
July 31, 2024