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

main
ts 91 lines 3.25 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { Graph } from "./Graph.ts";
4
5 /**
6 * Shared scaffolding for Amazon Detective 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 Detective operation scoped to a behavior
16 * {@link Graph}: the deploy-time half grants `actions` on the bound graph's
17 * ARN, and the runtime half injects the graph's `GraphArn` into every
18 * request.
19 */
20 export const makeDetectiveGraphHttpBinding = <I extends { GraphArn: string }, A, E, R>(options: {
21 /** Fully-qualified binding tag, e.g. `AWS.Detective.ListMembers`. */
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 behavior graph 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* (graph: Graph) {
33 const GraphArn = yield* graph.graphArn;
34 if (!globalThis.__ALCHEMY_RUNTIME__) {
35 yield* bind`${options.tag}(${graph})`({
36 policyStatements: [
37 {
38 Effect: "Allow",
39 Action: [...options.actions],
40 Resource: [graph.graphArn],
41 },
42 ],
43 });
44 }
45 return Effect.fn(`${options.tag}(${graph.LogicalId})`)(function* (
46 request?: Omit<I, "GraphArn">,
47 ) {
48 const graphArn = yield* GraphArn;
49 return yield* op({ ...request, GraphArn: graphArn } as I);
50 });
51 });
52 });
53
54 /**
55 * Build the impl Effect for an account-level Detective operation — the
56 * member-account invitation flow (`AcceptInvitation`, `RejectInvitation`,
57 * `DisassociateMembership`, `ListInvitations`,
58 * `BatchGetMembershipDatasources`) and the organization-admin actions. The
59 * deploy-time half grants `actions` on `*`: these operations either take no
60 * resource at all or target a behavior graph owned by a *different* (admin)
61 * account whose ARN is only known at runtime.
62 */
63 export const makeDetectiveAccountHttpBinding = <I, A, E, R>(options: {
64 /** Fully-qualified binding tag, e.g. `AWS.Detective.ListInvitations`. */
65 tag: string;
66 /** The distilled operation, invoked with the caller's request as-is. */
67 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
68 /** IAM actions granted on `*`. */
69 actions: readonly string[];
70 }) =>
71 Effect.gen(function* () {
72 const bind = yield* AWSBindings.Bind;
73 const op = yield* AWSBindings.withCredentials(options.operation);
74
75 return Effect.fn(function* () {
76 if (!globalThis.__ALCHEMY_RUNTIME__) {
77 yield* bind`${options.tag}()`({
78 policyStatements: [
79 {
80 Effect: "Allow",
81 Action: [...options.actions],
82 Resource: ["*"],
83 },
84 ],
85 });
86 }
87 return Effect.fn(options.tag)(function* (request?: I) {
88 return yield* op((request ?? {}) as I);
89 });
90 });
91 });