@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 109 lines 3.72 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { Export } from "./Export.ts";
4
5 /**
6 * Shared HTTP scaffolding for the BCM Data Exports runtime bindings.
7 *
8 * Every capability follows the same shape — resolve the distilled operation,
9 * register an IAM policy statement on the binding host, and return a runtime
10 * callable. The only variation is the operation, the IAM action(s), and
11 * whether the binding is scoped to one {@link Export} (injecting its
12 * `ExportArn`) or to the whole account (table-dictionary and list
13 * operations, which are not resource-scoped).
14 *
15 * BCM Data Exports is a global service — its endpoint resolver routes every
16 * standard-partition region to the implicit global endpoint, so no region
17 * pinning is needed.
18 *
19 * @internal — not exported from `index.ts`.
20 */
21
22 /**
23 * Build the implementation effect for an export-scoped capability: the
24 * runtime callable injects the bound {@link Export}'s ARN as `ExportArn` and
25 * the deploy-time half grants `iamActions` on the export ARN.
26 */
27 export const makeExportHttpBinding = <I extends { ExportArn: string }, A, E, R>(options: {
28 /**
29 * Short capability name used in the binding sid and runtime span, e.g.
30 * `"GetExecution"`.
31 */
32 capability: string;
33 /**
34 * IAM actions granted on the export ARN, e.g.
35 * `["bcm-data-exports:GetExecution"]`.
36 */
37 iamActions: readonly string[];
38 /**
39 * The distilled operation; `ExportArn` is injected from the resource.
40 */
41 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
42 }) =>
43 Effect.gen(function* () {
44 const bind = yield* AWSBindings.Bind;
45 const op = yield* AWSBindings.withCredentials(options.operation);
46
47 return Effect.fn(function* (dataExport: Export) {
48 const ExportArn = yield* dataExport.exportArn;
49 if (!globalThis.__ALCHEMY_RUNTIME__) {
50 yield* bind`AWS.BCMDataExports.${options.capability}(${dataExport})`({
51 policyStatements: [
52 {
53 Effect: "Allow",
54 Action: [...options.iamActions],
55 Resource: [dataExport.exportArn],
56 },
57 ],
58 });
59 }
60 return Effect.fn(`AWS.BCMDataExports.${options.capability}(${dataExport.LogicalId})`)(
61 function* (request?: Omit<I, "ExportArn">) {
62 return yield* op({ ...request, ExportArn: yield* ExportArn } as I);
63 },
64 );
65 });
66 });
67
68 /**
69 * Build the implementation effect for an account-level capability (no
70 * export argument — the table dictionary and list operations). The
71 * deploy-time half grants `iamActions` on `*` because these operations are
72 * not resource-scoped.
73 */
74 export const makeDataExportsAccountHttpBinding = <I extends object, A, E, R>(options: {
75 /**
76 * Short capability name used in the binding sid and runtime span, e.g.
77 * `"ListTables"`.
78 */
79 capability: string;
80 /**
81 * IAM actions granted on `Resource: ["*"]`.
82 */
83 iamActions: readonly string[];
84 /**
85 * The distilled operation implementing the capability.
86 */
87 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
88 }) =>
89 Effect.gen(function* () {
90 const bind = yield* AWSBindings.Bind;
91 const op = yield* AWSBindings.withCredentials(options.operation);
92
93 return Effect.fn(function* () {
94 if (!globalThis.__ALCHEMY_RUNTIME__) {
95 yield* bind`AWS.BCMDataExports.${options.capability}()`({
96 policyStatements: [
97 {
98 Effect: "Allow",
99 Action: [...options.iamActions],
100 Resource: ["*"],
101 },
102 ],
103 });
104 }
105 return Effect.fn(`AWS.BCMDataExports.${options.capability}`)(function* (request?: I) {
106 return yield* op((request ?? {}) as I);
107 });
108 });
109 });