| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Binding from "../../Binding.ts"; |
| 3 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 4 | import type { Endpoint } from "./Endpoint.ts"; |
| 5 | import type { FeatureGroup } from "./FeatureGroup.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for Amazon SageMaker HTTP bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 11 | * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders |
| 12 | * below. Everything except the operation, the IAM action list, and the |
| 13 | * request shaping is boilerplate. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * Build the impl Effect for a SageMaker Feature Store data-plane operation |
| 18 | * scoped to a {@link FeatureGroup}: the deploy-time half grants `actions` on |
| 19 | * the bound feature group's ARN, and the runtime half injects the feature |
| 20 | * group's name into every request via `prepare`. |
| 21 | */ |
| 22 | export const makeFeatureGroupHttpBinding = <Req, I, A, E, R>(options: { |
| 23 | /** Fully-qualified binding tag, e.g. `AWS.SageMaker.GetRecord`. */ |
| 24 | tag: string; |
| 25 | /** The distilled operation. */ |
| 26 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 27 | /** IAM actions granted on the feature group ARN. */ |
| 28 | actions: readonly string[]; |
| 29 | /** Shape the caller's request into the wire request (injecting the name). */ |
| 30 | prepare: (request: Req, featureGroupName: string) => I; |
| 31 | }) => |
| 32 | Effect.gen(function* () { |
| 33 | const op = yield* options.operation; |
| 34 | |
| 35 | return Effect.fn(function* (featureGroup: FeatureGroup) { |
| 36 | const FeatureGroupName = yield* featureGroup.featureGroupName; |
| 37 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 38 | const host = yield* Binding.Host; |
| 39 | if (isBindingHost(host)) { |
| 40 | yield* host.bind`Allow(${host}, ${options.tag}(${featureGroup}))`({ |
| 41 | policyStatements: [ |
| 42 | { |
| 43 | Effect: "Allow", |
| 44 | Action: [...options.actions], |
| 45 | Resource: [featureGroup.featureGroupArn], |
| 46 | }, |
| 47 | ], |
| 48 | }); |
| 49 | } |
| 50 | } |
| 51 | return Effect.fn(`${options.tag}(${featureGroup.LogicalId})`)(function* ( |
| 52 | request: Req, |
| 53 | ) { |
| 54 | const featureGroupName = yield* FeatureGroupName; |
| 55 | return yield* op(options.prepare(request, featureGroupName)); |
| 56 | }); |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | /** |
| 61 | * Build the impl Effect for a SageMaker control-plane operation scoped to an |
| 62 | * {@link Endpoint}: the deploy-time half grants `actions` on the bound |
| 63 | * endpoint's ARN, and the runtime half injects the endpoint's name into |
| 64 | * every request. |
| 65 | */ |
| 66 | export const makeEndpointHttpBinding = < |
| 67 | I extends { EndpointName?: string }, |
| 68 | A, |
| 69 | E, |
| 70 | R, |
| 71 | >(options: { |
| 72 | /** Fully-qualified binding tag, e.g. `AWS.SageMaker.DescribeEndpoint`. */ |
| 73 | tag: string; |
| 74 | /** The distilled operation. */ |
| 75 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 76 | /** IAM actions granted on the endpoint ARN. */ |
| 77 | actions: readonly string[]; |
| 78 | }) => |
| 79 | Effect.gen(function* () { |
| 80 | const op = yield* options.operation; |
| 81 | |
| 82 | return Effect.fn(function* (endpoint: Endpoint) { |
| 83 | const EndpointName = yield* endpoint.endpointName; |
| 84 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 85 | const host = yield* Binding.Host; |
| 86 | if (isBindingHost(host)) { |
| 87 | yield* host.bind`Allow(${host}, ${options.tag}(${endpoint}))`({ |
| 88 | policyStatements: [ |
| 89 | { |
| 90 | Effect: "Allow", |
| 91 | Action: [...options.actions], |
| 92 | Resource: [endpoint.endpointArn], |
| 93 | }, |
| 94 | ], |
| 95 | }); |
| 96 | } |
| 97 | } |
| 98 | return Effect.fn(`${options.tag}(${endpoint.LogicalId})`)(function* ( |
| 99 | request?: Omit<I, "EndpointName">, |
| 100 | ) { |
| 101 | const endpointName = yield* EndpointName; |
| 102 | return yield* op({ ...request, EndpointName: endpointName } as I); |
| 103 | }); |
| 104 | }); |
| 105 | }); |