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

102 lines 5.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AdRendererBasePlugin = exports.AdRendererBaseInstanceContext = void 0; const jsesc_1 = __importDefault(require("jsesc")); const lodash_1 = __importDefault(require("lodash")); const BasePlugin_1 = require("../../common/BasePlugin"); const index_1 = require("../utils/index"); class AdRendererBaseInstanceContext { } exports.AdRendererBaseInstanceContext = AdRendererBaseInstanceContext; class AdRendererBasePlugin extends BasePlugin_1.BasePlugin { constructor(enableThrottling = false) { super(enableThrottling); this.displayContextHeader = 'x-mics-display-context'; this.initAdContentsRoute(); this.setErrorHandler(); } // Helper to fetch the Display Ad resource with caching async fetchDisplayAd(displayAdId, forceReload = false) { const response = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/creatives/${displayAdId}`, undefined, { 'force-reload': forceReload }); 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 async fetchDisplayAdProperties(displayAdId, forceReload = false) { const creativePropertyResponse = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/creatives/${displayAdId}/renderer_properties`, undefined, { 'force-reload': forceReload }); this.logger.debug(`Fetched Creative Properties: ${displayAdId} - ${JSON.stringify(creativePropertyResponse.data)}`); return creativePropertyResponse.data; } // Method to build an instance context getEncodedClickUrl(redirectUrls) { return (0, index_1.generateEncodedClickUrl)(redirectUrls); } // To be overriden to get a custom behavior async instanceContextBuilder(creativeId, forceReload = false) { const displayAdP = this.fetchDisplayAd(creativeId, forceReload); const displayAdPropsP = this.fetchDisplayAdProperties(creativeId, forceReload); const results = await Promise.all([displayAdP, displayAdPropsP]); const displayAd = results[0]; const displayAdProps = results[1]; const context = { displayAd: displayAd, properties: new BasePlugin_1.PropertiesWrapper(displayAdProps), }; return Promise.resolve(context); } async getInstanceContext(creativeId, forceReload) { 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); } initAdContentsRoute() { this.app.post('/v1/ad_contents', this.asyncMiddleware(async (req, res) => { if (!this.httpIsReady()) { const msg = { error: 'Plugin not initialized', }; this.logger.error('POST /v1/ad_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/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); } // We flush the Plugin Gateway cache during previews const forceReload = adRendererRequest.context === 'PREVIEW' || adRendererRequest.context === 'STAGE'; const instanceContext = await this.getInstanceContext(adRendererRequest.creative_id, forceReload); const adRendererResponse = await this.onAdContents(adRendererRequest, instanceContext); return res .header(this.displayContextHeader, (0, jsesc_1.default)(adRendererResponse.displayContext, { json: true })) .status(200) .send(adRendererResponse.html); } })); } } exports.AdRendererBasePlugin = AdRendererBasePlugin; //# sourceMappingURL=AdRendererBasePlugin.js.map