@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.21 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 { FileSystem } from "./FileSystem.ts";
5
6 /**
7 * Shared scaffolding for the FSx runtime bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, makeFSx…HttpBinding({ … }))` over one of the two
11 * builders below. Everything except the operation, the IAM action list, and
12 * (for file-system-scoped operations) the injected file system id is
13 * boilerplate.
14 */
15
16 /**
17 * Build the impl Effect for a file-system-scoped FSx operation: the runtime
18 * callable injects the bound {@link FileSystem}'s id (as `FileSystemId`, or
19 * as `FileSystemIds: [id]` for the describe operation) and the deploy-time
20 * half grants `actions` on the file system's ARN.
21 */
22 export const makeFSxFileSystemHttpBinding = <
23 I extends { FileSystemId?: string; FileSystemIds?: string[] },
24 A,
25 E,
26 R,
27 >(options: {
28 /** Fully-qualified binding tag, e.g. `AWS.FSx.CreateBackup`. */
29 tag: string;
30 /** The distilled operation; the file system id is injected. */
31 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
32 /** IAM actions granted on the file system ARN (+ `extraResourceArns`). */
33 actions: readonly string[];
34 /**
35 * How the bound file system's id is injected into the request:
36 * `FileSystemId` (default) or `FileSystemIds` (`DescribeFileSystems`
37 * takes a plural array).
38 * @default "FileSystemId"
39 */
40 requestKey?: "FileSystemId" | "FileSystemIds";
41 /**
42 * Additional ARN patterns granted alongside the file system ARN. Used for
43 * operations that also authorize on a resource created by the call itself
44 * (e.g. `CreateBackup` authorizes on the new backup's ARN, unknowable at
45 * deploy time — grant `arn:aws:fsx:*:*:backup/*`).
46 */
47 extraResourceArns?: readonly string[];
48 }) =>
49 Effect.gen(function* () {
50 const bind = yield* AWSBindings.Bind;
51 const op = yield* AWSBindings.withCredentials(options.operation);
52
53 return Effect.fn(function* (fileSystem: FileSystem) {
54 const FileSystemId = yield* fileSystem.fileSystemId;
55 if (!globalThis.__ALCHEMY_RUNTIME__) {
56 yield* bind`${options.tag}(${fileSystem})`({
57 policyStatements: [
58 {
59 Effect: "Allow",
60 Action: [...options.actions],
61 Resource: [
62 Output.interpolate`${fileSystem.fileSystemArn}`,
63 ...(options.extraResourceArns ?? []),
64 ],
65 },
66 ],
67 });
68 }
69 return Effect.fn(`${options.tag}(${fileSystem.LogicalId})`)(function* (
70 request?: Omit<I, "FileSystemId" | "FileSystemIds">,
71 ) {
72 const id = yield* FileSystemId;
73 return yield* op({
74 ...request,
75 ...(options.requestKey === "FileSystemIds"
76 ? { FileSystemIds: [id] }
77 : { FileSystemId: id }),
78 } as I);
79 });
80 });
81 });
82
83 /**
84 * Build the impl Effect for an account-level FSx operation (no file-system
85 * argument): the deploy-time half grants `actions` on `*`. Used for
86 * operations that authorize against ARNs unknowable at deploy time (e.g.
87 * backups, snapshots, and data repository tasks created at runtime) and for
88 * account-wide describes.
89 */
90 export const makeFSxAccountHttpBinding = <I, A, E, R>(options: {
91 /** Fully-qualified binding tag, e.g. `AWS.FSx.DescribeBackups`. */
92 tag: string;
93 /** The distilled operation, invoked with the caller's request as-is. */
94 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
95 /** IAM actions granted on `*`. */
96 actions: readonly string[];
97 }) =>
98 Effect.gen(function* () {
99 const bind = yield* AWSBindings.Bind;
100 const op = yield* AWSBindings.withCredentials(options.operation);
101
102 return Effect.fn(function* () {
103 if (!globalThis.__ALCHEMY_RUNTIME__) {
104 yield* bind`${options.tag}()`({
105 policyStatements: [
106 {
107 Effect: "Allow",
108 Action: [...options.actions],
109 Resource: ["*"],
110 },
111 ],
112 });
113 }
114 return Effect.fn(options.tag)(function* (request?: I) {
115 return yield* op((request ?? {}) as I);
116 });
117 });
118 });