wretch
Version:
A tiny wrapper built around fetch with an intuitive syntax.
3 lines • 4.13 kB
JavaScript
function e(e={}){let t=e instanceof Array?Object.fromEntries(e):e;for(let e in t)if(e.toLowerCase()===`content-type`)return t[e]}function t(e){return/^application\/.*json/.test(e)}const n=(e,t,r=!1)=>{let i={...e};for(let a in t){if(!Object.prototype.hasOwnProperty.call(t,a))continue;let o=e[a],s=t[a];i[a]=Array.isArray(o)&&Array.isArray(s)?r?[...o,...s]:s:typeof o==`object`&&typeof s==`object`?n(o,s,r):s}return i},r=Symbol(),i=Symbol(),a=e=>t=>e.reduceRight((e,t)=>t(e),t);var o=class extends Error{};const s=e=>{let t=Object.create(null);e=e._addons.reduce((n,r)=>r.beforeRequest&&r.beforeRequest(n,e._options,t)||n,e);let{_url:n,_options:s,_fetch:c,_errorTransformer:l,_catchers:u,_resolvers:d,_middlewares:f,_addons:p}=e,m=new Map(u),h=s,g=n,_=a(f)((e,t)=>(g=e,(c||fetch)(e,t)))(n,h),v=Error(),y=_.then(async e=>{if(!e.ok){let t=new o;if(t.cause=v,t.stack+=`
CAUSE: `+v.stack,t.response=e,t.status=e.status,t.url=g,e.type===`opaque`||l)t.message=e.statusText;else try{t.message=await e.clone().text()}catch{t.message=e.statusText}throw t}return e}),b=t=>t.catch(async t=>{let n=m.get(t?.status)||m.get(t?.name)||!(t instanceof o)&&m.get(r)||m.get(i);if(t.response&&l&&(t=await l(t,t.response,e)),n)return n(t,e);throw t}),x=e=>t=>{let n=e?y.then(t=>t?.[e]()):y;return b(t?n.then(t):n)},S={_wretchReq:e,_fetchReq:_,_sharedState:t,res:x(null),json:x(`json`),blob:x(`blob`),formData:x(`formData`),arrayBuffer:x(`arrayBuffer`),text:x(`text`),error(e,t){return m.set(e,t),this},badRequest(e){return this.error(400,e)},unauthorized(e){return this.error(401,e)},forbidden(e){return this.error(403,e)},notFound(e){return this.error(404,e)},timeout(e){return this.error(408,e)},internalError(e){return this.error(500,e)},fetchError(e){return this.error(r,e)}},C=p.reduce((e,t)=>({...e,...typeof t.resolver==`function`?t.resolver(e):t.resolver}),S);return d.reduce((t,n)=>n(t,e),C)},c={_url:``,_options:{},_catchers:new Map,_resolvers:[],_deferred:[],_middlewares:[],_addons:[],addon(e){let t=Array.isArray(e)?e:[e],n=t.reduce((e,t)=>({...e,...t.wretch}),{});return{...this,_addons:[...this._addons,...t],...n}},fetchPolyfill(e){return{...this,_fetch:e}},url(e,t=!1){if(t)return{...this,_url:e};let n=this._url.indexOf(`?`);return{...this,_url:n>-1?this._url.slice(0,n)+e+this._url.slice(n):this._url+e}},options(e,t=!1){return{...this,_options:t?e:n(this._options,e)}},headers(e){let t=e?Array.isArray(e)?Object.fromEntries(e):`entries`in e?Object.fromEntries(e.entries()):e:{};return{...this,_options:n(this._options,{headers:t})}},accept(e){return this.headers({Accept:e})},content(e){return this.headers({"Content-Type":e})},auth(e){return this.headers({Authorization:e})},catcher(e,t){let n=new Map(this._catchers),r=Array.isArray(e)?e:[e];for(let e of r)n.set(e,t);return{...this,_catchers:n}},catcherFallback(e){return this.catcher(i,e)},customError(e){return{...this,_errorTransformer:e}},resolve(e,t=!1){return{...this,_resolvers:t?[e]:[...this._resolvers,e]}},defer(e,t=!1){return{...this,_deferred:t?[e]:[...this._deferred,e]}},middlewares(e,t=!1){return{...this,_middlewares:t?e:[...this._middlewares,...e]}},fetch(n=this._options.method,r=``,i=null){let a=this.url(r).options({method:n}),o=e(a._options.headers),c=typeof i==`object`&&!(i instanceof FormData)&&(!a._options.headers||!o||t(o));return a=i?c?a.json(i,o):a.body(i):a,s(a._deferred.reduce((e,t)=>t(e,e._url,e._options),a))},get(e=``){return this.fetch(`GET`,e)},delete(e=``){return this.fetch(`DELETE`,e)},put(e,t=``){return this.fetch(`PUT`,t,e)},post(e,t=``){return this.fetch(`POST`,t,e)},patch(e,t=``){return this.fetch(`PATCH`,t,e)},head(e=``){return this.fetch(`HEAD`,e)},opts(e=``){return this.fetch(`OPTIONS`,e)},body(e){return{...this,_options:{...this._options,body:e}}},json(n,r){let i=e(this._options.headers);return this.content(r||t(i)&&i||`application/json`).body(JSON.stringify(n))},toFetch(){return(e,t={})=>this.url(e).options(t).catcherFallback(e=>{if(e instanceof o)return e.response;throw e}).fetch().res()}},l=(e=``,t={})=>({...c,_url:e,_options:t});l.default=l,l.WretchError=o;export{l as default};
//# sourceMappingURL=wretch.min.mjs.map