1import * as Lambda from "@/AWS/Lambda";
2import * as Duration from "effect/Duration";
3import * as Effect from "effect/Effect";
4import { HttpServerRequest } from "effect/http/HttpServerRequest";
5import * as HttpServerResponse from "effect/http/HttpServerResponse";
6import path from "pathe";
7
8const main = path.resolve(import.meta.dirname, "http-handler.ts");
9
10export class HttpApiTestFunction extends Lambda.Function<Lambda.Function>()(
11 "HttpApiTestFunction",
12) {}
13
14/**
15 * Fixture Lambda for the HTTP API e2e test. Payload format 2.0 events flow
16 * through `makeFunctionHttpHandler`'s Function-URL path unchanged, so a
17 * plain Effect `fetch` handler serves the API. One route per behavior:
18 *
19 * - `GET /echo?..` → method, path, and query string round-trip
20 * - `GET /items/{id}` → path parameter routing
21 * - `POST /items` → JSON body round-trip
22 * - anything else → 404 with the observed method/path
23 */
24export default HttpApiTestFunction.make(
25 {
26 main,
27 timeout: Duration.seconds(30),
28 },
29 Effect.gen(function* () {
30 return {
31 fetch: Effect.gen(function* () {
32 const request = yield* HttpServerRequest;
33 const url = new URL(request.originalUrl);
34 const pathname = url.pathname;
35
36 if (request.method === "GET" && pathname === "/echo") {
37 return yield* HttpServerResponse.json({
38 method: request.method,
39 path: pathname,
40 query: Object.fromEntries(url.searchParams.entries()),
41 });
42 }
43
44 const itemMatch = pathname.match(/^\/items\/([^/]+)$/);
45 if (request.method === "GET" && itemMatch) {
46 return yield* HttpServerResponse.json({ id: itemMatch[1] });
47 }
48
49 if (request.method === "POST" && pathname === "/items") {
50 const body = (yield* request.json) as unknown;
51 return yield* HttpServerResponse.json(
52 { received: body },
53 { status: 201 },
54 );
55 }
56
57 return yield* HttpServerResponse.json(
58 { error: "Not found", method: request.method, path: pathname },
59 { status: 404 },
60 );
61 }).pipe(Effect.orDie),
62 };
63 }),
64);