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

main
ts 222 lines 8.6 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Route53Domains from "@alchemy.run/aws/Route53Domains";
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 Result from "effect/Result";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 export class Route53DomainsTestFunction extends Lambda.Function<Lambda.Function>()(
14 "Route53DomainsTestFunction",
15 ) {}
16
17 // Serialize a Result into { ok, errorTag } JSON — routes that exercise
18 // error paths must reach the API (proving the IAM grant and the us-east-1
19 // pin) and fail with a typed domain-level error, never AccessDenied.
20 const errorRoute = <A, E extends { _tag: string }>(
21 effect: Effect.Effect<A, E>,
22 onSuccess: (value: A) => object,
23 ) =>
24 Effect.gen(function* () {
25 const result = yield* Effect.result(effect);
26 if (Result.isFailure(result)) {
27 return yield* HttpServerResponse.json({
28 ok: false,
29 errorTag: result.failure._tag,
30 errorMessage: String((result.failure as { message?: unknown }).message ?? ""),
31 });
32 }
33 return yield* HttpServerResponse.json({
34 ok: true,
35 ...onSuccess(result.success),
36 });
37 });
38
39 export default Route53DomainsTestFunction.make(
40 {
41 main,
42 functionUrl: true,
43 // Route 53 Domains calls cross to us-east-1; give cold starts headroom
44 // over the 3s default.
45 timeout: Duration.seconds(30),
46 },
47 Effect.gen(function* () {
48 const checkDomainAvailability = yield* Route53Domains.CheckDomainAvailability();
49 const checkDomainTransferability = yield* Route53Domains.CheckDomainTransferability();
50 const getDomainDetail = yield* Route53Domains.GetDomainDetail();
51 const getDomainSuggestions = yield* Route53Domains.GetDomainSuggestions();
52 const getOperationDetail = yield* Route53Domains.GetOperationDetail();
53 const listDomains = yield* Route53Domains.ListDomains();
54 const listOperations = yield* Route53Domains.ListOperations();
55 const listPrices = yield* Route53Domains.ListPrices();
56 const registerDomain = yield* Route53Domains.RegisterDomain();
57 const renewDomain = yield* Route53Domains.RenewDomain();
58 const retrieveDomainAuthCode = yield* Route53Domains.RetrieveDomainAuthCode();
59 const updateDomainNameservers = yield* Route53Domains.UpdateDomainNameservers();
60
61 return {
62 fetch: Effect.gen(function* () {
63 const request = yield* HttpServerRequest;
64 const url = new URL(request.originalUrl);
65 const pathname = url.pathname;
66
67 // Cheap readiness route — no Route 53 Domains call.
68 if (request.method === "GET" && pathname === "/ping") {
69 return yield* HttpServerResponse.json({ ok: true });
70 }
71
72 if (request.method === "GET" && pathname === "/availability") {
73 const result = yield* checkDomainAvailability({
74 // Deterministic probe name — availability checks are read-only
75 // and free; this suite NEVER registers a domain.
76 DomainName: "alchemy-effect-r53d-probe-a32892.com",
77 });
78 return yield* HttpServerResponse.json({
79 availability: result.Availability,
80 });
81 }
82
83 if (request.method === "GET" && pathname === "/transferability") {
84 const result = yield* checkDomainTransferability({
85 DomainName: "example.com",
86 });
87 return yield* HttpServerResponse.json({
88 transferable: result.Transferability?.Transferable,
89 });
90 }
91
92 if (request.method === "GET" && pathname === "/suggestions") {
93 const result = yield* getDomainSuggestions({
94 DomainName: "example.com",
95 SuggestionCount: 5,
96 OnlyAvailable: false,
97 });
98 return yield* HttpServerResponse.json({
99 count: (result.SuggestionsList ?? []).length,
100 });
101 }
102
103 if (request.method === "GET" && pathname === "/prices") {
104 const result = yield* listPrices({ Tld: "com" });
105 return yield* HttpServerResponse.json({
106 count: (result.Prices ?? []).length,
107 registrationPrice: result.Prices?.[0]?.RegistrationPrice?.Price,
108 });
109 }
110
111 if (request.method === "GET" && pathname === "/operations") {
112 const result = yield* listOperations({ MaxItems: 10 });
113 return yield* HttpServerResponse.json({
114 count: (result.Operations ?? []).length,
115 });
116 }
117
118 if (request.method === "GET" && pathname === "/domains") {
119 const result = yield* listDomains({ MaxItems: 100 });
120 return yield* HttpServerResponse.json({
121 count: (result.Domains ?? []).length,
122 names: (result.Domains ?? [])
123 .map((domain) => domain.DomainName)
124 .filter((name) => name !== undefined),
125 });
126 }
127
128 if (request.method === "GET" && pathname === "/detail") {
129 // example.com is never registered in the test account — the call
130 // must reach the API (proving the IAM grant and us-east-1 pin)
131 // and come back as a typed domain-level error, not AccessDenied.
132 return yield* errorRoute(getDomainDetail({ DomainName: "example.com" }), (detail) => ({
133 domainName: detail.DomainName,
134 }));
135 }
136
137 if (request.method === "GET" && pathname === "/operation-detail") {
138 // A syntactically valid but non-existent operation id — the call
139 // must reach the API and fail with a typed InvalidInput, never
140 // AccessDenied.
141 return yield* errorRoute(
142 getOperationDetail({
143 OperationId: "00000000-0000-0000-0000-000000000000",
144 }),
145 (detail) => ({ status: detail.Status }),
146 );
147 }
148
149 if (request.method === "GET" && pathname === "/auth-code") {
150 // example.com is not in the account — typed domain-level error.
151 // On success the AuthCode would be Redacted; this route never
152 // returns it.
153 return yield* errorRoute(retrieveDomainAuthCode({ DomainName: "example.com" }), () => ({
154 hasAuthCode: true,
155 }));
156 }
157
158 if (request.method === "GET" && pathname === "/nameservers") {
159 // example.com is not in the account — typed domain-level error.
160 return yield* errorRoute(
161 updateDomainNameservers({
162 DomainName: "example.com",
163 Nameservers: [{ Name: "ns-1.awsdns-01.org" }],
164 }),
165 (r) => ({ operationId: r.OperationId }),
166 );
167 }
168
169 if (request.method === "GET" && pathname === "/renew") {
170 // example.com is not in the account — typed domain-level error.
171 // No renewal is ever billed by this suite.
172 return yield* errorRoute(
173 renewDomain({
174 DomainName: "example.com",
175 DurationInYears: 1,
176 CurrentExpiryYear: 2030,
177 }),
178 (r) => ({ operationId: r.OperationId }),
179 );
180 }
181
182 if (request.method === "GET" && pathname === "/register") {
183 // Deliberately unsupported TLD — the request reaches the API and
184 // is rejected with a typed error before any registration or
185 // billing can occur. This suite NEVER registers a domain.
186 return yield* errorRoute(
187 registerDomain({
188 DomainName: "alchemy-effect-r53d-probe-a32892.invalidtld99",
189 DurationInYears: 1,
190 AdminContact: {},
191 RegistrantContact: {},
192 TechContact: {},
193 }),
194 (r) => ({ operationId: r.OperationId }),
195 );
196 }
197
198 return yield* HttpServerResponse.json(
199 { error: "Not found", method: request.method, pathname },
200 { status: 404 },
201 );
202 }).pipe(Effect.orDie),
203 };
204 }).pipe(
205 Effect.provide(
206 Layer.mergeAll(
207 Route53Domains.CheckDomainAvailabilityHttp,
208 Route53Domains.CheckDomainTransferabilityHttp,
209 Route53Domains.GetDomainDetailHttp,
210 Route53Domains.GetDomainSuggestionsHttp,
211 Route53Domains.GetOperationDetailHttp,
212 Route53Domains.ListDomainsHttp,
213 Route53Domains.ListOperationsHttp,
214 Route53Domains.ListPricesHttp,
215 Route53Domains.RegisterDomainHttp,
216 Route53Domains.RenewDomainHttp,
217 Route53Domains.RetrieveDomainAuthCodeHttp,
218 Route53Domains.UpdateDomainNameserversHttp,
219 ),
220 ),
221 ),
222 );