The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as AWSBindings from "../AWSBindings.ts"; |
| 3 | import type { Repository } from "./Repository.ts"; |
| 4 | |
| 5 | /** |
| 6 | * Shared scaffolding for the ECR HTTP bindings. |
| 7 | * |
| 8 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 9 | * a `Layer.effect(Cap, makeEcrRepositoryHttpBinding({ … }))`. Everything |
| 10 | * except the operation and the IAM action list is boilerplate: per the ECR |
| 11 | * service authorization reference every repository-scoped action authorizes |
| 12 | * against the repository ARN, and the runtime callable injects the bound |
| 13 | * {@link Repository}'s `repositoryName` into every request. |
| 14 | */ |
| 15 | |
| 16 | /** The repository-scoping fields every repository-scoped ECR operation takes. */ |
| 17 | export interface RepositoryScopedRequest { |
| 18 | registryId?: string; |
| 19 | repositoryName: string; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Build the impl Effect for a repository-scoped ECR operation. The |
| 24 | * deploy-time half grants `iamActions` on the bound repository's ARN; the |
| 25 | * runtime callable injects the repository's `repositoryName` into every |
| 26 | * request (the registry defaults to the caller's own account). |
| 27 | * |
| 28 | * Registry-level operations (`ecr:GetAuthorizationToken`, which authorizes |
| 29 | * only on `Resource: ["*"]`) stay bespoke in their own `{Op}Http.ts`. |
| 30 | */ |
| 31 | export const makeEcrRepositoryHttpBinding = <I extends RepositoryScopedRequest, A, E, R>(options: { |
| 32 | /** |
| 33 | * Short capability name used in the binding sid and runtime span, e.g. |
| 34 | * `"DescribeImages"`. |
| 35 | */ |
| 36 | capability: string; |
| 37 | /** The distilled operation implementing the capability. */ |
| 38 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 39 | /** IAM actions granted on the bound repository's ARN. */ |
| 40 | iamActions: readonly string[]; |
| 41 | }) => |
| 42 | Effect.gen(function* () { |
| 43 | const bind = yield* AWSBindings.Bind; |
| 44 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 45 | |
| 46 | return Effect.fn(function* (repository: Repository) { |
| 47 | const RepositoryName = yield* repository.repositoryName; |
| 48 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 49 | yield* bind`AWS.ECR.${options.capability}(${repository})`({ |
| 50 | policyStatements: [ |
| 51 | { |
| 52 | Effect: "Allow", |
| 53 | Action: [...options.iamActions], |
| 54 | Resource: [repository.repositoryArn], |
| 55 | }, |
| 56 | ], |
| 57 | }); |
| 58 | } |
| 59 | // The request is optional at the impl level so contracts whose |
| 60 | // remaining fields are all optional (DescribeImages, ListImages, …) can |
| 61 | // declare `(request?: …)`; contracts with required fields keep the |
| 62 | // parameter required and narrow this signature. |
| 63 | return Effect.fn(`AWS.ECR.${options.capability}(${repository.LogicalId})`)(function* ( |
| 64 | request?: Omit<I, keyof RepositoryScopedRequest>, |
| 65 | ) { |
| 66 | return yield* op({ |
| 67 | ...request, |
| 68 | repositoryName: yield* RepositoryName, |
| 69 | } as unknown as I); |
| 70 | }); |
| 71 | }); |
| 72 | }); |