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