Build 586984stable
Module 191203
6,561 characters · in 52082.07941602b067c1b4.jsDependencies
Source Raw
function(t,e,r){"use strict";var o=r(72290);let n=r(159093),i=r(17101),a=i.isObject,s=i.hasOwn;function l(){}t.exports=l,l.prototype.clearTimeout=function(){return clearTimeout(this._timer),clearTimeout(this._responseTimeoutTimer),clearTimeout(this._uploadTimeoutTimer),delete this._timer,delete this._responseTimeoutTimer,delete this._uploadTimeoutTimer,this},l.prototype.parse=function(t){return this._parser=t,this},l.prototype.responseType=function(t){return this._responseType=t,this},l.prototype.serialize=function(t){return this._serializer=t,this},l.prototype.timeout=function(t){if(!t||"object"!=typeof t)return this._timeout=t,this._responseTimeout=0,this._uploadTimeout=0,this;for(let e in t)if(s(t,e))switch(e){case"deadline":this._timeout=t.deadline;break;case"response":this._responseTimeout=t.response;break;case"upload":this._uploadTimeout=t.upload;break;default:console.warn("Unknown timeout option",e)}return this},l.prototype.retry=function(t,e){return(0==arguments.length||!0===t)&&(t=1),t<=0&&(t=0),this._maxRetries=t,this._retries=0,this._retryCallback=e,this};let p=new Set(["ETIMEDOUT","ECONNRESET","EADDRINUSE","ECONNREFUSED","EPIPE","ENOTFOUND","ENETUNREACH","EAI_AGAIN"]),c=new Set([408,413,429,500,502,503,504,521,522,524]);l.prototype._shouldRetry=function(t,e){if(!this._maxRetries||this._retries++>=this._maxRetries)return!1;if(this._retryCallback)try{let r=this._retryCallback(t,e);if(!0===r)return!0;if(!1===r)return!1}catch(t){console.error(t)}return!!(e&&e.status&&c.has(e.status)||t&&(t.code&&p.has(t.code)||t.timeout&&"ECONNABORTED"===t.code||t.crossDomain))||!1},l.prototype._retry=function(){return this.clearTimeout(),this.req&&(this.req=null,this.req=this.request()),this._aborted=!1,this.timedout=!1,this.timedoutError=null,this._end()},l.prototype.then=function(t,e){if(!this._fullfilledPromise){let t=this;this._endCalled&&console.warn("Warning: superagent request was sent twice, because both .end() and .then() were called. Never call .end() if you use promises"),this._fullfilledPromise=new Promise((e,r)=>{t.on("abort",()=>{if(this._maxRetries&&this._maxRetries>this._retries)return;if(this.timedout&&this.timedoutError)return void r(this.timedoutError);let t=Error("Aborted");t.code="ABORTED",t.status=this.status,t.method=this.method,t.url=this.url,r(t)}),t.end((t,o)=>{t?r(t):e(o)})})}return this._fullfilledPromise.then(t,e)},l.prototype.catch=function(t){return this.then(void 0,t)},l.prototype.use=function(t){return t(this),this},l.prototype.ok=function(t){if("function"!=typeof t)throw Error("Callback required");return this._okCallback=t,this},l.prototype._isResponseOK=function(t){return!!t&&(this._okCallback?this._okCallback(t):t.status>=200&&t.status<300)},l.prototype.get=function(t){return this._header[t.toLowerCase()]},l.prototype.getHeader=l.prototype.get,l.prototype.set=function(t,e){if(a(t)){for(let e in t)s(t,e)&&this.set(e,t[e]);return this}return this._header[t.toLowerCase()]=e,this.header[t]=e,this},l.prototype.unset=function(t){return delete this._header[t.toLowerCase()],delete this.header[t],this},l.prototype.field=function(t,e,r){if(null==t)throw Error(".field(name, val) name can not be empty");if(this._data)throw Error(".field() can't be used if .send() is used. Please use only .send() or only .field() & .attach()");if(a(t)){for(let e in t)s(t,e)&&this.field(e,t[e]);return this}if(Array.isArray(e)){for(let r in e)s(e,r)&&this.field(t,e[r]);return this}if(null==e)throw Error(".field(name, val) val can not be empty");return"boolean"==typeof e&&(e=String(e)),r?this._getFormData().append(t,e,r):this._getFormData().append(t,e),this},l.prototype.abort=function(){if(this._aborted)return this;if(this._aborted=!0,this.xhr&&this.xhr.abort(),this.req){if(n.gte(o.version,"v13.0.0")&&n.lt(o.version,"v14.0.0"))throw Error("Superagent does not work in v13 properly with abort() due to Node.js core changes");this.req.abort()}return this.clearTimeout(),this.emit("abort"),this},l.prototype._auth=function(t,e,r,o){switch(r.type){case"basic":this.set("Authorization",`Basic ${o(`${t}:${e}`)}`);break;case"auto":this.username=t,this.password=e;break;case"bearer":this.set("Authorization",`Bearer ${t}`)}return this},l.prototype.withCredentials=function(t){return void 0===t&&(t=!0),this._withCredentials=t,this},l.prototype.redirects=function(t){return this._maxRedirects=t,this},l.prototype.maxResponseSize=function(t){if("number"!=typeof t)throw TypeError("Invalid argument");return this._maxResponseSize=t,this},l.prototype.toJSON=function(){return{method:this.method,url:this.url,data:this._data,headers:this._header}},l.prototype.send=function(t){let e=a(t),r=this._header["content-type"];if(this._formData)throw Error(".send() can't be used if .attach() or .field() is used. Please use only .send() or only .field() & .attach()");if(e&&!this._data)Array.isArray(t)?this._data=[]:this._isHost(t)||(this._data={});else if(t&&this._data&&this._isHost(this._data))throw Error("Can't merge these send calls");if(e&&a(this._data))for(let e in t){if("bigint"==typeof t[e]&&!t[e].toJSON)throw Error("Cannot serialize BigInt value to json");s(t,e)&&(this._data[e]=t[e])}else if("bigint"==typeof t)throw Error("Cannot send value of type BigInt");else"string"==typeof t?(r||this.type("form"),(r=this._header["content-type"])&&(r=r.toLowerCase().trim()),"application/x-www-form-urlencoded"===r?this._data=this._data?`${this._data}&${t}`:t:this._data=(this._data||"")+t):this._data=t;return!e||this._isHost(t)||r||this.type("json"),this},l.prototype.sortQuery=function(t){return this._sort=void 0===t||t,this},l.prototype._finalizeQueryString=function(){let t=this._query.join("&");if(t&&(this.url+=(this.url.includes("?")?"&":"?")+t),this._query.length=0,this._sort){let t=this.url.indexOf("?");if(t>=0){let e=this.url.slice(t+1).split("&");"function"==typeof this._sort?e.sort(this._sort):e.sort(),this.url=this.url.slice(0,t)+"?"+e.join("&")}}},l.prototype._appendQueryString=()=>{console.warn("Unsupported")},l.prototype._timeoutError=function(t,e,r){if(this._aborted)return;let o=Error(`${t+e}ms exceeded`);o.timeout=e,o.code="ECONNABORTED",o.errno=r,this.timedout=!0,this.timedoutError=o,this.abort(),this.callback(o)},l.prototype._setTimeouts=function(){let t=this;this._timeout&&!this._timer&&(this._timer=setTimeout(()=>{t._timeoutError("Timeout of ",t._timeout,"ETIME")},this._timeout)),this._responseTimeout&&!this._responseTimeoutTimer&&(this._responseTimeoutTimer=setTimeout(()=>{t._timeoutError("Response timeout of ",t._responseTimeout,"ETIMEDOUT")},this._responseTimeout))}}