UNPKG

@nextad/bid-requester

Version:

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

2 lines (1 loc) 2.83 kB
"use strict";var p=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var l=(i,t)=>{for(var n in t)p(i,n,{get:t[n],enumerable:!0})},h=(i,t,n,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of g(t))!m.call(i,r)&&r!==n&&p(i,r,{get:()=>t[r],enumerable:!(e=u(t,r))||e.enumerable});return i};var R=i=>h(p({},"__esModule",{value:!0}),i);var b={};l(b,{BidRequester:()=>d,InvalidBidRequestException:()=>a,NoBidResponseException:()=>c,UnexpectedError:()=>s,bidRequester:()=>q});module.exports=R(b);var a=class extends Error{name="InvalidBidRequestException";constructor(t){super(`Invalid bid request: ${t||"required parameters are missing or malformed."}`)}},c=class extends Error{name="NoBidResponseException";message="No bid response received from the auction."},s=class extends Error{};var o=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 c:e.status===400?new a(await e.text()):new s(`Unexpected HTTP response: received status code ${e.status}`)}catch(n){throw n instanceof Error?n:new s("Unexpected error")}}};var d=class{options;constructor(t={}){this.options=t}async requestV25(t,n,e){return e={...this.options,...e},new o({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 o({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 o({version:"3.0",endpoint:t,dataFormat:e.dataFormat,acceptEncoding:e.acceptEncoding,contentEncoding:e.contentEncoding,customHeaders:e.customHeaders,withCredentials:e.withCredentials}).request(n)}},q=new d;0&&(module.exports={BidRequester,InvalidBidRequestException,NoBidResponseException,UnexpectedError,bidRequester});