| 1 | import { Credentials } from "@distilled.cloud/gcp/Credentials"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpClient from "effect/http/HttpClient"; |
| 4 | import type { ConnectionsGitRepositoryLink } from "./ConnectionsGitRepositoryLink.ts"; |
| 5 | import { bindGcpHost } from "../Host.ts"; |
| 6 | import { type BindingIam, grantFor } from "../HttpBinding.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Shared HTTP scaffolding for Developer Connect git repository link |
| 10 | * bindings. NOT exported from index.ts. |
| 11 | * |
| 12 | * Distilled ops are `OperationMethod`s: yield them once at Layer |
| 13 | * construction (after providing Credentials + HttpClient) so the inner |
| 14 | * runtime Effect is `Effect<A, E>` and does not leak `GcpOpContext`. |
| 15 | */ |
| 16 | export const makeGitRepositoryLinkHttpBinding = < |
| 17 | I extends { gitRepositoryLink: string }, |
| 18 | A, |
| 19 | E, |
| 20 | >(options: { |
| 21 | tag: string; |
| 22 | iam: BindingIam; |
| 23 | operation: Effect.Effect< |
| 24 | (input: I) => Effect.Effect<A, E>, |
| 25 | never, |
| 26 | Credentials | HttpClient.HttpClient |
| 27 | >; |
| 28 | }) => |
| 29 | Effect.gen(function* () { |
| 30 | const run = yield* options.operation; |
| 31 | return Effect.fn(function* <T extends ConnectionsGitRepositoryLink>( |
| 32 | link: T, |
| 33 | ) { |
| 34 | yield* bindGcpHost({ |
| 35 | tag: options.tag, |
| 36 | resource: link, |
| 37 | iam: [grantFor(options.iam, link.name)], |
| 38 | }); |
| 39 | const name = yield* link.name; |
| 40 | return Effect.fn(`${options.tag}(${link.LogicalId})`)(function* ( |
| 41 | request?: Omit<I, "gitRepositoryLink">, |
| 42 | ) { |
| 43 | const gitRepositoryLink = yield* name; |
| 44 | return yield* run({ |
| 45 | ...(request ?? {}), |
| 46 | gitRepositoryLink, |
| 47 | } as I); |
| 48 | }); |
| 49 | }); |
| 50 | }); |