xfetch-js
Version:
> A extremely simple fetch extension for modern browsers inspired by [sindresorhus/ky](https://github.com/sindresorhus/ky). > > Which aims to be as small as possible and easy to use.
1 lines • 2.14 kB
JavaScript
((a,b)=>{"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?module.exports=b():a.xf=b()})(this,()=>{function a(b,c){const d=a=>a&&"object"==typeof a;return d(b)&&d(c)?(Object.keys(c).forEach(e=>{const f=b[e],g=c[e];b[e]=Array.isArray(f)&&Array.isArray(g)?f.concat(g):d(f)&&d(g)?a(Object.assign({},f),g):g}),b):c}const b=["get","post","put","patch","delete","head"];class c extends Error{constructor(a){super(a.statusText),this.name="HTTPError",this.response=a}}class d extends Promise{}for(const a of["arrayBuffer","blob","formData","json","text"])d.prototype[a]=function(b){return this.then(b=>b[a]()).then(b||(a=>a))};const{assign:e}=Object,f=a=>a.reduce((a,[b,c])=>(a[b]=c,a),{}),g=(...a)=>b=>a.some(a=>"string"==typeof a?typeof b==a:b instanceof a),h=g("string"),i=g("object"),j=g("undefined"),k=a=>h(a)||i(a),l=a=>{if(!a.ok)throw new c(a);return a},m="content-type",n=a=>{for(const b of Object.keys(a)){const c=b.toLowerCase();b!=c&&(a[c]=a[b],delete a[b])}},o=(i={})=>{const p=(b,c={})=>{a(c,i);const o=a=>new c.URLSearchParams(a).toString(),p=new c.URL(b,c.baseURI||void 0);if(c.headers?g(c.Headers)(c.headers)&&(c.headers=f([...c.headers.entries()])):c.headers={},n(c.headers),c.json)c.body=JSON.stringify(c.json),j(c.headers[m])&&(c.headers[m]="application/json");else if(k(c.urlencoded))c.body=h(c.urlencoded)?c.urlencoded:o(c.urlencoded),j(c.headers[m])&&(c.headers[m]="application/x-www-form-urlencoded");else if(g(c.FormData,"object")(c.formData)){if(!g(c.FormData)(c.formData)){const a=new c.FormData;for(const[b,d]of Object.entries(c.formData))a.append(b,d);c.formData=a}c.body=c.formData}return c.qs&&(h(c.qs)&&(c.qs=(a=>f([...new c.URLSearchParams(a).entries()]))(c.qs)),p.search=o(e(f([...p.searchParams.entries()]),c.qs))),c.credentials||(c.credentials="same-origin"),d.resolve(c.fetch(p,c).then(l))};for(const a of b)p[a]=(b,c={})=>(c.method=a.toUpperCase(),p(b,c));return p.extend=a=>o(e({},i,a)),p.HTTPError=c,p},p="undefined"!=typeof document,q="undefined"!=typeof self;return q?o({fetch:fetch.bind(self),URL,Response,URLSearchParams,Headers,FormData,baseURI:p?document.baseURI:""}):o()});