@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 62 lines 2.44 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 ApiGatewayV2 capabilities
8 * (`ExportApiHttp`, `ResetAuthorizersCacheHttp`, …).
9 *
10 * NOT exported from `index.ts` — this is internal plumbing, like
11 * Cloudflare's `BucketHttp.ts`.
12 *
13 * API Gateway's control plane (v2 included) authorizes by HTTP verb
14 * (`apigateway:GET`, `apigateway:DELETE`, …) against path-shaped ARNs
15 * (`arn:aws:apigateway:{region}::/apis/{apiId}/exports/{specification}`),
16 * so every capability reduces to { verb, ARN paths, operation }.
17 * Everything else — the runtime guard, host resolution, region lookup,
18 * and the `host.bind` policy registration — is identical and lives here.
19 *
20 * The `@connections` data plane (`execute-api:ManageConnections`) is the
21 * one genuinely different capability and stays bespoke in
22 * `ManageConnectionsHttp.ts`.
23 */
24
25 /** API Gateway authorizes control-plane calls by HTTP verb. */
26 export type ApiGatewayV2IamVerb = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
27
28 /**
29 * Deploy-time half of an ApiGatewayV2 HTTP binding: resolves the host
30 * Function, and registers an IAM policy statement for
31 * `apigateway:{verb}` over the given API Gateway ARN paths. A no-op at
32 * runtime (`__ALCHEMY_RUNTIME__`) and for non-Lambda hosts.
33 *
34 * @param cap fully-qualified capability name, e.g. `AWS.ApiGatewayV2.ExportApi`
35 * @param target the bound resource — used for the binding's identity label
36 * @param verb the API Gateway IAM verb the capability requires
37 * @param paths builds the ARN path list once the deploy region is known;
38 * entries may be `Output`-derived (e.g. `Output.interpolate`)
39 */
40 export const RegisterApiGatewayV2Binding = Effect.gen(function* () {
41 const bind = yield* AWSBindings.Bind;
42 return Effect.fn(function* (opts: {
43 cap: string;
44 target: unknown;
45 verb: ApiGatewayV2IamVerb;
46 paths: (region: string) => ReadonlyArray<Input<string>>;
47 }) {
48 if (globalThis.__ALCHEMY_RUNTIME__) return;
49 const { region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
50 region: string;
51 }>;
52 yield* bind`${opts.cap}(${opts.target})`({
53 policyStatements: [
54 {
55 Effect: "Allow",
56 Action: [`apigateway:${opts.verb}`],
57 Resource: [...opts.paths(region)],
58 },
59 ],
60 });
61 });
62 });