From c23b1427296fe2b4e133610e8a2357e9901f15d1 Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Sat, 25 May 2024 08:19:29 +0200 Subject: [PATCH] docs: Add example for getting raw request in platform README (#2850) --- packages/platform/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/platform/README.md b/packages/platform/README.md index a700fa09a4..67978e0692 100644 --- a/packages/platform/README.md +++ b/packages/platform/README.md @@ -1771,3 +1771,32 @@ curl -i http://localhost:3000 # Request with the valid cookie curl -i http://localhost:3000 --cookie "test=myvalue" ``` + +## ServerRequest + +### How do I get the raw request? + +The native request object depends on the platform you are using, and it is not directly modeled in `@effect/platform`. Instead, you need to refer to the specific platform package you are working with, such as `@effect/platform-node` or `@effect/platform-bun`. + +Here is an example using Node.js: + +```ts +import { HttpServer } from "@effect/platform" +import { NodeHttpServer } from "@effect/platform-node" +import { Effect } from "effect" +import { listen } from "./listen.js" + +const router = HttpServer.router.empty.pipe( + HttpServer.router.get( + "/", + Effect.gen(function* () { + const req = yield* HttpServer.request.ServerRequest + const raw = NodeHttpServer.request.toIncomingMessage(req) + console.log(raw) + return HttpServer.response.empty() + }) + ) +) + +listen(HttpServer.server.serve(router), 3000) +```