UNPKG

@restmatic/server

Version:

The core Pomegranate plugins for RestMatic

54 lines 2.69 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const fp_1 = require("lodash/fp"); const plugin_tools_1 = require("@pomegranate/plugin-tools"); const lodash_fun_1 = require("lodash-fun"); const joinMountPath = (path) => { let pathParts = fp_1.initial(path); let pathStart = pathParts.length ? pathParts : []; return `/${fp_1.last(path) === 'index' ? fp_1.join('/', pathStart) : fp_1.join('/', path)}`; }; exports.RoutesPlugin = plugin_tools_1.CreatePlugin('action') .configuration({ name: 'Router', depends: ['@restmatic/StaticFiles'] }) .directories([{ prop: 'main', path: '.' }]) .hooks({ load: (Injector, PluginLogger, PluginFiles, Express) => __awaiter(this, void 0, void 0, function* () { let RouteStructure = yield PluginFiles('main').fileListNested({ ext: '.js' }); let flatRoutes = lodash_fun_1.flattenKeys(RouteStructure); let RouteCount = 0; fp_1.each((route) => { let splitPath = fp_1.split('.', route); let routeFile = fp_1.get(route, flatRoutes); let pendingRoute = require(routeFile); let Routes = fp_1.get('Routes', pendingRoute); if (!Routes) { PluginLogger.error(`Router file ${route} does not export a Routes property.`, 0); throw new Error(`Route generation failed.`); } let injectedRoutes = Injector.inject(Routes); if (fp_1.isObject(injectedRoutes) && injectedRoutes.name === 'router') { let mountPath = joinMountPath(splitPath); RouteCount += 1; PluginLogger.log(`Loaded routes for ${mountPath}`); Express.use(mountPath, injectedRoutes); } else { PluginLogger.error(`Express Router: Attempted to load invalid route module: Route modules must return a Router Object.`); } }, fp_1.keys(flatRoutes)); PluginLogger.log(`Loaded ${RouteCount} route modules.`); }) }); //# sourceMappingURL=Routes.js.map