2019-09-28 16:12:42 +08:00

136 lines
7.8 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {value: true});
const _ = require('lodash');
const tslib_1 = require("tslib");
/** http方法名 */
const HTTP_METHODS = ['get', 'post', 'patch', 'del', 'options', 'put'];
let baseControllerArr = [];
class RouterDecorator {
constructor() {
HTTP_METHODS.forEach(httpMethod => {
this[httpMethod] = (url, ...beforeMiddlewares) => (target, name) => {
const routerOption = {
httpMethod,
beforeMiddlewares,
handlerName: name,
constructorFn: target.constructor,
className: target.constructor.name,
url: url
};
if (target.constructor.name === 'BaseController') {
baseControllerArr.push(routerOption)
} else {
this.__setRouter__(url, routerOption);
}
};
});
}
/** 推入路由配置 */
__setRouter__(url, routerOption) {
RouterDecorator.__router__[url] = RouterDecorator.__router__[url] || [];
RouterDecorator.__router__[url].push(routerOption);
}
/**
* 装饰Controller class的工厂函数
* 为一整个controller添加prefix
* 可以追加中间件
* @param {string} prefixUrl
* @param {...Middleware[]} beforeMiddlewares
* @param {any[]} baseFn
* @returns 装饰器函数
* @memberof RouterDecorator
*/
prefix(prefixUrl, baseFn = [], ...beforeMiddlewares) {
return function (targetControllerClass) {
RouterDecorator.__classPrefix__[targetControllerClass.name] = {
prefix: prefixUrl,
beforeMiddlewares: beforeMiddlewares,
baseFn: baseFn,
target: targetControllerClass
};
return targetControllerClass;
};
}
/**
* 注册路由
* 路由信息是通过装饰器收集的
* @export
* @param {Application} app eggApp实例
* @param {string} [options={ prefix: '' }] 举例: { prefix: '/api' }
*/
static initRouter(app, options = {prefix: ''}) {
let addUrl = [];
Object.keys(RouterDecorator.__router__).forEach(url => {
RouterDecorator.__router__[url].forEach((opt) => {
const controllerPrefixData = RouterDecorator.__classPrefix__[opt.className] || {
prefix: '',
beforeMiddlewares: [],
baseFn: [],
target: {}
};
let fullUrl = `${options.prefix}${controllerPrefixData.prefix}${url}`;
console.log(`>>>>>>>>custom register URL * ${opt.httpMethod.toUpperCase()} ${fullUrl} * ${opt.className}.${opt.handlerName}`);
if (!addUrl.includes(fullUrl)) {
app.router[opt.httpMethod](fullUrl, ...controllerPrefixData.beforeMiddlewares, ...opt.beforeMiddlewares, (ctx) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const ist = new opt.constructorFn(ctx);
yield ist[opt.handlerName](ctx);
}));
addUrl.push(fullUrl);
}
});
});
// 通用方法
const cArr = [].concat(_.uniq(baseControllerArr));
Object.keys(RouterDecorator.__classPrefix__).forEach(cl => {
const controllerPrefixData = RouterDecorator.__classPrefix__[cl] || {
prefix: '',
beforeMiddlewares: [],
baseFn: [],
target: {}
};
const setCArr = cArr.filter(c => {
if (RouterDecorator.__classPrefix__[cl].baseFn.includes(c.url.replace('/', ''))) {
return c;
}
});
setCArr.forEach(cf => {
let fullUrl = `${options.prefix}${controllerPrefixData.prefix}${cf.url}`;
console.log(`>>>>>>>>comm register URL * ${cf.httpMethod.toUpperCase()} ${fullUrl} * ${cl}.${cf.handlerName}`);
app.router[cf.httpMethod](fullUrl, ...controllerPrefixData.beforeMiddlewares, ...cf.beforeMiddlewares, (ctx) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const ist = new controllerPrefixData.target(ctx);
yield ist[cf.handlerName](ctx);
}));
});
});
}
}
/**
* 记录各个class的prefix以及相关中间件
* 最后统一设置
* @private
* @static
* @type {ClassPrefix}
* @memberof RouterDecorator
*/
RouterDecorator.__classPrefix__ = {};
/**
* 记录各个routerUrl的路由配置
* 最后统一设置
* @private
* @static
* @type {Router}
* @memberof RouterDecorator
*/
RouterDecorator.__router__ = {};
/** 暴露注册路由方法 */
exports.initRouter = RouterDecorator.initRouter;
/** 暴露实例的prefix和http的各个方法 */
exports.default = new RouterDecorator();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBb0N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