@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
88 lines • 4.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmailRendererPlugin = void 0;
const lodash_1 = __importDefault(require("lodash"));
const common_1 = require("../../common");
class EmailRendererPlugin extends common_1.BasePlugin {
constructor(enableThrottling = false) {
super(enableThrottling);
// We init the specific route to listen for email contents requests
this.initEmailContents();
this.setErrorHandler();
}
// Helper to fetch the creative resource with caching
async fetchCreative(id, forceReload = false) {
const response = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/creatives/${id}`, undefined, { 'force-reload': forceReload });
this.logger.debug(`Fetched Creative: ${id} - ${JSON.stringify(response.data)}`);
return response.data;
}
// Method to build an instance context
// To be overriden to get a cutom behavior
async fetchCreativeProperties(id, forceReload = false) {
const response = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/creatives/${id}/renderer_properties`, undefined, { 'force-reload': forceReload });
this.logger.debug(`Fetched Email Templates Properties: ${id} - ${JSON.stringify(response.data)}`);
return response.data;
}
// Method to process an Activity Analysis
// This is a default provided implementation
async instanceContextBuilder(creativeId, forceReload = false) {
const creativeP = this.fetchCreative(creativeId, forceReload);
const creativePropsP = this.fetchCreativeProperties(creativeId, forceReload);
const results = await Promise.all([creativeP, creativePropsP]);
const creative = results[0];
const creativeProps = results[1];
const context = {
creative: creative,
properties: new common_1.PropertiesWrapper(creativeProps),
};
return context;
}
async getInstanceContext(creativeId, forceReload = false) {
if (!this.pluginCache.get(creativeId) || forceReload) {
void this.pluginCache.put(creativeId, this.instanceContextBuilder(creativeId, forceReload).catch((err) => {
this.logger.error(`Error while caching instance context: ${err.message}`);
this.pluginCache.del(creativeId);
throw err;
}), this.getInstanceContextCacheExpiration());
}
return this.pluginCache.get(creativeId);
}
initEmailContents() {
this.app.post('/v1/email_contents', this.asyncMiddleware(async (req, res) => {
if (!this.httpIsReady()) {
const msg = {
error: 'Plugin not initialized',
};
this.logger.error('POST /v1/email_contents : %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_contents : %s', JSON.stringify(msg));
return res.status(500).json(msg);
}
else {
this.logger.debug(`POST /v1/email_contents ${JSON.stringify(req.body)}`);
const emailRenderRequest = req.body;
if (!this.onEmailContents) {
const errMsg = 'No Email Renderer listener registered!';
this.logger.error(errMsg);
return res.status(500).send({ error: errMsg });
}
// We flush the Plugin Gateway cache during previews
const forceReload = emailRenderRequest.context === 'PREVIEW' || emailRenderRequest.context === 'STAGE';
const instanceContext = await this.getInstanceContext(emailRenderRequest.creative_id, forceReload);
const response = await this.onEmailContents(emailRenderRequest, instanceContext);
this.logger.debug(`Returning: ${JSON.stringify(response)}`);
return res.status(200).send(JSON.stringify(response));
}
}));
}
}
exports.EmailRendererPlugin = EmailRendererPlugin;
//# sourceMappingURL=EmailRendererBasePlugin.js.map