From 3aa7673d5de43c68d1ef6e307dc0b591fd8ca227 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sat, 15 Mar 2025 23:32:54 +0100 Subject: [PATCH] refactor request handling to replace parsedUrl with url for consistency and improve type definitions --- src/index.ts | 12 ++++++------ src/router.ts | 2 +- src/types.d.ts | 7 ++++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6de22b6..1023f32 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,7 +62,7 @@ export default class Flummpress { await mw(req, res, () => {}); } - const route = this.router.getRoute(req.parsedUrl.pathname, req.method!); + const route = this.router.getRoute(req.url.pathname, req.method!); if(route) { const [pathPattern, methods] = route; @@ -75,7 +75,7 @@ export default class Flummpress { } if(handler) { - req.params = req.parsedUrl.pathname.match(new RegExp(pathPattern))?.groups || {}; + req.params = req.url.pathname.match(new RegExp(pathPattern))?.groups || {}; req.post = await this.readBody(req); handler(req, res); } @@ -95,7 +95,7 @@ export default class Flummpress { `[${new Date().toISOString()}]`, `${(process.hrtime(start)[1] / 1e6).toFixed(2)}ms`, `${req.method} ${res.statusCode}`, - req.parsedUrl.pathname, + req.url.pathname, ].join(" | ")); }).listen(...args); @@ -104,8 +104,8 @@ export default class Flummpress { private parseRequest(request: IncomingMessage): Request { const url = new URL(request.url!.replace(/(?!^.)(\/+)?$/, ""), "http://localhost"); - const req = request as Request; - req.parsedUrl = { + const req = request as unknown as Request; + req.url = { pathname: url.pathname, split: url.pathname.split("/").slice(1), searchParams: url.searchParams, @@ -153,7 +153,7 @@ export default class Flummpress { } private createResponse(response: ServerResponse): Response { - const res = response as Response; + const res: Response = response as Response; res.reply = ({ code = 200, type = "text/html", body }) => { res.writeHead(code, { "Content-Type": `${type}; charset=utf-8` }); diff --git a/src/router.ts b/src/router.ts index e0df380..4dcebc1 100644 --- a/src/router.ts +++ b/src/router.ts @@ -234,7 +234,7 @@ export default class Router { this.get(route, (req: Request, res: any) => { try { - const filename = req.parsedUrl.pathname.replace(route, "") || "index.html"; + const filename = req.url.pathname.replace(route, "") || "index.html"; const mime = this.mimes.get(filename.split(".").pop() || ""); const file = path.join(dir, filename); const stat = fs.statSync(file); diff --git a/src/types.d.ts b/src/types.d.ts index 187a3fe..d77296e 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,7 +1,8 @@ -import { IncomingMessage, ServerResponse } from "node:http"; +import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from "node:http"; +import { Socket } from "node:net"; -export interface Request extends IncomingMessage { - parsedUrl: { +export interface Request extends Omit { + url: { pathname: string; split: string[]; searchParams: URLSearchParams;