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

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