UNPKG

@kitmi/jacaranda

Version:

JavaScript application framework

128 lines (127 loc) 4.69 kB
/** * Enable web request routing. * @module Feature_Routing */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return _default; } }); const _utils = require("@kitmi/utils"); const _Feature = /*#__PURE__*/ _interop_require_default(require("../Feature")); const _routers = /*#__PURE__*/ _interop_require_wildcard(require("../routers")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const _default = { /** * This feature is loaded at ready (final) stage. * @member {string} */ stage: _Feature.default.PLUGIN, /** * Load the feature. * @param {Routable} app - The app module object * @param {object} routes - Routes and configuration * @returns {Promise.<*>} */ load_: (app, routes)=>{ if (app.router == null) { // if mount to server level app.createServerModuleRouter(); } app.once('after:' + _Feature.default.PLUGIN, ()=>{ app.useMiddleware(app.router, async (ctx, next)=>{ ctx.bus = (businessName, fromApp)=>{ if (fromApp) { let _app = app.getOtherApp(fromApp); return _app.bus(businessName, null, ctx); } return app.bus(businessName, fromApp, ctx); }; await next(); // clear bus interface delete ctx.bus; // clear db cache from controller if (ctx.dbClasses) { _utils._.each(ctx.dbClasses, (db)=>{ delete db.ctx; }); delete ctx.dbClasses; } }, 'ctxClean'); return (0, _utils.eachAsync_)(routes, async (routersConfig, route)=>{ if ((0, _utils.isPlainObject)(routersConfig)) { return (0, _utils.eachAsync_)(routersConfig, async (options, type)=>{ app.log('verbose', `A "${type}" router is created at "${route}" in app [${app.name}].`); return _routers[type](app, route, options); }); } else { // 'route': 'method:/path' let mainRoute = '/', baseRoute = route; let pos = route.indexOf(':/'); if (pos !== -1) { mainRoute = route.substring(0, pos + 2); baseRoute = route.substring(pos + 1); } else if (Array.isArray(routersConfig)) { //all handled by middleware chains mainRoute = 'all:/'; } let rules = { [mainRoute]: routersConfig }; app.log('verbose', `A "rule" router is created at "${baseRoute}" in app [${app.name}].`); return _routers['rule'](app, baseRoute, { rules: rules }); } }); }); } }; //# sourceMappingURL=routing.js.map