@fabrix/spool-tapestries
Version:
Spool - Tapestries, Easy RESTful Services
63 lines (62 loc) • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const lodash_1 = require("lodash");
const routes_1 = require("./routes");
const spool_router_1 = require("@fabrix/spool-router");
exports.Utils = {
getControllerMethods(app) {
let configMethod = app.config.get('tapestries.controllers.method') || ['GET', 'POST'];
if (configMethod instanceof Object) {
configMethod = Object.values(configMethod);
}
return typeof configMethod === 'string' ? [configMethod] : configMethod;
},
getControllerIgnore(app) {
const configIgnore = Object.values(app.config.get('tapestries.controllers.ignore') || []);
const defaultIgnore = ['TapestryController'];
return [...configIgnore, ...defaultIgnore];
},
getControllerTapestries(app) {
if (app.config.get('tapestries.controllers') === false) {
return {};
}
const controllers = lodash_1.omit(app.controllers || {}, exports.Utils.getControllerIgnore(app));
const routes = {};
Object.keys(controllers).forEach((controllerName) => {
controllers[controllerName].methods.forEach(handlerName => {
const route = {};
const path = exports.Utils.getHandlerPath(app, controllers[controllerName].id, handlerName);
exports.Utils.getControllerMethods(app).forEach(method => {
route[method] = exports.Utils.getControllerHandler(controllerName, handlerName);
});
routes[path] = route;
});
});
return routes;
},
getModelTapestries(app) {
const actionsConfig = app.config.get('tapestries.models.actions') || {};
const routes = {};
Object.keys(routes_1.Routes).forEach(path => {
Object.keys(routes_1.Routes[path]).map(m => {
if (spool_router_1.Utils.methods.indexOf(m) > -1) {
if (actionsConfig[exports.Utils.getHandlerName(routes_1.Routes[path][m])] === false) {
delete routes_1.Routes[path][m];
}
}
});
routes[`${path}`] = routes_1.Routes[path];
});
return routes;
},
getControllerHandler(controllerName, handlerName) {
return `${controllerName}.${handlerName}`;
},
getHandlerName(routeHandler = '') {
return (routeHandler.split('.')[1] || '').toString();
},
getHandlerPath(app, controllerId, handlerName) {
return path_1.join('/', controllerId, handlerName);
}
};