UNPKG

wretch

Version:

A tiny wrapper built around fetch with an intuitive syntax.

2 lines 1.11 kB
const e=(e,t)=>t.skipCache||t.method!==`GET`,t=(e,t)=>t.method+`@`+e,n=()=>!1,r=()=>null,i=e=>e.ok,a=({throttle:a=1e3,skip:o=e,key:s=t,clear:c=n,invalidate:l=r,condition:u=i,flagResponseOnCacheHit:d=`__cached`}={})=>{let f=new Map,p=new Map,m=new Set,h=e=>{a&&!m.has(e)&&(m.add(e),setTimeout(()=>{m.delete(e)},a))},g=e=>(t,n)=>{let r=s(t,n),i=l(t,n);if(i&&(i instanceof Array||(i=[i]),i.forEach(e=>{typeof e==`string`?f.delete(e):e instanceof RegExp&&f.forEach((t,n)=>{e.test(n)&&f.delete(n)})})),c(t,n)&&f.clear(),o(t,n))return e(t,n);if(m.has(r)){if(f.has(r)){let e=f.get(r).clone();return d&&Object.defineProperty(e,d,{value:r,enumerable:!1}),Promise.resolve(e)}else if(p.has(r))return new Promise((e,t)=>{p.get(r).push([e,t])})}return p.has(r)||p.set(r,[]),h(r),e(t,n).then(e=>(u(e.clone())&&f.set(r,e.clone()),p.get(r).forEach(([t])=>t(e.clone())),p.delete(r),e)).catch(e=>{throw p.get(r).forEach(([,t])=>t(e)),p.delete(r),e})};return g.cacheResponse=function(e,t){h(e),f.set(e,t)},g.cache=f,g.inflight=p,g.throttling=m,g};export{a as throttlingCache}; //# sourceMappingURL=throttlingCache.min.mjs.map