UNPKG

@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

111 lines 5.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EmailRouterPlugin = void 0; const lodash_1 = __importDefault(require("lodash")); const BasePlugin_1 = require("../common/BasePlugin"); class EmailRouterPlugin extends BasePlugin_1.BasePlugin { constructor(enableThrottling = false) { super(enableThrottling); // We init the specific route to listen for activity analysis requests this.initEmailRouting(); this.initEmailCheck(); this.setErrorHandler(); } // Method to build an instance context // To be overriden to get a cutom behavior async fetchEmailRouterProperties(id) { const response = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/email_routers/${id}/properties`); this.logger.debug(`Fetched Email Router Properties: ${id} - ${JSON.stringify(response.data)}`); return response.data; } // This is a default provided implementation async instanceContextBuilder(routerId) { const emailRouterProps = await this.fetchEmailRouterProperties(routerId); const context = { properties: new BasePlugin_1.PropertiesWrapper(emailRouterProps), }; return context; } async getInstanceContext(emailRouterId) { if (!this.pluginCache.get(emailRouterId)) { void this.pluginCache.put(emailRouterId, this.instanceContextBuilder(emailRouterId).catch((err) => { this.logger.error(`Error while caching instance context: ${err.message}`); this.pluginCache.del(emailRouterId); throw err; }), this.getInstanceContextCacheExpiration()); } return this.pluginCache.get(emailRouterId); } initEmailRouting() { this.app.post('/v1/email_routing', this.asyncMiddleware(async (req, res) => { if (!this.httpIsReady()) { const msg = { error: 'Plugin not initialized', }; this.logger.error('POST /v1/email_routing : %s', JSON.stringify(msg)); return res.status(500).json(msg); } else if (!req.body || lodash_1.default.isEmpty(req.body)) { const msg = { error: 'Missing request body', }; this.logger.error('POST /v1/email_routing : %s', JSON.stringify(msg)); return res.status(500).json(msg); } else { this.logger.debug(`POST /v1/email_routing ${JSON.stringify(req.body)}`); const emailRoutingRequest = req.body; if (!this.onEmailRouting) { const errMsg = 'No Email Routing listener registered!'; this.logger.error(errMsg); return res.status(500).json({ error: errMsg }); } const instanceContext = await this.getInstanceContext(emailRoutingRequest.email_router_id); const pluginResponse = await this.onEmailRouting(emailRoutingRequest, instanceContext); this.logger.debug(`Returning: ${JSON.stringify(pluginResponse)}`); res.status(200).send(JSON.stringify(pluginResponse)); } })); } initEmailCheck() { this.app.post('/v1/email_router_check', (req, res) => { if (!this.httpIsReady()) { const msg = { error: 'Plugin not initialized', }; this.logger.error('POST /v1/email_router_check : %s', JSON.stringify(msg)); return res.status(500).json(msg); } else if (!req.body || lodash_1.default.isEmpty(req.body)) { const msg = { error: 'Missing request body', }; this.logger.error('POST /v1/email_router_check : %s', JSON.stringify(msg)); res.status(500).json(msg); } else { this.logger.debug(`POST /v1/email_router_check ${JSON.stringify(req.body)}`); const emailCheckRequest = req.body; if (!this.onEmailRouting) { throw new Error('No Email Check listener registered!'); } this.getInstanceContext(emailCheckRequest.email_router_id) .then((instanceContext) => { return this.onEmailCheck(emailCheckRequest, instanceContext).then((response) => { this.logger.debug(`Returning: ${JSON.stringify(response)}`); res.status(200).send(JSON.stringify(response)); }); }) .catch((error) => { this.logger.error(`Something bad happened : ${error.message} - ${error.stack ? error.stack : 'stack undefined'}`); return res.status(500).send(`${error.message} \n ${error.stack ? error.stack : 'stack undefined'}`); }); } }); } } exports.EmailRouterPlugin = EmailRouterPlugin; //# sourceMappingURL=EmailRouterBasePlugin.js.map