t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/AWS/ApiGateway/fixtures/rest-api-event-source-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

77 lines
2.6 KiB
TypeScript

import * as ApiGateway from "@/AWS/ApiGateway";
import * as Lambda from "@/AWS/Lambda";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
export class RestApiEventSourceFunction extends Lambda.Function<RestApiEventSourceFunction>()(
"RestApiEventSourceFunction",
) {}
/**
* Fixture: a Lambda that owns a REST API and serves two routes through the
* `RestApiEventSource` (`onRestApiRoute`) binding. The event source
* materializes the `Resource` chain, `AWS_PROXY` `Method`s, and the invoke
* `Permission`; the `Deployment`/`Stage` below are ordered after them via
* the API's binding contract.
*
* Deliberately does NOT `yield*` any `api.*`/`stage.*` outputs: the API's
* full `create` settles only after every bound child (including the
* `Method`s that reference `host.functionArn`), so awaiting those outputs
* inside the host's own effect would deadlock the plan. The test discovers
* the REST API id out-of-band by its stack, stage, and logical-id tags.
*/
export default RestApiEventSourceFunction.make(
{
main: import.meta.url,
functionUrl: true,
},
Effect.gen(function* () {
const api = yield* ApiGateway.RestApi("AgEsApi", {
endpointConfiguration: { types: ["REGIONAL"] },
});
// GET /items — static JSON response.
yield* ApiGateway.onRestApiRoute(
api,
{ path: "/items", httpMethod: "GET" },
() =>
Effect.succeed({
statusCode: 200,
headers: { "content-type": "application/json" },
body: JSON.stringify({ items: ["alpha", "beta"] }),
}),
);
// POST /echo — proves the proxy event (method, resource, body) reaches
// the handler intact.
yield* ApiGateway.onRestApiRoute(
api,
{ path: "/echo", httpMethod: "POST" },
(event) =>
Effect.sync(() => ({
statusCode: 200,
headers: { "content-type": "application/json" },
body: JSON.stringify({
method: event.httpMethod,
resource: event.resource,
echoed: event.body === null ? null : JSON.parse(event.body),
}),
})),
);
const deployment = yield* ApiGateway.Deployment("AgEsDep", {
restApi: api,
});
yield* ApiGateway.Stage("AgEsStage", {
restApi: api,
stageName: "test",
deploymentId: deployment.deploymentId,
});
return {
// Readiness probe only — the REST routes are served via the event
// source, not the function URL.
fetch: Effect.succeed(HttpServerResponse.text("ok")),
};
}).pipe(Effect.provide(Lambda.RestApiEventSource)),
);