@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
96 lines (95 loc) • 4.99 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 AdRendererBasePlugin extends index_1.BasePlugin {
constructor() {
super();
this.displayContextHeader = "x-mics-display-context";
this.initAdContentsRoute();
this.setErrorHandler();
}
// Helper to fetch the Display Ad resource with caching
fetchDisplayAd(displayAdId) {
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/${displayAdId}`);
this.logger.debug(`Fetched Creative: ${displayAdId} - ${JSON.stringify(response.data)}`);
if (response.data.type !== "DISPLAY_AD") {
throw new Error(`crid: ${displayAdId} - When fetching DisplayAd, another creative type was returned!`);
}
return response.data;
});
}
// Helper to fetch the Display Ad properties resource with caching
fetchDisplayAdProperties(displayAdId) {
const _super = name => super[name];
return __awaiter(this, void 0, void 0, function* () {
const creativePropertyResponse = yield _super("requestGatewayHelper").call(this, "GET", `${this.outboundPlatformUrl}/v1/creatives/${displayAdId}/renderer_properties`);
this.logger.debug(`Fetched Creative Properties: ${displayAdId} - ${JSON.stringify(creativePropertyResponse.data)}`);
return creativePropertyResponse.data;
});
}
getEncodedClickUrl(redirectUrls) {
let urls = redirectUrls.slice(0);
return urls.reduceRight((acc, current) => current + encodeURIComponent(acc), "");
}
// Method to build an instance context
// To be overriden to get a custom behavior
instanceContextBuilder(creativeId) {
return __awaiter(this, void 0, void 0, function* () {
const displayAdP = this.fetchDisplayAd(creativeId);
const displayAdPropsP = this.fetchDisplayAdProperties(creativeId);
const results = yield Promise.all([displayAdP, displayAdPropsP]);
const displayAd = results[0];
const displayAdProps = results[1];
const context = {
displayAd: displayAd,
displayAdProperties: displayAdProps
};
return Promise.resolve(context);
});
}
initAdContentsRoute() {
this.app.post("/v1/ad_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/ad_contents : %s", JSON.stringify(msg));
return res.status(500).json(msg);
}
else {
this.logger.debug(`POST /v1/ad_contents ${JSON.stringify(req.body)}`);
const adRendererRequest = req.body;
if (!this.onAdContents) {
this.logger.error("POST /v1/ad_contents: No AdContents listener registered!");
const msg = {
error: "No AdContents listener registered!"
};
return res.status(500).json(msg);
}
if (!this.pluginCache.get(adRendererRequest.creative_id) ||
adRendererRequest.context === "PREVIEW" ||
adRendererRequest.context === "STAGE") {
this.pluginCache.put(adRendererRequest.creative_id, this.instanceContextBuilder(adRendererRequest.creative_id), this.INSTANCE_CONTEXT_CACHE_EXPIRATION);
}
const instanceContext = yield this.pluginCache.get(adRendererRequest.creative_id);
const adRendererResponse = yield this.onAdContents(adRendererRequest, instanceContext);
return res
.header(this.displayContextHeader, JSON.stringify(adRendererResponse.displayContext))
.status(200)
.send(adRendererResponse.html);
}
})));
}
}
exports.AdRendererBasePlugin = AdRendererBasePlugin;