The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | /** |
| 3 | * Shared scaffolding for RDS Data API HTTP bindings. |
| 4 | * |
| 5 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 6 | * a `Layer.effect(Cap, makeRDSDataHttpBinding({ … }))`. Every Data API |
| 7 | * operation follows the same shape: it is scoped to a Data-API-enabled |
| 8 | * {@link DBCluster} plus the Secrets Manager {@link Secret} holding the |
| 9 | * database credentials, the deploy-time half grants the `rds-data:*` action |
| 10 | * on the cluster + secret ARNs (plus `secretsmanager:GetSecretValue` / |
| 11 | * `DescribeSecret` on the secret, which the Data API resolves server-side), |
| 12 | * and the runtime half injects the resolved ARNs (and optional |
| 13 | * `database`/`schema`) into the wire request. Everything except the |
| 14 | * operation, the IAM action, and the request shaping is boilerplate. |
| 15 | */ |
| 16 | import * as AWSBindings from "../AWSBindings.ts"; |
| 17 | import type { DBCluster } from "../RDS/DBCluster.ts"; |
| 18 | import type { Secret } from "../SecretsManager/Secret.ts"; |
| 19 | |
| 20 | /** |
| 21 | * The common bind-time options every RDS Data API capability accepts: the |
| 22 | * credentials secret, plus the optional default `database`/`schema` injected |
| 23 | * into requests by operations that support them. |
| 24 | */ |
| 25 | export interface RDSDataBindingOptions { |
| 26 | secret: Secret; |
| 27 | database?: string; |
| 28 | schema?: string; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Build the impl Effect for an RDS Data API operation scoped to a |
| 33 | * {@link DBCluster} + credentials {@link Secret}: the deploy-time half grants |
| 34 | * `action` on the cluster and secret ARNs (plus Secrets Manager read on the |
| 35 | * secret), and the runtime half shapes the wire request via `makeInput` with |
| 36 | * the resolved ARNs and bind-time `database`/`schema`. |
| 37 | */ |
| 38 | export const makeRDSDataHttpBinding = <Req, I, A, E, R>(options: { |
| 39 | /** Fully-qualified binding tag, e.g. `AWS.RDSData.ExecuteStatement`. */ |
| 40 | tag: string; |
| 41 | /** IAM action granted on the cluster + secret ARNs, e.g. `rds-data:ExecuteStatement`. */ |
| 42 | action: string; |
| 43 | /** The distilled operation invoked with the shaped wire request. */ |
| 44 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 45 | /** |
| 46 | * Shape the wire request from the caller's request plus the resolved |
| 47 | * cluster/secret ARNs and the bind-time `database`/`schema`. |
| 48 | */ |
| 49 | makeInput: ( |
| 50 | request: Req, |
| 51 | context: { |
| 52 | resourceArn: string; |
| 53 | secretArn: string; |
| 54 | database: string | undefined; |
| 55 | schema: string | undefined; |
| 56 | }, |
| 57 | ) => I; |
| 58 | }) => |
| 59 | Effect.gen(function* () { |
| 60 | const bind = yield* AWSBindings.Bind; |
| 61 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 62 | |
| 63 | return Effect.fn(function* (cluster: DBCluster, bindOptions: RDSDataBindingOptions) { |
| 64 | const resourceArn = yield* cluster.dbClusterArn; |
| 65 | const secretArn = yield* bindOptions.secret.secretArn; |
| 66 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 67 | yield* bind`${options.tag}(${cluster}, ${bindOptions.secret})`({ |
| 68 | policyStatements: [ |
| 69 | { |
| 70 | Effect: "Allow", |
| 71 | Action: [options.action], |
| 72 | Resource: [cluster.dbClusterArn, bindOptions.secret.secretArn], |
| 73 | }, |
| 74 | { |
| 75 | Effect: "Allow", |
| 76 | Action: ["secretsmanager:GetSecretValue", "secretsmanager:DescribeSecret"], |
| 77 | Resource: [bindOptions.secret.secretArn], |
| 78 | }, |
| 79 | ], |
| 80 | }); |
| 81 | } |
| 82 | return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* ( |
| 83 | // optional so zero-arg callables (`BeginTransaction`) conform to |
| 84 | // their declared `() => Effect<…>` interface; `makeInput` receives |
| 85 | // `undefined` there and builds the request from context alone. |
| 86 | request?: Req, |
| 87 | ) { |
| 88 | return yield* op( |
| 89 | options.makeInput(request as Req, { |
| 90 | resourceArn: yield* resourceArn, |
| 91 | secretArn: yield* secretArn, |
| 92 | database: bindOptions.database, |
| 93 | schema: bindOptions.schema, |
| 94 | }), |
| 95 | ); |
| 96 | }); |
| 97 | }); |
| 98 | }); |