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);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    registerRoute(path, method, handlers) {
 | 
			
		||||
        if (!this.routes.has(path)) {
 | 
			
		||||
            this.routes.set(path, {});
 | 
			
		||||
        }
 | 
			
		||||
        this.routes.get(path)[method] = handlers.flat();
 | 
			
		||||
    registerRoute(path, method, handler) {
 | 
			
		||||
        var _a;
 | 
			
		||||
        const route = (_a = this.routes.get(path)) !== null && _a !== void 0 ? _a : {
 | 
			
		||||
            [method]: handler
 | 
			
		||||
        };
 | 
			
		||||
        this.routes.set(path, route);
 | 
			
		||||
        console.log("route set:", method.toUpperCase(), path);
 | 
			
		||||
        this.sortRoutes();
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    getRoute(path, method) {
 | 
			
		||||
        method = method.toLowerCase();
 | 
			
		||||
        return [...this.routes.entries()].find(r => {
 | 
			
		||||
        return [...this.routes.entries()]
 | 
			
		||||
            .find(r => {
 | 
			
		||||
            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() {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user