@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
83 lines (82 loc) • 4.23 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const index_1 = require("../../../index");
class EmailRendererPlugin extends index_1.BasePlugin {
// Helper to fetch the creative resource with caching
fetchCreative(id) {
const _super = name => super[name];
return __awaiter(this, void 0, void 0, function* () {
const response = yield _super("requestGatewayHelper").call(this, "GET", `${this.outboundPlatformUrl}/v1/creatives/${id}`);
this.logger.debug(`Fetched Creative: ${id} - ${JSON.stringify(response.data)}`);
return response.data;
});
}
fetchCreativeProperties(id) {
const _super = name => super[name];
return __awaiter(this, void 0, void 0, function* () {
const response = yield _super("requestGatewayHelper").call(this, "GET", `${this.outboundPlatformUrl}/v1/creatives/${id}/renderer_properties`);
this.logger.debug(`Fetched Creative Properties: ${id} - ${JSON.stringify(response.data)}`);
return response.data;
});
}
// Method to build an instance context
// To be overriden to get a cutom behavior
// This is a default provided implementation
instanceContextBuilder(creativeId) {
return __awaiter(this, void 0, void 0, function* () {
const creativeP = this.fetchCreative(creativeId);
const creativePropsP = this.fetchCreativeProperties(creativeId);
const results = yield Promise.all([creativeP, creativePropsP]);
const creative = results[0];
const creativeProps = results[1];
const context = {
creative: creative,
creativeProperties: creativeProps
};
return context;
});
}
initEmailContents() {
this.app.post("/v1/email_contents", this.asyncMiddleware((req, res) => __awaiter(this, void 0, void 0, function* () {
if (!req.body || _.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 });
}
if (!this.pluginCache.get(emailRenderRequest.email_renderer_id)) {
this.pluginCache.put(emailRenderRequest.email_renderer_id, this.instanceContextBuilder(emailRenderRequest.email_renderer_id), this.INSTANCE_CONTEXT_CACHE_EXPIRATION);
}
const instanceContext = yield this.pluginCache.get(emailRenderRequest.email_renderer_id);
const response = yield this.onEmailContents(emailRenderRequest, instanceContext);
this.logger.debug(`Returning: ${JSON.stringify(response)}`);
return res.status(200).send(JSON.stringify(response));
}
})));
}
constructor() {
super();
// We init the specific route to listen for email contents requests
this.initEmailContents();
this.setErrorHandler();
}
}
exports.EmailRendererPlugin = EmailRendererPlugin;