The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as GCPBindings from "../GCPBindings.ts"; |
| 3 | import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts"; |
| 4 | import type { Backup } from "./Backup.ts"; |
| 5 | import type { Cluster } from "./Cluster.ts"; |
| 6 | import type { ClustersUser } from "./ClustersUser.ts"; |
| 7 | import type { Instance } from "./Instance.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Shared HTTP scaffolding for AlloyDB cluster, instance, backup, and |
| 11 | * user bindings. NOT exported from index.ts. |
| 12 | */ |
| 13 | export const makeAlloyDbClusterHttpBinding = <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* (cluster: Cluster) { |
| 22 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 23 | yield* bind`${options.tag}(${cluster})`({ |
| 24 | iam: [grantFor(options.iam, cluster.name)], |
| 25 | }); |
| 26 | } |
| 27 | const name = yield* cluster.name; |
| 28 | return Effect.fn(`${options.tag}(${cluster.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 makeAlloyDbInstanceHttpBinding = <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* (instance: Instance) { |
| 48 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 49 | yield* bind`${options.tag}(${instance})`({ |
| 50 | iam: [grantFor(options.iam, instance.name)], |
| 51 | }); |
| 52 | } |
| 53 | const name = yield* instance.name; |
| 54 | return Effect.fn(`${options.tag}(${instance.LogicalId})`)(function* ( |
| 55 | request?: Omit<I, "name">, |
| 56 | ) { |
| 57 | return yield* run({ |
| 58 | ...(request as I), |
| 59 | name: yield* name, |
| 60 | } as I); |
| 61 | }); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | export const makeAlloyDbBackupHttpBinding = <I extends { name?: string }, A, E>(options: { |
| 66 | tag: string; |
| 67 | iam: BindingIam; |
| 68 | operation: GcpHttpOp<I, A, E>; |
| 69 | }) => |
| 70 | Effect.gen(function* () { |
| 71 | const bind = yield* GCPBindings.Bind; |
| 72 | const run = yield* GCPBindings.withCredentials(options.operation); |
| 73 | return Effect.fn(function* (backup: Backup) { |
| 74 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 75 | yield* bind`${options.tag}(${backup})`({ |
| 76 | iam: [grantFor(options.iam, backup.name)], |
| 77 | }); |
| 78 | } |
| 79 | const name = yield* backup.name; |
| 80 | return Effect.fn(`${options.tag}(${backup.LogicalId})`)(function* ( |
| 81 | request?: Omit<I, "name">, |
| 82 | ) { |
| 83 | return yield* run({ |
| 84 | ...(request as I), |
| 85 | name: yield* name, |
| 86 | } as I); |
| 87 | }); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | export const makeAlloyDbUserHttpBinding = <I extends { name?: string }, A, E>(options: { |
| 92 | tag: string; |
| 93 | iam: BindingIam; |
| 94 | operation: GcpHttpOp<I, A, E>; |
| 95 | }) => |
| 96 | Effect.gen(function* () { |
| 97 | const bind = yield* GCPBindings.Bind; |
| 98 | const run = yield* GCPBindings.withCredentials(options.operation); |
| 99 | return Effect.fn(function* (user: ClustersUser) { |
| 100 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 101 | yield* bind`${options.tag}(${user})`({ |
| 102 | iam: [grantFor(options.iam, user.name)], |
| 103 | }); |
| 104 | } |
| 105 | const name = yield* user.name; |
| 106 | return Effect.fn(`${options.tag}(${user.LogicalId})`)(function* (request?: Omit<I, "name">) { |
| 107 | return yield* run({ |
| 108 | ...(request as I), |
| 109 | name: yield* name, |
| 110 | } as I); |
| 111 | }); |
| 112 | }); |
| 113 | }); |
| 114 | |
| 115 | export const makeAlloyDbConnectionInfoHttpBinding = <I extends { parent?: string }, A, E>(options: { |
| 116 | tag: string; |
| 117 | iam: BindingIam; |
| 118 | operation: GcpHttpOp<I, A, E>; |
| 119 | }) => |
| 120 | Effect.gen(function* () { |
| 121 | const bind = yield* GCPBindings.Bind; |
| 122 | const run = yield* GCPBindings.withCredentials(options.operation); |
| 123 | return Effect.fn(function* (instance: Instance) { |
| 124 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 125 | yield* bind`${options.tag}(${instance})`({ |
| 126 | iam: [grantFor(options.iam, instance.name)], |
| 127 | }); |
| 128 | } |
| 129 | const name = yield* instance.name; |
| 130 | return Effect.fn(`${options.tag}(${instance.LogicalId})`)(function* ( |
| 131 | request?: Omit<I, "parent">, |
| 132 | ) { |
| 133 | return yield* run({ |
| 134 | ...(request as I), |
| 135 | parent: yield* name, |
| 136 | } as I); |
| 137 | }); |
| 138 | }); |
| 139 | }); |