The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { GeofenceCollection } from "./GeofenceCollection.ts"; |
| 5 | import type { Map as LocationMap } from "./Map.ts"; |
| 6 | import type { PlaceIndex } from "./PlaceIndex.ts"; |
| 7 | import type { RouteCalculator } from "./RouteCalculator.ts"; |
| 8 | import type { Tracker } from "./Tracker.ts"; |
| 9 | |
| 10 | /** |
| 11 | * Shared scaffolding for the Amazon Location Service runtime bindings. |
| 12 | * |
| 13 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 14 | * thin `Layer.effect(Cap, makeLocation…HttpBinding({ … }))` over one of the |
| 15 | * builders below. Everything except the operation and the IAM action list is |
| 16 | * boilerplate: each data-plane operation is scoped to exactly one Location |
| 17 | * resource (tracker, geofence collection, place index, route calculator, or |
| 18 | * map), whose physical name is injected into the request and whose ARN |
| 19 | * receives the grant. Batch metadata jobs are account-scoped. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Build the impl Effect for a tracker-scoped Location operation: the runtime |
| 24 | * callable injects the bound {@link Tracker}'s name as `TrackerName` and the |
| 25 | * deploy-time half grants `actions` on the tracker ARN. |
| 26 | */ |
| 27 | export const makeLocationTrackerHttpBinding = < |
| 28 | I extends { TrackerName: string }, |
| 29 | A, |
| 30 | E, |
| 31 | R, |
| 32 | >(options: { |
| 33 | /** Fully-qualified binding tag, e.g. `AWS.Location.GetDevicePosition`. */ |
| 34 | tag: string; |
| 35 | /** The distilled operation; `TrackerName` is injected from the tracker. */ |
| 36 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 37 | /** IAM actions granted on the tracker ARN. */ |
| 38 | actions: readonly string[]; |
| 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* (tracker: Tracker) { |
| 45 | const TrackerName = yield* tracker.trackerName; |
| 46 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 47 | yield* bind`${options.tag}(${tracker})`({ |
| 48 | policyStatements: [ |
| 49 | { |
| 50 | Effect: "Allow", |
| 51 | Action: [...options.actions], |
| 52 | Resource: [Output.interpolate`${tracker.trackerArn}`], |
| 53 | }, |
| 54 | ], |
| 55 | }); |
| 56 | } |
| 57 | return Effect.fn(`${options.tag}(${tracker.LogicalId})`)(function* ( |
| 58 | request?: Omit<I, "TrackerName">, |
| 59 | ) { |
| 60 | return yield* op({ |
| 61 | ...request, |
| 62 | TrackerName: yield* TrackerName, |
| 63 | } as I); |
| 64 | }); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | /** |
| 69 | * Build the impl Effect for a geofence-collection-scoped Location operation: |
| 70 | * the runtime callable injects the bound {@link GeofenceCollection}'s name as |
| 71 | * `CollectionName` and the deploy-time half grants `actions` on the |
| 72 | * collection ARN. |
| 73 | */ |
| 74 | export const makeLocationCollectionHttpBinding = < |
| 75 | I extends { CollectionName: string }, |
| 76 | A, |
| 77 | E, |
| 78 | R, |
| 79 | >(options: { |
| 80 | /** Fully-qualified binding tag, e.g. `AWS.Location.PutGeofence`. */ |
| 81 | tag: string; |
| 82 | /** The distilled operation; `CollectionName` is injected. */ |
| 83 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 84 | /** IAM actions granted on the geofence collection ARN. */ |
| 85 | actions: readonly string[]; |
| 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* (collection: GeofenceCollection) { |
| 92 | const CollectionName = yield* collection.collectionName; |
| 93 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 94 | yield* bind`${options.tag}(${collection})`({ |
| 95 | policyStatements: [ |
| 96 | { |
| 97 | Effect: "Allow", |
| 98 | Action: [...options.actions], |
| 99 | Resource: [Output.interpolate`${collection.collectionArn}`], |
| 100 | }, |
| 101 | ], |
| 102 | }); |
| 103 | } |
| 104 | return Effect.fn(`${options.tag}(${collection.LogicalId})`)(function* ( |
| 105 | request?: Omit<I, "CollectionName">, |
| 106 | ) { |
| 107 | return yield* op({ |
| 108 | ...request, |
| 109 | CollectionName: yield* CollectionName, |
| 110 | } as I); |
| 111 | }); |
| 112 | }); |
| 113 | }); |
| 114 | |
| 115 | /** |
| 116 | * Build the impl Effect for a place-index-scoped Location operation: the |
| 117 | * runtime callable injects the bound {@link PlaceIndex}'s name as `IndexName` |
| 118 | * and the deploy-time half grants `actions` on the index ARN. |
| 119 | */ |
| 120 | export const makeLocationPlaceIndexHttpBinding = < |
| 121 | I extends { IndexName: string }, |
| 122 | A, |
| 123 | E, |
| 124 | R, |
| 125 | >(options: { |
| 126 | /** Fully-qualified binding tag, e.g. `AWS.Location.SearchPlaceIndexForText`. */ |
| 127 | tag: string; |
| 128 | /** The distilled operation; `IndexName` is injected. */ |
| 129 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 130 | /** IAM actions granted on the place index ARN. */ |
| 131 | actions: readonly string[]; |
| 132 | }) => |
| 133 | Effect.gen(function* () { |
| 134 | const bind = yield* AWSBindings.Bind; |
| 135 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 136 | |
| 137 | return Effect.fn(function* (index: PlaceIndex) { |
| 138 | const IndexName = yield* index.indexName; |
| 139 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 140 | yield* bind`${options.tag}(${index})`({ |
| 141 | policyStatements: [ |
| 142 | { |
| 143 | Effect: "Allow", |
| 144 | Action: [...options.actions], |
| 145 | Resource: [Output.interpolate`${index.indexArn}`], |
| 146 | }, |
| 147 | ], |
| 148 | }); |
| 149 | } |
| 150 | return Effect.fn(`${options.tag}(${index.LogicalId})`)(function* ( |
| 151 | request?: Omit<I, "IndexName">, |
| 152 | ) { |
| 153 | return yield* op({ |
| 154 | ...request, |
| 155 | IndexName: yield* IndexName, |
| 156 | } as I); |
| 157 | }); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | /** |
| 162 | * Build the impl Effect for a route-calculator-scoped Location operation: the |
| 163 | * runtime callable injects the bound {@link RouteCalculator}'s name as |
| 164 | * `CalculatorName` and the deploy-time half grants `actions` on the |
| 165 | * calculator ARN. |
| 166 | */ |
| 167 | export const makeLocationCalculatorHttpBinding = < |
| 168 | I extends { CalculatorName: string }, |
| 169 | A, |
| 170 | E, |
| 171 | R, |
| 172 | >(options: { |
| 173 | /** Fully-qualified binding tag, e.g. `AWS.Location.CalculateRoute`. */ |
| 174 | tag: string; |
| 175 | /** The distilled operation; `CalculatorName` is injected. */ |
| 176 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 177 | /** IAM actions granted on the route calculator ARN. */ |
| 178 | actions: readonly string[]; |
| 179 | }) => |
| 180 | Effect.gen(function* () { |
| 181 | const bind = yield* AWSBindings.Bind; |
| 182 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 183 | |
| 184 | return Effect.fn(function* (calculator: RouteCalculator) { |
| 185 | const CalculatorName = yield* calculator.calculatorName; |
| 186 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 187 | yield* bind`${options.tag}(${calculator})`({ |
| 188 | policyStatements: [ |
| 189 | { |
| 190 | Effect: "Allow", |
| 191 | Action: [...options.actions], |
| 192 | Resource: [Output.interpolate`${calculator.calculatorArn}`], |
| 193 | }, |
| 194 | ], |
| 195 | }); |
| 196 | } |
| 197 | return Effect.fn(`${options.tag}(${calculator.LogicalId})`)(function* ( |
| 198 | request?: Omit<I, "CalculatorName">, |
| 199 | ) { |
| 200 | return yield* op({ |
| 201 | ...request, |
| 202 | CalculatorName: yield* CalculatorName, |
| 203 | } as I); |
| 204 | }); |
| 205 | }); |
| 206 | }); |
| 207 | |
| 208 | /** |
| 209 | * Build the impl Effect for a map-scoped Location operation: the runtime |
| 210 | * callable injects the bound {@link LocationMap}'s name as `MapName` and the |
| 211 | * deploy-time half grants `actions` on the map ARN. |
| 212 | */ |
| 213 | export const makeLocationMapHttpBinding = <I extends { MapName: string }, A, E, R>(options: { |
| 214 | /** Fully-qualified binding tag, e.g. `AWS.Location.GetMapTile`. */ |
| 215 | tag: string; |
| 216 | /** The distilled operation; `MapName` is injected. */ |
| 217 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 218 | /** IAM actions granted on the map ARN. */ |
| 219 | actions: readonly string[]; |
| 220 | }) => |
| 221 | Effect.gen(function* () { |
| 222 | const bind = yield* AWSBindings.Bind; |
| 223 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 224 | |
| 225 | return Effect.fn(function* (map: LocationMap) { |
| 226 | const MapName = yield* map.mapName; |
| 227 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 228 | yield* bind`${options.tag}(${map})`({ |
| 229 | policyStatements: [ |
| 230 | { |
| 231 | Effect: "Allow", |
| 232 | Action: [...options.actions], |
| 233 | Resource: [Output.interpolate`${map.mapArn}`], |
| 234 | }, |
| 235 | ], |
| 236 | }); |
| 237 | } |
| 238 | return Effect.fn(`${options.tag}(${map.LogicalId})`)(function* ( |
| 239 | request?: Omit<I, "MapName">, |
| 240 | ) { |
| 241 | return yield* op({ |
| 242 | ...request, |
| 243 | MapName: yield* MapName, |
| 244 | } as I); |
| 245 | }); |
| 246 | }); |
| 247 | }); |
| 248 | |
| 249 | /** |
| 250 | * Build the impl Effect for an account-scoped Location operation (the batch |
| 251 | * metadata jobs API: `ListJobs`, `GetJob`, `CancelJob`): jobs are created at |
| 252 | * runtime so their ARNs are unknowable at deploy time — the deploy-time half |
| 253 | * grants `actions` on `*`. |
| 254 | */ |
| 255 | export const makeLocationAccountHttpBinding = <I, A, E, R>(options: { |
| 256 | /** Fully-qualified binding tag, e.g. `AWS.Location.ListJobs`. */ |
| 257 | tag: string; |
| 258 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 259 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 260 | /** IAM actions granted on `*`. */ |
| 261 | actions: readonly string[]; |
| 262 | }) => |
| 263 | Effect.gen(function* () { |
| 264 | const bind = yield* AWSBindings.Bind; |
| 265 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 266 | |
| 267 | return Effect.fn(function* () { |
| 268 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 269 | yield* bind`${options.tag}()`({ |
| 270 | policyStatements: [ |
| 271 | { |
| 272 | Effect: "Allow", |
| 273 | Action: [...options.actions], |
| 274 | Resource: ["*"], |
| 275 | }, |
| 276 | ], |
| 277 | }); |
| 278 | } |
| 279 | return Effect.fn(options.tag)(function* (request?: I) { |
| 280 | return yield* op((request ?? {}) as I); |
| 281 | }); |
| 282 | }); |
| 283 | }); |