@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 103 lines 3.64 KB
Raw
1 import * as ApiGatewayV2 from "@alchemy.run/aws/ApiGatewayV2";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import * as Stream from "effect/Stream";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
11
12 export class ApiGatewayV2BindingsFunction extends Lambda.Function<Lambda.Function>()(
13 "ApiGatewayV2BindingsFunction",
14 ) {}
15
16 /**
17 * Fixture Lambda for the ApiGatewayV2 runtime bindings. It owns a minimal
18 * HTTP API (one `HTTP_PROXY` route + an auto-deployed `$default` stage) so
19 * the control-plane bindings have a live target, and exposes one route per
20 * binding:
21 *
22 * - `GET /export` → `ExportApi` (OpenAPI 3.0 export)
23 * - `POST /reset-authorizers` → `ResetAuthorizersCache`
24 */
25 export default ApiGatewayV2BindingsFunction.make(
26 {
27 main,
28 functionUrl: true,
29 },
30 Effect.gen(function* () {
31 // Minimal HTTP API + route + stage the bindings target.
32 const api = yield* ApiGatewayV2.Api("AgV2BindingsApi", {
33 protocolType: "HTTP",
34 });
35 const integration = yield* ApiGatewayV2.Integration("AgV2BindingsUpstream", {
36 api,
37 integrationType: "HTTP_PROXY",
38 integrationUri: "https://example.com",
39 integrationMethod: "ANY",
40 payloadFormatVersion: "1.0",
41 });
42 yield* ApiGatewayV2.Route("AgV2BindingsPing", {
43 api,
44 routeKey: "GET /ping",
45 integration,
46 });
47 const stage = yield* ApiGatewayV2.Stage("AgV2BindingsStage", {
48 api,
49 autoDeploy: true,
50 });
51
52 const exportApi = yield* ApiGatewayV2.ExportApi(api);
53 const resetAuthorizersCache = yield* ApiGatewayV2.ResetAuthorizersCache(stage);
54
55 return {
56 fetch: Effect.gen(function* () {
57 const request = yield* HttpServerRequest;
58 const url = new URL(request.originalUrl);
59 const pathname = url.pathname;
60
61 if (request.method === "GET" && pathname === "/export") {
62 const exported = yield* exportApi({ OutputType: "JSON" }).pipe(Effect.orDie);
63 const document = exported.body
64 ? yield* Stream.mkString(Stream.decodeText(exported.body)).pipe(Effect.orDie)
65 : "";
66 const spec = yield* Effect.try(
67 () =>
68 JSON.parse(document) as {
69 openapi?: string;
70 paths?: Record<string, unknown>;
71 },
72 ).pipe(Effect.orDie);
73 return yield* HttpServerResponse.json({
74 openapi: spec.openapi,
75 paths: Object.keys(spec.paths ?? {}),
76 });
77 }
78
79 if (request.method === "POST" && pathname === "/reset-authorizers") {
80 // The stage has no Lambda-authorizer cache, so API Gateway may
81 // answer with a typed NotFound — the IAM grant is still
82 // exercised (a missing grant surfaces as an auth error instead,
83 // failing the route with a 500).
84 const flushed = yield* resetAuthorizersCache().pipe(
85 Effect.map(() => true),
86 Effect.catchTag("NotFoundException", () => Effect.succeed(false)),
87 Effect.orDie,
88 );
89 return yield* HttpServerResponse.json({ flushed });
90 }
91
92 return yield* HttpServerResponse.json(
93 { error: "Not found", method: request.method, path: pathname },
94 { status: 404 },
95 );
96 }).pipe(Effect.orDie),
97 };
98 }).pipe(
99 Effect.provide(
100 Layer.mergeAll(ApiGatewayV2.ExportApiHttp, ApiGatewayV2.ResetAuthorizersCacheHttp),
101 ),
102 ),
103 );