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

main
ts 161 lines 5.52 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { HealthCheck } from "./HealthCheck.ts";
5 import type { HostedZone } from "./HostedZone.ts";
6
7 /**
8 * Shared scaffolding for the Route 53 runtime bindings.
9 *
10 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
11 * thin `Layer.effect(Cap, makeRoute53…HttpBinding({ … }))` over one of the
12 * three builders below. Everything except the operation, the IAM action
13 * list, and (for scoped operations) the injected zone / health check id is
14 * boilerplate.
15 *
16 * Route 53 ARNs are global and account-less:
17 * `arn:aws:route53:::hostedzone/{Id}` / `arn:aws:route53:::healthcheck/{Id}`.
18 */
19
20 /**
21 * Build the impl Effect for a hosted-zone-scoped Route 53 operation: the
22 * runtime callable injects the bound {@link HostedZone}'s id under
23 * `requestKey` and the deploy-time half grants `actions` on the zone's ARN
24 * (or `*` when the action does not support resource-level permissions).
25 */
26 export const makeRoute53ZoneHttpBinding = <I, A, E, R>(options: {
27 /** Fully-qualified binding tag, e.g. `AWS.Route53.ListResourceRecordSets`. */
28 tag: string;
29 /** The distilled operation; the zone id is injected under `requestKey`. */
30 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
31 /** IAM actions granted on the hosted zone ARN. */
32 actions: readonly string[];
33 /**
34 * Request key the zone id is injected under (`getHostedZone` uses `Id`,
35 * everything else `HostedZoneId`).
36 * @default "HostedZoneId"
37 */
38 requestKey?: "HostedZoneId" | "Id";
39 /**
40 * Grant on `*` instead of the zone ARN — for actions without
41 * resource-level permission support (e.g. `route53:TestDNSAnswer`).
42 */
43 wildcardIam?: boolean;
44 }) =>
45 Effect.gen(function* () {
46 const bind = yield* AWSBindings.Bind;
47 const op = yield* AWSBindings.withCredentials(options.operation);
48 const requestKey = options.requestKey ?? "HostedZoneId";
49
50 return Effect.fn(function* (zone: HostedZone) {
51 const zoneId = yield* zone.id;
52 if (!globalThis.__ALCHEMY_RUNTIME__) {
53 yield* bind`${options.tag}(${zone})`({
54 policyStatements: [
55 {
56 Effect: "Allow",
57 Action: [...options.actions],
58 Resource: options.wildcardIam
59 ? ["*"]
60 : [Output.interpolate`arn:aws:route53:::hostedzone/${zone.id}`],
61 },
62 ],
63 });
64 }
65 return Effect.fn(`${options.tag}(${zone.LogicalId})`)(function* (
66 request?: Omit<I, "HostedZoneId" | "Id">,
67 ) {
68 return yield* op({
69 ...request,
70 [requestKey]: yield* zoneId,
71 } as I);
72 });
73 });
74 });
75
76 /**
77 * Build the impl Effect for a health-check-scoped Route 53 operation: the
78 * runtime callable injects the bound {@link HealthCheck}'s id as
79 * `HealthCheckId` and the deploy-time half grants `actions` on the health
80 * check's ARN.
81 */
82 export const makeRoute53HealthCheckHttpBinding = <
83 I extends { HealthCheckId: string },
84 A,
85 E,
86 R,
87 >(options: {
88 /** Fully-qualified binding tag, e.g. `AWS.Route53.GetHealthCheckStatus`. */
89 tag: string;
90 /** The distilled operation; `HealthCheckId` is injected from the check. */
91 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
92 /** IAM actions granted on the health check ARN. */
93 actions: readonly string[];
94 }) =>
95 Effect.gen(function* () {
96 const bind = yield* AWSBindings.Bind;
97 const op = yield* AWSBindings.withCredentials(options.operation);
98
99 return Effect.fn(function* (healthCheck: HealthCheck) {
100 const healthCheckId = yield* healthCheck.id;
101 if (!globalThis.__ALCHEMY_RUNTIME__) {
102 yield* bind`${options.tag}(${healthCheck})`({
103 policyStatements: [
104 {
105 Effect: "Allow",
106 Action: [...options.actions],
107 Resource: [Output.interpolate`arn:aws:route53:::healthcheck/${healthCheck.id}`],
108 },
109 ],
110 });
111 }
112 return Effect.fn(`${options.tag}(${healthCheck.LogicalId})`)(function* (
113 request?: Omit<I, "HealthCheckId">,
114 ) {
115 return yield* op({
116 ...request,
117 HealthCheckId: yield* healthCheckId,
118 } as I);
119 });
120 });
121 });
122
123 /**
124 * Build the impl Effect for an account-level Route 53 operation (no resource
125 * argument): the deploy-time half grants `actions` on `resources`
126 * (default `*`).
127 */
128 export const makeRoute53AccountHttpBinding = <I, A, E, R>(options: {
129 /** Fully-qualified binding tag, e.g. `AWS.Route53.ListHostedZones`. */
130 tag: string;
131 /** The distilled operation, invoked with the caller's request as-is. */
132 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
133 /** IAM actions granted on `resources`. */
134 actions: readonly string[];
135 /**
136 * IAM resources the actions are granted on.
137 * @default ["*"]
138 */
139 resources?: readonly string[];
140 }) =>
141 Effect.gen(function* () {
142 const bind = yield* AWSBindings.Bind;
143 const op = yield* AWSBindings.withCredentials(options.operation);
144
145 return Effect.fn(function* () {
146 if (!globalThis.__ALCHEMY_RUNTIME__) {
147 yield* bind`${options.tag}()`({
148 policyStatements: [
149 {
150 Effect: "Allow",
151 Action: [...options.actions],
152 Resource: [...(options.resources ?? ["*"])],
153 },
154 ],
155 });
156 }
157 return Effect.fn(options.tag)(function* (request?: I) {
158 return yield* op((request ?? {}) as I);
159 });
160 });
161 });