f2e-server3
Version:
f2e-server 3.0
52 lines (51 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RouterDecorator = exports.addRoute = void 0;
const utils_1 = require("../../utils");
const routes_1 = require("../../routes");
const routes = [];
const addRoute = function (path, handler, ext) {
if (typeof handler === 'function') {
utils_1.logger.debug(`add route ${path} ${ext?.method || '*'}`);
const reg = new RegExp((0, utils_1.pathname_fixer)(path).replace(/:(\w+)/g, '(?<$1>[^/]+)'));
routes.push({
method: '*',
...(ext || {}),
path: reg,
handler: function (body, ctx) {
return handler(body, {
...ctx,
params: (reg.exec(ctx.pathname)?.groups || {})
});
},
});
}
};
exports.addRoute = addRoute;
/**
* 路由装饰器
*/
const RouterDecorator = function (path, ext) {
return function (...args) {
const [_, __, descriptor] = args;
if (typeof descriptor.value === 'function') {
(0, exports.addRoute)(path, descriptor.value, ext);
}
return descriptor;
};
};
exports.RouterDecorator = RouterDecorator;
const middleware_routes = {
mode: ['dev', 'prod'],
name: 'routes',
execute: async (conf) => {
const route = new routes_1.Route(conf);
route.routes = routes;
return {
onRoute: function (...args) {
return route.execute.apply(route, args);
},
};
}
};
exports.default = middleware_routes;