t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/AWS/ApiGatewayV2/bindings-handler.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

114 lines
3.7 KiB
TypeScript

import * as ApiGatewayV2 from "@/AWS/ApiGatewayV2";
import * as Lambda from "@/AWS/Lambda";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Stream from "effect/Stream";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import path from "pathe";
const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
export class ApiGatewayV2BindingsFunction extends Lambda.Function<Lambda.Function>()(
"ApiGatewayV2BindingsFunction",
) {}
/**
* Fixture Lambda for the ApiGatewayV2 runtime bindings. It owns a minimal
* HTTP API (one `HTTP_PROXY` route + an auto-deployed `$default` stage) so
* the control-plane bindings have a live target, and exposes one route per
* binding:
*
* - `GET /export` → `ExportApi` (OpenAPI 3.0 export)
* - `POST /reset-authorizers` → `ResetAuthorizersCache`
*/
export default ApiGatewayV2BindingsFunction.make(
{
main,
functionUrl: true,
},
Effect.gen(function* () {
// Minimal HTTP API + route + stage the bindings target.
const api = yield* ApiGatewayV2.Api("AgV2BindingsApi", {
protocolType: "HTTP",
});
const integration = yield* ApiGatewayV2.Integration(
"AgV2BindingsUpstream",
{
api,
integrationType: "HTTP_PROXY",
integrationUri: "https://example.com",
integrationMethod: "ANY",
payloadFormatVersion: "1.0",
},
);
yield* ApiGatewayV2.Route("AgV2BindingsPing", {
api,
routeKey: "GET /ping",
integration,
});
const stage = yield* ApiGatewayV2.Stage("AgV2BindingsStage", {
api,
autoDeploy: true,
});
const exportApi = yield* ApiGatewayV2.ExportApi(api);
const resetAuthorizersCache =
yield* ApiGatewayV2.ResetAuthorizersCache(stage);
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const url = new URL(request.originalUrl);
const pathname = url.pathname;
if (request.method === "GET" && pathname === "/export") {
const exported = yield* exportApi({ OutputType: "JSON" }).pipe(
Effect.orDie,
);
const document = exported.body
? yield* Stream.mkString(Stream.decodeText(exported.body)).pipe(
Effect.orDie,
)
: "";
const spec = yield* Effect.try(
() =>
JSON.parse(document) as {
openapi?: string;
paths?: Record<string, unknown>;
},
).pipe(Effect.orDie);
return yield* HttpServerResponse.json({
openapi: spec.openapi,
paths: Object.keys(spec.paths ?? {}),
});
}
if (request.method === "POST" && pathname === "/reset-authorizers") {
// The stage has no Lambda-authorizer cache, so API Gateway may
// answer with a typed NotFound — the IAM grant is still
// exercised (a missing grant surfaces as an auth error instead,
// failing the route with a 500).
const flushed = yield* resetAuthorizersCache().pipe(
Effect.map(() => true),
Effect.catchTag("NotFoundException", () => Effect.succeed(false)),
Effect.orDie,
);
return yield* HttpServerResponse.json({ flushed });
}
return yield* HttpServerResponse.json(
{ error: "Not found", method: request.method, path: pathname },
{ status: 404 },
);
}).pipe(Effect.orDie),
};
}).pipe(
Effect.provide(
Layer.mergeAll(
ApiGatewayV2.ExportApiHttp,
ApiGatewayV2.ResetAuthorizersCacheHttp,
),
),
),
);