The best way to control Claude Code, Codex and more from any computer or phone

main
ts 64 lines 1.95 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3 import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
4 import { BackendApi, Note, NotesList } from "./BackendApi";
5 import { DB } from "./Bindings";
6
7 export const BackendHandlers = HttpApiBuilder.group(
8 BackendApi,
9 "Backend",
10 Effect.fn(function* (handlers) {
11 const db = yield* Cloudflare.D1.QueryDatabase(DB);
12
13 return handlers
14 .handle("listNotes", () =>
15 db
16 .prepare("SELECT id, title, source, created_at FROM notes ORDER BY created_at DESC")
17 .all<{
18 id: string;
19 title: string;
20 source: string;
21 created_at: number;
22 }>()
23 .pipe(
24 Effect.map(
25 ({ results }) =>
26 new NotesList({
27 notes: (results ?? []).map(
28 (row) =>
29 new Note({
30 id: row.id,
31 title: row.title,
32 source: row.source,
33 createdAt: row.created_at,
34 }),
35 ),
36 }),
37 ),
38 Effect.orDie,
39 ),
40 )
41 .handle("createNote", ({ payload }) =>
42 Effect.gen(function* () {
43 const note = new Note({
44 id: crypto.randomUUID(),
45 title: payload.title,
46 source: "effect-httpapi",
47 createdAt: Date.now(),
48 });
49 yield* db
50 .prepare("INSERT INTO notes (id, title, source, created_at) VALUES (?, ?, ?, ?)")
51 .bind(note.id, note.title, note.source, note.createdAt)
52 .run();
53 return note;
54 }).pipe(Effect.orDie),
55 )
56 .handle("deleteNote", ({ params }) =>
57 db
58 .prepare("DELETE FROM notes WHERE id = ?")
59 .bind(params.id)
60 .run()
61 .pipe(Effect.asVoid, Effect.orDie),
62 );
63 }),
64 );