| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type { CustomClass } from "./CustomClass.ts"; |
| 3 | import type { PhraseSet } from "./PhraseSet.ts"; |
| 4 | import { bindGcpHost } from "../Host.ts"; |
| 5 | import { grantFor, type BindingIam, type GcpHttpOp } from "../HttpBinding.ts"; |
| 6 | |
| 7 | const makeNamedHttpBinding = < |
| 8 | Resource extends CustomClass | PhraseSet, |
| 9 | I, |
| 10 | A, |
| 11 | E, |
| 12 | Req = void, |
| 13 | >(options: { |
| 14 | tag: string; |
| 15 | iam: BindingIam; |
| 16 | operation: GcpHttpOp<I, A, E>; |
| 17 | toInput: (name: string, request: Req | undefined) => I; |
| 18 | }) => |
| 19 | Effect.gen(function* () { |
| 20 | const run = yield* options.operation; |
| 21 | return Effect.fn(function* (resource: Resource) { |
| 22 | yield* bindGcpHost({ |
| 23 | tag: options.tag, |
| 24 | resource: resource, |
| 25 | iam: [grantFor(options.iam, resource.name)], |
| 26 | }); |
| 27 | const name = yield* resource.name; |
| 28 | return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* ( |
| 29 | request?: Req, |
| 30 | ) { |
| 31 | const resourceName = yield* name; |
| 32 | return yield* run(options.toInput(resourceName, request)); |
| 33 | }); |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | /** |
| 38 | * Shared HTTP scaffolding for Speech-to-Text bindings. |
| 39 | * NOT exported from index.ts. |
| 40 | */ |
| 41 | export const makeCustomClassHttpBinding = <I, A, E, Req = void>(options: { |
| 42 | tag: string; |
| 43 | iam: BindingIam; |
| 44 | operation: GcpHttpOp<I, A, E>; |
| 45 | toInput: (name: string, request: Req | undefined) => I; |
| 46 | }) => makeNamedHttpBinding<CustomClass, I, A, E, Req>(options); |
| 47 | |
| 48 | export const makePhraseSetHttpBinding = <I, A, E, Req = void>(options: { |
| 49 | tag: string; |
| 50 | iam: BindingIam; |
| 51 | operation: GcpHttpOp<I, A, E>; |
| 52 | toInput: (name: string, request: Req | undefined) => I; |
| 53 | }) => makeNamedHttpBinding<PhraseSet, I, A, E, Req>(options); |