@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 59 lines 2.3 KB
Raw
1 import type { Input } from "alchemy/Input";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import { AWSEnvironment } from "../Environment.ts";
5
6 /**
7 * Shared HTTP-binding scaffolding for the ApiGateway capabilities
8 * (`CreateApiKeyHttp`, `GetUsageHttp`, `FlushStageCacheHttp`, …).
9 *
10 * NOT exported from `index.ts` — this is internal plumbing, like
11 * Cloudflare's `BucketHttp.ts`.
12 *
13 * API Gateway's control plane authorizes by HTTP verb (`apigateway:GET`,
14 * `apigateway:POST`, …) against path-shaped ARNs
15 * (`arn:aws:apigateway:{region}::/usageplans/{id}/keys`), so every
16 * capability reduces to { verb, ARN paths, operation }. Everything else —
17 * the runtime guard, host resolution, region lookup, and the
18 * `host.bind` policy registration — is identical and lives here.
19 */
20
21 /** API Gateway authorizes control-plane calls by HTTP verb. */
22 export type ApiGatewayIamVerb = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
23
24 /**
25 * Deploy-time half of an ApiGateway HTTP binding: resolves the host
26 * Function, and registers an IAM policy statement for
27 * `apigateway:{verb}` over the given API Gateway ARN paths. A no-op at
28 * runtime (`__ALCHEMY_RUNTIME__`) and for non-Lambda hosts.
29 *
30 * @param cap fully-qualified capability name, e.g. `AWS.ApiGateway.GetUsage`
31 * @param target the bound resource (or a string literal for account-scoped
32 * capabilities) — used for the binding's identity label
33 * @param verb the API Gateway IAM verb the capability requires
34 * @param paths builds the ARN path list once the deploy region is known;
35 * entries may be `Output`-derived (e.g. `Output.interpolate`)
36 */
37 export const RegisterApiGatewayBinding = Effect.gen(function* () {
38 const bind = yield* AWSBindings.Bind;
39 return Effect.fn(function* (opts: {
40 cap: string;
41 target: unknown;
42 verb: ApiGatewayIamVerb;
43 paths: (region: string) => ReadonlyArray<Input<string>>;
44 }) {
45 if (globalThis.__ALCHEMY_RUNTIME__) return;
46 const { region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
47 region: string;
48 }>;
49 yield* bind`${opts.cap}(${opts.target})`({
50 policyStatements: [
51 {
52 Effect: "Allow",
53 Action: [`apigateway:${opts.verb}`],
54 Resource: [...opts.paths(region)],
55 },
56 ],
57 });
58 });
59 });