From 308301cb2c29bf961e1cf1bd86a1b985d5720a01 Mon Sep 17 00:00:00 2001 From: Flummi Date: Mon, 17 Mar 2025 09:34:41 +0100 Subject: [PATCH] refactor router methods to use rest parameters for handler functions --- dist/router.js | 82 +++++++++++++++++++++++++------------------------- src/router.ts | 53 +++++++++++++++++--------------- 2 files changed, 70 insertions(+), 65 deletions(-) diff --git a/dist/router.js b/dist/router.js index 2eec438..7ea3172 100644 --- a/dist/router.js +++ b/dist/router.js @@ -77,28 +77,28 @@ export default class Router { this.tpl = obj; } } - get(path, cb) { - this.registerRoute(path, "get", cb); + get(path, ...callback) { + this.registerRoute(path, "get", callback); return this; } - post(path, cb) { - this.registerRoute(path, "post", cb); + post(path, ...callback) { + this.registerRoute(path, "post", callback); return this; } - head(path, cb) { - this.registerRoute(path, "head", cb); + head(path, ...callback) { + this.registerRoute(path, "head", callback); return this; } - put(path, cb) { - this.registerRoute(path, "put", cb); + put(path, ...callback) { + this.registerRoute(path, "put", callback); return this; } - delete(path, cb) { - this.registerRoute(path, "delete", cb); + delete(path, ...callback) { + this.registerRoute(path, "delete", callback); return this; } - patch(path, cb) { - this.registerRoute(path, "patch", cb); + patch(path, ...callback) { + this.registerRoute(path, "patch", callback); return this; } registerRoute(path, method, handler) { @@ -157,37 +157,37 @@ export default class Router { static({ dir = path.resolve() + "/public", route = /^\/public/ }) { if (!this.mimes.size) this.readMimes(); - this.get(route, [(req, res) => { - try { - 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); - if (req.headers.range) { - const [startStr, endStr] = req.headers.range.replace(/bytes=/, "").split("-"); - const start = parseInt(startStr, 10); - const end = endStr ? parseInt(endStr, 10) : stat.size - 1; - res.writeHead(206, { - "Content-Range": `bytes ${start}-${end}/${stat.size}`, - "Accept-Ranges": "bytes", - "Content-Length": end - start + 1, - "Content-Type": mime, - }); - fs.createReadStream(file, { start, end }).pipe(res); - } - else { - res.writeHead(200, { - "Content-Length": stat.size, - "Content-Type": mime, - }); - fs.createReadStream(file).pipe(res); - } + this.get(route, (req, res, next) => { + try { + 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); + if (req.headers.range) { + const [startStr, endStr] = req.headers.range.replace(/bytes=/, "").split("-"); + const start = parseInt(startStr, 10); + const end = endStr ? parseInt(endStr, 10) : stat.size - 1; + res.writeHead(206, { + "Content-Range": `bytes ${start}-${end}/${stat.size}`, + "Accept-Ranges": "bytes", + "Content-Length": end - start + 1, + "Content-Type": mime, + }); + fs.createReadStream(file, { start, end }).pipe(res); } - catch (err) { - console.error(err); - res.reply({ code: 404, body: "404 - File not found" }); + else { + res.writeHead(200, { + "Content-Length": stat.size, + "Content-Type": mime, + }); + fs.createReadStream(file).pipe(res); } - }]); + } + catch (err) { + console.error(err); + res.reply({ code: 404, body: "404 - File not found" }); + } + }); return this; } } diff --git a/src/router.ts b/src/router.ts index 4eccbeb..af92eb5 100644 --- a/src/router.ts +++ b/src/router.ts @@ -106,74 +106,79 @@ export default class Router { /** * Registers a route for HTTP GET requests. * @param {string | RegExp} path - The URL path or pattern for the route. - * @param {Handler[]} cb - An array of middleware or handler functions to execute for this route. + * @param {Handler[]} callback - An array of middleware or handler functions to execute for this route. * @returns {this} The current instance for method chaining. */ - get(path: string | RegExp, cb: Handler[]): this { - this.registerRoute(path, "get", cb); + get(path: string | RegExp, ...callback: Handler[]): this { + this.registerRoute(path, "get", callback); return this; } /** * Registers a route for HTTP POST requests. * @param {string | RegExp} path - The URL path or pattern for the route. - * @param {Handler[]} cb - An array of middleware or handler functions to execute for this route. + * @param {Handler[]} callback - An array of middleware or handler functions to execute for this route. * @returns {this} The current instance for method chaining. */ - post(path: string | RegExp, cb: Handler[]): this { - this.registerRoute(path, "post", cb); + post(path: string | RegExp, ...callback: Handler[]): this { + this.registerRoute(path, "post", callback); return this; } /** * Registers a route for HTTP HEAD requests. * @param {string | RegExp} path - The URL path or pattern for the route. - * @param {Handler[]} cb - An array of middleware or handler functions to execute for this route. + * @param {Handler[]} callback - An array of middleware or handler functions to execute for this route. * @returns {this} The current instance for method chaining. */ - head(path: string | RegExp, cb: Handler[]): this { - this.registerRoute(path, "head", cb); + head(path: string | RegExp, ...callback: Handler[]): this { + this.registerRoute(path, "head", callback); return this; } /** * Registers a route for HTTP PUT requests. * @param {string | RegExp} path - The URL path or pattern for the route. - * @param {Handler[]} cb - An array of middleware or handler functions to execute for this route. + * @param {Handler[]} callback - An array of middleware or handler functions to execute for this route. * @returns {this} The current instance for method chaining. */ - put(path: string | RegExp, cb: Handler[]): this { - this.registerRoute(path, "put", cb); + put(path: string | RegExp, ...callback: Handler[]): this { + this.registerRoute(path, "put", callback); return this; } /** * Registers a route for HTTP DELETE requests. * @param {string | RegExp} path - The URL path or pattern for the route. - * @param {Handler[]} cb - An array of middleware or handler functions to execute for this route. + * @param {Handler[]} callback - An array of middleware or handler functions to execute for this route. * @returns {this} The current instance for method chaining. */ - delete(path: string | RegExp, cb: Handler[]): this { - this.registerRoute(path, "delete", cb); + delete(path: string | RegExp, ...callback: Handler[]): this { + this.registerRoute(path, "delete", callback); return this; } /** * Registers a route for HTTP PATCH requests. * @param {string | RegExp} path - The URL path or pattern for the route. - * @param {Handler[]} cb - An array of middleware or handler functions to execute for this route. + * @param {Handler[]} callback - An array of middleware or handler functions to execute for this route. * @returns {this} The current instance for method chaining. */ - patch(path: string | RegExp, cb: Handler[]): this { - this.registerRoute(path, "patch", cb); + patch(path: string | RegExp, ...callback: Handler[]): this { + this.registerRoute(path, "patch", callback); return this; } /** - * Registers a route for HTTP OPTIONS requests. - * @param {string | RegExp} path - The URL path or pattern for the route. - * @param {Handler[]} cb - An array of middleware or handler functions to execute - * @returns {this} The current instance for method chaining. + * Registers a route with a specified path, HTTP method, and handler(s). + * If the route already exists, the provided handler(s) will be appended + * to the existing method's handlers. + * + * @private + * @param {string|RegExp} path - The path of the route, which can be a string or a RegExp. + * @param {string} method - The HTTP method for the route (e.g., "GET", "POST"). + * @param {Handler[]} handler - An array of handler functions to be associated with the route and method. + * @returns {this} Returns the current instance to allow method chaining. */ private registerRoute( path: string | RegExp, @@ -267,7 +272,7 @@ export default class Router { if(!this.mimes.size) this.readMimes(); - this.get(route, [(req: Request, res: Response) => { + this.get(route, (req: Request, res: Response, next?: () => void) => { try { const filename = req.url.pathname.replace(route, "") || "index.html"; const mime = this.mimes.get(filename.split(".").pop() || ""); @@ -299,7 +304,7 @@ export default class Router { console.error(err); res.reply({ code: 404, body: "404 - File not found" }); } - }]); + }); return this; }