| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Option from "effect/Option"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | |
| 6 | export const TestHttpEffect = Effect.gen(function* () { |
| 7 | const request = yield* HttpServerRequest; |
| 8 | const url = new URL(request.originalUrl); |
| 9 | const pathname = url.pathname; |
| 10 | |
| 11 | if (request.method === "GET" && pathname === "/inspect") { |
| 12 | return yield* HttpServerResponse.json({ |
| 13 | method: request.method, |
| 14 | url: request.url, |
| 15 | originalUrl: request.originalUrl, |
| 16 | host: request.headers.host, |
| 17 | protocol: request.headers["x-forwarded-proto"], |
| 18 | requestId: request.headers["x-request-id"], |
| 19 | remoteAddress: Option.getOrUndefined(request.remoteAddress), |
| 20 | query: Object.fromEntries(url.searchParams.entries()), |
| 21 | cookies: request.cookies, |
| 22 | }); |
| 23 | } |
| 24 | |
| 25 | if (request.method === "POST" && pathname === "/jobs") { |
| 26 | const payload = (yield* request.json) as { content: string }; |
| 27 | const response = yield* HttpServerResponse.json( |
| 28 | { |
| 29 | method: request.method, |
| 30 | url: request.url, |
| 31 | payload, |
| 32 | }, |
| 33 | { |
| 34 | status: 201, |
| 35 | headers: { |
| 36 | "x-handler": "lambda-http", |
| 37 | }, |
| 38 | }, |
| 39 | ); |
| 40 | |
| 41 | return HttpServerResponse.setCookieUnsafe( |
| 42 | response, |
| 43 | "job-session", |
| 44 | "created", |
| 45 | { |
| 46 | httpOnly: true, |
| 47 | path: "/", |
| 48 | }, |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | if (request.method === "GET" && pathname === "/binary") { |
| 53 | return HttpServerResponse.uint8Array(new TextEncoder().encode("alchemy"), { |
| 54 | headers: { |
| 55 | "content-type": "application/octet-stream", |
| 56 | }, |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | return HttpServerResponse.text("Not found", { status: 404 }); |
| 61 | }); |