-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
116 lines (109 loc) · 3.33 KB
/
api.ts
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { ulid } from "@std/ulid";
import { Todo } from "./models.ts";
const kv = await Deno.openKv();
const api = {
todos: {
getOrCreateItem: async (req: Request): Promise<Response> => {
if (req.method === "POST" && req.body) {
const formdata = await req.formData();
kv.set(["todos", ulid()], {
description: formdata.get("description"),
pending: true,
});
return new Response(null, {
status: 204,
headers: { "HX-Trigger": "todos" },
});
}
const iter = kv.list<Todo>({ prefix: ["todos"] });
let todos = "";
let total = 0;
let pending = 0;
for await (const res of iter) {
const id = res.key[1].toString();
const isPending = res.value?.pending ? "check_circle" : "cancel";
let description = res.value?.description.toString();
total++;
if (res.value?.pending) {
description = `<ins>${description}</ins>`;
pending++;
} else {
description = `<del>${description}</del>`;
}
todos += `<tr>
<td scope="row">${description}</td>
<td>
<a href="javascript:void(0);" hx-patch="/api/v1/todos/${id}/edit" hx-trigger="click"><span class="material-symbols-rounded">${isPending}</span></a> <a href="javascript:void(0);" hx-delete="/api/v1/todos/${id}" hx-trigger="click"><span class="material-symbols-rounded">delete</span></a>
</td>
</tr>`;
}
if (!todos) {
todos = `<tr><td colspan="2">No items added yet</td></tr>`;
}
return new Response(
todos,
{
status: 200,
headers: {
"Content-Type": "text/html",
"HX-Trigger": JSON.stringify({ count: { total, pending } }),
},
},
);
},
getOrDeleteItem: async (
req: Request,
_info?: Deno.ServeHandlerInfo,
params?: URLPatternResult | null,
): Promise<Response> => {
const ulid = params?.pathname.groups.ulid;
if (!ulid) {
return new Response("Not found", {
status: 404,
});
}
const todo = await kv.get(["todos", ulid]);
console.log(todo);
if (!todo.value && !todo.versionstamp) {
return new Response("Not found", {
status: 404,
});
}
if (req.method === "DELETE") {
await kv.delete(["todos", ulid]);
return new Response(null, {
status: 200,
headers: { "HX-Trigger": "todos" },
});
} else {
return Response.json({ todo: todo.value }, {
status: 200,
});
}
},
patchItem: async (
req: Request,
_info?: Deno.ServeHandlerInfo,
params?: URLPatternResult | null,
): Promise<Response> => {
const ulid = params?.pathname.groups.ulid;
if (!ulid) {
return new Response("Not found", {
status: 404,
});
}
const todo = await kv.get<Todo>(["todos", ulid]);
if (req.method === "PATCH" && todo) {
await kv.set(["todos", ulid], {
...todo.value,
pending: !todo.value?.pending,
});
}
return new Response(null, {
status: 200,
headers: { "HX-Trigger": "todos" },
});
},
},
};
export { api };