@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 178 lines 6.28 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 { Dashboard } from "./Dashboard.ts";
5 import type { DataSet } from "./DataSet.ts";
6
7 /**
8 * Shared scaffolding for Amazon QuickSight 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 builders
12 * below. Everything except the operation, the scoping resource, and the IAM
13 * action list is boilerplate.
14 *
15 * Every QuickSight API call requires the `AwsAccountId`. The runtime half of
16 * each binding derives it from the bound resource's ARN
17 * (`arn:aws:quicksight:{region}:{account}:{type}/{id}`), so no extra
18 * environment plumbing is needed inside the Lambda.
19 */
20
21 /** Extract the AWS account id from a QuickSight resource ARN. */
22 export const accountIdFromArn = (arn: string): string => arn.split(":")[4]!;
23
24 /**
25 * Build the impl Effect for a QuickSight operation scoped to a
26 * {@link Dashboard}: the deploy-time half grants `actions` on the bound
27 * dashboard's ARN, and the runtime half injects `AwsAccountId` (parsed from
28 * the dashboard ARN) and `DashboardId` into every request.
29 */
30 export const makeQuickSightDashboardHttpBinding = <
31 I extends { AwsAccountId: string; DashboardId: string },
32 A,
33 E,
34 R,
35 >(options: {
36 /** Fully-qualified binding tag, e.g. `AWS.QuickSight.StartDashboardSnapshotJob`. */
37 tag: string;
38 /** The distilled operation; `AwsAccountId` and `DashboardId` are injected. */
39 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
40 /** IAM actions granted on the dashboard ARN. */
41 actions: readonly string[];
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* (dashboard: Dashboard) {
48 const DashboardId = yield* dashboard.dashboardId;
49 const Arn = yield* dashboard.arn;
50 if (!globalThis.__ALCHEMY_RUNTIME__) {
51 yield* bind`${options.tag}(${dashboard})`({
52 policyStatements: [
53 {
54 Effect: "Allow",
55 Action: [...options.actions],
56 Resource: [dashboard.arn],
57 },
58 ],
59 });
60 }
61 return Effect.fn(`${options.tag}(${dashboard.LogicalId})`)(function* (
62 request: Omit<I, "AwsAccountId" | "DashboardId">,
63 ) {
64 const arn = yield* Arn;
65 return yield* op({
66 ...request,
67 AwsAccountId: accountIdFromArn(arn),
68 DashboardId: yield* DashboardId,
69 } as I);
70 });
71 });
72 });
73
74 /**
75 * Build the impl Effect for a QuickSight operation scoped to a
76 * {@link DataSet}: the deploy-time half grants `actions` on the bound
77 * dataset's ARN and its `…/ingestion/*` sub-resources, and the runtime half
78 * injects `AwsAccountId` (parsed from the dataset ARN) and `DataSetId` into
79 * every request.
80 */
81 export const makeQuickSightDataSetHttpBinding = <
82 I extends { AwsAccountId: string; DataSetId: string },
83 A,
84 E,
85 R,
86 >(options: {
87 /** Fully-qualified binding tag, e.g. `AWS.QuickSight.CreateIngestion`. */
88 tag: string;
89 /** The distilled operation; `AwsAccountId` and `DataSetId` are injected. */
90 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
91 /** IAM actions granted on the dataset ARN + its ingestion sub-resources. */
92 actions: readonly string[];
93 }) =>
94 Effect.gen(function* () {
95 const bind = yield* AWSBindings.Bind;
96 const op = yield* AWSBindings.withCredentials(options.operation);
97
98 return Effect.fn(function* (dataSet: DataSet) {
99 const DataSetId = yield* dataSet.dataSetId;
100 const Arn = yield* dataSet.arn;
101 if (!globalThis.__ALCHEMY_RUNTIME__) {
102 yield* bind`${options.tag}(${dataSet})`({
103 policyStatements: [
104 {
105 Effect: "Allow",
106 Action: [...options.actions],
107 Resource: [dataSet.arn, Output.interpolate`${dataSet.arn}/ingestion/*`],
108 },
109 ],
110 });
111 }
112 return Effect.fn(`${options.tag}(${dataSet.LogicalId})`)(function* (
113 request?: Omit<I, "AwsAccountId" | "DataSetId">,
114 ) {
115 const arn = yield* Arn;
116 return yield* op({
117 ...request,
118 AwsAccountId: accountIdFromArn(arn),
119 DataSetId: yield* DataSetId,
120 } as I);
121 });
122 });
123 });
124
125 /**
126 * Build the impl Effect for a QuickSight embed-URL operation scoped to a
127 * {@link Dashboard}: the deploy-time half grants `actions` on `*` (embed-URL
128 * actions authorize against user/namespace ARNs that are unknown at deploy
129 * time), and the runtime half injects `AwsAccountId` and defaults the
130 * experience configuration to the bound dashboard via `applyDefaults`.
131 */
132 export const makeQuickSightEmbedHttpBinding = <
133 I extends { AwsAccountId: string },
134 Req,
135 A,
136 E,
137 R,
138 >(options: {
139 /** Fully-qualified binding tag, e.g. `AWS.QuickSight.GenerateEmbedUrlForRegisteredUser`. */
140 tag: string;
141 /** The distilled operation; `AwsAccountId` is injected. */
142 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
143 /** IAM actions granted on `*`. */
144 actions: readonly string[];
145 /** Fill dashboard-derived defaults into the caller's request. */
146 applyDefaults: (
147 request: Req,
148 dashboard: { dashboardId: string; arn: string },
149 ) => Omit<I, "AwsAccountId">;
150 }) =>
151 Effect.gen(function* () {
152 const bind = yield* AWSBindings.Bind;
153 const op = yield* AWSBindings.withCredentials(options.operation);
154
155 return Effect.fn(function* (dashboard: Dashboard) {
156 const DashboardId = yield* dashboard.dashboardId;
157 const Arn = yield* dashboard.arn;
158 if (!globalThis.__ALCHEMY_RUNTIME__) {
159 yield* bind`${options.tag}(${dashboard})`({
160 policyStatements: [
161 {
162 Effect: "Allow",
163 Action: [...options.actions],
164 Resource: ["*"],
165 },
166 ],
167 });
168 }
169 return Effect.fn(`${options.tag}(${dashboard.LogicalId})`)(function* (request: Req) {
170 const arn = yield* Arn;
171 const dashboardId = yield* DashboardId;
172 return yield* op({
173 ...options.applyDefaults(request, { dashboardId, arn }),
174 AwsAccountId: accountIdFromArn(arn),
175 } as I);
176 });
177 });
178 });