refactor router to improve route registration and handler management

This commit is contained in:
2025-03-17 06:46:53 +01:00
parent 0415507c48
commit ce9f313220
2 changed files with 29 additions and 24 deletions

20
dist/router.js vendored
View File

@ -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() {