1import * as Effect from "effect/Effect";
2import type { Service } from "./Service.ts";
3import type { ServicesConnector } from "./ServicesConnector.ts";
4import { bindGcpHost } from "../Host.ts";
5import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts";
6
7/**
8 * Shared HTTP scaffolding for Firebase Data Connect bindings.
9 * NOT exported from index.ts.
10 */
11export const makeServiceHttpBinding = <
12 I extends { name: string },
13 A,
14 E,
15>(options: {
16 tag: string;
17 iam: BindingIam;
18 operation: GcpHttpOp<I, A, E>;
19}) =>
20 Effect.gen(function* () {
21 const run = yield* options.operation;
22 return Effect.fn(function* (service: Service) {
23 yield* bindGcpHost({
24 tag: options.tag,
25 resource: service,
26 iam: [grantFor(options.iam, service.name)],
27 });
28 const name = yield* service.name;
29 return Effect.fn(`${options.tag}(${service.LogicalId})`)(function* (
30 request: Omit<I, "name">,
31 ) {
32 return yield* run({
33 ...request,
34 name: yield* name,
35 } as I);
36 });
37 });
38 });
39
40export const makeConnectorHttpBinding = <
41 I extends { name: string },
42 A,
43 E,
44>(options: {
45 tag: string;
46 iam: BindingIam;
47 operation: GcpHttpOp<I, A, E>;
48}) =>
49 Effect.gen(function* () {
50 const run = yield* options.operation;
51 return Effect.fn(function* (connector: ServicesConnector) {
52 yield* bindGcpHost({
53 tag: options.tag,
54 resource: connector,
55 iam: [grantFor(options.iam, connector.name)],
56 });
57 const name = yield* connector.name;
58 return Effect.fn(`${options.tag}(${connector.LogicalId})`)(function* (
59 request: Omit<I, "name">,
60 ) {
61 return yield* run({
62 ...request,
63 name: yield* name,
64 } as I);
65 });
66 });
67 });