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

main
ts 146 lines 5.73 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { WirelessDevice } from "./WirelessDevice.ts";
4 import type { WirelessGateway } from "./WirelessGateway.ts";
5
6 /**
7 * Shared HTTP scaffolding for the AWS IoT Wireless runtime 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 builders
11 * below. Everything except the operation, the IAM action, and the request
12 * shaping is boilerplate.
13 */
14
15 /**
16 * Build the impl Effect for an IoT Wireless operation scoped to one
17 * {@link WirelessDevice}: the deploy-time half grants `iamActions` on the
18 * bound device's ARN, and the runtime half injects the device's
19 * server-assigned id into every request via `prepare`.
20 */
21 export const makeIotWirelessDeviceHttpBinding = <I, A, E, R, Req>(options: {
22 /**
23 * Short capability name used in the binding sid and runtime span, e.g.
24 * `"SendDataToWirelessDevice"`.
25 */
26 capability: string;
27 /** IAM actions granted on the bound device's ARN. */
28 iamActions: readonly string[];
29 /** The distilled operation implementing the capability. */
30 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
31 /** Map the public request shape + bound device id onto the wire request. */
32 prepare: (request: Req, wirelessDeviceId: string) => I;
33 /**
34 * IAM resource scope. Defaults to the bound device's ARN. The position
35 * operations (`GetResourcePosition` / `UpdateResourcePosition`) must use
36 * `"any"`: IoT Wireless authorizes them against a type-level ARN
37 * (`…:WirelessDevice/WirelessDevice`, built from the `resourceType`
38 * query parameter), which the device ARN never matches.
39 */
40 resourceScope?: "device" | "any";
41 }) =>
42 Effect.gen(function* () {
43 const bind = yield* AWSBindings.Bind;
44 const op = yield* AWSBindings.withCredentials(options.operation);
45
46 return Effect.fn(function* (device: WirelessDevice) {
47 // Output yields a DEFERRED effect — resolve again per invocation below.
48 const DeviceId = yield* device.wirelessDeviceId;
49 if (!globalThis.__ALCHEMY_RUNTIME__) {
50 yield* bind`AWS.IoTWireless.${options.capability}(${device})`({
51 policyStatements: [
52 {
53 Effect: "Allow",
54 Action: [...options.iamActions],
55 Resource: options.resourceScope === "any" ? ["*"] : [device.wirelessDeviceArn],
56 },
57 ],
58 });
59 }
60 return Effect.fn(`AWS.IoTWireless.${options.capability}(${device.LogicalId})`)(function* (
61 request: Req,
62 ) {
63 const wirelessDeviceId = yield* DeviceId;
64 return yield* op(options.prepare(request, wirelessDeviceId));
65 });
66 });
67 });
68
69 /**
70 * Build the impl Effect for an IoT Wireless operation scoped to one
71 * {@link WirelessGateway}: the deploy-time half grants `iamActions` on the
72 * bound gateway's ARN, and the runtime half injects the gateway's
73 * server-assigned id into every request via `prepare`.
74 */
75 export const makeIotWirelessGatewayHttpBinding = <I, A, E, R, Req>(options: {
76 /** Short capability name used in the binding sid and runtime span. */
77 capability: string;
78 /** IAM actions granted on the bound gateway's ARN. */
79 iamActions: readonly string[];
80 /** The distilled operation implementing the capability. */
81 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
82 /** Map the public request shape + bound gateway id onto the wire request. */
83 prepare: (request: Req, wirelessGatewayId: string) => I;
84 }) =>
85 Effect.gen(function* () {
86 const bind = yield* AWSBindings.Bind;
87 const op = yield* AWSBindings.withCredentials(options.operation);
88
89 return Effect.fn(function* (gateway: WirelessGateway) {
90 const GatewayId = yield* gateway.wirelessGatewayId;
91 if (!globalThis.__ALCHEMY_RUNTIME__) {
92 yield* bind`AWS.IoTWireless.${options.capability}(${gateway})`({
93 policyStatements: [
94 {
95 Effect: "Allow",
96 Action: [...options.iamActions],
97 Resource: [gateway.wirelessGatewayArn],
98 },
99 ],
100 });
101 }
102 return Effect.fn(`AWS.IoTWireless.${options.capability}(${gateway.LogicalId})`)(function* (
103 request: Req,
104 ) {
105 const wirelessGatewayId = yield* GatewayId;
106 return yield* op(options.prepare(request, wirelessGatewayId));
107 });
108 });
109 });
110
111 /**
112 * Build the impl Effect for an account-level IoT Wireless operation (e.g.
113 * `GetServiceEndpoint` or `GetPositionEstimate`, which are not scoped to a
114 * single resource). Grants `iamActions` on `Resource: ["*"]`.
115 */
116 export const makeIotWirelessAccountHttpBinding = <I, A, E, R, Req>(options: {
117 /** Short capability name used in the binding sid and runtime span. */
118 capability: string;
119 /** IAM actions granted on `Resource: ["*"]`. */
120 iamActions: readonly string[];
121 /** The distilled operation implementing the capability. */
122 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
123 /** Map the public request shape onto the wire request. */
124 prepare: (request: Req) => I;
125 }) =>
126 Effect.gen(function* () {
127 const bind = yield* AWSBindings.Bind;
128 const op = yield* AWSBindings.withCredentials(options.operation);
129
130 return Effect.fn(function* () {
131 if (!globalThis.__ALCHEMY_RUNTIME__) {
132 yield* bind`AWS.IoTWireless.${options.capability}()`({
133 policyStatements: [
134 {
135 Effect: "Allow",
136 Action: [...options.iamActions],
137 Resource: ["*"],
138 },
139 ],
140 });
141 }
142 return Effect.fn(`AWS.IoTWireless.${options.capability}`)(function* (request: Req) {
143 return yield* op(options.prepare(request));
144 });
145 });
146 });