UNPKG

4.62 kBJavaScriptView Raw
1module.exports=function(t){function e(r){if(s[r])return s[r].exports;var n=s[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,e),n.l=!0,n.exports}var s={};return e.m=t,e.c=s,e.d=function(t,s,r){e.o(t,s)||Object.defineProperty(t,s,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var s=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(s,"a",s),s},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,s){t.exports=s(1)},function(t,e,s){const r="undefined"!=typeof window,n=s(2),o=s(3),i=s(r?4:5);class a extends i.Extension{constructor(t,e,s={}){super(),this.options=Object.assign({version:1,qs:n,followRedirects:!0},s),this.request=i.buildRequest.call(this,t,e,s),s.query&&this.query(s.query),s.data&&this.send(s.data)}query(t,e){if(this._checkModify(),this.request.query||(this.request.query={}),null!==t&&"object"==typeof t)for(const[e,s]of Object.entries(t))this.query(e,s);else this.request.query[t]=e;return this}set(t,e){if(this._checkModify(),null!==t&&"object"==typeof t)for(const e of Object.keys(t))this.set(e,t[e]);else this.request.setHeader(t,e);return this}attach(...t){this._checkModify();const e=this._getFormData();if("object"==typeof t[0])for(const[e,s]of Object.entries(t[0]))this.attach(e,s);else e.append(...t);return this}send(t){if(this._checkModify(),t instanceof i.FormData||i.shouldSendRaw(t))this.data=t;else if(null!==t&&"object"==typeof t){const e=this.request.getHeader("content-type");let s;e?e.includes("json")?s=JSON.stringify:e.includes("urlencoded")&&(s=this.options.qs.stringify):(this.set("Content-Type","application/json"),s=JSON.stringify),this.data=s(t)}else this.data=t;return this}then(t,e){return this._response?this._response.then(t,e):this._response=i.finalizeRequest.call(this).then(({response:t,raw:e,redirect:s,headers:r})=>{if(s){let e=this.request.method;[301,302].includes(t.statusCode)?("HEAD"!==e&&(e="GET"),this.data=null):303===t.statusCode&&(e="GET");const r=this.request.getHeaders();return delete r.host,new a(e,s,{data:this.data,headers:r})}const n=t.statusCode||t.status,o=this,h={request:this.request,get body(){delete h.body;const t=this.headers["content-type"];if(t&&t.includes("application/json"))try{h.body=JSON.parse(h.text)}catch(t){h.body=h.text}else t&&t.includes("application/x-www-form-urlencoded")?h.body=o.options.qs.parse(h.text):h.body=e;return h.body},text:e.toString(),ok:n>=200&&n<400,headers:r||t.headers,status:n,statusText:t.statusText||i.STATUS_CODES[t.statusCode]};if(h.ok)return h;{const t=new Error(`${h.status} ${h.statusText}`.trim());return Object.assign(t,h),Promise.reject(t)}}).then(t,e)}catch(t){return this.then(null,t)}end(t){return this.then(e=>t?t(null,e):e,e=>t?t(e,e.status?e:null):Promise.reject(e))}_getFormData(){return this.data instanceof i.FormData||(this.data=new i.FormData),this.data}_finalizeRequest(){if(this.request&&(this.request.getHeader("user-agent")||this.set("User-Agent",`snekfetch/${a.version} (${o.homepage})`),"HEAD"!==this.request.method&&this.set("Accept-Encoding","gzip, deflate"),this.data&&this.data.getBoundary&&this.set("Content-Type",`multipart/form-data; boundary=${this.data.getBoundary()}`),this.request.query)){const[t,e]=this.request.path.split("?");this.request.path=`${t}?${this.options.qs.stringify(this.request.query)}${e?`&${e}`:""}`}}_checkModify(){if(this.response)throw new Error("Cannot modify request after it has been sent!")}}a.version=o.version,a.METHODS=i.METHODS.concat("BREW").filter(t=>"M-SEARCH"!==t);for(const t of a.METHODS)a[t.toLowerCase()]=((e,s)=>new a(t,e,s));t.exports=a},function(t,e){e.parse=(t=>{const e={};for(const[s,r]of new Window.URLSearchParams(t).entries())e[s]=r;return e}),e.stringify=(t=>new window.URLSearchParams(t).toString())},function(t,e){t.exports={name:"snekfetch",homepage:"https://github.com/devsnek/snekfetch"}},function(t,e){t.exports={buildRequest:function(t,e){return{method:t,path:e,redirect:this.options.followRedirects?"follow":"manual",headers:{},setHeader(t,e){this.headers[t.toLowerCase()]=e},getHeader(t){return this.headers[t.toLowerCase()]},getHeaders(){return this.headers}}},finalizeRequest:function(){return this._finalizeRequest(),this.data&&(this.request.body=this.data),window.fetch(this.request.path,this.request).then(t=>t.text().then(e=>{const s={};for(const[e,r]of t.headers.entries())s[e.toLowerCase()]=r;return{response:t,raw:e,headers:s}}))},shouldSendRaw:()=>!1,METHODS:["GET","HEAD","POST","PUT","DELETE","CONNECT","OPTIONS","PATCH"],STATUS_CODES:{},Extension:Object,FormData:window.FormData}},function(t,e){}]);