Effect-TS / effect

An ecosystem of tools to build robust applications in TypeScript
https://effect.website
MIT License
7.59k stars 241 forks source link

From Discord: Discussion on Correct Usage of URL Parsing in ServerRequest and Naming Conventions #3639

Closed effect-bot closed 1 month ago

effect-bot commented 1 month ago

Summary

The user is discussing a potential issue in the Effect-TS codebase, specifically in the httpMiddleware.ts file. They suggest that the code should use ServerRequest.searchParamsFromURL(new URL(request.originalUrl)) instead of the current implementation because ServerRequest.url is stripped.

Additionally, the user questions the naming conventions used in the ServerRequest object, particularly the distinction between ServerRequest.url and ServerRequest.originalUrl. They express concern that deviating from the web standard Request's url property might be confusing and suggest using a different term for the parsed URL, similar to how Next.js uses NextRequest.pathname.

Key Takeaways:

  1. Code Suggestion: The user proposes a change to how URL search parameters are parsed.
  2. Naming Conventions: There is confusion about the use of ServerRequest.url vs. ServerRequest.originalUrl.
  3. Consistency with Web Standards: The user advocates for adhering to web standards or using clear, distinct naming conventions like Next.js.

Discord thread

https://discord.com/channels/795981131316985866/1286042509616746519

ethanniser commented 1 month ago

REPRO:

import {
  HttpMiddleware,
  HttpServer,
  HttpServerRequest,
  HttpServerResponse,
} from "@effect/platform";
import { NodeHttpServer, NodeRuntime } from "@effect/platform-node";
import { Layer, Effect } from "effect";
import { createServer } from "node:http";

const ServerLive = NodeHttpServer.layer(() => createServer(), { port: 3000 });

const HttpLive = HttpServer.serve(
  Effect.gen(function* () {
    const req = yield* HttpServerRequest.HttpServerRequest;
    console.log(req.url);
    console.log(req.originalUrl);
    const searchParams = yield* HttpServerRequest.ParsedSearchParams;
    console.log(searchParams);
    return HttpServerResponse.text("hi");
  }).pipe(HttpMiddleware.searchParamsParser, HttpMiddleware.logger)
).pipe(HttpServer.withLogAddress, Layer.provide(ServerLive));

NodeRuntime.runMain(Layer.launch(HttpLive));
[16:37:43.744] INFO (#0): Listening on http://0.0.0.0:3000
[16:37:47.884] INFO (#19) http.span.1=1ms:
TypeError: "/" cannot be parsed as a URL.
    at URL (native)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/@effect/platform/dist/esm/internal/httpMiddleware.js:111:56)
    at runLoop (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:1100:34)
    at evaluateEffect (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:703:27)
    at start (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:756:14)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/runtime.js:52:18)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/FiberSet.js:214:19)
    at handler (/Users/ethan/src/personal/effect-playground-2/node_modules/@effect/platform-node/dist/esm/internal/httpServer.js:85:19)
    at emit (node:events:180:48)
    at fetch (node:http:482:12)
    at http.server GET
http.status: 500
http.method: GET
http.url: /
[16:38:00.391] INFO (#20) http.span.2=0ms:
TypeError: "/foo/bar?bar=baz" cannot be parsed as a URL.
    at URL (native)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/@effect/platform/dist/esm/internal/httpMiddleware.js:111:56)
    at runLoop (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:1100:34)
    at evaluateEffect (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:703:27)
    at start (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/fiberRuntime.js:756:14)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/internal/runtime.js:52:18)
    at <anonymous> (/Users/ethan/src/personal/effect-playground-2/node_modules/effect/dist/esm/FiberSet.js:214:19)
    at handler (/Users/ethan/src/personal/effect-playground-2/node_modules/@effect/platform-node/dist/esm/internal/httpServer.js:85:19)
    at emit (node:events:180:48)
    at fetch (node:http:482:12)
    at http.server GET
http.status: 500
http.method: GET
http.url: /foo/bar?bar=baz