@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 98 lines 3.4 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import { OrderEvents, type OrderEvent } from "./resources.ts";
6
7 const badRequest = (error: string) => HttpServerResponse.json({ error }, { status: 400 });
8
9 /**
10 * The public orders API — the producer.
11 *
12 * It publishes one event per state change and returns. It knows nothing
13 * about who consumes the events: adding a consumer is a new subscription
14 * on the topic, not a change here.
15 *
16 * - `POST /orders` — `{ email, total }` → publishes `order.created`,
17 * returns `202 { orderId, eventId }`.
18 * - `POST /orders/:id/cancel` — `{ email }` → publishes `order.cancelled`,
19 * returns `202 { orderId, eventId }`.
20 */
21 export default class Orders extends GCP.Function<Orders>()(
22 "Orders",
23 {
24 main: import.meta.url,
25 invokerIamDisabled: true,
26 },
27 Effect.gen(function* () {
28 const events = yield* GCP.PubSub.WriteTopic(OrderEvents);
29
30 const publish = (event: OrderEvent) =>
31 events
32 .publish({
33 data: JSON.stringify(event),
34 // Subscription filters match attributes, never the body, so the
35 // type goes here as well.
36 attributes: { type: event.type },
37 })
38 .pipe(Effect.orDie);
39
40 const accepted = (event: OrderEvent) =>
41 HttpServerResponse.json({ orderId: event.orderId, eventId: event.eventId }, { status: 202 });
42
43 return {
44 fetch: Effect.gen(function* () {
45 const request = yield* HttpServerRequest;
46 const url = new URL(request.originalUrl);
47 const segments = url.pathname.split("/").filter(Boolean);
48
49 if (request.method === "GET" && segments.length === 0) {
50 return HttpServerResponse.text("ok");
51 }
52
53 if (request.method !== "POST" || segments[0] !== "orders") {
54 return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 });
55 }
56
57 const body = (yield* request.json.pipe(Effect.orElseSucceed(() => ({})))) as {
58 email?: unknown;
59 total?: unknown;
60 };
61 if (typeof body.email !== "string" || !body.email.includes("@")) {
62 return yield* badRequest("email is required");
63 }
64
65 if (segments.length === 1) {
66 if (typeof body.total !== "number" || body.total <= 0) {
67 return yield* badRequest("total must be a positive number");
68 }
69 const event: OrderEvent = {
70 eventId: crypto.randomUUID(),
71 type: "order.created",
72 orderId: crypto.randomUUID(),
73 email: body.email,
74 total: body.total,
75 occurredAt: new Date().toISOString(),
76 };
77 yield* publish(event);
78 return yield* accepted(event);
79 }
80
81 if (segments.length === 3 && segments[2] === "cancel") {
82 const event: OrderEvent = {
83 eventId: crypto.randomUUID(),
84 type: "order.cancelled",
85 orderId: segments[1]!,
86 email: body.email,
87 total: 0,
88 occurredAt: new Date().toISOString(),
89 };
90 yield* publish(event);
91 return yield* accepted(event);
92 }
93
94 return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 });
95 }),
96 };
97 }).pipe(Effect.provide(GCP.PubSub.WriteTopicHttp)),
98 ) {}