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

main
ts 43 lines 1.5 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Http from "alchemy/Http";
3 import * as Effect from "effect/Effect";
4 import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
5 import * as HttpRouter from "effect/http/HttpRouter";
6 import * as Layer from "effect/Layer";
7 import { LinkNotFound, newCode, type Link } from "./Link.ts";
8 import { ShortyApi } from "./ShortyApi.ts";
9
10 export default class Api extends Cloudflare.Worker<Api>()(
11 "Api",
12 { main: import.meta.url },
13 Effect.gen(function* () {
14 // Lives in one isolate only: gone on the next deploy or cold start.
15 const links = new Map<string, Link>();
16
17 const handlers = HttpApiBuilder.group(ShortyApi, "links", (handlers) =>
18 handlers
19 .handle("create", ({ payload }) =>
20 Effect.sync(() => {
21 const link = { code: newCode(), url: payload.url, createdAt: new Date().toISOString() };
22 links.set(link.code, link);
23 return link;
24 }),
25 )
26 .handle("list", () => Effect.sync(() => [...links.values()]))
27 .handle("get", ({ params }) => {
28 const link = links.get(params.code);
29 return link ? Effect.succeed(link) : Effect.fail(new LinkNotFound({ code: params.code }));
30 }),
31 );
32
33 return {
34 fetch: yield* HttpRouter.toHttpEffect(
35 HttpApiBuilder.layer(ShortyApi).pipe(
36 Layer.provide(handlers),
37 Layer.provide(Http.Platform),
38 Layer.provide(HttpRouter.cors()),
39 ),
40 ),
41 };
42 }),
43 ) {}