UNPKG

wretch

Version:

A tiny wrapper built around fetch with an intuitive syntax.

3 lines (2 loc) 4.28 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).wretch=t()}(this,(function(){"use strict";const e="Content-Type",t=Symbol();function r(t={}){var r;return null===(r=Object.entries(t).find((([t])=>t.toLowerCase()===e.toLowerCase())))||void 0===r?void 0:r[1]}function o(e){return/^application\/.*json.*/.test(e)}const s=function(e,t,r=0){return Object.entries(t).reduce(((t,[o,n])=>{const i=e[o];return Array.isArray(i)&&Array.isArray(n)?t[o]=r?[...i,...n]:n:t[o]="object"==typeof i&&"object"==typeof n?s(i,n,r):n,t}),{...e})},n={options:{},errorType:"text",polyfills:{},polyfill(e,t=1,r=0,...o){const s=this.polyfills[e]||("undefined"!=typeof self?self[e]:null)||("undefined"!=typeof global?global[e]:null);if(t&&!s)throw new Error(e+" is not defined");return r&&s?new s(...o):s}};class i extends Error{}const h=e=>{const{_url:r,_options:o,_config:n,_catchers:h,_resolvers:u,_middlewares:c,_addons:l}=e,f=new Map(h),a=s(n.options,o);l.forEach((t=>t.beforeRequest&&t.beforeRequest(e,a)));const d=(e=>t=>e.reduceRight(((e,t)=>t(e)),t)||t)(c)(n.polyfill("fetch"))(r,a),p=new Error,_=d.catch((e=>{throw{__wrap:e}})).then((e=>{if(!e.ok){const t=new i;if(t.cause=p,t.stack=t.stack+"\nCAUSE: "+p.stack,t.response=e,"opaque"===e.type)throw t;return e[n.errorType]().then((r=>{throw t.message=r,t[n.errorType]=r,t.status=e.status,t}))}return e})),y=r=>o=>(r?_.then((e=>e&&e[r]())).then((e=>o?o(e):e)):_.then((e=>o?o(e):e))).catch((r=>{const o=r.__wrap||r,s=r.__wrap&&f.has(t)?f.get(t):f.get(o.status)||f.get(o.name);if(s)return s(o,e);throw o})),w={_wretchReq:e,_fetchReq:d,res:y(null),json:y("json"),blob:y("blob"),formData:y("formData"),arrayBuffer:y("arrayBuffer"),text:y("text"),error(e,t){return f.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(t,e)}},b=l.reduce(((e,t)=>({...e,...t.resolver})),w);return u.reduce(((t,r)=>r(t,e)),b)},u={_url:"",_options:{},_config:n,_catchers:new Map,_resolvers:[],_deferred:[],_middlewares:[],_addons:[],addon(e){return{...this,_addons:[...this._addons,e],...e.wretch}},errorType(e){return{...this,_config:{...this._config,errorType:e}}},polyfills(e,t=0){return{...this,_config:{...this._config,polyfills:t?e:s(this._config.polyfills,e)}}},url(e,t=0){if(t)return{...this,_url:e};const r=this._url.split("?");return{...this,_url:r.length>1?r[0]+e+"?"+r[1]:this._url+e}},options(e,t=0){return{...this,_options:t?e:s(this._options,e)}},headers(e){return{...this,_options:s(this._options,{headers:e||{}})}},accept(e){return this.headers({Accept:e})},content(t){return this.headers({[e]:t})},auth(e){return this.headers({Authorization:e})},catcher(e,t){const r=new Map(this._catchers);return r.set(e,t),{...this,_catchers:r}},resolve(e,t=0){return{...this,_resolvers:t?[e]:[...this._resolvers,e]}},defer(e,t=0){return{...this,_deferred:t?[e]:[...this._deferred,e]}},middlewares(e,t=0){return{...this,_middlewares:t?e:[...this._middlewares,...e]}},fetch(e=this._options.method,t="",s=null){let n=this.url(t).options({method:e});const i=r(n._options.headers),u="object"==typeof s&&(!n._options.headers||!i||o(i));return n=s?u?n.json(s,i):n.body(s):n,h(n._deferred.reduce(((e,t)=>t(e,e._url,e._options)),n))},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(e,t){const s=r(this._options.headers);return this.content(t||o(s)&&s||"application/json").body(JSON.stringify(e))}};function c(e="",t={}){return{...u,_url:e,_options:t}}return c.default=c,c.options=function(e,t=0){n.options=t?e:s(n.options,e)},c.errorType=function(e){n.errorType=e},c.polyfills=function(e,t=0){n.polyfills=t?e:s(n.polyfills,e)},c.WretchError=i,c})); //# sourceMappingURL=wretch.min.js.map