@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, 04:45 UTC

main
ts 99 lines 3.78 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as NotificationsContacts from "@alchemy.run/aws/NotificationsContacts";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import * as Redacted from "effect/Redacted";
9 import * as Result from "effect/Result";
10 import path from "pathe";
11
12 const main = path.resolve(import.meta.dirname, "handler.ts");
13
14 /** Deterministic identity for the fixture's contact. */
15 export const CONTACT_NAME = "alchemy-test-contact-bindings";
16 export const CONTACT_EMAIL = "sam+alchemy-test-contact-bindings@alchemy.run";
17
18 export class NotificationsContactsTestFunction extends Lambda.Function<Lambda.Function>()(
19 "NotificationsContactsTestFunction",
20 ) {}
21
22 export default NotificationsContactsTestFunction.make(
23 {
24 main,
25 functionUrl: true,
26 timeout: Duration.seconds(30),
27 },
28 Effect.gen(function* () {
29 const contact = yield* NotificationsContacts.EmailContact("BindingsContact", {
30 name: CONTACT_NAME,
31 emailAddress: CONTACT_EMAIL,
32 tags: { fixture: "notificationscontacts-bindings" },
33 });
34
35 const getContact = yield* NotificationsContacts.GetEmailContact(contact);
36 const sendActivationCode = yield* NotificationsContacts.SendActivationCode(contact);
37 const activate = yield* NotificationsContacts.ActivateEmailContact(contact);
38
39 return {
40 fetch: Effect.gen(function* () {
41 const request = yield* HttpServerRequest;
42 const pathname = new URL(request.originalUrl).pathname;
43
44 // Cheap readiness route — no AWS call.
45 if (request.method === "GET" && pathname === "/ping") {
46 return yield* HttpServerResponse.json({ ok: true });
47 }
48
49 if (request.method === "GET" && pathname === "/contact") {
50 const result = yield* getContact();
51 // name/address are sensitive — assert on shape/status only.
52 return yield* HttpServerResponse.json({
53 arn: result.emailContact.arn,
54 status: result.emailContact.status,
55 });
56 }
57
58 if (request.method === "POST" && pathname === "/send-code") {
59 const sent = yield* sendActivationCode().pipe(
60 Effect.map(() => ({ sent: true, conflict: false })),
61 // Already-active contacts (or a recent resend) surface as the
62 // typed ConflictException — either outcome proves the binding.
63 Effect.catchTag("ConflictException", () =>
64 Effect.succeed({ sent: false, conflict: true }),
65 ),
66 );
67 return yield* HttpServerResponse.json(sent);
68 }
69
70 if (request.method === "POST" && pathname === "/activate-bogus") {
71 // Activation needs the code from the activation email (a human
72 // loop) — a bogus code must surface a TYPED error, which proves
73 // both the IAM grant and the request wiring.
74 const result = yield* Effect.result(activate({ code: Redacted.make("000000") }));
75 if (Result.isFailure(result)) {
76 return yield* HttpServerResponse.json({
77 activated: false,
78 errorTag: result.failure._tag,
79 });
80 }
81 return yield* HttpServerResponse.json({ activated: true });
82 }
83
84 return yield* HttpServerResponse.json(
85 { error: "Not found", method: request.method, pathname },
86 { status: 404 },
87 );
88 }).pipe(Effect.orDie),
89 };
90 }).pipe(
91 Effect.provide(
92 Layer.mergeAll(
93 NotificationsContacts.GetEmailContactHttp,
94 NotificationsContacts.SendActivationCodeHttp,
95 NotificationsContacts.ActivateEmailContactHttp,
96 ),
97 ),
98 ),
99 );