@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 118 lines 4.06 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { Budget } from "./Budget.ts";
5 import type { BudgetAction } from "./BudgetAction.ts";
6
7 /**
8 * Shared scaffolding for the AWS Budgets 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, the IAM action list, and
13 * the injected identifiers (`AccountId` + `BudgetName`, plus `ActionId` for
14 * action-scoped operations) is boilerplate.
15 *
16 * Budgets is a global service — its ARNs have no region component and the
17 * endpoint resolver routes every standard-partition region to the global
18 * endpoint.
19 */
20
21 /**
22 * Build the impl Effect for a budget-scoped operation: the runtime callable
23 * injects the bound {@link Budget}'s `AccountId` and `BudgetName` and the
24 * deploy-time half grants `actions` on the budget ARN.
25 */
26 export const makeBudgetHttpBinding = <
27 I extends { AccountId: string; BudgetName: string },
28 A,
29 E,
30 R,
31 >(options: {
32 /** Fully-qualified binding tag, e.g. `AWS.Budgets.DescribeBudget`. */
33 tag: string;
34 /** The distilled operation; `AccountId`/`BudgetName` are injected. */
35 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
36 /** IAM actions granted on the budget ARN. */
37 actions: readonly string[];
38 }) =>
39 Effect.gen(function* () {
40 const bind = yield* AWSBindings.Bind;
41 const op = yield* AWSBindings.withCredentials(options.operation);
42
43 return Effect.fn(function* (budget: Budget) {
44 const AccountId = yield* budget.accountId;
45 const BudgetName = yield* budget.budgetName;
46 if (!globalThis.__ALCHEMY_RUNTIME__) {
47 yield* bind`${options.tag}(${budget})`({
48 policyStatements: [
49 {
50 Effect: "Allow",
51 Action: [...options.actions],
52 Resource: [Output.interpolate`${budget.budgetArn}`],
53 },
54 ],
55 });
56 }
57 return Effect.fn(`${options.tag}(${budget.LogicalId})`)(function* (
58 request?: Omit<I, "AccountId" | "BudgetName">,
59 ) {
60 return yield* op({
61 ...request,
62 AccountId: yield* AccountId,
63 BudgetName: yield* BudgetName,
64 } as I);
65 });
66 });
67 });
68
69 /**
70 * Build the impl Effect for a budget-action-scoped operation: the runtime
71 * callable injects the bound {@link BudgetAction}'s `AccountId`,
72 * `BudgetName`, and `ActionId`, and the deploy-time half grants `actions` on
73 * the action ARN.
74 */
75 export const makeBudgetActionHttpBinding = <
76 I extends { AccountId: string; BudgetName: string; ActionId: string },
77 A,
78 E,
79 R,
80 >(options: {
81 /** Fully-qualified binding tag, e.g. `AWS.Budgets.ExecuteBudgetAction`. */
82 tag: string;
83 /** The distilled operation; the action identifiers are injected. */
84 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
85 /** IAM actions granted on the budget action ARN. */
86 actions: readonly string[];
87 }) =>
88 Effect.gen(function* () {
89 const bind = yield* AWSBindings.Bind;
90 const op = yield* AWSBindings.withCredentials(options.operation);
91
92 return Effect.fn(function* (action: BudgetAction) {
93 const AccountId = yield* action.accountId;
94 const BudgetName = yield* action.budgetName;
95 const ActionId = yield* action.actionId;
96 if (!globalThis.__ALCHEMY_RUNTIME__) {
97 yield* bind`${options.tag}(${action})`({
98 policyStatements: [
99 {
100 Effect: "Allow",
101 Action: [...options.actions],
102 Resource: [Output.interpolate`${action.actionArn}`],
103 },
104 ],
105 });
106 }
107 return Effect.fn(`${options.tag}(${action.LogicalId})`)(function* (
108 request: Omit<I, "AccountId" | "BudgetName" | "ActionId">,
109 ) {
110 return yield* op({
111 ...request,
112 AccountId: yield* AccountId,
113 BudgetName: yield* BudgetName,
114 ActionId: yield* ActionId,
115 } as I);
116 });
117 });
118 });