UNPKG

snekfetch

Version:

Just do http requests without all that weird nastiness from other libs

1 lines 4.13 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Snekfetch=e():t.Snekfetch=e()}("undefined"!=typeof self?self:this,function(){return function(t){var e={};function s(o){if(e[o])return e[o].exports;var n=e[o]={i:o,l:!1,exports:{}};return t[o].call(n.exports,n,n.exports,s),n.l=!0,n.exports}return s.m=t,s.c=e,s.d=function(t,e,o){s.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:o})},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=0)}([function(t,e,s){"use strict";const o=s("undefined"!=typeof window?1:2);class n extends o.Parent{constructor(t,e,s={}){super(),this.options=Object.assign({qs:o.querystring,method:t,url:e,redirect:"follow"},s,{headers:{},query:void 0,data:void 0}),s.headers&&this.set(s.headers),s.query&&this.query(s.query),s.data&&this.send(s.data)}query(t,e){return void 0===this.options.query&&(this.options.query={}),"object"==typeof t?Object.assign(this.options.query,t):this.options.query[t]=e,this}set(t,e){if("object"==typeof t)for(const[e,s]of Object.entries(t))this.options.headers[e.toLowerCase()]=s;else this.options.headers[t.toLowerCase()]=e;return this}attach(...t){const e=this.options.data instanceof o.FormData?this.options.data:this.options.data=new o.FormData;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(t instanceof o.FormData||o.shouldSendRaw(t))this.options.data=t;else if(null!==t&&"object"==typeof t){const e=this.options.headers["content-type"];let s;e?e.includes("application/json")?s=JSON.stringify:e.includes("urlencoded")&&(s=this.options.qs.stringify):(this.set("Content-Type","application/json"),s=JSON.stringify),this.options.data=s(t)}else this.options.data=t;return this}then(t,e){return this._response?this._response.then(t,e):(this._finalizeRequest(),this._response=o.request(this).then(({raw:t,headers:e,statusCode:s,statusText:o})=>{const n=this,r={request:this.request,get body(){delete r.body;const e=r.headers["content-type"];if(t instanceof ArrayBuffer&&(t=new window.TextDecoder("utf8").decode(t)),/application\/json/.test(e))try{r.body=JSON.parse(t)}catch(e){r.body=String(t)}else/application\/x-www-form-urlencoded/.test(e)?r.body=n.options.qs.parse(String(t)):r.body=t;return r.body},raw:t,ok:s>=200&&s<400,headers:e,statusCode:s,statusText:o};if(r.ok)return r;const i=new Error(`${s} ${o}`.trim());return Object.assign(i,r),Promise.reject(i)}).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))}_finalizeRequest(){if("HEAD"!==this.options.method&&this.set("Accept-Encoding","gzip, deflate"),this.options.data&&this.options.data.getBoundary&&this.set("Content-Type",`multipart/form-data; boundary=${this.options.data.getBoundary()}`),this.options.query){const[t,e]=this.options.url.split("?");this.options.url=`${t}?${this.options.qs.stringify(this.options.query)}${e?`&${e}`:""}`}}_read(){this.resume(),this._response||this.catch(t=>this.emit("error",t))}}n.METHODS=o.METHODS.filter(t=>"M-SEARCH"!==t);for(const t of n.METHODS)n[t.toLowerCase()]=function(e,s){return new(this&&this.prototype instanceof n?this:n)(t,e,s)};t.exports=n},function(t,e,s){"use strict";t.exports={request:function(t){t.options.body=t.options.data;const e="arraybuffer"===t.options.responseType?"arrayBuffer":"text";return window.fetch(t.options.url,t.options).then(t=>t[e]().then(e=>{const s={};for(const[e,o]of t.headers.entries())s[e.toLowerCase()]=o;return{raw:e,headers:s,statusCode:t.status,statusText:t.statusText}}))},shouldSendRaw:()=>!1,METHODS:["GET","HEAD","POST","PUT","DELETE","CONNECT","OPTIONS","PATCH"],Parent:Object,FormData:window.FormData,querystring:{parse:t=>{const e={};for(const[s,o]of new window.URLSearchParams(t).entries())e[s]=o;return e},stringify:t=>new window.URLSearchParams(t).toString()}}},function(t,e){}])});