refactor request handling to replace parsedUrl with url for consistency and improve type definitions
This commit is contained in:
parent
4514a37999
commit
3aa7673d5d
12
src/index.ts
12
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` });
|
||||
|
@ -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);
|
||||
|
7
src/types.d.ts
vendored
7
src/types.d.ts
vendored
@ -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<IncomingMessage, 'url'> {
|
||||
url: {
|
||||
pathname: string;
|
||||
split: string[];
|
||||
searchParams: URLSearchParams;
|
||||
|
Loading…
x
Reference in New Issue
Block a user