refactor router to improve route registration and handler management
This commit is contained in:
		
							
								
								
									
										20
									
								
								dist/router.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										20
									
								
								dist/router.js
									
									
									
									
										vendored
									
									
								
							| @@ -101,20 +101,24 @@ export default class Router { | |||||||
|         this.registerRoute(path, "patch", cb); |         this.registerRoute(path, "patch", cb); | ||||||
|         return this; |         return this; | ||||||
|     } |     } | ||||||
|     registerRoute(path, method, handlers) { |     registerRoute(path, method, handler) { | ||||||
|         if (!this.routes.has(path)) { |         var _a; | ||||||
|             this.routes.set(path, {}); |         const route = (_a = this.routes.get(path)) !== null && _a !== void 0 ? _a : { | ||||||
|         } |             [method]: handler | ||||||
|         this.routes.get(path)[method] = handlers.flat(); |         }; | ||||||
|  |         this.routes.set(path, route); | ||||||
|         console.log("route set:", method.toUpperCase(), path); |         console.log("route set:", method.toUpperCase(), path); | ||||||
|         this.sortRoutes(); |         this.sortRoutes(); | ||||||
|         return this; |         return this; | ||||||
|     } |     } | ||||||
|     getRoute(path, method) { |     getRoute(path, method) { | ||||||
|         method = method.toLowerCase(); |         return [...this.routes.entries()] | ||||||
|         return [...this.routes.entries()].find(r => { |             .find(r => { | ||||||
|             var _a, _b; |             var _a, _b; | ||||||
|             return (typeof r[0] === "string" ? r[0] === path : (_b = (_a = r[0]).exec) === null || _b === void 0 ? void 0 : _b.call(_a, path)) && r[1][method]; |             return typeof r[0] === "string" | ||||||
|  |                 ? r[0] === path | ||||||
|  |                 : ((_b = (_a = r[0]).exec) === null || _b === void 0 ? void 0 : _b.call(_a, path)) | ||||||
|  |                     && r[1][method.toLowerCase()]; | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|     sortRoutes() { |     sortRoutes() { | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ import Tpl from "./template.js"; | |||||||
| import { Request, Response, Handler } from "./types.js"; | import { Request, Response, Handler } from "./types.js"; | ||||||
|  |  | ||||||
| export default class Router { | 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 tpl?: Tpl; | ||||||
|   private mimes: Map<string, string>; |   private mimes: Map<string, string>; | ||||||
|  |  | ||||||
| @@ -179,13 +179,12 @@ export default class Router { | |||||||
|   private registerRoute( |   private registerRoute( | ||||||
|     path: string | RegExp, |     path: string | RegExp, | ||||||
|     method: string, |     method: string, | ||||||
|     handlers: Handler[] |     handler: Handler[] | ||||||
|   ): this { |   ): this { | ||||||
|     if(!this.routes.has(path)) { |     const route: { [key: string]: Handler[] } = this.routes.get(path) ?? { | ||||||
|       this.routes.set(path, {}); |       [ method ]: handler | ||||||
|     } |     }; | ||||||
|    |     this.routes.set(path, route); | ||||||
|     this.routes.get(path)![method] = handlers.flat(); |  | ||||||
|  |  | ||||||
|     console.log("route set:", method.toUpperCase(), path); |     console.log("route set:", method.toUpperCase(), path); | ||||||
|     this.sortRoutes(); |     this.sortRoutes(); | ||||||
| @@ -199,10 +198,12 @@ export default class Router { | |||||||
|    * @returns The matching route or undefined. |    * @returns The matching route or undefined. | ||||||
|    */ |    */ | ||||||
|   getRoute(path: string, method: string): any { |   getRoute(path: string, method: string): any { | ||||||
|     method = method.toLowerCase(); |     return [...this.routes.entries()] | ||||||
|     return [...this.routes.entries()].find(r => { |       .find(r => typeof r[0] === "string" | ||||||
|       return (typeof r[0] === "string" ? r[0] === path : r[0].exec?.(path)) && r[1][method]; |         ? r[0] === path | ||||||
|     }); |         : r[0].exec?.(path) | ||||||
|  |        && r[1][method.toLowerCase()] | ||||||
|  |     ); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user