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

main
ts 63 lines 2.44 KB
Raw
1 import type * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { App } from "./App.ts";
5
6 /**
7 * Shared scaffolding for Amplify's app-scoped HTTP bindings. Every Amplify
8 * data-plane operation takes the `appId` of the {@link App} it acts on and is
9 * authorized by a single `amplify:*` IAM action over an app-derived ARN — this
10 * factory captures that shape once so each `{Op}Http.ts` is a thin one-call
11 * export.
12 *
13 * NOT exported from `index.ts` — internal scaffolding only.
14 */
15 export interface AmplifyHttpBindingOptions<I extends { appId: string }, A, E, R> {
16 /** Capability name, e.g. `"StartJob"` — used for the bind sid and span name. */
17 name: string;
18 /** The distilled Amplify operation implementing the capability. */
19 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
20 /** IAM actions granted to the host, e.g. `["amplify:StartJob"]`. */
21 actions: string[];
22 /** IAM resource ARNs (derived from the bound app) the actions apply to. */
23 resources: (app: App) => Array<string | Output.Output<string>>;
24 }
25
26 /**
27 * Build the init Effect of an Amplify app-scoped HTTP binding: resolves the
28 * operation once at layer init, grants the IAM statement on the binding host
29 * at deploy time, and returns a runtime callable that injects the bound app's
30 * `appId` into every request.
31 */
32 export const makeAmplifyHttpBinding = <I extends { appId: string }, A, E, R>(
33 options: AmplifyHttpBindingOptions<I, A, E, R>,
34 ) =>
35 Effect.gen(function* () {
36 const bind = yield* AWSBindings.Bind;
37 const operation = yield* AWSBindings.withCredentials(options.operation);
38
39 return Effect.fn(function* (app: App) {
40 const AppId = yield* app.appId;
41 if (!globalThis.__ALCHEMY_RUNTIME__) {
42 yield* bind`AWS.Amplify.${options.name}(${app})`({
43 policyStatements: [
44 {
45 Effect: "Allow",
46 Action: options.actions,
47 Resource: options.resources(app),
48 },
49 ],
50 });
51 }
52 return Effect.fn(`AWS.Amplify.${options.name}(${app.LogicalId})`)(function* (
53 request: Omit<I, "appId">,
54 ) {
55 // Omit<I, "appId"> + the injected appId reconstructs I; TS cannot
56 // prove that for a generic I, hence the assertion.
57 return yield* operation({
58 ...request,
59 appId: yield* AppId,
60 } as unknown as I);
61 });
62 });
63 });