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

62 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AdRendererTemplatePlugin = void 0; const utils_1 = require("../../../utils"); const AdRendererBasePlugin_1 = require("../base/AdRendererBasePlugin"); class AdRendererTemplatePlugin extends AdRendererBasePlugin_1.AdRendererBasePlugin { constructor(enableThrottling = false) { super(enableThrottling); } /** * Build a basic InstanceContext for "template" aware AdRenderer. * This method can be overriden by your implementation (and you can then still call it with `super.instanceContextBuilder(creativeId, forceReload)`) * * This instanceContext takes the hypothesis that: * - You have exactly one "URL" Plugin property on your instance * - You have one "STRING" Plugin property on your instance called "additional_html" that contains 'templateable' HTML * - You have one "STRING" Plugin property on your instance called "ias_client_id" that contains an IAS Client Id as a String * * If your Plugin instance don't respect those hypothesis, the returned InstanceContext will have `undefined` values in some/all fields. * * If you want to do Templating but you don't want to validate the above hypothesis, you're encouraged to build your Plugin Impl. by extending `AdRendererBasePlugin<AdRendererBaseInstanceContext>` * instead of this class. This class should then only be used as an example. * * @param creativeId * @param forceReload */ async instanceContextBuilder(creativeId, forceReload = false) { const baseInstanceContext = await super.instanceContextBuilder(creativeId, forceReload); 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 urlProperty = baseInstanceContext.properties.findUrlProperty(); if (!urlProperty) { const msg = `crid: ${creativeId} - url property is undefined`; this.logger.warn(msg); } const creativeClickUrl = (0, utils_1.map)(urlProperty, (p) => p.value.url); const compiledClickUrl = (0, utils_1.map)(creativeClickUrl, (url) => this.engineBuilder.compile(url)); const additionalHTMLProperty = baseInstanceContext.properties.findStringProperty('additional_html'); const additionalHTML = (0, utils_1.map)(additionalHTMLProperty, (p) => p.value.value); const compiledAdditionalHTML = (0, utils_1.map)(additionalHTML, (html) => this.engineBuilder.compile(html)); const IASProperty = baseInstanceContext.properties.findStringProperty('ias_client_id'); const IASClientId = (0, utils_1.map)(IASProperty, (p) => p.value.value); const width = baseInstanceContext.displayAd.format.split('x')[0]; const height = baseInstanceContext.displayAd.format.split('x')[1]; const context = { displayAd: baseInstanceContext.displayAd, properties: baseInstanceContext.properties, width: width, height: height, creative_click_url: creativeClickUrl, render_click_url: compiledClickUrl, render_additional_html: compiledAdditionalHTML, ias_client_id: IASClientId, }; return context; } } exports.AdRendererTemplatePlugin = AdRendererTemplatePlugin; //# sourceMappingURL=AdRendererTemplatePlugin.js.map