@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 89 lines 3.19 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
5 /**
6 * Options for {@link makeAppFlowHttpBinding}. Everything except these five
7 * inputs is identical across AppFlow's per-operation HTTP bindings.
8 */
9 export interface AppFlowHttpBindingOptions<
10 R extends { readonly LogicalId: string },
11 K extends string,
12 I extends { [P in K]?: string },
13 A,
14 E,
15 OpR,
16 > {
17 /**
18 * The AppFlow action name, e.g. `"StartFlow"`. Becomes the IAM action
19 * (`appflow:StartFlow`), the bind label, and the runtime span name.
20 */
21 action: string;
22 /** The distilled AppFlow operation invoked at runtime. */
23 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, OpR>;
24 /**
25 * Resolve the identifier the operation is keyed by (the flow name or the
26 * connector profile name).
27 */
28 identifier: (resource: R) => Output.Output<string, never>;
29 /** The request field the resolved identifier is injected as. */
30 requestKey: K;
31 /** The IAM policy resources the action is granted on. */
32 resources: (resource: R) => Array<string | Output.Output<string, never>>;
33 }
34
35 /**
36 * Shared scaffolding for AppFlow per-operation HTTP bindings.
37 *
38 * Every AppFlow runtime binding follows the same recipe: resolve the
39 * identifier (flow name / connector profile name), register the IAM grant on
40 * the binding host at deploy time, then invoke the distilled operation with
41 * the identifier injected. This factory owns that recipe so each `{Op}Http.ts`
42 * is a thin `Layer.effect(Cap, makeAppFlowHttpBinding({ ... }))` call whose
43 * request/response/error types are still checked against the capability
44 * contract at the `Layer.effect` site.
45 *
46 * Internal scaffolding — NOT exported from `index.ts`.
47 */
48 export const makeAppFlowHttpBinding = <
49 R extends { readonly LogicalId: string },
50 K extends string,
51 I extends { [P in K]?: string },
52 A,
53 E,
54 OpR,
55 >(
56 options: AppFlowHttpBindingOptions<R, K, I, A, E, OpR>,
57 ) =>
58 Effect.gen(function* () {
59 const bind = yield* AWSBindings.Bind;
60 const operation = yield* AWSBindings.withCredentials(options.operation);
61
62 return Effect.fn(function* (resource: R) {
63 const Identifier = yield* options.identifier(resource);
64 if (!globalThis.__ALCHEMY_RUNTIME__) {
65 yield* bind`AWS.AppFlow.${options.action}(${resource})`({
66 policyStatements: [
67 {
68 Effect: "Allow",
69 Action: [`appflow:${options.action}`],
70 Resource: options.resources(resource),
71 },
72 ],
73 });
74 }
75 return Effect.fn(`AWS.AppFlow.${options.action}(${resource.LogicalId})`)(function* (
76 request?: Omit<I, K>,
77 ) {
78 // The compiler cannot relate `Omit<I, K>` plus a computed generic
79 // key back to `I`, so the identifier injection carries one
80 // contained cast. Concrete request/response types are still fully
81 // checked where each `Layer.effect(Cap, ...)` matches this factory's
82 // inferred shape against the capability contract.
83 return yield* operation({
84 ...request,
85 [options.requestKey]: yield* Identifier,
86 } as unknown as I);
87 });
88 });
89 });