The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as GCPBindings from "../GCPBindings.ts"; |
| 4 | import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts"; |
| 5 | import type { Cluster } from "./Cluster.ts"; |
| 6 | import type { Instance } from "./Instance.ts"; |
| 7 | import type { Table } from "./Table.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Shared HTTP scaffolding for Bigtable instance, cluster, and table |
| 11 | * bindings. NOT exported from index.ts. |
| 12 | */ |
| 13 | export const makeBigtableInstanceHttpBinding = <I extends { name?: string }, A, E>(options: { |
| 14 | tag: string; |
| 15 | iam: BindingIam; |
| 16 | operation: GcpHttpOp<I, A, E>; |
| 17 | }) => |
| 18 | Effect.gen(function* () { |
| 19 | const bind = yield* GCPBindings.Bind; |
| 20 | const run = yield* GCPBindings.withCredentials(options.operation); |
| 21 | return Effect.fn(function* (instance: Instance) { |
| 22 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 23 | yield* bind`${options.tag}(${instance})`({ |
| 24 | iam: [grantFor(options.iam, instance.name)], |
| 25 | }); |
| 26 | } |
| 27 | const name = yield* instance.name; |
| 28 | return Effect.fn(`${options.tag}(${instance.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 | }); |
| 38 | |
| 39 | export const makeBigtableClusterHttpBinding = <I extends { name?: string }, A, E>(options: { |
| 40 | tag: string; |
| 41 | iam: BindingIam; |
| 42 | operation: GcpHttpOp<I, A, E>; |
| 43 | }) => |
| 44 | Effect.gen(function* () { |
| 45 | const bind = yield* GCPBindings.Bind; |
| 46 | const run = yield* GCPBindings.withCredentials(options.operation); |
| 47 | return Effect.fn(function* (cluster: Cluster) { |
| 48 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 49 | yield* bind`${options.tag}(${cluster})`({ |
| 50 | iam: [ |
| 51 | grantFor( |
| 52 | options.iam, |
| 53 | Output.interpolate`projects/${cluster.project}/instances/${cluster.instanceId}`, |
| 54 | ), |
| 55 | ], |
| 56 | }); |
| 57 | } |
| 58 | const name = yield* cluster.name; |
| 59 | return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* ( |
| 60 | request?: Omit<I, "name">, |
| 61 | ) { |
| 62 | return yield* run({ |
| 63 | ...(request as I), |
| 64 | name: yield* name, |
| 65 | } as I); |
| 66 | }); |
| 67 | }); |
| 68 | }); |
| 69 | |
| 70 | export const makeBigtableTableHttpBinding = <I extends { name?: string }, A, E>(options: { |
| 71 | tag: string; |
| 72 | iam: BindingIam; |
| 73 | operation: GcpHttpOp<I, A, E>; |
| 74 | }) => |
| 75 | Effect.gen(function* () { |
| 76 | const bind = yield* GCPBindings.Bind; |
| 77 | const run = yield* GCPBindings.withCredentials(options.operation); |
| 78 | return Effect.fn(function* (table: Table) { |
| 79 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 80 | yield* bind`${options.tag}(${table})`({ |
| 81 | iam: [grantFor(options.iam, table.name)], |
| 82 | }); |
| 83 | } |
| 84 | const name = yield* table.name; |
| 85 | return Effect.fn(`${options.tag}(${table.LogicalId})`)(function* (request?: Omit<I, "name">) { |
| 86 | return yield* run({ |
| 87 | ...(request as I), |
| 88 | name: yield* name, |
| 89 | } as I); |
| 90 | }); |
| 91 | }); |
| 92 | }); |