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

main
ts 156 lines 5.93 KB
Raw
1 import * as Account from "@alchemy.run/aws/Account";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import * as Redacted from "effect/Redacted";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 // The Account API marks contact fields sensitive at the wire level, so
13 // distilled returns them as `string | Redacted<string>`. The fixture never
14 // echoes the PII itself — only presence/shape — so unwrap locally.
15 const unwrap = (value: string | Redacted.Redacted<string> | undefined) =>
16 value === undefined ? undefined : typeof value === "string" ? value : Redacted.value(value);
17
18 export class AccountTestFunction extends Lambda.Function<Lambda.Function>()(
19 "AccountTestFunction",
20 ) {}
21
22 export default AccountTestFunction.make(
23 {
24 main,
25 functionUrl: true,
26 },
27 Effect.gen(function* () {
28 // --- account-level bindings (Account Management is an account
29 // singleton; these are the settings reads an ops/compliance function
30 // uses) ---
31 const getAccountInformation = yield* Account.GetAccountInformation();
32 const getContactInformation = yield* Account.GetContactInformation();
33 const getAlternateContact = yield* Account.GetAlternateContact();
34 const listRegions = yield* Account.ListRegions();
35 const getRegionOptStatus = yield* Account.GetRegionOptStatus();
36
37 const bound = {
38 getAccountInformation,
39 getContactInformation,
40 getAlternateContact,
41 listRegions,
42 getRegionOptStatus,
43 };
44
45 return {
46 fetch: Effect.gen(function* () {
47 const request = yield* HttpServerRequest;
48 const url = new URL(request.originalUrl);
49 const pathname = url.pathname;
50
51 if (request.method === "GET" && pathname === "/bindings") {
52 return yield* HttpServerResponse.json({
53 bound: Object.keys(bound),
54 });
55 }
56
57 if (request.method === "GET" && pathname === "/account-info") {
58 const result = yield* getAccountInformation().pipe(
59 Effect.map((r) => ({
60 ok: true as const,
61 accountId: r.AccountId ?? null,
62 accountState: r.AccountState ?? null,
63 hasAccountName: (unwrap(r.AccountName) ?? "").length > 0,
64 })),
65 Effect.catchTag("AccessDeniedException", (e) =>
66 Effect.succeed({ ok: false as const, tag: e._tag }),
67 ),
68 );
69 return yield* HttpServerResponse.json(result);
70 }
71
72 if (request.method === "GET" && pathname === "/contact-info") {
73 const result = yield* getContactInformation().pipe(
74 Effect.map((r) => ({
75 ok: true as const,
76 hasFullName: (unwrap(r.ContactInformation?.FullName) ?? "").length > 0,
77 hasCountryCode: (unwrap(r.ContactInformation?.CountryCode) ?? "").length > 0,
78 })),
79 Effect.catchTag(["AccessDeniedException", "ResourceNotFoundException"], (e) =>
80 Effect.succeed({ ok: false as const, tag: e._tag }),
81 ),
82 );
83 return yield* HttpServerResponse.json(result);
84 }
85
86 if (request.method === "GET" && pathname === "/alternate-contact") {
87 // The BILLING alternate contact may or may not be set on the
88 // account — the typed ResourceNotFoundException proves the grant
89 // just as well as live data.
90 const result = yield* getAlternateContact({
91 AlternateContactType: "BILLING",
92 }).pipe(
93 Effect.map((r) => ({
94 ok: true as const,
95 contactType: r.AlternateContact?.AlternateContactType ?? null,
96 })),
97 Effect.catchTag(["AccessDeniedException", "ResourceNotFoundException"], (e) =>
98 Effect.succeed({ ok: false as const, tag: e._tag }),
99 ),
100 );
101 return yield* HttpServerResponse.json(result);
102 }
103
104 if (request.method === "GET" && pathname === "/regions") {
105 // The default page size (20) cuts the alphabetical list off before
106 // us-east-1 — ask for the maximum page.
107 const result = yield* listRegions({ MaxResults: 50 }).pipe(
108 Effect.map((r) => ({
109 ok: true as const,
110 count: r.Regions?.length ?? 0,
111 regionNames: (r.Regions ?? [])
112 .map((region) => region.RegionName)
113 .filter((name): name is string => name !== undefined),
114 })),
115 Effect.catchTag("AccessDeniedException", (e) =>
116 Effect.succeed({ ok: false as const, tag: e._tag }),
117 ),
118 );
119 return yield* HttpServerResponse.json(result);
120 }
121
122 if (request.method === "GET" && pathname === "/region-opt-status") {
123 // us-east-1 is enabled by default in every account.
124 const result = yield* getRegionOptStatus({
125 RegionName: "us-east-1",
126 }).pipe(
127 Effect.map((r) => ({
128 ok: true as const,
129 regionName: r.RegionName ?? null,
130 regionOptStatus: r.RegionOptStatus ?? null,
131 })),
132 Effect.catchTag("AccessDeniedException", (e) =>
133 Effect.succeed({ ok: false as const, tag: e._tag }),
134 ),
135 );
136 return yield* HttpServerResponse.json(result);
137 }
138
139 return yield* HttpServerResponse.json(
140 { error: "Not found", method: request.method, pathname },
141 { status: 404 },
142 );
143 }).pipe(Effect.orDie),
144 };
145 }).pipe(
146 Effect.provide(
147 Layer.mergeAll(
148 Account.GetAccountInformationHttp,
149 Account.GetContactInformationHttp,
150 Account.GetAlternateContactHttp,
151 Account.ListRegionsHttp,
152 Account.GetRegionOptStatusHttp,
153 ),
154 ),
155 ),
156 );