| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type { Repository } from "./Repository.ts"; |
| 3 | import { bindGcpHost } from "../Host.ts"; |
| 4 | import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared HTTP scaffolding for Cloud Build v2 repository bindings. |
| 8 | * NOT exported from index.ts. |
| 9 | */ |
| 10 | export const makeRepositoryHttpBinding = < |
| 11 | I extends { repository: string }, |
| 12 | A, |
| 13 | E, |
| 14 | >(options: { |
| 15 | tag: string; |
| 16 | iam: BindingIam; |
| 17 | operation: GcpHttpOp<I, A, E> | ((input: I) => Effect.Effect<A, E>); |
| 18 | }) => |
| 19 | Effect.gen(function* () { |
| 20 | const run = Effect.isEffect(options.operation) |
| 21 | ? yield* options.operation as GcpHttpOp<I, A, E> |
| 22 | : (options.operation as (input: I) => Effect.Effect<A, E>); |
| 23 | return Effect.fn(function* <T extends Repository>(repository: T) { |
| 24 | yield* bindGcpHost({ |
| 25 | tag: options.tag, |
| 26 | resource: repository, |
| 27 | iam: [grantFor(options.iam, repository.name)], |
| 28 | }); |
| 29 | const name = yield* repository.name; |
| 30 | return Effect.fn(`${options.tag}(${repository.LogicalId})`)(function* ( |
| 31 | request?: Omit<I, "repository">, |
| 32 | ) { |
| 33 | return yield* run({ |
| 34 | ...(request as I), |
| 35 | repository: yield* name, |
| 36 | } as I); |
| 37 | }); |
| 38 | }); |
| 39 | }); |