1/**
2 * Shared scaffolding for EventBridge Pipes HTTP bindings.
3 *
4 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
5 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
6 * below. Everything except the operation and the IAM action list is
7 * boilerplate:
8 *
9 * - Pipe-scoped operations (`pipes:DescribePipe`, `pipes:StartPipe`,
10 * `pipes:StopPipe`) inject the bound {@link Pipe}'s name as the request's
11 * `Name` field and are granted on the pipe ARN.
12 * - Account-level operations (`pipes:ListPipes`) take the caller's request
13 * as-is and are granted on `*`.
14 */
15import * as Effect from "effect/Effect";
16import * as Binding from "../../Binding.ts";
17import { isBindingHost } from "../Lambda/Function.ts";
18import type { Pipe } from "./Pipe.ts";
19
20/**
21 * Build the impl Effect for a Pipes operation scoped to a {@link Pipe}: the
22 * deploy-time half grants `actions` on the bound pipe's ARN, and the runtime
23 * half injects the pipe's name as the request's `Name` field.
24 */
25export const makePipesHttpBinding = <
26 I extends { Name: string },
27 A,
28 E,
29 R,
30>(options: {
31 /** Fully-qualified binding tag, e.g. `AWS.Pipes.StartPipe`. */
32 tag: string;
33 /** The distilled operation; `Name` is injected from the pipe. */
34 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
35 /** IAM actions granted on the pipe ARN. */
36 actions: readonly string[];
37}) =>
38 Effect.gen(function* () {
39 const op = yield* options.operation;
40
41 return Effect.fn(function* (pipe: Pipe) {
42 const PipeName = yield* pipe.pipeName;
43 if (!globalThis.__ALCHEMY_RUNTIME__) {
44 const host = yield* Binding.Host;
45 if (isBindingHost(host)) {
46 yield* host.bind`Allow(${host}, ${options.tag}(${pipe}))`({
47 policyStatements: [
48 {
49 Effect: "Allow",
50 Action: [...options.actions],
51 Resource: [pipe.pipeArn],
52 },
53 ],
54 });
55 }
56 }
57 return Effect.fn(`${options.tag}(${pipe.LogicalId})`)(function* (
58 request?: Omit<I, "Name">,
59 ) {
60 return yield* op({
61 ...request,
62 Name: yield* PipeName,
63 } as I);
64 });
65 });
66 });
67
68/**
69 * Build the impl Effect for an account-level Pipes operation (pipe listing).
70 * The deploy-time half grants `actions` on `*` — these operations are not
71 * scoped to a single pipe resource.
72 */
73export const makePipesAccountHttpBinding = <I, A, E, R>(options: {
74 /** Fully-qualified binding tag, e.g. `AWS.Pipes.ListPipes`. */
75 tag: string;
76 /** The distilled operation, invoked with the caller's request as-is. */
77 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
78 /** IAM actions granted on `*`. */
79 actions: readonly string[];
80}) =>
81 Effect.gen(function* () {
82 const op = yield* options.operation;
83
84 return Effect.fn(function* () {
85 if (!globalThis.__ALCHEMY_RUNTIME__) {
86 const host = yield* Binding.Host;
87 if (isBindingHost(host)) {
88 yield* host.bind`Allow(${host}, ${options.tag}())`({
89 policyStatements: [
90 {
91 Effect: "Allow",
92 Action: [...options.actions],
93 Resource: ["*"],
94 },
95 ],
96 });
97 }
98 }
99 return Effect.fn(options.tag)(function* (request?: I) {
100 return yield* op((request ?? {}) as I);
101 });
102 });
103 });