@openrtb/macro-replacer
Version:
TypeScript macro replacer library compliant with OpenRTB 2.6/3.0
2 lines (1 loc) • 2.39 kB
JavaScript
;var c=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var I=(a,r)=>{for(var o in r)c(a,o,{get:r[o],enumerable:!0})},p=(a,r,o,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of C(r))!x.call(a,n)&&n!==o&&c(a,n,{get:()=>r[n],enumerable:!(i=M(r,n))||i.enumerable});return a};var _=a=>p(c({},"__esModule",{value:!0}),a);var T={};I(T,{MacroReplacer:()=>s});module.exports=_(T);var s=class{macroMap;context;version="both";constructor(r={},o="both"){this.context=r,this.version=o,this.macroMap=this.initializeMacroMap()}initializeMacroMap(){let r=new Map,o=t=>t?.toString()??"",i={"${OPENRTB_ID}":t=>o(t.id),"${OPENRTB_BID_ID}":t=>o(t.bidId),"${OPENRTB_ITEM_ID}":t=>o(t.itemId),"${OPENRTB_SEAT_ID}":t=>o(t.seatId),"${OPENRTB_PRICE}":t=>o(t.price),"${OPENRTB_CURRENCY}":t=>o(t.currency),"${OPENRTB_MBR}":t=>o(t.mbr),"${OPENRTB_LOSS}":t=>o(t.loss),"${OPENRTB_MIN_TO_WIN}":t=>o(t.minToWin),"${AUCTION_ID}":t=>o(t.id),"${AUCTION_BID_ID}":t=>o(t.bidId),"${AUCTION_IMP_ID}":t=>o(t.itemId),"${AUCTION_SEAT_ID}":t=>o(t.seatId),"${AUCTION_PRICE}":t=>o(t.price),"${AUCTION_CURRENCY}":t=>o(t.currency),"${AUCTION_MBR}":t=>o(t.mbr),"${AUCTION_LOSS}":t=>o(t.loss),"${AUCTION_MIN_TO_WIN}":t=>o(t.minToWin)},n={openrtb:{"${OPENRTB_MEDIA_ID}":t=>o(t.mediaId),"${OPENRTB_ITEM_QTY}":t=>o(t.itemQty)},auction:{"${AUCTION_AD_ID}":t=>o(t.adId),"${AUCTION_MULTIPLIER}":t=>o(t.multiplier),"${AUCTION_IMP_TS}":t=>o(t.impTs)}};return(this.version==="both"||this.version==="3")&&Object.entries(i).forEach(([t,e])=>{t.startsWith("${OPENRTB_")&&r.set(t,e)}),(this.version==="both"||this.version==="2")&&Object.entries(i).forEach(([t,e])=>{t.startsWith("${AUCTION_")&&r.set(t,e)}),(this.version==="both"||this.version==="3")&&Object.entries(n.openrtb).forEach(([t,e])=>r.set(t,e)),(this.version==="both"||this.version==="2")&&Object.entries(n.auction).forEach(([t,e])=>r.set(t,e)),r}updateContext(r){this.context={...this.context,...r}}replace(r,o){let i=r;for(let[n,t]of this.macroMap)try{let e=t({...this.context,...o});i=i.replace(new RegExp(n.replace(/\$/g,"\\$"),"g"),e)}catch(e){if(e instanceof Error)throw new Error(`Error replacing macro ${n}: ${e.message}`)}return i}getSupportedMacros(){return Array.from(this.macroMap.keys())}getContext(){return{...this.context}}};0&&(module.exports={MacroReplacer});