1import { Credentials } from "@distilled.cloud/gcp/Credentials";
2import * as Effect from "effect/Effect";
3import * as HttpClient from "effect/http/HttpClient";
4import type { ConnectionsEntityTypesEntity } from "./ConnectionsEntityTypesEntity.ts";
5import { bindGcpHost } from "../Host.ts";
6import { type BindingIam, grantFor } from "../HttpBinding.ts";
7
8type 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 */
19export 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 });