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