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

main
ts 97 lines 3.52 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 { Application } from "./Application.ts";
5
6 /**
7 * Shared scaffolding for AWS Kinesis Analytics v2 (Managed Service for
8 * Apache Flink) HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
11 * thin `Layer.effect(Cap, makeKinesisAnalyticsHttpBinding({ … }))` over the
12 * builder below. Everything except the operation and the IAM action list is
13 * boilerplate.
14 *
15 * Every bound operation addresses a single application: the request carries
16 * an `ApplicationName` field which the runtime callable injects from the
17 * bound {@link Application}, and the deploy-time half grants `actions` on
18 * the application's ARN (all `kinesisanalytics:*Application*` actions
19 * authorize against the application resource).
20 */
21 /**
22 * Account-level variant of {@link makeKinesisAnalyticsHttpBinding} for
23 * operations that take no application argument (`ListApplications`). The
24 * deploy-time half grants `actions` on `*` — Kinesis Analytics list calls
25 * are not resource-scoped.
26 */
27 export const makeKinesisAnalyticsAccountHttpBinding = <I, A, E, R>(options: {
28 /** Fully-qualified binding tag, e.g. `AWS.KinesisAnalyticsV2.ListApplications`. */
29 tag: string;
30 /** The distilled operation, invoked with the caller's request as-is. */
31 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
32 /** IAM actions granted on `*`. */
33 actions: readonly string[];
34 }) =>
35 Effect.gen(function* () {
36 const bind = yield* AWSBindings.Bind;
37 const op = yield* AWSBindings.withCredentials(options.operation);
38
39 return Effect.fn(function* () {
40 if (!globalThis.__ALCHEMY_RUNTIME__) {
41 yield* bind`${options.tag}()`({
42 policyStatements: [
43 {
44 Effect: "Allow",
45 Action: [...options.actions],
46 Resource: ["*"],
47 },
48 ],
49 });
50 }
51 return Effect.fn(options.tag)(function* (request?: I) {
52 return yield* op((request ?? {}) as I);
53 });
54 });
55 });
56
57 export const makeKinesisAnalyticsHttpBinding = <
58 I extends { ApplicationName?: string },
59 A,
60 E,
61 R,
62 >(options: {
63 /** Fully-qualified binding tag, e.g. `AWS.KinesisAnalyticsV2.StartApplication`. */
64 tag: string;
65 /** The distilled operation; `ApplicationName` is injected from the application. */
66 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
67 /** IAM actions granted on the application ARN. */
68 actions: readonly string[];
69 }) =>
70 Effect.gen(function* () {
71 const bind = yield* AWSBindings.Bind;
72 const op = yield* AWSBindings.withCredentials(options.operation);
73
74 return Effect.fn(function* <App extends Application>(application: App) {
75 // Outputs yield a DEFERRED effect — resolve again per invocation below.
76 const ApplicationName = yield* application.applicationName;
77 if (!globalThis.__ALCHEMY_RUNTIME__) {
78 yield* bind`${options.tag}(${application})`({
79 policyStatements: [
80 {
81 Effect: "Allow",
82 Action: [...options.actions],
83 Resource: [Output.interpolate`${application.applicationArn}`],
84 },
85 ],
86 });
87 }
88 return Effect.fn(`${options.tag}(${application.LogicalId})`)(function* (
89 request?: Omit<I, "ApplicationName">,
90 ) {
91 return yield* op({
92 ...request,
93 ApplicationName: yield* ApplicationName,
94 } as I);
95 });
96 });
97 });