@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 124 lines 4.75 KB
Raw
1 import type * as aas from "@distilled.cloud/aws/application-auto-scaling";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { ScalableTarget } from "./ScalableTarget.ts";
5 import type { ScalingPolicy } from "./ScalingPolicy.ts";
6
7 /**
8 * Shared scaffolding for Application Auto Scaling HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
11 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
12 * builders below. Everything except the operation and the IAM action list is
13 * boilerplate: the runtime callable injects the scalable target's identity
14 * triple (`ServiceNamespace`/`ResourceId`/`ScalableDimension`) — plus
15 * `PolicyName` for policy-scoped operations — from the bound resource.
16 *
17 * All grants use `Resource: ["*"]` because Application Auto Scaling's
18 * describe/get actions do not support resource-level permissions (only the
19 * tagging actions accept the `scalable-target/…` ARN).
20 */
21
22 /** The request fields identifying a scalable target, injected from the bound resource. */
23 interface TargetTriple {
24 ServiceNamespace: aas.ServiceNamespace;
25 ResourceId: string;
26 ScalableDimension: aas.ScalableDimension;
27 }
28
29 /**
30 * Build the impl Effect for a scalable-target-scoped operation: the runtime
31 * callable injects the bound {@link ScalableTarget}'s identity triple and the
32 * deploy-time half grants `actions` on `*`.
33 */
34 export const makeTargetScopedHttpBinding = <I extends TargetTriple, A, E, R>(options: {
35 /** Fully-qualified binding tag, e.g. `AWS.ApplicationAutoScaling.DescribeScalingActivities`. */
36 tag: string;
37 /** The distilled operation; the identity triple is injected from the target. */
38 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
39 /** IAM actions granted on `*` (no resource-level permission support). */
40 actions: readonly string[];
41 }) =>
42 Effect.gen(function* () {
43 const bind = yield* AWSBindings.Bind;
44 const op = yield* AWSBindings.withCredentials(options.operation);
45
46 return Effect.fn(function* (target: ScalableTarget) {
47 const ServiceNamespace = yield* target.serviceNamespace;
48 const ResourceId = yield* target.resourceId;
49 const ScalableDimension = yield* target.scalableDimension;
50 if (!globalThis.__ALCHEMY_RUNTIME__) {
51 yield* bind`${options.tag}(${target})`({
52 policyStatements: [
53 {
54 Effect: "Allow",
55 Action: [...options.actions],
56 Resource: ["*"],
57 },
58 ],
59 });
60 }
61 return Effect.fn(`${options.tag}(${target.LogicalId})`)(function* (
62 request?: Omit<I, keyof TargetTriple>,
63 ) {
64 return yield* op({
65 ...request,
66 ServiceNamespace: yield* ServiceNamespace,
67 ResourceId: yield* ResourceId,
68 ScalableDimension: yield* ScalableDimension,
69 } as I);
70 });
71 });
72 });
73
74 /**
75 * Build the impl Effect for a scaling-policy-scoped operation: the runtime
76 * callable injects the bound {@link ScalingPolicy}'s identity triple and
77 * `PolicyName`, and the deploy-time half grants `actions` on `*`.
78 */
79 export const makePolicyScopedHttpBinding = <
80 I extends TargetTriple & { PolicyName: string },
81 A,
82 E,
83 R,
84 >(options: {
85 /** Fully-qualified binding tag, e.g. `AWS.ApplicationAutoScaling.GetPredictiveScalingForecast`. */
86 tag: string;
87 /** The distilled operation; the triple and `PolicyName` are injected from the policy. */
88 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
89 /** IAM actions granted on `*` (no resource-level permission support). */
90 actions: readonly string[];
91 }) =>
92 Effect.gen(function* () {
93 const bind = yield* AWSBindings.Bind;
94 const op = yield* AWSBindings.withCredentials(options.operation);
95
96 return Effect.fn(function* (policy: ScalingPolicy) {
97 const ServiceNamespace = yield* policy.serviceNamespace;
98 const ResourceId = yield* policy.resourceId;
99 const ScalableDimension = yield* policy.scalableDimension;
100 const PolicyName = yield* policy.policyName;
101 if (!globalThis.__ALCHEMY_RUNTIME__) {
102 yield* bind`${options.tag}(${policy})`({
103 policyStatements: [
104 {
105 Effect: "Allow",
106 Action: [...options.actions],
107 Resource: ["*"],
108 },
109 ],
110 });
111 }
112 return Effect.fn(`${options.tag}(${policy.LogicalId})`)(function* (
113 request: Omit<I, keyof TargetTriple | "PolicyName">,
114 ) {
115 return yield* op({
116 ...request,
117 ServiceNamespace: yield* ServiceNamespace,
118 ResourceId: yield* ResourceId,
119 ScalableDimension: yield* ScalableDimension,
120 PolicyName: yield* PolicyName,
121 } as I);
122 });
123 });
124 });