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 (110 loc) 6.05 kB
"use strict"; 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 AdRendererTemplatePlugin extends index_1.AdRendererBasePlugin { /** * Helper to fetch the content of a template * @param templatePath The raw (e.g. non URL encoded) mics URI to the template file as a string. * @returns A Buffer with the file content in it. This have to be decoded with the proper encoding. */ fetchTemplateContent(templatePath) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { const templateContent = yield _super("fetchDataFile").call(this, templatePath); this.logger.debug(`Fetched template : ${templateContent}`); return templateContent; }); } /** * Helper to fetch the properties of a template */ fetchTemplateProperties(organisationId, adLayoutId, versionId) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { const templateProperties = yield _super("requestGatewayHelper").call(this, "GET", `${this .outboundPlatformUrl}/v1/ad_layouts/${adLayoutId}/versions/${versionId}?organisation_id=${organisationId}`); return templateProperties; }); } instanceContextBuilder(creativeId, template) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { const baseInstanceContext = yield _super("instanceContextBuilder").call(this, creativeId); const urlProperty = _.find(baseInstanceContext.displayAdProperties, p => p.property_type === "URL"); if (!urlProperty) { const msg = `crid: ${creativeId} - url property is undefined`; this.logger.warn(msg); } const IASProperty = _.find(baseInstanceContext.displayAdProperties, p => p.technical_name === "ias_client_id"); const additionalHTMLProperty = _.find(baseInstanceContext.displayAdProperties, p => p.technical_name === "additional_html"); // If no 'predefined' template was provided, we retrieve it from the platform if (!template) { const adLayoutProperty = _.find(baseInstanceContext.displayAdProperties, p => p.property_type === "AD_LAYOUT"); if (!adLayoutProperty || !adLayoutProperty.value || !adLayoutProperty.value.id || !adLayoutProperty.value.version) { const msg = `crid: ${creativeId} - Ad layout undefined`; this.logger.error(msg); throw new Error(msg); } const templateProperties = yield this.fetchTemplateProperties(baseInstanceContext.displayAd.organisation_id, adLayoutProperty.value.id, adLayoutProperty.value.version); this.logger.info(`crid: ${creativeId} - Loaded template properties ${adLayoutProperty.value.id} ${adLayoutProperty.value.version} => ${JSON.stringify(templateProperties)}`); const templatePath = templateProperties.data.template; // We assume that the template is in UTF-8 template = (yield this.fetchTemplateContent(templatePath)).toString("utf8"); this.logger.info(`crid: ${creativeId} - Loaded template content ${templatePath} => ${JSON.stringify(template)}`); } if (!this.engineBuilder) { throw new Error(`No engine builder have been added to the plugin An engine builder is mandatory to extend this plugin class`); } this.engineBuilder.init(); const compiledTemplate = this.engineBuilder.compile(template); const creativeClickUrl = urlProperty && urlProperty.value.url ? urlProperty.value.url : undefined; const compiledClickUrl = creativeClickUrl ? this.engineBuilder.compile(creativeClickUrl) : undefined; const additionalHTML = additionalHTMLProperty && additionalHTMLProperty.value && additionalHTMLProperty.value.value ? this.engineBuilder.compile(additionalHTMLProperty.value .value) : undefined; const IASClientId = IASProperty && IASProperty.value && IASProperty.value.value ? IASProperty.value.value : undefined; const width = baseInstanceContext.displayAd.format.split("x")[0]; const height = baseInstanceContext.displayAd.format.split("x")[1]; const context = { displayAd: baseInstanceContext.displayAd, displayAdProperties: baseInstanceContext.displayAdProperties, width: width, height: height, creative_click_url: creativeClickUrl, compiled_click_url: compiledClickUrl, template: template, compiled_template: compiledTemplate, compiled_additional_html: additionalHTML, ias_client_id: IASClientId }; return context; }); } constructor() { super(); } } exports.AdRendererTemplatePlugin = AdRendererTemplatePlugin;