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

main
ts 54 lines 2.19 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import { withPricingRegion } from "./internal.ts";
4
5 /**
6 * Shared HTTP scaffolding for the AWS Price List runtime bindings.
7 *
8 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
9 * thin `Layer.effect(Cap, makePricingHttpBinding({ … }))` over the builder
10 * below. Everything except the operation and the IAM action is boilerplate.
11 *
12 * The Price List Query API is a global, data-only service served from
13 * `us-east-1` (and `ap-south-1`) only, so every operation is resolved with
14 * the Region pinned via {@link withPricingRegion}. None of the `pricing:*`
15 * actions support resource-level IAM, so every grant is on
16 * `Resource: ["*"]`.
17 */
18 export const makePricingHttpBinding = <I extends object, A, E, R>(options: {
19 /**
20 * Short capability name used in the binding sid and runtime span, e.g.
21 * `"GetProducts"`.
22 */
23 capability: string;
24 /** IAM actions granted on `Resource: ["*"]` (no resource-level IAM). */
25 iamActions: readonly string[];
26 /** The distilled operation implementing the capability. */
27 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
28 }) =>
29 Effect.gen(function* () {
30 const bind = yield* AWSBindings.Bind;
31 // Capture the client with the region pinned to us-east-1 — the Price
32 // List Query API is not served from most regions.
33 const op = yield* options.operation.pipe(withPricingRegion, AWSBindings.withCredentials);
34
35 return Effect.fn(function* () {
36 if (!globalThis.__ALCHEMY_RUNTIME__) {
37 yield* bind`AWS.Pricing.${options.capability}()`({
38 policyStatements: [
39 {
40 Effect: "Allow",
41 Action: [...options.iamActions],
42 Resource: ["*"],
43 },
44 ],
45 });
46 }
47 return Effect.fn(`AWS.Pricing.${options.capability}`)(function* (request?: I) {
48 // The region must also be pinned at the call site: the yield-time
49 // snapshot is only a fallback — the calling fiber's ambient Region
50 // (the host Function's own region) wins over it.
51 return yield* withPricingRegion(op((request ?? {}) as I));
52 });
53 });
54 });