The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as AWSBindings from "../AWSBindings.ts"; |
| 3 | import type { Asset } from "./Asset.ts"; |
| 4 | |
| 5 | /** |
| 6 | * Shared HTTP scaffolding for the AWS IoT SiteWise runtime bindings. |
| 7 | * |
| 8 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 9 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders |
| 10 | * below. Everything except the operation, the IAM action, and the request |
| 11 | * shaping is boilerplate. |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Build the impl Effect for an IoT SiteWise operation scoped to one |
| 16 | * {@link Asset}: the deploy-time half grants `iamActions` on the bound |
| 17 | * asset's ARN, and the runtime half injects the asset's service-assigned id |
| 18 | * into every request via `prepare`. |
| 19 | * |
| 20 | * Note: requests that identify the target data stream by `propertyAlias` |
| 21 | * instead of `assetId`/`propertyId` authorize against the time-series |
| 22 | * resource, which this asset-scoped grant does not cover. |
| 23 | */ |
| 24 | export const makeSiteWiseAssetHttpBinding = <I, A, E, R, Req>(options: { |
| 25 | /** |
| 26 | * Short capability name used in the binding sid and runtime span, e.g. |
| 27 | * `"GetAssetPropertyValue"`. |
| 28 | */ |
| 29 | capability: string; |
| 30 | /** IAM actions granted on the bound asset's ARN. */ |
| 31 | iamActions: readonly string[]; |
| 32 | /** The distilled operation implementing the capability. */ |
| 33 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 34 | /** Map the public request shape + bound asset id onto the wire request. */ |
| 35 | prepare: (request: Req, assetId: string) => I; |
| 36 | }) => |
| 37 | Effect.gen(function* () { |
| 38 | const bind = yield* AWSBindings.Bind; |
| 39 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 40 | |
| 41 | return Effect.fn(function* (asset: Asset) { |
| 42 | // Output yields a DEFERRED effect — resolve again per invocation below. |
| 43 | const AssetId = yield* asset.assetId; |
| 44 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 45 | yield* bind`AWS.IoTSiteWise.${options.capability}(${asset})`({ |
| 46 | policyStatements: [ |
| 47 | { |
| 48 | Effect: "Allow", |
| 49 | Action: [...options.iamActions], |
| 50 | Resource: [asset.assetArn], |
| 51 | }, |
| 52 | ], |
| 53 | }); |
| 54 | } |
| 55 | return Effect.fn(`AWS.IoTSiteWise.${options.capability}(${asset.LogicalId})`)(function* ( |
| 56 | request: Req, |
| 57 | ) { |
| 58 | const assetId = yield* AssetId; |
| 59 | return yield* op(options.prepare(request, assetId)); |
| 60 | }); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | /** |
| 65 | * Build the impl Effect for an account-level IoT SiteWise operation (e.g. |
| 66 | * `ExecuteQuery`, which queries across all asset models and assets and is |
| 67 | * not scoped to a single resource). Grants `iamActions` on |
| 68 | * `Resource: ["*"]`. |
| 69 | */ |
| 70 | export const makeSiteWiseAccountHttpBinding = <I, A, E, R>(options: { |
| 71 | /** Short capability name used in the binding sid and runtime span. */ |
| 72 | capability: string; |
| 73 | /** IAM actions granted on `Resource: ["*"]`. */ |
| 74 | iamActions: readonly string[]; |
| 75 | /** The distilled operation implementing the capability. */ |
| 76 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 77 | }) => |
| 78 | Effect.gen(function* () { |
| 79 | const bind = yield* AWSBindings.Bind; |
| 80 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 81 | |
| 82 | return Effect.fn(function* () { |
| 83 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 84 | yield* bind`AWS.IoTSiteWise.${options.capability}()`({ |
| 85 | policyStatements: [ |
| 86 | { |
| 87 | Effect: "Allow", |
| 88 | Action: [...options.iamActions], |
| 89 | Resource: ["*"], |
| 90 | }, |
| 91 | ], |
| 92 | }); |
| 93 | } |
| 94 | return Effect.fn(`AWS.IoTSiteWise.${options.capability}`)(function* (request: I) { |
| 95 | return yield* op(request); |
| 96 | }); |
| 97 | }); |
| 98 | }); |