@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 50 lines 1.71 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 { Product, ProductNotFound, ProductsApi } from "./Api.ts";
8 import { Catalog, HOSTNAME } from "./Store.ts";
9
10 /** `GET /products/*` — read-only access to the catalog. */
11 export default class Products extends Cloudflare.Worker<Products>()(
12 "Products",
13 {
14 main: import.meta.url,
15 routes: [{ pattern: `${HOSTNAME}/products*` }],
16 },
17 Effect.gen(function* () {
18 const catalog = yield* Cloudflare.KV.ReadNamespace(Catalog);
19
20 const get = (id: string) =>
21 catalog.get<Product>(id, "json").pipe(
22 Effect.orDie,
23 Effect.flatMap((product) =>
24 product ? Effect.succeed(new Product(product)) : Effect.fail(new ProductNotFound({ id })),
25 ),
26 );
27
28 return {
29 fetch: yield* HttpRouter.toHttpEffect(
30 HttpApiBuilder.layer(ProductsApi).pipe(
31 Layer.provide(Http.Platform),
32 Layer.provide(
33 HttpApiBuilder.group(ProductsApi, "products", (handlers) =>
34 handlers
35 .handle("list", () =>
36 catalog.list().pipe(
37 Effect.orDie,
38 Effect.flatMap(({ keys }) =>
39 Effect.forEach(keys, ({ name }) => get(name).pipe(Effect.orDie)),
40 ),
41 ),
42 )
43 .handle("get", ({ params }) => get(params.id)),
44 ),
45 ),
46 ),
47 ),
48 };
49 }).pipe(Effect.provide(Cloudflare.KV.ReadNamespaceBinding)),
50 ) {}