1import * as Effect from "effect/Effect";
2import type { Table } from "./Table.ts";
3import { bindGcpHost } from "../Host.ts";
4import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts";
5
6/**
7 * Shared HTTP scaffolding for BigQuery table bindings.
8 * NOT exported from index.ts.
9 */
10export const makeTableHttpBinding = <
11 I extends { projectId: string; datasetId: string; tableId: 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* (table: Table) {
22 yield* bindGcpHost({
23 tag: options.tag,
24 resource: table,
25 iam: [grantFor(options.iam, table.name)],
26 });
27 const project = yield* table.project;
28 const datasetId = yield* table.datasetId;
29 const tableId = yield* table.tableId;
30 return Effect.fn(`${options.tag}(${table.LogicalId})`)(function* (
31 request?: Omit<I, "projectId" | "datasetId" | "tableId">,
32 ) {
33 return yield* run({
34 ...request,
35 projectId: yield* project,
36 datasetId: yield* datasetId,
37 tableId: yield* tableId,
38 } as I);
39 });
40 });
41 });