| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type { AdaptiveMtDataset } from "./AdaptiveMtDataset.ts"; |
| 3 | import type { GlossariesGlossaryEntry } from "./GlossariesGlossaryEntry.ts"; |
| 4 | import { locationParentOf } from "./internal.ts"; |
| 5 | import type { Model } from "./Model.ts"; |
| 6 | import { bindGcpHost } from "../Host.ts"; |
| 7 | import { grantFor, type BindingIam, type GcpHttpOp } from "../HttpBinding.ts"; |
| 8 | |
| 9 | const closeOver = <I, A, E>(operation: GcpHttpOp<I, A, E>) => operation; |
| 10 | |
| 11 | const makeNamedHttpBinding = < |
| 12 | Resource extends AdaptiveMtDataset | GlossariesGlossaryEntry | Model, |
| 13 | I extends { name: string }, |
| 14 | A, |
| 15 | E, |
| 16 | >(options: { |
| 17 | tag: string; |
| 18 | iam: BindingIam; |
| 19 | operation: GcpHttpOp<I, A, E>; |
| 20 | }) => |
| 21 | Effect.gen(function* () { |
| 22 | const run = yield* closeOver(options.operation); |
| 23 | return Effect.fn(function* (resource: Resource) { |
| 24 | yield* bindGcpHost({ |
| 25 | tag: options.tag, |
| 26 | resource: resource, |
| 27 | iam: [grantFor(options.iam, resource.name)], |
| 28 | }); |
| 29 | const name = yield* resource.name; |
| 30 | return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* ( |
| 31 | request?: Omit<I, "name">, |
| 32 | ) { |
| 33 | return yield* run({ |
| 34 | ...(request ?? {}), |
| 35 | name: yield* name, |
| 36 | } as I); |
| 37 | }); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | const makeLocationParentHttpBinding = < |
| 42 | Resource extends AdaptiveMtDataset | Model, |
| 43 | I extends { parent: string }, |
| 44 | A, |
| 45 | E, |
| 46 | >(options: { |
| 47 | tag: string; |
| 48 | iam: BindingIam; |
| 49 | operation: GcpHttpOp<I, A, E>; |
| 50 | withBody: (name: string, request: Omit<I, "parent"> | undefined) => I; |
| 51 | }) => |
| 52 | Effect.gen(function* () { |
| 53 | const run = yield* closeOver(options.operation); |
| 54 | return Effect.fn(function* (resource: Resource) { |
| 55 | yield* bindGcpHost({ |
| 56 | tag: options.tag, |
| 57 | resource: resource, |
| 58 | iam: [grantFor(options.iam, resource.name)], |
| 59 | }); |
| 60 | const name = yield* resource.name; |
| 61 | return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* ( |
| 62 | request?: Omit<I, "parent">, |
| 63 | ) { |
| 64 | return yield* run(options.withBody(yield* name, request)); |
| 65 | }); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | /** |
| 70 | * Shared HTTP scaffolding for Cloud Translation bindings. |
| 71 | * NOT exported from index.ts. |
| 72 | */ |
| 73 | export const makeAdaptiveMtDatasetHttpBinding = < |
| 74 | I extends { name: string }, |
| 75 | A, |
| 76 | E, |
| 77 | >(options: { |
| 78 | tag: string; |
| 79 | iam: BindingIam; |
| 80 | operation: GcpHttpOp<I, A, E>; |
| 81 | }) => makeNamedHttpBinding<AdaptiveMtDataset, I, A, E>(options); |
| 82 | |
| 83 | export const makeGlossaryEntryHttpBinding = < |
| 84 | I extends { name: string }, |
| 85 | A, |
| 86 | E, |
| 87 | >(options: { |
| 88 | tag: string; |
| 89 | iam: BindingIam; |
| 90 | operation: GcpHttpOp<I, A, E>; |
| 91 | }) => makeNamedHttpBinding<GlossariesGlossaryEntry, I, A, E>(options); |
| 92 | |
| 93 | export const makeModelHttpBinding = < |
| 94 | I extends { name: string }, |
| 95 | A, |
| 96 | E, |
| 97 | >(options: { |
| 98 | tag: string; |
| 99 | iam: BindingIam; |
| 100 | operation: GcpHttpOp<I, A, E>; |
| 101 | }) => makeNamedHttpBinding<Model, I, A, E>(options); |
| 102 | |
| 103 | export const makeAdaptiveMtTranslateBinding = < |
| 104 | I extends { parent: string }, |
| 105 | A, |
| 106 | E, |
| 107 | >(options: { |
| 108 | tag: string; |
| 109 | iam: BindingIam; |
| 110 | operation: GcpHttpOp<I, A, E>; |
| 111 | withBody: (name: string, request: Omit<I, "parent"> | undefined) => I; |
| 112 | }) => makeLocationParentHttpBinding<AdaptiveMtDataset, I, A, E>(options); |
| 113 | |
| 114 | export const makeTranslateTextBinding = < |
| 115 | I extends { parent: string }, |
| 116 | A, |
| 117 | E, |
| 118 | >(options: { |
| 119 | tag: string; |
| 120 | iam: BindingIam; |
| 121 | operation: GcpHttpOp<I, A, E>; |
| 122 | withBody: (name: string, request: Omit<I, "parent"> | undefined) => I; |
| 123 | }) => makeLocationParentHttpBinding<Model, I, A, E>(options); |
| 124 | |
| 125 | export { locationParentOf }; |