The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import type { Output as OutputType } from "alchemy/Output"; |
| 2 | import type { ResourceLike } from "alchemy/Resource"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as AWSBindings from "../AWSBindings.ts"; |
| 5 | import { inFleetWiseRegion } from "./internal.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for AWS IoT FleetWise HTTP bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 11 | * a `Layer.effect(Cap, makeFleetWise…HttpBinding({ … }))` over one of the two |
| 12 | * builders below. Everything except the operation, the injected identifier, |
| 13 | * and the IAM action list + granted ARNs is boilerplate. Every runtime call |
| 14 | * is piped through {@link inFleetWiseRegion} because AWS IoT FleetWise is |
| 15 | * offered only in `us-east-1`/`eu-central-1` — a Lambda running anywhere else |
| 16 | * transparently calls the service's home region. |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * Build the impl Effect for a FleetWise operation scoped to one resource |
| 21 | * (vehicle, fleet, campaign, or one of the manifests/catalogs): the |
| 22 | * deploy-time half grants `actions` on `resources`, and the runtime callable |
| 23 | * injects the bound resource's identifier as the `requestKey` field of every |
| 24 | * request. |
| 25 | */ |
| 26 | export const makeFleetWiseResourceHttpBinding = < |
| 27 | Res extends ResourceLike, |
| 28 | K extends string, |
| 29 | I extends { [P in K]: string }, |
| 30 | A, |
| 31 | E, |
| 32 | R, |
| 33 | >(options: { |
| 34 | /** Fully-qualified binding tag, e.g. `AWS.IoTFleetWise.GetVehicleStatus`. */ |
| 35 | tag: string; |
| 36 | /** The distilled operation; the identifier is injected. */ |
| 37 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 38 | /** IAM actions granted on `resources`. */ |
| 39 | actions: readonly string[]; |
| 40 | /** The request field the bound resource's identifier is injected as. */ |
| 41 | requestKey: K; |
| 42 | /** |
| 43 | * Resolve the injected identifier from the bound resource. `Req` is pinned |
| 44 | * to `never` (resource attributes are `Output<string, never>`): the default |
| 45 | * `Output<string>` widens `Req` to `any`, which leaks into the per-resource |
| 46 | * Effect's requirements and breaks the binding contract's `never`. |
| 47 | */ |
| 48 | identifier: (resource: Res) => OutputType<string, never>; |
| 49 | /** ARNs the actions are granted on. */ |
| 50 | resources: (resource: Res) => (OutputType<string> | string)[]; |
| 51 | }) => |
| 52 | Effect.gen(function* () { |
| 53 | const bind = yield* AWSBindings.Bind; |
| 54 | // Capture the client in the FleetWise home region: yielding an |
| 55 | // operation captures its services (region, credentials, HTTP client) |
| 56 | // once, so the pin must wrap the capture, not the later calls. |
| 57 | const op = yield* options.operation.pipe(inFleetWiseRegion, AWSBindings.withCredentials); |
| 58 | |
| 59 | return Effect.fn(function* (resource: Res) { |
| 60 | const Identifier = yield* options.identifier(resource); |
| 61 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 62 | yield* bind`${options.tag}(${resource})`({ |
| 63 | policyStatements: [ |
| 64 | { |
| 65 | Effect: "Allow", |
| 66 | Action: [...options.actions], |
| 67 | Resource: options.resources(resource), |
| 68 | }, |
| 69 | ], |
| 70 | }); |
| 71 | } |
| 72 | return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* (request?: Omit<I, K>) { |
| 73 | return yield* op({ |
| 74 | ...request, |
| 75 | [options.requestKey]: yield* Identifier, |
| 76 | } as unknown as I); |
| 77 | }); |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | /** |
| 82 | * Build the impl Effect for an account-level FleetWise operation (no bound |
| 83 | * resource): the deploy-time half grants `actions` on `resources` |
| 84 | * (default `*` — batch vehicle operations authorize on vehicle ARNs that |
| 85 | * only exist at runtime), and the runtime callable passes the caller's |
| 86 | * request through unchanged. |
| 87 | */ |
| 88 | export const makeFleetWiseAccountHttpBinding = <I, A, E, R>(options: { |
| 89 | /** Fully-qualified binding tag, e.g. `AWS.IoTFleetWise.ListVehicles`. */ |
| 90 | tag: string; |
| 91 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 92 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 93 | /** IAM actions granted on `resources`. */ |
| 94 | actions: readonly string[]; |
| 95 | /** |
| 96 | * ARN patterns the actions are granted on. |
| 97 | * @default ["*"] |
| 98 | */ |
| 99 | resources?: readonly string[]; |
| 100 | }) => |
| 101 | Effect.gen(function* () { |
| 102 | const bind = yield* AWSBindings.Bind; |
| 103 | // See makeFleetWiseResourceHttpBinding: the region pin wraps the |
| 104 | // service capture, not the later calls. |
| 105 | const op = yield* options.operation.pipe(inFleetWiseRegion, AWSBindings.withCredentials); |
| 106 | |
| 107 | return Effect.fn(function* () { |
| 108 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 109 | yield* bind`${options.tag}()`({ |
| 110 | policyStatements: [ |
| 111 | { |
| 112 | Effect: "Allow", |
| 113 | Action: [...options.actions], |
| 114 | Resource: [...(options.resources ?? ["*"])], |
| 115 | }, |
| 116 | ], |
| 117 | }); |
| 118 | } |
| 119 | return Effect.fn(options.tag)(function* (request?: I) { |
| 120 | return yield* op((request ?? {}) as I); |
| 121 | }); |
| 122 | }); |
| 123 | }); |