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

main
ts 97 lines 3.6 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { EventDataStore } from "./EventDataStore.ts";
4
5 /**
6 * Shared scaffolding for AWS CloudTrail 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, the IAM action list, and (for
11 * Lake operations) the injected event data store is boilerplate.
12 */
13
14 /**
15 * Build the impl Effect for a CloudTrail Lake operation scoped to an
16 * {@link EventDataStore}: the deploy-time half grants `actions` on the bound
17 * store's ARN. Query operations are keyed by `QueryId` and need no request
18 * injection; list/generate operations reference the store in the request via
19 * `injectEventDataStore` / `injectEventDataStores`.
20 */
21 export const makeCloudTrailEventDataStoreHttpBinding = <I, A, E, R>(options: {
22 /** Fully-qualified binding tag, e.g. `AWS.CloudTrail.GetQueryResults`. */
23 tag: string;
24 /** The distilled operation. */
25 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
26 /** IAM actions granted on the event data store ARN. */
27 actions: readonly string[];
28 /** Inject the bound store's ARN as the request's `EventDataStore` field. */
29 injectEventDataStore?: boolean;
30 /** Inject the bound store's ARN as the request's `EventDataStores` list. */
31 injectEventDataStores?: boolean;
32 }) =>
33 Effect.gen(function* () {
34 const bind = yield* AWSBindings.Bind;
35 const op = yield* AWSBindings.withCredentials(options.operation);
36
37 return Effect.fn(function* (store: EventDataStore) {
38 const Arn = yield* store.eventDataStoreArn;
39 if (!globalThis.__ALCHEMY_RUNTIME__) {
40 yield* bind`${options.tag}(${store})`({
41 policyStatements: [
42 {
43 Effect: "Allow",
44 Action: [...options.actions],
45 Resource: [store.eventDataStoreArn],
46 },
47 ],
48 });
49 }
50 return Effect.fn(`${options.tag}(${store.LogicalId})`)(function* (
51 request?: Omit<I, "EventDataStore" | "EventDataStores">,
52 ) {
53 const arn = yield* Arn;
54 return yield* op({
55 ...request,
56 ...(options.injectEventDataStore ? { EventDataStore: arn } : {}),
57 ...(options.injectEventDataStores ? { EventDataStores: [arn] } : {}),
58 } as I);
59 });
60 });
61 });
62
63 /**
64 * Build the impl Effect for an account-level CloudTrail operation (event
65 * history lookup, digest public keys, Insights metrics). The deploy-time
66 * half grants `actions` on `*` — these CloudTrail actions are not
67 * resource-scoped.
68 */
69 export const makeCloudTrailAccountHttpBinding = <I, A, E, R>(options: {
70 /** Fully-qualified binding tag, e.g. `AWS.CloudTrail.LookupEvents`. */
71 tag: string;
72 /** The distilled operation, invoked with the caller's request as-is. */
73 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
74 /** IAM actions granted on `*`. */
75 actions: readonly string[];
76 }) =>
77 Effect.gen(function* () {
78 const bind = yield* AWSBindings.Bind;
79 const op = yield* AWSBindings.withCredentials(options.operation);
80
81 return Effect.fn(function* () {
82 if (!globalThis.__ALCHEMY_RUNTIME__) {
83 yield* bind`${options.tag}()`({
84 policyStatements: [
85 {
86 Effect: "Allow",
87 Action: [...options.actions],
88 Resource: ["*"],
89 },
90 ],
91 });
92 }
93 return Effect.fn(options.tag)(function* (request?: I) {
94 return yield* op((request ?? {}) as I);
95 });
96 });
97 });