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

main
ts 114 lines 3.93 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 { Cluster } from "./Cluster.ts";
5
6 /**
7 * Shared scaffolding for Amazon MemoryDB HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
11 * below. Everything except the operation, the identifier resolver, and the
12 * IAM action list is boilerplate. The env-only side of `ConnectHttp` stays
13 * bespoke — it attaches environment variables in addition to IAM policy.
14 */
15
16 /**
17 * MemoryDB snapshot ARNs embed the snapshot name, which is runtime data for
18 * every snapshot-addressed operation, so snapshot-scoped grants use this
19 * wildcard.
20 */
21 export const SNAPSHOT_ARN_WILDCARD = "arn:aws:memorydb:*:*:snapshot/*";
22
23 /**
24 * Build the impl Effect for an account-level operation (snapshot management,
25 * cluster/event monitoring). The deploy-time half grants `actions` on
26 * `resources` (default `*`) — these operations address clusters and
27 * snapshots by names that are runtime data.
28 */
29 export const makeMemoryDBAccountHttpBinding = <I, A, E, R>(options: {
30 /** Fully-qualified binding tag, e.g. `AWS.MemoryDB.DescribeEvents`. */
31 tag: string;
32 /** The distilled operation, invoked with the caller's request as-is. */
33 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
34 /** IAM actions granted. */
35 actions: readonly string[];
36 /**
37 * IAM resources the actions are granted on.
38 * @default ["*"]
39 */
40 resources?: 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* () {
47 if (!globalThis.__ALCHEMY_RUNTIME__) {
48 yield* bind`${options.tag}()`({
49 policyStatements: [
50 {
51 Effect: "Allow",
52 Action: [...options.actions],
53 Resource: [...(options.resources ?? ["*"])],
54 },
55 ],
56 });
57 }
58 return Effect.fn(options.tag)(function* (request?: I) {
59 return yield* op((request ?? {}) as I);
60 });
61 });
62 });
63
64 /**
65 * Build the impl Effect for a cluster-scoped operation: the runtime callable
66 * injects the bound {@link Cluster}'s name as `ClusterName` and the
67 * deploy-time half grants `actions` on the cluster ARN (plus any
68 * `extraResources`, e.g. the snapshot ARN wildcard for snapshot creation).
69 */
70 export const makeMemoryDBClusterHttpBinding = <
71 I extends { ClusterName: string },
72 A,
73 E,
74 R,
75 >(options: {
76 /** Fully-qualified binding tag, e.g. `AWS.MemoryDB.FailoverShard`. */
77 tag: string;
78 /** The distilled operation; `ClusterName` is injected from the cluster. */
79 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
80 /** IAM actions granted on the cluster ARN. */
81 actions: readonly string[];
82 /** Static IAM resources granted in addition to the cluster ARN. */
83 extraResources?: readonly string[];
84 }) =>
85 Effect.gen(function* () {
86 const bind = yield* AWSBindings.Bind;
87 const op = yield* AWSBindings.withCredentials(options.operation);
88
89 return Effect.fn(function* (cluster: Cluster) {
90 const ClusterName = yield* cluster.clusterName;
91 if (!globalThis.__ALCHEMY_RUNTIME__) {
92 yield* bind`${options.tag}(${cluster})`({
93 policyStatements: [
94 {
95 Effect: "Allow",
96 Action: [...options.actions],
97 Resource: [
98 Output.interpolate`${cluster.clusterArn}`,
99 ...(options.extraResources ?? []),
100 ],
101 },
102 ],
103 });
104 }
105 return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* (
106 request?: Omit<I, "ClusterName">,
107 ) {
108 return yield* op({
109 ...request,
110 ClusterName: yield* ClusterName,
111 } as I);
112 });
113 });
114 });