The best way to control Claude Code, Codex and more from any computer or phone

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