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

main
ts 133 lines 4.82 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { Channel } from "./Channel.ts";
4 import type { Input } from "./Input.ts";
5
6 /**
7 * Shared scaffolding for AWS Elemental MediaLive HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service
10 * is a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the three
11 * builders below. Everything except the operation and the IAM action list
12 * is boilerplate: channel-scoped bindings inject the bound channel's
13 * server-assigned id as the request's `ChannelId` and grant `actions` on
14 * the channel ARN; input-scoped bindings do the same with `InputId` and
15 * the input ARN; account-scoped bindings pass the request through and
16 * grant `actions` on `*`.
17 */
18
19 /**
20 * Build the impl Effect for a MediaLive operation scoped to a
21 * {@link Channel}: the deploy-time half grants `actions` on the bound
22 * channel's ARN, and the runtime half injects the channel's id into every
23 * request as `ChannelId`.
24 */
25 export const makeMediaLiveChannelHttpBinding = <I extends { ChannelId: string }, A, E, R>(options: {
26 /** Fully-qualified binding tag, e.g. `AWS.MediaLive.StartChannel`. */
27 tag: string;
28 /** The distilled operation; `ChannelId` is injected from the channel. */
29 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
30 /** IAM actions granted on the channel ARN. */
31 actions: readonly string[];
32 }) =>
33 Effect.gen(function* () {
34 const bind = yield* AWSBindings.Bind;
35 const op = yield* AWSBindings.withCredentials(options.operation);
36
37 return Effect.fn(function* (channel: Channel) {
38 const ChannelId = yield* channel.channelId;
39 if (!globalThis.__ALCHEMY_RUNTIME__) {
40 yield* bind`${options.tag}(${channel})`({
41 policyStatements: [
42 {
43 Effect: "Allow",
44 Action: [...options.actions],
45 Resource: [channel.channelArn],
46 },
47 ],
48 });
49 }
50 return Effect.fn(`${options.tag}(${channel.LogicalId})`)(function* (
51 request?: Omit<I, "ChannelId">,
52 ) {
53 const channelId = yield* ChannelId;
54 return yield* op({ ...request, ChannelId: channelId } as I);
55 });
56 });
57 });
58
59 /**
60 * Build the impl Effect for a MediaLive operation scoped to an
61 * {@link Input}: the deploy-time half grants `actions` on the bound
62 * input's ARN, and the runtime half injects the input's id into every
63 * request as `InputId`.
64 */
65 export const makeMediaLiveInputHttpBinding = <I extends { InputId: string }, A, E, R>(options: {
66 /** Fully-qualified binding tag, e.g. `AWS.MediaLive.DescribeInput`. */
67 tag: string;
68 /** The distilled operation; `InputId` is injected from the input. */
69 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
70 /** IAM actions granted on the input ARN. */
71 actions: readonly string[];
72 }) =>
73 Effect.gen(function* () {
74 const bind = yield* AWSBindings.Bind;
75 const op = yield* AWSBindings.withCredentials(options.operation);
76
77 return Effect.fn(function* (input: Input) {
78 const InputId = yield* input.inputId;
79 if (!globalThis.__ALCHEMY_RUNTIME__) {
80 yield* bind`${options.tag}(${input})`({
81 policyStatements: [
82 {
83 Effect: "Allow",
84 Action: [...options.actions],
85 Resource: [input.inputArn],
86 },
87 ],
88 });
89 }
90 return Effect.fn(`${options.tag}(${input.LogicalId})`)(function* (
91 request?: Omit<I, "InputId">,
92 ) {
93 const inputId = yield* InputId;
94 return yield* op({ ...request, InputId: inputId } as I);
95 });
96 });
97 });
98
99 /**
100 * Build the impl Effect for an account-level MediaLive operation (e.g.
101 * enumerating the account's channels or inputs). The deploy-time half
102 * grants `actions` on `*` — these list operations are not scoped to a
103 * single resource.
104 */
105 export const makeMediaLiveAccountHttpBinding = <I, A, E, R>(options: {
106 /** Fully-qualified binding tag, e.g. `AWS.MediaLive.ListChannels`. */
107 tag: string;
108 /** The distilled operation, invoked with the caller's request as-is. */
109 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
110 /** IAM actions granted on `*`. */
111 actions: readonly string[];
112 }) =>
113 Effect.gen(function* () {
114 const bind = yield* AWSBindings.Bind;
115 const op = yield* AWSBindings.withCredentials(options.operation);
116
117 return Effect.fn(function* () {
118 if (!globalThis.__ALCHEMY_RUNTIME__) {
119 yield* bind`${options.tag}()`({
120 policyStatements: [
121 {
122 Effect: "Allow",
123 Action: [...options.actions],
124 Resource: ["*"],
125 },
126 ],
127 });
128 }
129 return Effect.fn(options.tag)(function* (request?: I) {
130 return yield* op((request ?? {}) as I);
131 });
132 });
133 });