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

main
ts 107 lines 3.79 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { ManagedThing } from "./ManagedThing.ts";
4
5 /**
6 * Shared scaffolding for the AWS IoT Managed Integrations HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
9 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
10 * below. Everything except the operation, the IAM action list, and (for the
11 * thing-scoped builder) the injected identifier field is boilerplate.
12 */
13
14 /**
15 * Build the impl Effect for an operation scoped to one {@link ManagedThing}.
16 * The deploy-time half grants `iamActions` on the bound thing's ARN; the
17 * runtime half injects the thing's service-generated id into the request
18 * under `key` (the API is split between `Identifier` and `ManagedThingId`
19 * request fields).
20 */
21 export const makeManagedThingHttpBinding = <
22 I extends object,
23 A,
24 E,
25 R,
26 K extends "Identifier" | "ManagedThingId",
27 >(options: {
28 /**
29 * Short capability name used in the binding sid and runtime span, e.g.
30 * `"SendManagedThingCommand"`.
31 */
32 capability: string;
33 /** IAM actions granted on the bound managed thing's ARN. */
34 iamActions: readonly string[];
35 /** The distilled operation; the field named `key` is injected. */
36 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
37 /** Request field that carries the bound thing's id. */
38 key: K;
39 }) =>
40 Effect.gen(function* () {
41 const bind = yield* AWSBindings.Bind;
42 const op = yield* AWSBindings.withCredentials(options.operation);
43
44 return Effect.fn(function* (thing: ManagedThing) {
45 // Output yields a DEFERRED effect — resolve again per invocation below.
46 const ManagedThingId = yield* thing.managedThingId;
47 if (!globalThis.__ALCHEMY_RUNTIME__) {
48 yield* bind`AWS.IoTManagedIntegrations.${options.capability}(${thing})`({
49 policyStatements: [
50 {
51 Effect: "Allow",
52 Action: [...options.iamActions],
53 Resource: [thing.managedThingArn],
54 },
55 ],
56 });
57 }
58 return Effect.fn(`AWS.IoTManagedIntegrations.${options.capability}(${thing.LogicalId})`)(
59 function* (request?: Omit<I, K>) {
60 return yield* op({
61 ...request,
62 [options.key]: yield* ManagedThingId,
63 } as I);
64 },
65 );
66 });
67 });
68
69 /**
70 * Build the impl Effect for an account-level IoT Managed Integrations
71 * operation (device discovery, the schema catalog, the custom endpoint, and
72 * connector events — per the `iotmanagedintegrations` service authorization
73 * reference these authorize account-wide, so the grant is on
74 * `Resource: ["*"]`).
75 */
76 export const makeManagedIntegrationsHttpBinding = <I extends object, A, E, R>(options: {
77 /**
78 * Short capability name used in the binding sid and runtime span, e.g.
79 * `"StartDeviceDiscovery"`.
80 */
81 capability: string;
82 /** IAM actions granted on `Resource: ["*"]`. */
83 iamActions: readonly string[];
84 /** The distilled operation implementing the capability. */
85 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
86 }) =>
87 Effect.gen(function* () {
88 const bind = yield* AWSBindings.Bind;
89 const op = yield* AWSBindings.withCredentials(options.operation);
90
91 return Effect.fn(function* () {
92 if (!globalThis.__ALCHEMY_RUNTIME__) {
93 yield* bind`AWS.IoTManagedIntegrations.${options.capability}()`({
94 policyStatements: [
95 {
96 Effect: "Allow",
97 Action: [...options.iamActions],
98 Resource: ["*"],
99 },
100 ],
101 });
102 }
103 return Effect.fn(`AWS.IoTManagedIntegrations.${options.capability}`)(function* (request?: I) {
104 return yield* op((request ?? {}) as I);
105 });
106 });
107 });