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

main
ts 184 lines 6.96 KB
Raw
1 import * as Output from "alchemy/Output";
2 import type { Output as OutputType } from "alchemy/Output";
3 import * as Effect from "effect/Effect";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import type { DataSet } from "./DataSet.ts";
6 import type { Revision } from "./Revision.ts";
7
8 /**
9 * Shared scaffolding for AWS Data Exchange HTTP bindings.
10 *
11 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
12 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the three
13 * builders below. Everything except the operation, the IAM action list, and
14 * the granted ARNs is boilerplate: the runtime callable injects `DataSetId`
15 * (and `RevisionId` for revision-scoped operations) from the bound resource.
16 */
17
18 /** The bound data set's own ARN (data-set-level actions). */
19 export const dataSetArns = (dataSet: DataSet): OutputType<string>[] => [dataSet.dataSetArn];
20
21 /**
22 * ARN matching every asset in the bound data set
23 * (`arn:…:data-sets/{id}/revisions/star/assets/star`) — asset-level actions
24 * like `dataexchange:SendApiAsset` authorize against the asset resource.
25 */
26 export const dataSetAssetArns = (dataSet: DataSet): OutputType<string>[] => [
27 dataSet.dataSetArn.pipe(Output.map((arn) => `${arn}/revisions/*/assets/*`)),
28 ];
29
30 /**
31 * The bound data set's ARN plus a wildcard over every sub-resource
32 * (`arn:…:data-sets/{id}` and `arn:…:data-sets/{id}/star`) — IAM wildcards
33 * match across `/`, so one pattern covers revisions and assets alike. Used
34 * by the revision/asset data-plane bindings that receive `RevisionId` and
35 * `AssetId` at runtime (e.g. a Lambda that publishes a fresh revision on a
36 * schedule needs the grant to cover revisions that do not exist yet).
37 */
38 export const dataSetAndSubresourceArns = (dataSet: DataSet): OutputType<string>[] => [
39 dataSet.dataSetArn,
40 Output.interpolate`${dataSet.dataSetArn}/*`,
41 ];
42
43 /** The bound revision's own ARN (revision-level actions). */
44 export const revisionArns = (revision: Revision): OutputType<string>[] => [revision.revisionArn];
45
46 /**
47 * ARN matching every asset in the bound revision
48 * (`arn:…:data-sets/{ds}/revisions/{rev}/assets/star`) — asset-level
49 * actions like `dataexchange:GetAsset` authorize against the asset resource.
50 */
51 export const revisionAssetArns = (revision: Revision): OutputType<string>[] => [
52 revision.revisionArn.pipe(Output.map((arn) => `${arn}/assets/*`)),
53 ];
54
55 /**
56 * Build the impl Effect for a Data Exchange operation scoped to a
57 * {@link DataSet}: the deploy-time half grants `actions` on `resources`
58 * (default: the data set's ARN), and the runtime half injects the data set's
59 * `DataSetId` into every request.
60 */
61 export const makeDataSetHttpBinding = <I extends { DataSetId: string }, A, E, R>(options: {
62 /** Fully-qualified binding tag, e.g. `AWS.DataExchange.GetDataSet`. */
63 tag: string;
64 /** The distilled operation. */
65 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
66 /** IAM actions granted on `resources`. */
67 actions: readonly string[];
68 /** ARNs the actions are granted on. @default the data set ARN */
69 resources?: (dataSet: DataSet) => OutputType<string>[];
70 }) =>
71 Effect.gen(function* () {
72 const bind = yield* AWSBindings.Bind;
73 const op = yield* AWSBindings.withCredentials(options.operation);
74
75 return Effect.fn(function* (dataSet: DataSet) {
76 const DataSetId = yield* dataSet.dataSetId;
77 if (!globalThis.__ALCHEMY_RUNTIME__) {
78 yield* bind`${options.tag}(${dataSet})`({
79 policyStatements: [
80 {
81 Effect: "Allow",
82 Action: [...options.actions],
83 Resource: (options.resources ?? dataSetArns)(dataSet),
84 },
85 ],
86 });
87 }
88 return Effect.fn(`${options.tag}(${dataSet.LogicalId})`)(function* (
89 request?: Omit<I, "DataSetId">,
90 ) {
91 const dataSetId = yield* DataSetId;
92 return yield* op({ ...request, DataSetId: dataSetId } as I);
93 });
94 });
95 });
96
97 /**
98 * Build the impl Effect for a Data Exchange operation scoped to a
99 * {@link Revision}: the deploy-time half grants `actions` on `resources`
100 * (default: the revision's ARN), and the runtime half injects the revision's
101 * `DataSetId` and `RevisionId` into every request.
102 */
103 export const makeRevisionHttpBinding = <
104 I extends { DataSetId: string; RevisionId: string },
105 A,
106 E,
107 R,
108 >(options: {
109 /** Fully-qualified binding tag, e.g. `AWS.DataExchange.GetRevision`. */
110 tag: string;
111 /** The distilled operation. */
112 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
113 /** IAM actions granted on `resources`. */
114 actions: readonly string[];
115 /** ARNs the actions are granted on. @default the revision ARN */
116 resources?: (revision: Revision) => OutputType<string>[];
117 }) =>
118 Effect.gen(function* () {
119 const bind = yield* AWSBindings.Bind;
120 const op = yield* AWSBindings.withCredentials(options.operation);
121
122 return Effect.fn(function* (revision: Revision) {
123 const DataSetId = yield* revision.dataSetId;
124 const RevisionId = yield* revision.revisionId;
125 if (!globalThis.__ALCHEMY_RUNTIME__) {
126 yield* bind`${options.tag}(${revision})`({
127 policyStatements: [
128 {
129 Effect: "Allow",
130 Action: [...options.actions],
131 Resource: (options.resources ?? revisionArns)(revision),
132 },
133 ],
134 });
135 }
136 return Effect.fn(`${options.tag}(${revision.LogicalId})`)(function* (
137 request?: Omit<I, "DataSetId" | "RevisionId">,
138 ) {
139 const dataSetId = yield* DataSetId;
140 const revisionId = yield* RevisionId;
141 return yield* op({
142 ...request,
143 DataSetId: dataSetId,
144 RevisionId: revisionId,
145 } as unknown as I);
146 });
147 });
148 });
149
150 /**
151 * Build the impl Effect for an account-level Data Exchange operation (jobs,
152 * data grants, enumerations). The deploy-time half grants `actions` on `*`
153 * — jobs and data grants are account-scoped resources whose ids only exist
154 * at runtime, so there is no resource ARN to scope down to.
155 */
156 export const makeDataExchangeAccountHttpBinding = <I, A, E, R>(options: {
157 /** Fully-qualified binding tag, e.g. `AWS.DataExchange.ListDataSets`. */
158 tag: string;
159 /** The distilled operation, invoked with the caller's request as-is. */
160 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
161 /** IAM actions granted on `*`. */
162 actions: readonly string[];
163 }) =>
164 Effect.gen(function* () {
165 const bind = yield* AWSBindings.Bind;
166 const op = yield* AWSBindings.withCredentials(options.operation);
167
168 return Effect.fn(function* () {
169 if (!globalThis.__ALCHEMY_RUNTIME__) {
170 yield* bind`${options.tag}()`({
171 policyStatements: [
172 {
173 Effect: "Allow",
174 Action: [...options.actions],
175 Resource: ["*"],
176 },
177 ],
178 });
179 }
180 return Effect.fn(options.tag)(function* (request?: I) {
181 return yield* op((request ?? {}) as I);
182 });
183 });
184 });