@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 56 lines 2.04 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Stripe from "@alchemy.run/stripe";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6
7 export default class StripeEventSourceWorker extends Cloudflare.Worker<StripeEventSourceWorker>()(
8 "StripeEventSourceWorker",
9 {
10 main: import.meta.url,
11 },
12 Effect.gen(function* () {
13 const log = yield* Cloudflare.KV.Namespace("EventLog");
14 const kv = yield* Cloudflare.KV.ReadWriteNamespace(log);
15 const createCustomer = yield* Stripe.CreateCustomer();
16
17 yield* Stripe.consumeEvents(
18 "Events",
19 { events: [Stripe.CustomerCreated] },
20 Effect.fn(function* (event) {
21 yield* kv.put(event.object.id, "1").pipe(Effect.orDie);
22 yield* kv.put("lastCustomerId", event.object.id).pipe(Effect.orDie);
23 }),
24 ).pipe(Effect.orDie);
25
26 return {
27 fetch: Effect.gen(function* () {
28 const request = yield* HttpServerRequest;
29 if (request.url.startsWith("/last/")) {
30 const id = request.url.slice("/last/".length).split("?")[0];
31 const seen = yield* kv.get(id).pipe(Effect.orDie);
32 return yield* HttpServerResponse.json({
33 id: seen === "1" ? id : null,
34 });
35 }
36 if (request.url.startsWith("/last")) {
37 const id = yield* kv.get("lastCustomerId").pipe(Effect.orDie);
38 return yield* HttpServerResponse.json({ id: id ?? null });
39 }
40 if (request.method === "POST" && request.url.startsWith("/customers")) {
41 const customer = yield* createCustomer({
42 email: "event-source@example.com",
43 }).pipe(Effect.orDie);
44 return yield* HttpServerResponse.json({ id: customer.id }, { status: 201 });
45 }
46 return HttpServerResponse.text("ok");
47 }),
48 };
49 }).pipe(
50 Effect.provide([
51 Cloudflare.KV.ReadWriteNamespaceBinding,
52 Stripe.CreateCustomerHttp,
53 Stripe.ConsumeEventsLive,
54 ]),
55 ),
56 ) {}