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 { Analyzer } from "./Analyzer.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for AccessAnalyzer HTTP 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 two |
| 11 | * builders below. Everything except the operation, the IAM action list, and |
| 12 | * (for analyzer-scoped operations) the injected `analyzerArn` is boilerplate. |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Build the impl Effect for an analyzer-scoped operation: the runtime |
| 17 | * callable injects the bound {@link Analyzer}'s ARN as `analyzerArn` and the |
| 18 | * deploy-time half grants `actions` on the analyzer ARN. |
| 19 | */ |
| 20 | export const makeAnalyzerScopedHttpBinding = <I extends { analyzerArn: string }, A, E, R>(options: { |
| 21 | /** Fully-qualified binding tag, e.g. `AWS.AccessAnalyzer.ListFindingsV2`. */ |
| 22 | tag: string; |
| 23 | /** The distilled operation; `analyzerArn` is injected from the analyzer. */ |
| 24 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 25 | /** IAM actions granted on the analyzer ARN. */ |
| 26 | actions: readonly string[]; |
| 27 | }) => |
| 28 | Effect.gen(function* () { |
| 29 | const bind = yield* AWSBindings.Bind; |
| 30 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 31 | |
| 32 | return Effect.fn(function* (analyzer: Analyzer) { |
| 33 | const AnalyzerArn = yield* analyzer.analyzerArn; |
| 34 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 35 | yield* bind`${options.tag}(${analyzer})`({ |
| 36 | policyStatements: [ |
| 37 | { |
| 38 | Effect: "Allow", |
| 39 | Action: [...options.actions], |
| 40 | Resource: [Output.interpolate`${analyzer.analyzerArn}`], |
| 41 | }, |
| 42 | ], |
| 43 | }); |
| 44 | } |
| 45 | return Effect.fn(`${options.tag}(${analyzer.LogicalId})`)(function* ( |
| 46 | request?: Omit<I, "analyzerArn">, |
| 47 | ) { |
| 48 | return yield* op({ |
| 49 | ...request, |
| 50 | analyzerArn: yield* AnalyzerArn, |
| 51 | } as I); |
| 52 | }); |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | /** |
| 57 | * Build the impl Effect for an account-level operation (no target analyzer — |
| 58 | * the policy-check, policy-validation, and policy-generation APIs). The |
| 59 | * deploy-time half grants `actions` on `*` because these IAM actions do not |
| 60 | * support resource-level scoping. |
| 61 | */ |
| 62 | export const makeAccessAnalyzerAccountHttpBinding = <I, A, E, R>(options: { |
| 63 | /** Fully-qualified binding tag, e.g. `AWS.AccessAnalyzer.ValidatePolicy`. */ |
| 64 | tag: string; |
| 65 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 66 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 67 | /** IAM actions granted on `*`. */ |
| 68 | actions: readonly string[]; |
| 69 | }) => |
| 70 | Effect.gen(function* () { |
| 71 | const bind = yield* AWSBindings.Bind; |
| 72 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 73 | |
| 74 | return Effect.fn(function* () { |
| 75 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 76 | yield* bind`${options.tag}()`({ |
| 77 | policyStatements: [ |
| 78 | { |
| 79 | Effect: "Allow", |
| 80 | Action: [...options.actions], |
| 81 | Resource: ["*"], |
| 82 | }, |
| 83 | ], |
| 84 | }); |
| 85 | } |
| 86 | return Effect.fn(options.tag)(function* (request?: I) { |
| 87 | return yield* op((request ?? {}) as I); |
| 88 | }); |
| 89 | }); |
| 90 | }); |