81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
var router_exports = {};
|
|
__export(router_exports, {
|
|
SmartRouter: () => SmartRouter
|
|
});
|
|
module.exports = __toCommonJS(router_exports);
|
|
var import_router = require("../../router");
|
|
class SmartRouter {
|
|
name = "SmartRouter";
|
|
routers = [];
|
|
routes = [];
|
|
constructor(init) {
|
|
Object.assign(this, init);
|
|
}
|
|
add(method, path, handler) {
|
|
if (!this.routes) {
|
|
throw new Error(import_router.MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
}
|
|
this.routes.push([method, path, handler]);
|
|
}
|
|
match(method, path) {
|
|
if (!this.routes) {
|
|
throw new Error("Fatal error");
|
|
}
|
|
const { routers, routes } = this;
|
|
const len = routers.length;
|
|
let i = 0;
|
|
let res;
|
|
for (; i < len; i++) {
|
|
const router = routers[i];
|
|
try {
|
|
routes.forEach((args) => {
|
|
router.add(...args);
|
|
});
|
|
res = router.match(method, path);
|
|
} catch (e) {
|
|
if (e instanceof import_router.UnsupportedPathError) {
|
|
continue;
|
|
}
|
|
throw e;
|
|
}
|
|
this.match = router.match.bind(router);
|
|
this.routers = [router];
|
|
this.routes = void 0;
|
|
break;
|
|
}
|
|
if (i === len) {
|
|
throw new Error("Fatal error");
|
|
}
|
|
this.name = `SmartRouter + ${this.activeRouter.name}`;
|
|
return res;
|
|
}
|
|
get activeRouter() {
|
|
if (this.routes || this.routers.length !== 1) {
|
|
throw new Error("No active router has been determined yet.");
|
|
}
|
|
return this.routers[0];
|
|
}
|
|
}
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
SmartRouter
|
|
});
|