UNPKG

@mvx/mvc

Version:

@mvx/mvc is mvc framework on server, base on koa, @tsdi frameworks

55 lines (53 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Router = void 0; const tslib_1 = require("tslib"); const ioc_1 = require("@tsdi/ioc"); const MvcMiddleware_1 = require("../middlewares/MvcMiddleware"); const RouteChecker_1 = require("../services/RouteChecker"); const Route_1 = require("./Route"); let Router = class Router extends MvcMiddleware_1.CompositeMiddleware { constructor() { super(...arguments); this.sorted = false; } vaild(ctx) { return (!ctx.status || ctx.status === 404) && this.isRouteUrl(ctx.url); } async execute(ctx, next) { if (!this.sorted) { this.handles = this.handles.sort((a, b) => { if (a instanceof Route_1.MvcRoute && b instanceof Route_1.MvcRoute) { return (b.url || '').length - (a.url || '').length; } return -1; }); this.resetFuncs(); this.sorted = true; } await super.execute(ctx); if (next) { return await next(); } } getChecker() { return this.getInjector().get(RouteChecker_1.RouteChecker); } isRouteUrl(route) { return this.getChecker().isRoute(route); } routes(...routes) { routes.forEach(r => this.use(r)); this.sorted = false; return this; } static ρAnn() { return { "name": "Router", "params": { "vaild": ["ctx"], "execute": ["ctx", "next"], "isRouteUrl": ["route"], "routes": ["routes"] } }; } }; RouterNPT = true; Router = tslib_1.__decorate([ ioc_1.Singleton() ], Router); exports.Router = Router; //# sourceMappingURL=../sourcemaps/router/Router.js.map