refactor router methods to use rest parameters for handler functions
This commit is contained in:
		@@ -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;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user