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

main
ts 110 lines 4.14 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 { Monitor } from "./Monitor.ts";
5
6 /**
7 * Shared scaffolding for Amazon CloudWatch Internet Monitor HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
11 * builders below. Everything except the operation and the IAM action is
12 * boilerplate:
13 *
14 * - {@link makeInternetMonitorMonitorHttpBinding} — operations scoped to one
15 * bound {@link Monitor} (`ListHealthEvents`, `GetHealthEvent`, the query
16 * interface). The runtime callable injects the monitor's name as the
17 * request's `MonitorName`; the deploy-time half grants `actions` on the
18 * monitor ARN plus its sub-resources (health events are addressed as
19 * `{monitorArn}/health-event/{id}`).
20 * - {@link makeInternetMonitorAccountHttpBinding} — account-level operations
21 * (`GetInternetEvent`, `ListInternetEvents`). Internet events are global
22 * outage records, not ARN-addressable resources, so the deploy-time half
23 * grants `actions` on `*`.
24 */
25
26 /**
27 * Build the impl Effect for an operation scoped to a single bound
28 * {@link Monitor}. The runtime callable injects the monitor's name as the
29 * request's `MonitorName`; the deploy-time half grants `actions` on the
30 * monitor ARN and its sub-resources.
31 */
32 export const makeInternetMonitorMonitorHttpBinding = <
33 I extends { MonitorName: string },
34 A,
35 E,
36 R,
37 >(options: {
38 /** Fully-qualified binding tag, e.g. `AWS.InternetMonitor.ListHealthEvents`. */
39 tag: string;
40 /** The distilled operation; `MonitorName` is injected from the resource. */
41 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
42 /** IAM actions granted on the monitor ARN (and its sub-resources). */
43 actions: readonly string[];
44 }) =>
45 Effect.gen(function* () {
46 const bind = yield* AWSBindings.Bind;
47 const op = yield* AWSBindings.withCredentials(options.operation);
48
49 return Effect.fn(function* (monitor: Monitor) {
50 const MonitorName = yield* monitor.monitorName;
51 if (!globalThis.__ALCHEMY_RUNTIME__) {
52 yield* bind`${options.tag}(${monitor})`({
53 policyStatements: [
54 {
55 Effect: "Allow",
56 Action: [...options.actions],
57 // Health events (and other per-monitor entities) are
58 // addressed as sub-resources of the monitor ARN, e.g.
59 // `{monitorArn}/health-event/{eventId}`.
60 Resource: [monitor.monitorArn, Output.interpolate`${monitor.monitorArn}/*`],
61 },
62 ],
63 });
64 }
65 return Effect.fn(`${options.tag}(${monitor.LogicalId})`)(function* (
66 request?: Omit<I, "MonitorName">,
67 ) {
68 return yield* op({
69 ...request,
70 MonitorName: yield* MonitorName,
71 } as I);
72 });
73 });
74 });
75
76 /**
77 * Build the impl Effect for an account-level Internet Monitor operation. The
78 * runtime callable passes the caller's request through unchanged; the
79 * deploy-time half grants `actions` on `*` (internet events do not support
80 * resource-level permissions).
81 */
82 export const makeInternetMonitorAccountHttpBinding = <I, A, E, R>(options: {
83 /** Fully-qualified binding tag, e.g. `AWS.InternetMonitor.ListInternetEvents`. */
84 tag: string;
85 /** The distilled operation, invoked with the caller's request as-is. */
86 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
87 /** IAM actions granted on `*`. */
88 actions: readonly string[];
89 }) =>
90 Effect.gen(function* () {
91 const bind = yield* AWSBindings.Bind;
92 const op = yield* AWSBindings.withCredentials(options.operation);
93
94 return Effect.fn(function* () {
95 if (!globalThis.__ALCHEMY_RUNTIME__) {
96 yield* bind`${options.tag}()`({
97 policyStatements: [
98 {
99 Effect: "Allow",
100 Action: [...options.actions],
101 Resource: ["*"],
102 },
103 ],
104 });
105 }
106 return Effect.fn(options.tag)(function* (request?: I) {
107 return yield* op((request ?? {}) as I);
108 });
109 });
110 });