@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 207 lines 8.28 KB
Raw
1 import * as TSQ from "@distilled.cloud/aws/timestream-query";
2 import * as TSW from "@distilled.cloud/aws/timestream-write";
3 import * as Output from "alchemy/Output";
4 import * as Effect from "effect/Effect";
5 import * as AWSBindings from "../AWSBindings.ts";
6 import { discover, withEndpoint } from "./internal.ts";
7 import type { Table } from "./Table.ts";
8
9 /**
10 * Shared scaffolding for Amazon Timestream HTTP bindings.
11 *
12 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
13 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
14 * below. Everything except the operation, the IAM action list, and the
15 * injected identifier is boilerplate.
16 *
17 * Timestream's wrinkle: every request must be routed through the
18 * cell-specific endpoint returned by `DescribeEndpoints` (see
19 * {@link discover}), so each builder captures its service's
20 * `describeEndpoints` at layer init (yield-first, so the runtime callable is
21 * requirement-free) and every binding additionally grants the unscoped
22 * `timestream:DescribeEndpoints` the discovery flow needs.
23 */
24
25 const describeEndpointsStatement = {
26 Effect: "Allow" as const,
27 // Endpoint discovery is required and is not scoped to a resource.
28 Action: ["timestream:DescribeEndpoints"],
29 Resource: ["*"],
30 };
31
32 /**
33 * Build the impl Effect for a table-scoped `timestream-write` operation. The
34 * deploy-time half grants `actions` on the bound {@link Table}'s ARN (plus
35 * its owning database's ARN when `grantDatabaseArn` is set — batch load
36 * authorizes against both); the runtime callable shapes the caller's request
37 * via `toRequest`, injecting the table's physical names.
38 */
39 export const makeWriteTableHttpBinding = <Req, I, A, E, R>(options: {
40 /** Fully-qualified binding tag, e.g. `AWS.Timestream.WriteRecords`. */
41 tag: string;
42 /** The distilled `timestream-write` operation. */
43 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
44 /** IAM actions granted on the table ARN. */
45 actions: readonly string[];
46 /** Also grant `actions` on the owning database's ARN (batch load). */
47 grantDatabaseArn?: boolean;
48 /** Shape the wire request from the caller's request + the table names. */
49 toRequest: (request: Req, names: { DatabaseName: string; TableName: string }) => I;
50 }) =>
51 Effect.gen(function* () {
52 const bind = yield* AWSBindings.Bind;
53 // Yield-first captures the operations' services (Credentials/Region/
54 // HttpClient) at layer init so the runtime callable is requirement-free.
55 const op = yield* AWSBindings.withCredentials(options.operation);
56 const describeEndpoints = yield* AWSBindings.withCredentials(TSW.describeEndpoints);
57 const withWriteEndpoint = withEndpoint(discover("write", describeEndpoints({})));
58 return Effect.fn(function* (table: Table) {
59 const DatabaseName = yield* table.databaseName;
60 const TableName = yield* table.tableName;
61 if (!globalThis.__ALCHEMY_RUNTIME__) {
62 yield* bind`${options.tag}(${table})`({
63 policyStatements: [
64 {
65 Effect: "Allow",
66 Action: [...options.actions],
67 Resource: options.grantDatabaseArn
68 ? [
69 Output.interpolate`${table.tableArn}`,
70 // The owning database's ARN is the table ARN minus its
71 // `/table/{name}` suffix.
72 table.tableArn.pipe(
73 Output.map((arn: string) => arn.replace(/\/table\/[^/]*$/, "")),
74 ),
75 ]
76 : [Output.interpolate`${table.tableArn}`],
77 },
78 describeEndpointsStatement,
79 ],
80 });
81 }
82 return Effect.fn(`${options.tag}(${table.LogicalId})`)(function* (request: Req) {
83 return yield* withWriteEndpoint(
84 op(
85 options.toRequest(request, {
86 DatabaseName: yield* DatabaseName,
87 TableName: yield* TableName,
88 }),
89 ),
90 );
91 });
92 });
93 });
94
95 /**
96 * Build the impl Effect for a table-scoped `timestream-query` operation
97 * (`Query`, `PrepareQuery`). Timestream authorizes query actions against the
98 * tables the SQL references, so the deploy-time half grants `actions` on the
99 * bound {@link Table}'s ARN; the request passes through as-is (the SQL
100 * references the database and table by name).
101 */
102 export const makeQueryTableHttpBinding = <I, A, E, R>(options: {
103 /** Fully-qualified binding tag, e.g. `AWS.Timestream.Query`. */
104 tag: string;
105 /** The distilled `timestream-query` operation. */
106 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
107 /** IAM actions granted on the table ARN. */
108 actions: readonly string[];
109 }) =>
110 Effect.gen(function* () {
111 const bind = yield* AWSBindings.Bind;
112 const op = yield* AWSBindings.withCredentials(options.operation);
113 const describeEndpoints = yield* AWSBindings.withCredentials(TSQ.describeEndpoints);
114 const withQueryEndpoint = withEndpoint(discover("query", describeEndpoints({})));
115 return Effect.fn(function* (table: Table) {
116 if (!globalThis.__ALCHEMY_RUNTIME__) {
117 yield* bind`${options.tag}(${table})`({
118 policyStatements: [
119 {
120 Effect: "Allow",
121 Action: [...options.actions],
122 Resource: [Output.interpolate`${table.tableArn}`],
123 },
124 describeEndpointsStatement,
125 ],
126 });
127 }
128 return Effect.fn(`${options.tag}(${table.LogicalId})`)(function* (request: I) {
129 return yield* withQueryEndpoint(op(request));
130 });
131 });
132 });
133
134 /**
135 * Build the impl Effect for an account-level `timestream-write` operation
136 * (batch-load task reads/resume — authorized against `*`, keyed by TaskId in
137 * the request). Invoked with no resource argument; the request passes
138 * through as-is.
139 */
140 export const makeWriteAccountHttpBinding = <I, A, E, R>(options: {
141 /** Fully-qualified binding tag, e.g. `AWS.Timestream.DescribeBatchLoadTask`. */
142 tag: string;
143 /** The distilled `timestream-write` operation. */
144 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
145 /** IAM actions granted on `*`. */
146 actions: readonly string[];
147 }) =>
148 Effect.gen(function* () {
149 const bind = yield* AWSBindings.Bind;
150 const op = yield* AWSBindings.withCredentials(options.operation);
151 const describeEndpoints = yield* AWSBindings.withCredentials(TSW.describeEndpoints);
152 const withWriteEndpoint = withEndpoint(discover("write", describeEndpoints({})));
153 return Effect.fn(function* () {
154 if (!globalThis.__ALCHEMY_RUNTIME__) {
155 yield* bind`${options.tag}()`({
156 policyStatements: [
157 {
158 Effect: "Allow",
159 Action: [...options.actions],
160 Resource: ["*"],
161 },
162 describeEndpointsStatement,
163 ],
164 });
165 }
166 return Effect.fn(options.tag)(function* (request: I) {
167 return yield* withWriteEndpoint(op(request));
168 });
169 });
170 });
171
172 /**
173 * Build the impl Effect for an account-level `timestream-query` operation
174 * (`CancelQuery` — authorized against `*`, keyed by QueryId in the request).
175 * Invoked with no resource argument; the request passes through as-is.
176 */
177 export const makeQueryAccountHttpBinding = <I, A, E, R>(options: {
178 /** Fully-qualified binding tag, e.g. `AWS.Timestream.CancelQuery`. */
179 tag: string;
180 /** The distilled `timestream-query` operation. */
181 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
182 /** IAM actions granted on `*`. */
183 actions: readonly string[];
184 }) =>
185 Effect.gen(function* () {
186 const bind = yield* AWSBindings.Bind;
187 const op = yield* AWSBindings.withCredentials(options.operation);
188 const describeEndpoints = yield* AWSBindings.withCredentials(TSQ.describeEndpoints);
189 const withQueryEndpoint = withEndpoint(discover("query", describeEndpoints({})));
190 return Effect.fn(function* () {
191 if (!globalThis.__ALCHEMY_RUNTIME__) {
192 yield* bind`${options.tag}()`({
193 policyStatements: [
194 {
195 Effect: "Allow",
196 Action: [...options.actions],
197 Resource: ["*"],
198 },
199 describeEndpointsStatement,
200 ],
201 });
202 }
203 return Effect.fn(options.tag)(function* (request: I) {
204 return yield* withQueryEndpoint(op(request));
205 });
206 });
207 });