@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 89 lines 3.3 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as SQL from "@alchemy.run/cloudflare/D1/SQL";
3 import * as Http from "alchemy/Http";
4 import * as Effect from "effect/Effect";
5 import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
6 import * as HttpRouter from "effect/http/HttpRouter";
7 import { HttpServerRequest } from "effect/http/HttpServerRequest";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 import * as Layer from "effect/Layer";
10 import { Db } from "./Db.ts";
11 import { LinkNotFound, newCode, type Link } from "./Link.ts";
12 import LinkRoom from "./LinkRoom.ts";
13 import { ShortyApi } from "./ShortyApi.ts";
14
15 export default class Api extends Cloudflare.Worker<Api>()(
16 "Api",
17 { main: import.meta.url },
18 Effect.gen(function* () {
19 const d1 = yield* Cloudflare.D1.QueryDatabase(Db);
20 const sql = yield* SQL.D1(d1);
21 const rooms = yield* LinkRoom;
22
23 const findLink = Effect.fn(function* (code: string) {
24 const [link] = yield* sql<Link>`
25 SELECT code, url, created_at AS "createdAt" FROM links WHERE code = ${code}`;
26 if (!link) return yield* new LinkNotFound({ code });
27 return link;
28 });
29
30 const handlers = HttpApiBuilder.group(ShortyApi, "links", (handlers) =>
31 handlers
32 .handle("create", ({ payload }) =>
33 Effect.gen(function* () {
34 const link = { code: newCode(), url: payload.url, createdAt: new Date().toISOString() };
35 yield* sql`
36 INSERT INTO links (code, url, created_at)
37 VALUES (${link.code}, ${link.url}, ${link.createdAt})`;
38 return link;
39 }).pipe(Effect.orDie),
40 )
41 .handle("list", () =>
42 sql<Link>`
43 SELECT code, url, created_at AS "createdAt" FROM links
44 ORDER BY created_at DESC`.pipe(Effect.orDie),
45 )
46 .handle("get", ({ params }) =>
47 findLink(params.code).pipe(Effect.catchTag("SqlError", Effect.die)),
48 ),
49 );
50
51 const api = yield* HttpRouter.toHttpEffect(
52 HttpApiBuilder.layer(ShortyApi).pipe(
53 Layer.provide(handlers),
54 Layer.provide(Http.Platform),
55 Layer.provide(HttpRouter.cors()),
56 ),
57 );
58
59 return {
60 fetch: Effect.gen(function* () {
61 const request = yield* HttpServerRequest;
62 const [, first, code, action] = new URL(request.url, "http://localhost").pathname.split(
63 "/",
64 );
65
66 // GET /links/:code/live → a WebSocket to the link's room
67 if (first === "links" && code && action === "live") {
68 return yield* rooms.getByName(code).fetch(request);
69 }
70
71 // GET /:code → count the click, then redirect
72 if (request.method === "GET" && first && first !== "links" && code === undefined) {
73 const link = yield* findLink(first);
74 yield* rooms.getByName(link.code).record(1);
75 return HttpServerResponse.redirect(link.url, { status: 302 });
76 }
77
78 return yield* api;
79 }).pipe(
80 Effect.catchTags({
81 LinkNotFound: ({ code }) =>
82 HttpServerResponse.json({ _tag: "LinkNotFound", code }, { status: 404 }),
83 SqlError: () =>
84 HttpServerResponse.json({ error: "database unavailable" }, { status: 503 }),
85 }),
86 ),
87 };
88 }).pipe(Effect.provide(Cloudflare.D1.QueryDatabaseBinding)),
89 ) {}