@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 79 lines 2.95 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 { ShortyApi } from "./ShortyApi.ts";
13
14 export default class Api extends Cloudflare.Worker<Api>()(
15 "Api",
16 { main: import.meta.url },
17 Effect.gen(function* () {
18 const d1 = yield* Cloudflare.D1.QueryDatabase(Db);
19 const sql = yield* SQL.D1(d1);
20
21 const findLink = Effect.fn(function* (code: string) {
22 const [link] = yield* sql<Link>`
23 SELECT code, url, created_at AS "createdAt" FROM links WHERE code = ${code}`;
24 if (!link) return yield* new LinkNotFound({ code });
25 return link;
26 });
27
28 const handlers = HttpApiBuilder.group(ShortyApi, "links", (handlers) =>
29 handlers
30 .handle("create", ({ payload }) =>
31 Effect.gen(function* () {
32 const link = { code: newCode(), url: payload.url, createdAt: new Date().toISOString() };
33 yield* sql`
34 INSERT INTO links (code, url, created_at)
35 VALUES (${link.code}, ${link.url}, ${link.createdAt})`;
36 return link;
37 }).pipe(Effect.orDie),
38 )
39 .handle("list", () =>
40 sql<Link>`
41 SELECT code, url, created_at AS "createdAt" FROM links
42 ORDER BY created_at DESC`.pipe(Effect.orDie),
43 )
44 .handle("get", ({ params }) =>
45 findLink(params.code).pipe(Effect.catchTag("SqlError", Effect.die)),
46 ),
47 );
48
49 const api = yield* HttpRouter.toHttpEffect(
50 HttpApiBuilder.layer(ShortyApi).pipe(
51 Layer.provide(handlers),
52 Layer.provide(Http.Platform),
53 Layer.provide(HttpRouter.cors()),
54 ),
55 );
56
57 return {
58 fetch: Effect.gen(function* () {
59 const request = yield* HttpServerRequest;
60 const [, code, rest] = new URL(request.url, "http://localhost").pathname.split("/");
61
62 // GET /:code → the short link's destination
63 if (request.method === "GET" && code && code !== "links" && rest === undefined) {
64 const link = yield* findLink(code);
65 return HttpServerResponse.redirect(link.url, { status: 302 });
66 }
67
68 return yield* api;
69 }).pipe(
70 Effect.catchTags({
71 LinkNotFound: ({ code }) =>
72 HttpServerResponse.json({ _tag: "LinkNotFound", code }, { status: 404 }),
73 SqlError: () =>
74 HttpServerResponse.json({ error: "database unavailable" }, { status: 503 }),
75 }),
76 ),
77 };
78 }).pipe(Effect.provide(Cloudflare.D1.QueryDatabaseBinding)),
79 ) {}