fetch.min.js 6.2 KB
(function(){if(!self.__disableNativeFetch&&self.fetch){return}function a(q){if(typeof q!=="string"){q=String(q)}if(/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(q)){throw new TypeError("Invalid character in header field name")}return q.toLowerCase()}function e(q){if(typeof q!=="string"){q=String(q)}return q}function j(q){this.map={};if(q instanceof j){q.forEach(function(s,r){this.append(r,s)},this)}else{if(q){Object.getOwnPropertyNames(q).forEach(function(r){this.append(r,q[r])},this)}}}j.prototype.append=function(q,s){q=a(q);s=e(s);var r=this.map[q];if(!r){r=[];this.map[q]=r}r.push(s)};j.prototype["delete"]=function(q){delete this.map[a(q)]};j.prototype.get=function(q){var r=this.map[a(q)];return r?r[0]:null};j.prototype.getAll=function(q){return this.map[a(q)]||[]};j.prototype.has=function(q){return this.map.hasOwnProperty(a(q))};j.prototype.set=function(q,r){this.map[a(q)]=[e(r)]};j.prototype.forEach=function(r,q){Object.getOwnPropertyNames(this.map).forEach(function(s){this.map[s].forEach(function(t){r.call(q,t,s,this)},this)},this)};function n(q){if(q.bodyUsed){return Promise.reject(new TypeError("Already read"))}q.bodyUsed=true}function o(q){return new Promise(function(s,r){q.onload=function(){s(q.result)};q.onerror=function(){r(q.error)}})}function d(q){var r=new FileReader();r.readAsArrayBuffer(q);return o(r)}function p(r){var t=new FileReader();var q=r.type.match(/charset\=[0-9a-zA-Z\-\_]*;?/);var s=[r];if(q){s.push(q[0].replace(/^charset\=/,"").replace(/;$/,""))}t.readAsText.apply(t,s);return o(t)}var l={blob:"FileReader" in self&&"Blob" in self&&(function(){try{new Blob();return true}catch(q){return false}})(),formData:"FormData" in self,arrayBuffer:"ArrayBuffer" in self};function k(){this.bodyUsed=false;this._initBody=function(q){this._bodyInit=q;if(typeof q==="string"){this._bodyText=q}else{if(l.blob&&Blob.prototype.isPrototypeOf(q)){this._bodyBlob=q}else{if(l.formData&&FormData.prototype.isPrototypeOf(q)){this._bodyFormData=q}else{if(!q){this._bodyText=""}else{if(l.arrayBuffer&&ArrayBuffer.prototype.isPrototypeOf(q)){}else{throw new Error("unsupported BodyInit type")}}}}}};if(l.blob){this.blob=function(){var q=n(this);if(q){return q}if(this._bodyBlob){return Promise.resolve(this._bodyBlob)}else{if(this._bodyFormData){throw new Error("could not read FormData body as blob")}else{return Promise.resolve(new Blob([this._bodyText]))}}};this.arrayBuffer=function(){return this.blob().then(d)};this.text=function(){var q=n(this);if(q){return q}if(this._bodyBlob){return p(this._bodyBlob)}else{if(this._bodyFormData){throw new Error("could not read FormData body as text")}else{return Promise.resolve(this._bodyText)}}}}else{this.text=function(){var q=n(this);return q?q:Promise.resolve(this._bodyText)}}if(l.formData){this.formData=function(){return this.text().then(h)}}this.json=function(){if(window.JSON&&window.JSON.parse){return this.text().then(function(q){return window.JSON.parse(q)})}else{return this.text().then(function(q){return(new Function("return "+q))()})}};return this}var c=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];function i(r){var q=r.toUpperCase();return(c.indexOf(q)>-1)?q:r}function g(r,s){s=s||{};var q=s.body;if(g.prototype.isPrototypeOf(r)){if(r.bodyUsed){throw new TypeError("Already read")}this.url=r.url;this.credentials=r.credentials;if(!s.headers){this.headers=new j(r.headers)}this.method=r.method;this.mode=r.mode;if(!q){q=r._bodyInit;r.bodyUsed=true}}else{this.url=r}this.credentials=s.credentials||this.credentials||"omit";if(s.headers||!this.headers){this.headers=new j(s.headers)}this.method=i(s.method||this.method||"GET");this.mode=s.mode||this.mode||null;this.referrer=null;if((this.method==="GET"||this.method==="HEAD")&&q){throw new TypeError("Body not allowed for GET or HEAD requests")}this._initBody(q)}g.prototype.clone=function(){return new g(this)};function h(q){var r=new FormData();q.trim().split("&").forEach(function(s){if(s){var u=s.split("=");var t=u.shift().replace(/\+/g," ");var v=u.join("=").replace(/\+/g," ");r.append(decodeURIComponent(t),decodeURIComponent(v))}});return r}function b(s){var q=new j();var r=s.getAllResponseHeaders().trim().split("\n");r.forEach(function(w){var u=w.trim().split(":");var t=u.shift().trim();var v=u.join(":").trim();q.append(t,v)});return q}k.call(g.prototype);function f(r,q){if(!q){q={}}this._initBody(r);this.type="default";this.status=q.status;this.ok=this.status>=200&&this.status<300;this.statusText=q.statusText;this.headers=q.headers instanceof j?q.headers:new j(q.headers);this.url=q.url||""}k.call(f.prototype);f.prototype.clone=function(){return new f(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new j(this.headers),url:this.url})};f.error=function(){var q=new f(null,{status:0,statusText:""});q.type="error";return q};var m=[301,302,303,307,308];f.redirect=function(q,r){if(m.indexOf(r)===-1){throw new RangeError("Invalid status code")}return new f(null,{status:r,headers:{location:q}})};self.Headers=j;self.Request=g;self.Response=f;self.fetch=function(q,r){return new Promise(function(v,s){var u;if(g.prototype.isPrototypeOf(q)&&!r){u=q}else{u=new g(q,r)}var y=new XMLHttpRequest();function x(){if("responseURL" in y){return y.responseURL}if(/^X-Request-URL:/m.test(y.getAllResponseHeaders())){return y.getResponseHeader("X-Request-URL")}return}var t=false;function z(){if(y.readyState!==4){return}var C=(y.status===1223)?204:y.status;if(C<100||C>599){if(t){return}else{t=true}s(new TypeError("Network request failed"));return}var B={status:C,statusText:y.statusText,headers:b(y),url:x()};var A="response" in y?y.response:y.responseText;if(t){return}else{t=true}v(new f(A,B))}y.onreadystatechange=z;y.onload=z;y.onerror=function(){if(t){return}else{t=true}s(new TypeError("Network request failed"))};y.open(u.method,u.url,true);try{if(u.credentials==="include"){if("withCredentials" in y){y.withCredentials=true}else{console&&console.warn&&console.warn("withCredentials is not supported, you can ignore this warning")}}}catch(w){console&&console.warn&&console.warn("set withCredentials error:"+w)}if("responseType" in y&&l.blob){y.responseType="blob"}u.headers.forEach(function(B,A){y.setRequestHeader(A,B)});y.send(typeof u._bodyInit==="undefined"?null:u._bodyInit)})};self.fetch.polyfill=true;if(typeof module!=="undefined"&&module.exports){module.exports=fetch}})();