UNPKG

wretch

Version:

A tiny wrapper built around fetch with an intuitive syntax.

3 lines (2 loc) 4.06 kB
const r=Symbol();function t(r={}){var t;return null===(t=Object.entries(r).find((([r])=>r.toLowerCase()==="Content-Type".toLowerCase())))||void 0===t?void 0:t[1]}function e(r){return/^application\/.*json.*/.test(r)}const o=function(r,t,e=0){return Object.entries(t).reduce(((t,[s,n])=>{const i=r[s];return Array.isArray(i)&&Array.isArray(n)?t[s]=e?[...i,...n]:n:t[s]="object"==typeof i&&"object"==typeof n?o(i,n,e):n,t}),{...r})},s={options:{},errorType:"text",polyfills:{},polyfill(r,t=1,e=0,...o){const s=this.polyfills[r]||("undefined"!=typeof self?self[r]:null)||("undefined"!=typeof global?global[r]:null);if(t&&!s)throw new Error(r+" is not defined");return e&&s?new s(...o):s}};class n extends Error{}const i=t=>{const{_url:e,_options:s,_config:i,_catchers:h,_resolvers:u,_middlewares:c,_addons:l}=t,a=new Map(h),f=o(i.options,s);l.forEach((r=>r.beforeRequest&&r.beforeRequest(t,f)));const d=(r=>t=>r.reduceRight(((r,t)=>t(r)),t)||t)(c)(i.polyfill("fetch"))(e,f),p=new Error,_=d.catch((r=>{throw{__wrap:r}})).then((r=>{if(!r.ok){const t=new n;if(t.cause=p,t.stack=t.stack+"\nCAUSE: "+p.stack,t.response=r,"opaque"===r.type)throw t;return r[i.errorType]().then((e=>{throw t.message=e,t[i.errorType]=e,t.status=r.status,t}))}return r})),y=e=>o=>(e?_.then((r=>r&&r[e]())).then((r=>o?o(r):r)):_.then((r=>o?o(r):r))).catch((e=>{const o=e.__wrap||e,s=e.__wrap&&a.has(r)?a.get(r):a.get(o.status)||a.get(o.name);if(s)return s(o,t);throw o})),w={_wretchReq:t,_fetchReq:d,res:y(null),json:y("json"),blob:y("blob"),formData:y("formData"),arrayBuffer:y("arrayBuffer"),text:y("text"),error(r,t){return a.set(r,t),this},badRequest(r){return this.error(400,r)},unauthorized(r){return this.error(401,r)},forbidden(r){return this.error(403,r)},notFound(r){return this.error(404,r)},timeout(r){return this.error(408,r)},internalError(r){return this.error(500,r)},fetchError(t){return this.error(r,t)}},b=l.reduce(((r,t)=>({...r,...t.resolver})),w);return u.reduce(((r,e)=>e(r,t)),b)},h={_url:"",_options:{},_config:s,_catchers:new Map,_resolvers:[],_deferred:[],_middlewares:[],_addons:[],addon(r){return{...this,_addons:[...this._addons,r],...r.wretch}},errorType(r){return{...this,_config:{...this._config,errorType:r}}},polyfills(r,t=0){return{...this,_config:{...this._config,polyfills:t?r:o(this._config.polyfills,r)}}},url(r,t=0){if(t)return{...this,_url:r};const e=this._url.split("?");return{...this,_url:e.length>1?e[0]+r+"?"+e[1]:this._url+r}},options(r,t=0){return{...this,_options:t?r:o(this._options,r)}},headers(r){return{...this,_options:o(this._options,{headers:r||{}})}},accept(r){return this.headers({Accept:r})},content(r){return this.headers({"Content-Type":r})},auth(r){return this.headers({Authorization:r})},catcher(r,t){const e=new Map(this._catchers);return e.set(r,t),{...this,_catchers:e}},resolve(r,t=0){return{...this,_resolvers:t?[r]:[...this._resolvers,r]}},defer(r,t=0){return{...this,_deferred:t?[r]:[...this._deferred,r]}},middlewares(r,t=0){return{...this,_middlewares:t?r:[...this._middlewares,...r]}},fetch(r=this._options.method,o="",s=null){let n=this.url(o).options({method:r});const h=t(n._options.headers),u="object"==typeof s&&(!n._options.headers||!h||e(h));return n=s?u?n.json(s,h):n.body(s):n,i(n._deferred.reduce(((r,t)=>t(r,r._url,r._options)),n))},get(r=""){return this.fetch("GET",r)},delete(r=""){return this.fetch("DELETE",r)},put(r,t=""){return this.fetch("PUT",t,r)},post(r,t=""){return this.fetch("POST",t,r)},patch(r,t=""){return this.fetch("PATCH",t,r)},head(r=""){return this.fetch("HEAD",r)},opts(r=""){return this.fetch("OPTIONS",r)},body(r){return{...this,_options:{...this._options,body:r}}},json(r,o){const s=t(this._options.headers);return this.content(o||e(s)&&s||"application/json").body(JSON.stringify(r))}};function u(r="",t={}){return{...h,_url:r,_options:t}}u.default=u,u.options=function(r,t=0){s.options=t?r:o(s.options,r)},u.errorType=function(r){s.errorType=r},u.polyfills=function(r,t=0){s.polyfills=t?r:o(s.polyfills,r)},u.WretchError=n;export{u as default}; //# sourceMappingURL=wretch.min.mjs.map