@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 114 lines 3.99 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 { ServerlessCache } from "./ServerlessCache.ts";
5
6 /**
7 * Shared scaffolding for AWS ElastiCache 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 and the IAM action list is
12 * boilerplate. The env-only `ConnectHttp` binding stays bespoke — it attaches
13 * environment variables instead of IAM policy.
14 */
15
16 /**
17 * Serverless cache snapshot ARNs embed the snapshot name, which is runtime
18 * data for every snapshot-addressed operation, so snapshot-scoped grants use
19 * this wildcard.
20 */
21 export const SERVERLESS_SNAPSHOT_ARN_WILDCARD = "arn:aws:elasticache:*:*:serverlesscachesnapshot:*";
22
23 /**
24 * Build the impl Effect for an account-level operation (snapshot management,
25 * cache/event monitoring). The deploy-time half grants `actions` on
26 * `resources` (default `*`) — these operations address caches and snapshots
27 * by names that are runtime data.
28 */
29 export const makeElastiCacheAccountHttpBinding = <I, A, E, R>(options: {
30 /** Fully-qualified binding tag, e.g. `AWS.ElastiCache.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 cache-scoped operation: the runtime callable
66 * injects the bound {@link ServerlessCache}'s name as `ServerlessCacheName`
67 * and the deploy-time half grants `actions` on the cache ARN (plus any
68 * `extraResources`).
69 */
70 export const makeElastiCacheCacheHttpBinding = <
71 I extends { ServerlessCacheName: string },
72 A,
73 E,
74 R,
75 >(options: {
76 /** Fully-qualified binding tag, e.g. `AWS.ElastiCache.CreateServerlessCacheSnapshot`. */
77 tag: string;
78 /** The distilled operation; `ServerlessCacheName` is injected from the cache. */
79 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
80 /** IAM actions granted on the cache ARN. */
81 actions: readonly string[];
82 /** Static IAM resources granted in addition to the cache 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* (cache: ServerlessCache) {
90 const ServerlessCacheName = yield* cache.serverlessCacheName;
91 if (!globalThis.__ALCHEMY_RUNTIME__) {
92 yield* bind`${options.tag}(${cache})`({
93 policyStatements: [
94 {
95 Effect: "Allow",
96 Action: [...options.actions],
97 Resource: [
98 Output.interpolate`${cache.serverlessCacheArn}`,
99 ...(options.extraResources ?? []),
100 ],
101 },
102 ],
103 });
104 }
105 return Effect.fn(`${options.tag}(${cache.LogicalId})`)(function* (
106 request?: Omit<I, "ServerlessCacheName">,
107 ) {
108 return yield* op({
109 ...request,
110 ServerlessCacheName: yield* ServerlessCacheName,
111 } as I);
112 });
113 });
114 });