@mediarithmics/plugins-nodejs-sdk
Version:
This is the mediarithmics nodejs to help plugin developers bootstrapping their plugin without having to deal with most of the plugin boilerplate
105 lines • 4.68 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-misused-promises */
/* eslint-disable @typescript-eslint/unbound-method */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomActionBasePlugin = void 0;
const lodash_1 = __importDefault(require("lodash"));
const common_1 = require("../common");
class CustomActionBasePlugin extends common_1.BasePlugin {
constructor(enableThrottling = false) {
super(enableThrottling);
this.initCustomAction();
this.setErrorHandler();
}
/**
*
* @param customActionId
*/
async fetchCustomAction(customActionId) {
const customActionResponse = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/scenario_custom_actions/${customActionId}`);
this.logger.debug(`Fetched Custom Action: ${customActionId} - %j`, customActionResponse.data);
return customActionResponse.data;
}
/**
*
* @param customActionId
*/
async fetchCustomActionProperties(customActionId) {
const customActionPropertiesResponse = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/scenario_custom_actions/${customActionId}/properties`);
this.logger.debug(`Fetched Custom Action Properties: ${customActionId} - %j`, customActionPropertiesResponse.data);
return customActionPropertiesResponse.data;
}
async instanceContextBuilder(customActionId) {
const [customAction, properties] = await Promise.all([
this.fetchCustomAction(customActionId),
this.fetchCustomActionProperties(customActionId),
]);
const context = {
customAction: customAction,
properties: new common_1.PropertiesWrapper(properties),
};
return context;
}
async getInstanceContext(customActionId) {
if (!this.pluginCache.get(customActionId)) {
void this.pluginCache.put(customActionId, this.instanceContextBuilder(customActionId).catch((err) => {
this.logger.error(`Error while caching instance context: ${err.message}`);
this.pluginCache.del(customActionId);
throw err;
}), this.getInstanceContextCacheExpiration());
}
return this.pluginCache.get(customActionId);
}
emptyBodyFilter(req, res, next) {
if (!req.body || lodash_1.default.isEmpty(req.body)) {
const msg = {
error: 'Missing request body',
};
this.logger.error(`POST /v1/${req.url} : %j`, msg);
res.status(500).json(msg);
}
else {
next();
}
}
initCustomAction() {
// route /v1/scenario_custom_actions
this.app.post('/v1/scenario_custom_actions', this.emptyBodyFilter, async (req, res) => {
try {
this.logger.debug(`POST /v1/scenario_custom_actions %j`, req.body);
const request = req.body;
if (!this.onCustomActionCall) {
throw new Error('No Scenario Custom Action listener registered!');
}
const instanceContext = await this.getInstanceContext(request.custom_action_id);
const response = await this.onCustomActionCall(request, instanceContext);
this.logger.debug(`CustomActionId: ${request.custom_action_id} - Plugin impl returned: %j`, response);
const pluginResponse = {
status: response.status,
};
let statusCode;
switch (response.status) {
case 'ok':
statusCode = 200;
break;
case 'ko':
statusCode = 500;
break;
default:
statusCode = 500;
}
this.logger.debug(`CustomActionId: ${request.custom_action_id} - Returning : ${statusCode} - %j`, response);
return res.status(statusCode).send(JSON.stringify(pluginResponse));
}
catch (err) {
this.logger.error(`Something bad happened : ${err.message} - ${err.stack ? err.stack : 'stack undefined'}`);
return res.status(500).send({ status: 'error', message: `${err.message}` });
}
});
}
}
exports.CustomActionBasePlugin = CustomActionBasePlugin;
//# sourceMappingURL=CustomActionBasePlugin.js.map