| 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 { ConnectionsEntityTypesEntity } from "./ConnectionsEntityTypesEntity.ts"; |
| 5 | import { bindGcpHost } from "../Host.ts"; |
| 6 | import { type BindingIam, grantFor } from "../HttpBinding.ts"; |
| 7 | |
| 8 | type GcpHttpOp<I, A, E> = Effect.Effect< |
| 9 | (input: I) => Effect.Effect<A, E>, |
| 10 | never, |
| 11 | Credentials | HttpClient.HttpClient |
| 12 | > & |
| 13 | ((input: I) => Effect.Effect<A, E, Credentials | HttpClient.HttpClient>); |
| 14 | |
| 15 | /** |
| 16 | * Shared HTTP scaffolding for Integration Connectors entity bindings. |
| 17 | * NOT exported from index.ts. |
| 18 | */ |
| 19 | export const makeEntityHttpBinding = < |
| 20 | I extends { name: string }, |
| 21 | A, |
| 22 | E, |
| 23 | >(options: { |
| 24 | tag: string; |
| 25 | iam: BindingIam; |
| 26 | operation: GcpHttpOp<I, A, E>; |
| 27 | }) => |
| 28 | Effect.gen(function* () { |
| 29 | const run = yield* options.operation; |
| 30 | return Effect.fn(function* (entity: ConnectionsEntityTypesEntity) { |
| 31 | yield* bindGcpHost({ |
| 32 | tag: options.tag, |
| 33 | resource: entity, |
| 34 | iam: [grantFor(options.iam, entity.name)], |
| 35 | }); |
| 36 | const name = yield* entity.name; |
| 37 | return Effect.fn(`${options.tag}(${entity.LogicalId})`)(function* ( |
| 38 | request?: Omit<I, "name">, |
| 39 | ) { |
| 40 | return yield* run({ |
| 41 | ...request, |
| 42 | name: yield* name, |
| 43 | } as I); |
| 44 | }); |
| 45 | }); |
| 46 | }); |