refactor router methods to use rest parameters for handler functions
This commit is contained in:
		
							
								
								
									
										82
									
								
								dist/router.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										82
									
								
								dist/router.js
									
									
									
									
										vendored
									
									
								
							@@ -77,28 +77,28 @@ export default class Router {
 | 
				
			|||||||
            this.tpl = obj;
 | 
					            this.tpl = obj;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    get(path, cb) {
 | 
					    get(path, ...callback) {
 | 
				
			||||||
        this.registerRoute(path, "get", cb);
 | 
					        this.registerRoute(path, "get", callback);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    post(path, cb) {
 | 
					    post(path, ...callback) {
 | 
				
			||||||
        this.registerRoute(path, "post", cb);
 | 
					        this.registerRoute(path, "post", callback);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    head(path, cb) {
 | 
					    head(path, ...callback) {
 | 
				
			||||||
        this.registerRoute(path, "head", cb);
 | 
					        this.registerRoute(path, "head", callback);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    put(path, cb) {
 | 
					    put(path, ...callback) {
 | 
				
			||||||
        this.registerRoute(path, "put", cb);
 | 
					        this.registerRoute(path, "put", callback);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    delete(path, cb) {
 | 
					    delete(path, ...callback) {
 | 
				
			||||||
        this.registerRoute(path, "delete", cb);
 | 
					        this.registerRoute(path, "delete", callback);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    patch(path, cb) {
 | 
					    patch(path, ...callback) {
 | 
				
			||||||
        this.registerRoute(path, "patch", cb);
 | 
					        this.registerRoute(path, "patch", callback);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    registerRoute(path, method, handler) {
 | 
					    registerRoute(path, method, handler) {
 | 
				
			||||||
@@ -157,37 +157,37 @@ export default class Router {
 | 
				
			|||||||
    static({ dir = path.resolve() + "/public", route = /^\/public/ }) {
 | 
					    static({ dir = path.resolve() + "/public", route = /^\/public/ }) {
 | 
				
			||||||
        if (!this.mimes.size)
 | 
					        if (!this.mimes.size)
 | 
				
			||||||
            this.readMimes();
 | 
					            this.readMimes();
 | 
				
			||||||
        this.get(route, [(req, res) => {
 | 
					        this.get(route, (req, res, next) => {
 | 
				
			||||||
                try {
 | 
					            try {
 | 
				
			||||||
                    const filename = req.url.pathname.replace(route, "") || "index.html";
 | 
					                const filename = req.url.pathname.replace(route, "") || "index.html";
 | 
				
			||||||
                    const mime = this.mimes.get(filename.split(".").pop() || "");
 | 
					                const mime = this.mimes.get(filename.split(".").pop() || "");
 | 
				
			||||||
                    const file = path.join(dir, filename);
 | 
					                const file = path.join(dir, filename);
 | 
				
			||||||
                    const stat = fs.statSync(file);
 | 
					                const stat = fs.statSync(file);
 | 
				
			||||||
                    if (req.headers.range) {
 | 
					                if (req.headers.range) {
 | 
				
			||||||
                        const [startStr, endStr] = req.headers.range.replace(/bytes=/, "").split("-");
 | 
					                    const [startStr, endStr] = req.headers.range.replace(/bytes=/, "").split("-");
 | 
				
			||||||
                        const start = parseInt(startStr, 10);
 | 
					                    const start = parseInt(startStr, 10);
 | 
				
			||||||
                        const end = endStr ? parseInt(endStr, 10) : stat.size - 1;
 | 
					                    const end = endStr ? parseInt(endStr, 10) : stat.size - 1;
 | 
				
			||||||
                        res.writeHead(206, {
 | 
					                    res.writeHead(206, {
 | 
				
			||||||
                            "Content-Range": `bytes ${start}-${end}/${stat.size}`,
 | 
					                        "Content-Range": `bytes ${start}-${end}/${stat.size}`,
 | 
				
			||||||
                            "Accept-Ranges": "bytes",
 | 
					                        "Accept-Ranges": "bytes",
 | 
				
			||||||
                            "Content-Length": end - start + 1,
 | 
					                        "Content-Length": end - start + 1,
 | 
				
			||||||
                            "Content-Type": mime,
 | 
					                        "Content-Type": mime,
 | 
				
			||||||
                        });
 | 
					                    });
 | 
				
			||||||
                        fs.createReadStream(file, { start, end }).pipe(res);
 | 
					                    fs.createReadStream(file, { start, end }).pipe(res);
 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    else {
 | 
					 | 
				
			||||||
                        res.writeHead(200, {
 | 
					 | 
				
			||||||
                            "Content-Length": stat.size,
 | 
					 | 
				
			||||||
                            "Content-Type": mime,
 | 
					 | 
				
			||||||
                        });
 | 
					 | 
				
			||||||
                        fs.createReadStream(file).pipe(res);
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                catch (err) {
 | 
					                else {
 | 
				
			||||||
                    console.error(err);
 | 
					                    res.writeHead(200, {
 | 
				
			||||||
                    res.reply({ code: 404, body: "404 - File not found" });
 | 
					                        "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;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -106,74 +106,79 @@ export default class Router {
 | 
				
			|||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Registers a route for HTTP GET requests.
 | 
					   * Registers a route for HTTP GET requests.
 | 
				
			||||||
   * @param {string | RegExp} path - The URL path or pattern for the route.
 | 
					   * @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.
 | 
					   * @returns {this} The current instance for method chaining.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  get(path: string | RegExp, cb: Handler[]): this {
 | 
					  get(path: string | RegExp, ...callback: Handler[]): this {
 | 
				
			||||||
    this.registerRoute(path, "get", cb);
 | 
					    this.registerRoute(path, "get", callback);
 | 
				
			||||||
    return this;
 | 
					    return this;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Registers a route for HTTP POST requests.
 | 
					   * Registers a route for HTTP POST requests.
 | 
				
			||||||
   * @param {string | RegExp} path - The URL path or pattern for the route.
 | 
					   * @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.
 | 
					   * @returns {this} The current instance for method chaining.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  post(path: string | RegExp, cb: Handler[]): this {
 | 
					  post(path: string | RegExp, ...callback: Handler[]): this {
 | 
				
			||||||
    this.registerRoute(path, "post", cb);
 | 
					    this.registerRoute(path, "post", callback);
 | 
				
			||||||
    return this;
 | 
					    return this;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Registers a route for HTTP HEAD requests.
 | 
					   * Registers a route for HTTP HEAD requests.
 | 
				
			||||||
   * @param {string | RegExp} path - The URL path or pattern for the route.
 | 
					   * @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.
 | 
					   * @returns {this} The current instance for method chaining.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  head(path: string | RegExp, cb: Handler[]): this {
 | 
					  head(path: string | RegExp, ...callback: Handler[]): this {
 | 
				
			||||||
    this.registerRoute(path, "head", cb);
 | 
					    this.registerRoute(path, "head", callback);
 | 
				
			||||||
    return this;
 | 
					    return this;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Registers a route for HTTP PUT requests.
 | 
					   * Registers a route for HTTP PUT requests.
 | 
				
			||||||
   * @param {string | RegExp} path - The URL path or pattern for the route.
 | 
					   * @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.
 | 
					   * @returns {this} The current instance for method chaining.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  put(path: string | RegExp, cb: Handler[]): this {
 | 
					  put(path: string | RegExp, ...callback: Handler[]): this {
 | 
				
			||||||
    this.registerRoute(path, "put", cb);
 | 
					    this.registerRoute(path, "put", callback);
 | 
				
			||||||
    return this;
 | 
					    return this;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Registers a route for HTTP DELETE requests.
 | 
					   * Registers a route for HTTP DELETE requests.
 | 
				
			||||||
   * @param {string | RegExp} path - The URL path or pattern for the route.
 | 
					   * @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.
 | 
					   * @returns {this} The current instance for method chaining.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  delete(path: string | RegExp, cb: Handler[]): this {
 | 
					  delete(path: string | RegExp, ...callback: Handler[]): this {
 | 
				
			||||||
    this.registerRoute(path, "delete", cb);
 | 
					    this.registerRoute(path, "delete", callback);
 | 
				
			||||||
    return this;
 | 
					    return this;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Registers a route for HTTP PATCH requests.
 | 
					   * Registers a route for HTTP PATCH requests.
 | 
				
			||||||
   * @param {string | RegExp} path - The URL path or pattern for the route.
 | 
					   * @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.
 | 
					   * @returns {this} The current instance for method chaining.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  patch(path: string | RegExp, cb: Handler[]): this {
 | 
					  patch(path: string | RegExp, ...callback: Handler[]): this {
 | 
				
			||||||
    this.registerRoute(path, "patch", cb);
 | 
					    this.registerRoute(path, "patch", callback);
 | 
				
			||||||
    return this;
 | 
					    return this;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Registers a route for HTTP OPTIONS requests.
 | 
					   * Registers a route with a specified path, HTTP method, and handler(s).
 | 
				
			||||||
   * @param {string | RegExp} path - The URL path or pattern for the route.
 | 
					   * If the route already exists, the provided handler(s) will be appended
 | 
				
			||||||
   * @param {Handler[]} cb - An array of middleware or handler functions to execute
 | 
					   * to the existing method's handlers.
 | 
				
			||||||
   * @returns {this} The current instance for method chaining.
 | 
					   *
 | 
				
			||||||
 | 
					   * @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(
 | 
					  private registerRoute(
 | 
				
			||||||
    path: string | RegExp,
 | 
					    path: string | RegExp,
 | 
				
			||||||
@@ -267,7 +272,7 @@ export default class Router {
 | 
				
			|||||||
    if(!this.mimes.size)
 | 
					    if(!this.mimes.size)
 | 
				
			||||||
      this.readMimes();
 | 
					      this.readMimes();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.get(route, [(req: Request, res: Response) => {
 | 
					    this.get(route, (req: Request, res: Response, next?: () => void) => {
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        const filename = req.url.pathname.replace(route, "") || "index.html";
 | 
					        const filename = req.url.pathname.replace(route, "") || "index.html";
 | 
				
			||||||
        const mime = this.mimes.get(filename.split(".").pop() || "");
 | 
					        const mime = this.mimes.get(filename.split(".").pop() || "");
 | 
				
			||||||
@@ -299,7 +304,7 @@ export default class Router {
 | 
				
			|||||||
        console.error(err);
 | 
					        console.error(err);
 | 
				
			||||||
        res.reply({ code: 404, body: "404 - File not found" });
 | 
					        res.reply({ code: 404, body: "404 - File not found" });
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }]);
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return this;
 | 
					    return this;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user