1import * as Effect from "effect/Effect";
2import type { CryptoKey } from "./CryptoKey.ts";
3import { bindGcpHost } from "../Host.ts";
4import { 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 */
10export 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 });