The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { Connection } from "./Connection.ts"; |
| 5 | import type { RepositoryLink } from "./RepositoryLink.ts"; |
| 6 | import type { SyncConfiguration } from "./SyncConfiguration.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Shared HTTP scaffolding for the CodeConnections runtime bindings. |
| 10 | * |
| 11 | * Every capability follows the same shape — resolve the distilled |
| 12 | * operation, register an IAM policy statement on the binding host, and |
| 13 | * return a runtime callable. The only variation is the operation, the IAM |
| 14 | * action(s), and the identifier(s) injected from the bound resource: |
| 15 | * a {@link Connection}'s ARN, a {@link RepositoryLink}'s ID, or a |
| 16 | * {@link SyncConfiguration}'s (resource name, sync type) identity. |
| 17 | * |
| 18 | * NOT exported from `index.ts`. |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * Build the impl Effect for a connection-scoped operation: the runtime |
| 23 | * callable injects the bound {@link Connection}'s ARN as `ConnectionArn` |
| 24 | * and the deploy-time half grants `actions` on the connection ARN. |
| 25 | */ |
| 26 | export const makeConnectionScopedHttpBinding = < |
| 27 | I extends { ConnectionArn: string }, |
| 28 | A, |
| 29 | E, |
| 30 | R, |
| 31 | >(options: { |
| 32 | /** Fully-qualified binding tag, e.g. `AWS.CodeConnections.GetConnection`. */ |
| 33 | tag: string; |
| 34 | /** The distilled operation; `ConnectionArn` is injected from the resource. */ |
| 35 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 36 | /** IAM actions granted on the connection ARN. */ |
| 37 | actions: readonly string[]; |
| 38 | }) => |
| 39 | Effect.gen(function* () { |
| 40 | const bind = yield* AWSBindings.Bind; |
| 41 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 42 | |
| 43 | return Effect.fn(function* (connection: Connection) { |
| 44 | const ConnectionArn = yield* connection.connectionArn; |
| 45 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 46 | yield* bind`${options.tag}(${connection})`({ |
| 47 | policyStatements: [ |
| 48 | { |
| 49 | Effect: "Allow", |
| 50 | Action: [...options.actions], |
| 51 | Resource: [Output.interpolate`${connection.connectionArn}`], |
| 52 | }, |
| 53 | ], |
| 54 | }); |
| 55 | } |
| 56 | return Effect.fn(`${options.tag}(${connection.LogicalId})`)(function* ( |
| 57 | request?: Omit<I, "ConnectionArn">, |
| 58 | ) { |
| 59 | return yield* op({ |
| 60 | ...request, |
| 61 | ConnectionArn: yield* ConnectionArn, |
| 62 | } as I); |
| 63 | }); |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | /** |
| 68 | * Build the impl Effect for a repository-link-scoped operation: the runtime |
| 69 | * callable injects the bound {@link RepositoryLink}'s ID as |
| 70 | * `RepositoryLinkId` and the deploy-time half grants `actions` on the |
| 71 | * repository link ARN. |
| 72 | */ |
| 73 | export const makeRepositoryLinkScopedHttpBinding = < |
| 74 | I extends { RepositoryLinkId: string }, |
| 75 | A, |
| 76 | E, |
| 77 | R, |
| 78 | >(options: { |
| 79 | /** Fully-qualified binding tag. */ |
| 80 | tag: string; |
| 81 | /** The distilled operation; `RepositoryLinkId` is injected from the resource. */ |
| 82 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 83 | /** IAM actions granted on the repository link ARN. */ |
| 84 | actions: readonly string[]; |
| 85 | }) => |
| 86 | Effect.gen(function* () { |
| 87 | const bind = yield* AWSBindings.Bind; |
| 88 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 89 | |
| 90 | return Effect.fn(function* (repositoryLink: RepositoryLink) { |
| 91 | const RepositoryLinkId = yield* repositoryLink.repositoryLinkId; |
| 92 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 93 | yield* bind`${options.tag}(${repositoryLink})`({ |
| 94 | policyStatements: [ |
| 95 | { |
| 96 | Effect: "Allow", |
| 97 | Action: [...options.actions], |
| 98 | Resource: [Output.interpolate`${repositoryLink.repositoryLinkArn}`], |
| 99 | }, |
| 100 | ], |
| 101 | }); |
| 102 | } |
| 103 | return Effect.fn(`${options.tag}(${repositoryLink.LogicalId})`)(function* ( |
| 104 | request: Omit<I, "RepositoryLinkId">, |
| 105 | ) { |
| 106 | return yield* op({ |
| 107 | ...request, |
| 108 | RepositoryLinkId: yield* RepositoryLinkId, |
| 109 | } as I); |
| 110 | }); |
| 111 | }); |
| 112 | }); |
| 113 | |
| 114 | /** |
| 115 | * Build the impl Effect for a sync-configuration-scoped operation: the |
| 116 | * runtime callable injects the bound {@link SyncConfiguration}'s |
| 117 | * `ResourceName` + `SyncType` identity. Sync operations are not |
| 118 | * resource-scoped in IAM (sync configurations have no ARN), so the |
| 119 | * deploy-time half grants `actions` on `*`. |
| 120 | */ |
| 121 | export const makeSyncConfigurationScopedHttpBinding = < |
| 122 | I extends { ResourceName: string; SyncType: string }, |
| 123 | A, |
| 124 | E, |
| 125 | R, |
| 126 | >(options: { |
| 127 | /** Fully-qualified binding tag. */ |
| 128 | tag: string; |
| 129 | /** The distilled operation; `ResourceName` + `SyncType` are injected. */ |
| 130 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 131 | /** IAM actions granted on `*`. */ |
| 132 | actions: readonly string[]; |
| 133 | }) => |
| 134 | Effect.gen(function* () { |
| 135 | const bind = yield* AWSBindings.Bind; |
| 136 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 137 | |
| 138 | return Effect.fn(function* (syncConfiguration: SyncConfiguration) { |
| 139 | const ResourceName = yield* syncConfiguration.resourceName; |
| 140 | const SyncType = yield* syncConfiguration.syncType; |
| 141 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 142 | yield* bind`${options.tag}(${syncConfiguration})`({ |
| 143 | policyStatements: [ |
| 144 | { |
| 145 | Effect: "Allow", |
| 146 | Action: [...options.actions], |
| 147 | Resource: ["*"], |
| 148 | }, |
| 149 | ], |
| 150 | }); |
| 151 | } |
| 152 | return Effect.fn(`${options.tag}(${syncConfiguration.LogicalId})`)(function* ( |
| 153 | request?: Omit<I, "ResourceName" | "SyncType">, |
| 154 | ) { |
| 155 | return yield* op({ |
| 156 | ...request, |
| 157 | ResourceName: yield* ResourceName, |
| 158 | SyncType: yield* SyncType, |
| 159 | } as I); |
| 160 | }); |
| 161 | }); |
| 162 | }); |
| 163 | |
| 164 | /** |
| 165 | * Build the impl Effect for an account-level operation (no target |
| 166 | * resource). The deploy-time half grants `actions` on `*`. |
| 167 | */ |
| 168 | export const makeCodeConnectionsAccountHttpBinding = <I, A, E, R>(options: { |
| 169 | /** Fully-qualified binding tag, e.g. `AWS.CodeConnections.ListConnections`. */ |
| 170 | tag: string; |
| 171 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 172 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 173 | /** IAM actions granted on `*`. */ |
| 174 | actions: readonly string[]; |
| 175 | }) => |
| 176 | Effect.gen(function* () { |
| 177 | const bind = yield* AWSBindings.Bind; |
| 178 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 179 | |
| 180 | return Effect.fn(function* () { |
| 181 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 182 | yield* bind`${options.tag}()`({ |
| 183 | policyStatements: [ |
| 184 | { |
| 185 | Effect: "Allow", |
| 186 | Action: [...options.actions], |
| 187 | Resource: ["*"], |
| 188 | }, |
| 189 | ], |
| 190 | }); |
| 191 | } |
| 192 | return Effect.fn(options.tag)(function* (request?: I) { |
| 193 | return yield* op((request ?? {}) as I); |
| 194 | }); |
| 195 | }); |
| 196 | }); |