-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchrequest_js.min.js
1 lines (1 loc) · 2.05 KB
/
batchrequest_js.min.js
1
function Do(object){return new BatchRequest(object).Do()}!function(r){var BatchRequest;BatchRequest=function(){var createRequest,parser;class BatchRequest{constructor(p_){var bP,batchPath;if(!p_.hasOwnProperty("requests"))throw new Error("'requests' property was not found in object.");if(!p_.hasOwnProperty("accessToken"))throw new Error("Please set the access token.");this.p=p_.requests,this.url="https://www.googleapis.com/batch",p_.batchPath&&(batchPath="",batchPath=(bP=p_.batchPath.trim()).includes("batch/")?bP.replace("batch",""):"/"===bP.slice(0,1)?bP:"/"+bP,this.url+=batchPath),this.at=p_.accessToken,this.lb="\r\n",this.boundary="xxxxxxxxxx"}Do(){var e,i,j,limit,ref,reqs,split;try{for(limit=100,split=Math.ceil(this.p.length/100),reqs=[],i=j=0,ref=split;0<=ref?j<ref:j>ref;i=0<=ref?++j:--j)reqs.push({url:this.url,params:createRequest.call(this,this.p.splice(0,100))});return Promise.all(reqs.map(({url:url,params:params},i)=>new Promise((resolve,reject)=>fetch(url,params).then(res=>res.text()).then(res=>resolve({raw:res,parsed:parser.call(this,res)})).catch(err=>reject(err)))))}catch(error){throw new Error(e=error)}}}return BatchRequest.name="BatchRequest_js",parser=function(d_){var regex,temp;return temp=d_.split("--batch"),regex=/{[\S\s]+}/g,temp.slice(1,temp.length-1).map((function(e){return regex.test(e)?JSON.parse(e.match(regex)[0]):e}))},createRequest=function(d_){var contentId,data,e,params;try{contentId=0,params={method:"POST",body:data=d_.reduce((s,e)=>(s+="Content-Type: application/http"+this.lb,s+="Content-ID: "+ ++contentId+this.lb+this.lb,s+=e.method+" "+e.endpoint+this.lb,s+=e.accessToken?"Authorization: Bearer "+e.accessToken+this.lb:"",s+=e.requestBody?"Content-Type: application/json; charset=utf-8"+this.lb+this.lb:this.lb,s+=e.requestBody?JSON.stringify(e.requestBody)+this.lb:"",s+="--"+this.boundary+this.lb),"--"+this.boundary+this.lb),headers:{Authorization:"Bearer "+this.at,"Content-Type":"multipart/mixed; boundary="+this.boundary}}}catch(error){throw new Error(e=error)}return params},BatchRequest}.call(this),r.BatchRequest=BatchRequest}(this);