@kezios/forest-express-decorator
Version:
🚀 Decorators for Express Forest Admin
59 lines (58 loc) • 2.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCollection = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const express_1 = __importDefault(require("express"));
const lodash_1 = require("lodash");
class BaseCollection {
initialize(app, Liana) {
if (this.name === undefined)
throw 'Cannot initialize collection with undefined name';
const config = {
actions: this.initializeSmartActions(app, Liana),
fields: this.initializeSmartField(),
};
return config;
}
initializeSmartActions(app, Liana) {
const actionsConfig = [];
//--------- Smart action properties
for (const smartActionName in this.actions) {
const fieldsConfig = [];
const hooksConfig = { change: {} };
if (this.actions[smartActionName].onLoad)
hooksConfig.load = this.actions[smartActionName].onLoad;
for (const field of this.actions[smartActionName].fields) {
const fieldConfig = (0, lodash_1.omit)(field, ['onChange']);
if (field.onChange !== undefined) {
const hookName = `on${((0, lodash_1.capitalize)(field.name))}Change`;
fieldConfig.hook = hookName;
hooksConfig.change[hookName] = field.onChange;
}
fieldsConfig.push(fieldConfig);
}
actionsConfig.push({
name: this.actions[smartActionName].label,
type: this.actions[smartActionName].type.toString(),
fields: fieldsConfig,
hooks: hooksConfig,
});
}
//--------- Smart action ROUTER
const router = express_1.default.Router();
const permissionMiddleware = new Liana.PermissionMiddlewareCreator(this.name);
for (const smartActionName in this.actions) {
router.post(`/${(0, lodash_1.kebabCase)(this.name)}/smart-action/${(0, lodash_1.kebabCase)(smartActionName)}`, permissionMiddleware.smartAction(), this.actions[smartActionName].onCall);
}
//---------
app.use(router);
return actionsConfig;
}
initializeSmartField() {
return this.fields;
}
}
exports.BaseCollection = BaseCollection;