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

main
ts 80 lines 2.99 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Route53Profiles from "@alchemy.run/aws/Route53Profiles";
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 path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 export class ProfilesTestFunction extends Lambda.Function<Lambda.Function>()(
13 "ProfilesTestFunction",
14 ) {}
15
16 export default ProfilesTestFunction.make(
17 {
18 main,
19 functionUrl: true,
20 timeout: Duration.seconds(30),
21 },
22 Effect.gen(function* () {
23 // The profile the bindings are bound to. Nothing is ever associated with
24 // it (no VPC association, no attached DNS resources), so both lists
25 // legitimately return empty item lists — which still proves the IAM
26 // grants and the ProfileId injection end-to-end.
27 const profile = yield* Route53Profiles.Profile("BindingProfile");
28
29 const listProfileAssociations = yield* Route53Profiles.ListProfileAssociations(profile);
30 const listProfileResourceAssociations =
31 yield* Route53Profiles.ListProfileResourceAssociations(profile);
32
33 const bound = { listProfileAssociations, listProfileResourceAssociations };
34
35 return {
36 fetch: Effect.gen(function* () {
37 const request = yield* HttpServerRequest;
38 const url = new URL(request.originalUrl);
39 const pathname = url.pathname;
40
41 if (request.method === "GET" && pathname === "/bindings") {
42 return yield* HttpServerResponse.json({
43 bound: Object.keys(bound),
44 });
45 }
46
47 // Account-wide list filtered to the bound profile: the ProfileId is
48 // injected from the binding.
49 if (request.method === "GET" && pathname === "/associations") {
50 const { ProfileAssociations = [] } = yield* listProfileAssociations();
51 return yield* HttpServerResponse.json({
52 count: ProfileAssociations.length,
53 resourceIds: ProfileAssociations.map((item) => item.ResourceId),
54 });
55 }
56
57 // Profile-scoped list: the ProfileId is injected from the binding.
58 if (request.method === "GET" && pathname === "/resource-associations") {
59 const { ProfileResourceAssociations = [] } = yield* listProfileResourceAssociations();
60 return yield* HttpServerResponse.json({
61 count: ProfileResourceAssociations.length,
62 resourceArns: ProfileResourceAssociations.map((item) => item.ResourceArn),
63 });
64 }
65
66 return yield* HttpServerResponse.json(
67 { error: "Not found", method: request.method, pathname },
68 { status: 404 },
69 );
70 }).pipe(Effect.orDie),
71 };
72 }).pipe(
73 Effect.provide(
74 Layer.mergeAll(
75 Route53Profiles.ListProfileAssociationsHttp,
76 Route53Profiles.ListProfileResourceAssociationsHttp,
77 ),
78 ),
79 ),
80 );