@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 61 lines 2.1 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Duration from "effect/Duration";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import path from "pathe";
7
8 const main = path.resolve(import.meta.dirname, "http-handler.ts");
9
10 export 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 */
24 export 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({ received: body }, { status: 201 });
52 }
53
54 return yield* HttpServerResponse.json(
55 { error: "Not found", method: request.method, path: pathname },
56 { status: 404 },
57 );
58 }).pipe(Effect.orDie),
59 };
60 }),
61 );