| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type { CryptoKey } from "./CryptoKey.ts"; |
| 3 | import { bindGcpHost } from "../Host.ts"; |
| 4 | import { grantFor, type BindingIam, type GcpHttpOp } from "../HttpBinding.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared HTTP scaffolding for Cloud KMS CryptoKey bindings. |
| 8 | * NOT exported from index.ts. |
| 9 | */ |
| 10 | export const makeCryptoKeyHttpBinding = < |
| 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* (key: CryptoKey) { |
| 22 | yield* bindGcpHost({ |
| 23 | tag: options.tag, |
| 24 | resource: key, |
| 25 | iam: [grantFor(options.iam, key.name)], |
| 26 | }); |
| 27 | const name = yield* key.name; |
| 28 | return Effect.fn(`${options.tag}(${key.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 | }); |