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

main
ts 144 lines 5.09 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { PolicyStatement } from "../IAM/Policy.ts";
4 import type { Service } from "./Service.ts";
5
6 /**
7 * Shared scaffolding for AWS Cloud Map HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the three
11 * builders below. Everything except the operation, the IAM action list, and
12 * the injected identifier is boilerplate.
13 */
14
15 /**
16 * Build the impl Effect for a service-scoped control/data operation. The
17 * runtime callable injects the bound {@link Service}'s id as the request's
18 * `ServiceId`; the deploy-time half grants `actions` on the service ARN
19 * plus any `extraStatements` (e.g. the Route 53 record management Cloud Map
20 * performs on the caller's behalf for DNS services).
21 */
22 export const makeCloudMapServiceHttpBinding = <I extends { ServiceId: string }, A, E, R>(options: {
23 /** Fully-qualified binding tag, e.g. `AWS.CloudMap.GetInstance`. */
24 tag: string;
25 /** The distilled operation; `ServiceId` is injected from the service. */
26 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
27 /** IAM actions granted on the service ARN. */
28 actions: readonly string[];
29 /** Additional statements granted alongside the service-scoped one. */
30 extraStatements?: readonly PolicyStatement[];
31 }) =>
32 Effect.gen(function* () {
33 const bind = yield* AWSBindings.Bind;
34 const op = yield* AWSBindings.withCredentials(options.operation);
35
36 return Effect.fn(function* (service: Service) {
37 const ServiceId = yield* service.serviceId;
38 if (!globalThis.__ALCHEMY_RUNTIME__) {
39 yield* bind`${options.tag}(${service})`({
40 policyStatements: [
41 {
42 Effect: "Allow",
43 Action: [...options.actions],
44 Resource: [service.serviceArn],
45 },
46 ...(options.extraStatements ?? []),
47 ],
48 });
49 }
50 return Effect.fn(`${options.tag}(${service.LogicalId})`)(function* (
51 request?: Omit<I, "ServiceId">,
52 ) {
53 return yield* op({
54 ...request,
55 ServiceId: yield* ServiceId,
56 } as I);
57 });
58 });
59 });
60
61 /**
62 * Build the impl Effect for a discovery data-plane operation
63 * (`DiscoverInstances` / `DiscoverInstancesRevision`). The runtime callable
64 * injects the bound {@link Service}'s namespace and service names; the
65 * deploy-time half grants `actions` on `*` — the discovery data-plane
66 * actions do not support resource-level IAM permissions.
67 */
68 export const makeCloudMapDiscoveryHttpBinding = <
69 I extends { NamespaceName: string; ServiceName: string },
70 A,
71 E,
72 R,
73 >(options: {
74 /** Fully-qualified binding tag, e.g. `AWS.CloudMap.DiscoverInstances`. */
75 tag: string;
76 /** The distilled operation; the names are injected from the service. */
77 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
78 /** IAM actions granted on `*` (no resource-level scoping). */
79 actions: readonly string[];
80 }) =>
81 Effect.gen(function* () {
82 const bind = yield* AWSBindings.Bind;
83 const op = yield* AWSBindings.withCredentials(options.operation);
84
85 return Effect.fn(function* (service: Service) {
86 const NamespaceName = yield* service.namespaceName;
87 const ServiceName = yield* service.serviceName;
88 if (!globalThis.__ALCHEMY_RUNTIME__) {
89 yield* bind`${options.tag}(${service})`({
90 policyStatements: [
91 {
92 Effect: "Allow",
93 Action: [...options.actions],
94 Resource: ["*"],
95 },
96 ],
97 });
98 }
99 return Effect.fn(`${options.tag}(${service.LogicalId})`)(function* (
100 request?: Omit<I, "NamespaceName" | "ServiceName">,
101 ) {
102 return yield* op({
103 ...request,
104 NamespaceName: yield* NamespaceName,
105 ServiceName: yield* ServiceName,
106 } as I);
107 });
108 });
109 });
110
111 /**
112 * Build the impl Effect for an account-level operation (`GetOperation`).
113 * The deploy-time half grants `actions` on `*` — these actions do not
114 * support resource-level IAM permissions.
115 */
116 export const makeCloudMapAccountHttpBinding = <I, A, E, R>(options: {
117 /** Fully-qualified binding tag, e.g. `AWS.CloudMap.GetOperation`. */
118 tag: string;
119 /** The distilled operation, invoked with the caller's request as-is. */
120 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
121 /** IAM actions granted on `*`. */
122 actions: readonly string[];
123 }) =>
124 Effect.gen(function* () {
125 const bind = yield* AWSBindings.Bind;
126 const op = yield* AWSBindings.withCredentials(options.operation);
127
128 return Effect.fn(function* () {
129 if (!globalThis.__ALCHEMY_RUNTIME__) {
130 yield* bind`${options.tag}()`({
131 policyStatements: [
132 {
133 Effect: "Allow",
134 Action: [...options.actions],
135 Resource: ["*"],
136 },
137 ],
138 });
139 }
140 return Effect.fn(options.tag)(function* (request?: I) {
141 return yield* op((request ?? {}) as I);
142 });
143 });
144 });