@mvx/mvc
Version:
@mvx/mvc is mvc framework on server, base on koa, @tsdi frameworks
55 lines (53 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RouteChecker = void 0;
const tslib_1 = require("tslib");
const ioc_1 = require("@tsdi/ioc");
const urlReg = /\/((\w|%|\.))+\.\w+$/;
const noParms = /\/\s*$/;
const hasParms = /\?\S*$/;
const subStart = /^\s*\/|\?/;
let RouteChecker = class RouteChecker extends ioc_1.IocCoreService {
constructor() {
super(...arguments);
this.assertUrlRegExp = urlReg;
}
isRoute(ctxUrl) {
return !this.assertUrlRegExp.test(ctxUrl);
}
getReqRoute(ctx) {
let reqUrl = this.vaildify(ctx.url, true);
let config = ctx.mvcContext.getConfiguration();
if (config.routePrefix) {
return reqUrl.replace(config.routePrefix, '');
}
return reqUrl;
}
vaildify(routePath, foreNull = false) {
if (foreNull && routePath === '/') {
routePath = '';
}
if (noParms.test(routePath)) {
routePath = routePath.substring(0, routePath.lastIndexOf('/'));
}
if (hasParms.test(routePath)) {
routePath = routePath.substring(0, routePath.lastIndexOf('?'));
}
return routePath;
}
isActiveRoute(ctx, route) {
let routeUrl = this.getReqRoute(ctx);
if (route === '' || route === routeUrl) {
return true;
}
return routeUrl.startsWith(route) && subStart.test(routeUrl.substring(route.length));
}
static ρAnn() {
return { "name": "RouteChecker", "params": { "isRoute": ["ctxUrl"], "getReqRoute": ["ctx"], "vaildify": ["routePath", "foreNull"], "isActiveRoute": ["ctx", "route"] } };
}
};
RouteChecker = tslib_1.__decorate([
ioc_1.Singleton()
], RouteChecker);
exports.RouteChecker = RouteChecker;
//# sourceMappingURL=../sourcemaps/services/RouteChecker.js.map