wretch
Version:
A tiny wrapper built around fetch with an intuitive syntax.
2 lines • 1.06 kB
JavaScript
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.wretchRetryMiddleware={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let t=(e,t)=>e*t,n=e=>e&&(e.ok||e.status>=400&&e.status<500);e.retry=({delayTimer:e=500,delayRamp:r=t,maxAttempts:i=10,until:a=n,onRetry:o=null,retryOnNetworkError:s=!1,resolveWithLatestResponse:c=!1,skip:l}={})=>t=>(n,u)=>{let d=0;if(l&&l(n,u))return t(n,u);let f=(l,p)=>Promise.resolve(a(l,p)).then(a=>a?l&&c?l:p?Promise.reject(p):l:(d++,!i||d<=i?new Promise(i=>{let a=r(e,d);setTimeout(()=>{typeof o==`function`?Promise.resolve(o({response:l,error:p,url:n,attempt:d,options:u})).then((e={})=>{i(t((e&&e.url)??n,(e&&e.options)??u))}):i(t(n,u))},a)}).then(f).catch(e=>{if(!s)throw e;return f(null,e)}):l&&c?l:Promise.reject(p||Error(`Number of attempts exceeded.`))));return t(n,u).then(f).catch(e=>{if(!s)throw e;return f(null,e)})}});
//# sourceMappingURL=retry.min.js.map