UNPKG

@nextad/bid-requester

Version:

TypeScript bid requester library compliant with OpenRTB 2.5/2.6/3.0

2 lines (1 loc) 2.26 kB
var o=class extends Error{name="InvalidBidRequestException";constructor(t){super(`Invalid bid request: ${t||"required parameters are missing or malformed."}`)}},a=class extends Error{name="NoBidResponseException";message="No bid response received from the auction."},r=class extends Error{};var i=class{endpoint;dataFormat;acceptEncoding;contentEncoding;cacheControl;version;customHeaders;withCredentials;constructor(t){this.endpoint=t.endpoint,this.dataFormat=t.dataFormat||"application/json",this.acceptEncoding=t.acceptEncoding||"gzip",this.contentEncoding=t.contentEncoding||"*",this.cacheControl=t.cacheControl||"no-store",this.version=t.version,this.customHeaders=t.customHeaders,this.withCredentials=t.withCredentials===!0}async request(t){try{let n={method:"POST",body:JSON.stringify(t),cache:this.cacheControl,headers:{...this.customHeaders,"Content-Type":this.dataFormat,"Accept-Encoding":this.acceptEncoding,"Content-Encoding":this.contentEncoding,"x-openrtb-version":this.version}};this.withCredentials&&(n.credentials="include");let e=await fetch(this.endpoint,n);if(e.status===200)return e.json();throw e.status===204?new a:e.status===400?new o(await e.text()):new r(`Unexpected HTTP response: received status code ${e.status}`)}catch(n){throw n instanceof Error?n:new r("Unexpected error")}}};var c=class{options;constructor(t={}){this.options=t}async requestV25(t,n,e){return e={...this.options,...e},new i({version:"2.5",endpoint:t,dataFormat:e.dataFormat,acceptEncoding:e.acceptEncoding,contentEncoding:e.contentEncoding,customHeaders:e.customHeaders,withCredentials:e.withCredentials}).request(n)}async requestV26(t,n,e){return e={...this.options,...e},new i({version:"2.6",endpoint:t,dataFormat:e.dataFormat,acceptEncoding:e.acceptEncoding,contentEncoding:e.contentEncoding,customHeaders:e.customHeaders,withCredentials:e.withCredentials}).request(n)}async requestV30(t,n,e){return e={...this.options,...e},new i({version:"3.0",endpoint:t,dataFormat:e.dataFormat,acceptEncoding:e.acceptEncoding,contentEncoding:e.contentEncoding,customHeaders:e.customHeaders,withCredentials:e.withCredentials}).request(n)}},l=new c;export{c as BidRequester,o as InvalidBidRequestException,a as NoBidResponseException,r as UnexpectedError,l as bidRequester};