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

main
ts 63 lines 2.58 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { AutoScalingGroup } from "./AutoScalingGroup.ts";
4
5 /**
6 * Shared scaffolding for EC2 Auto Scaling HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every single-operation `{Op}Http.ts` in this
9 * service is a thin `Layer.effect(Cap, makeGroupHttpBinding({ … }))` over the
10 * builder below. Everything except the operation and the IAM action list is
11 * boilerplate: the runtime callable injects the bound
12 * {@link AutoScalingGroup}'s `AutoScalingGroupName` into the request (the
13 * serializer drops the field for the few operations that do not accept it —
14 * those are authorized against the instance's owning group) and the
15 * deploy-time half grants `actions` on the group ARN, or on `*` for
16 * `Describe*` actions (EC2 Auto Scaling `Describe*` actions do not support
17 * resource-level permissions).
18 *
19 * Genuinely-different bindings (multi-operation clients like
20 * `CompleteLifecycleAction`, `Standby`, and `InstanceRefresh`, or custom
21 * request shaping like `DescribeAutoScalingGroup`) stay bespoke.
22 */
23 export const makeGroupHttpBinding = <I extends object, A, E, R>(options: {
24 /** Fully-qualified binding tag, e.g. `AWS.AutoScaling.SetDesiredCapacity`. */
25 tag: string;
26 /** The distilled operation; `AutoScalingGroupName` is injected from the group. */
27 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
28 /** IAM actions granted by the binding. */
29 actions: readonly string[];
30 /**
31 * IAM resource scope. Write actions support resource-level permissions on
32 * the group ARN (`"group"`, the default); `Describe*` actions do not
33 * (`"*"`).
34 */
35 resource?: "group" | "*";
36 }) =>
37 Effect.gen(function* () {
38 const bind = yield* AWSBindings.Bind;
39 const op = yield* AWSBindings.withCredentials(options.operation);
40
41 return Effect.fn(function* (group: AutoScalingGroup) {
42 const AutoScalingGroupName = yield* group.autoScalingGroupName;
43 if (!globalThis.__ALCHEMY_RUNTIME__) {
44 yield* bind`${options.tag}(${group})`({
45 policyStatements: [
46 {
47 Effect: "Allow",
48 Action: [...options.actions],
49 Resource: options.resource === "*" ? ["*"] : [group.autoScalingGroupArn],
50 },
51 ],
52 });
53 }
54 return Effect.fn(`${options.tag}(${group.LogicalId})`)(function* (
55 request?: Omit<I, "AutoScalingGroupName">,
56 ) {
57 return yield* op({
58 ...request,
59 AutoScalingGroupName: yield* AutoScalingGroupName,
60 } as I);
61 });
62 });
63 });