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

main
ts 108 lines 3.89 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 { Domain } from "./Domain.ts";
5 import type { Environment } from "./Environment.ts";
6
7 /**
8 * Shared scaffolding for Amazon DataZone HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
11 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
12 * below. Everything except the operation and the IAM action list is
13 * boilerplate: DataZone authorizes every action on the domain resource, so
14 * the deploy-time half always grants `actions` on the bound domain's ARN.
15 */
16
17 /**
18 * Build the impl Effect for a domain-scoped DataZone operation: the runtime
19 * callable injects the bound {@link Domain}'s id as `domainIdentifier` into
20 * every request, and the deploy-time half grants `actions` on the domain ARN.
21 */
22 export const makeDataZoneDomainHttpBinding = <
23 I extends { domainIdentifier: string },
24 A,
25 E,
26 R,
27 >(options: {
28 /** Fully-qualified binding tag, e.g. `AWS.DataZone.Search`. */
29 tag: string;
30 /** The distilled operation; `domainIdentifier` is injected. */
31 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
32 /** IAM actions granted on the domain ARN. */
33 actions: readonly string[];
34 }) =>
35 Effect.gen(function* () {
36 const bind = yield* AWSBindings.Bind;
37 const op = yield* AWSBindings.withCredentials(options.operation);
38
39 return Effect.fn(function* (domain: Domain) {
40 const DomainId = yield* domain.domainId;
41 if (!globalThis.__ALCHEMY_RUNTIME__) {
42 yield* bind`${options.tag}(${domain})`({
43 policyStatements: [
44 {
45 Effect: "Allow",
46 Action: [...options.actions],
47 Resource: [domain.domainArn],
48 },
49 ],
50 });
51 }
52 return Effect.fn(`${options.tag}(${domain.LogicalId})`)(function* (
53 request?: Omit<I, "domainIdentifier">,
54 ) {
55 const domainIdentifier = yield* DomainId;
56 return yield* op({ ...request, domainIdentifier } as I);
57 });
58 });
59 });
60
61 /**
62 * Build the impl Effect for an environment-scoped DataZone operation: the
63 * runtime callable injects the bound {@link Environment}'s domain id and
64 * environment id, and the deploy-time half grants `actions` on the parent
65 * domain's ARN (DataZone authorizes every action on the domain resource).
66 */
67 export const makeDataZoneEnvironmentHttpBinding = <
68 I extends { domainIdentifier: string; environmentIdentifier: string },
69 A,
70 E,
71 R,
72 >(options: {
73 /** Fully-qualified binding tag, e.g. `AWS.DataZone.GetEnvironmentCredentials`. */
74 tag: string;
75 /** The distilled operation; both identifiers are injected. */
76 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
77 /** IAM actions granted on the parent domain ARN. */
78 actions: readonly string[];
79 }) =>
80 Effect.gen(function* () {
81 const bind = yield* AWSBindings.Bind;
82 const op = yield* AWSBindings.withCredentials(options.operation);
83
84 return Effect.fn(function* (environment: Environment) {
85 const DomainId = yield* environment.domainId;
86 const EnvironmentId = yield* environment.environmentId;
87 if (!globalThis.__ALCHEMY_RUNTIME__) {
88 yield* bind`${options.tag}(${environment})`({
89 policyStatements: [
90 {
91 Effect: "Allow",
92 Action: [...options.actions],
93 Resource: [Output.interpolate`arn:aws:datazone:*:*:domain/${environment.domainId}`],
94 },
95 ],
96 });
97 }
98 return Effect.fn(`${options.tag}(${environment.LogicalId})`)(function* (
99 request?: Omit<I, "domainIdentifier" | "environmentIdentifier">,
100 ) {
101 return yield* op({
102 ...request,
103 domainIdentifier: yield* DomainId,
104 environmentIdentifier: yield* EnvironmentId,
105 } as I);
106 });
107 });
108 });