refactor router to improve route registration and handler management
This commit is contained in:
		@@ -5,7 +5,7 @@ import Tpl from "./template.js";
 | 
			
		||||
import { Request, Response, Handler } from "./types.js";
 | 
			
		||||
 | 
			
		||||
export default class Router {
 | 
			
		||||
  private routes: Map<RegExp | string, { [key: string]: Handler | Handler[] }>;
 | 
			
		||||
  private routes: Map<RegExp | string, { [key: string]: Handler[] }>;
 | 
			
		||||
  private tpl?: Tpl;
 | 
			
		||||
  private mimes: Map<string, string>;
 | 
			
		||||
 | 
			
		||||
@@ -42,7 +42,7 @@ export default class Router {
 | 
			
		||||
   */
 | 
			
		||||
  group(basePath: string | RegExp, callback: (methods: any) => void): this {
 | 
			
		||||
    const self = this;
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
    const methods = {
 | 
			
		||||
      get(path: string | RegExp, ...handlers: Handler[]) {
 | 
			
		||||
        const fullPath = self.combinePaths(basePath, path);
 | 
			
		||||
@@ -67,10 +67,10 @@ export default class Router {
 | 
			
		||||
    };
 | 
			
		||||
  
 | 
			
		||||
    callback(methods);
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
    return this;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Combines a base path and a sub path into a single path.
 | 
			
		||||
   * @param basePath - The base path or RegExp.
 | 
			
		||||
@@ -86,7 +86,7 @@ export default class Router {
 | 
			
		||||
      return new RegExp(`${basePath.replace(/\/$/, "")}${subPath.source}`);
 | 
			
		||||
    if(basePath instanceof RegExp && subPath instanceof RegExp)
 | 
			
		||||
      return new RegExp(`${basePath.source}${subPath.source}`);
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
    throw new TypeError("Invalid path types. Both basePath and subPath must be either string or RegExp.");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -179,14 +179,13 @@ export default class Router {
 | 
			
		||||
  private registerRoute(
 | 
			
		||||
    path: string | RegExp,
 | 
			
		||||
    method: string,
 | 
			
		||||
    handlers: Handler[]
 | 
			
		||||
    handler: Handler[]
 | 
			
		||||
  ): this {
 | 
			
		||||
    if(!this.routes.has(path)) {
 | 
			
		||||
      this.routes.set(path, {});
 | 
			
		||||
    }
 | 
			
		||||
  
 | 
			
		||||
    this.routes.get(path)![method] = handlers.flat();
 | 
			
		||||
  
 | 
			
		||||
    const route: { [key: string]: Handler[] } = this.routes.get(path) ?? {
 | 
			
		||||
      [ method ]: handler
 | 
			
		||||
    };
 | 
			
		||||
    this.routes.set(path, route);
 | 
			
		||||
 | 
			
		||||
    console.log("route set:", method.toUpperCase(), path);
 | 
			
		||||
    this.sortRoutes();
 | 
			
		||||
    return this;
 | 
			
		||||
@@ -199,10 +198,12 @@ export default class Router {
 | 
			
		||||
   * @returns The matching route or undefined.
 | 
			
		||||
   */
 | 
			
		||||
  getRoute(path: string, method: string): any {
 | 
			
		||||
    method = method.toLowerCase();
 | 
			
		||||
    return [...this.routes.entries()].find(r => {
 | 
			
		||||
      return (typeof r[0] === "string" ? r[0] === path : r[0].exec?.(path)) && r[1][method];
 | 
			
		||||
    });
 | 
			
		||||
    return [...this.routes.entries()]
 | 
			
		||||
      .find(r => typeof r[0] === "string"
 | 
			
		||||
        ? r[0] === path
 | 
			
		||||
        : r[0].exec?.(path)
 | 
			
		||||
       && r[1][method.toLowerCase()]
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user