xior
Version:
A lite http request lib based on fetch with plugins support and similar API to axios.
2 lines (1 loc) • 827 B
JavaScript
import{A as b,b as f,i,n as d,v as p,z as C}from"../chunk-JGM66FSA.mjs";import{d as l}from"../chunk-BOVQDIMN.mjs";import{lru as L}from"tiny-lru";function O(c={}){let{enableCache:y,defaultCache:X=L(c.cacheItems||100,c.cacheTime||1e3*60*5)}=c;return function(u){return e=>l(this,null,function*(){let{enableCache:s=y,forceUpdate:R,defaultCache:P=X,paramsSerializer:I}=e,T=e.method===d||e.isGet,h=typeof s,m=h===f,n=i;m&&(n=s(e)),n===i&&(n=m||h===`${i}`?T:!!s);let o="";if(n){let r=P;o=p(e.url&&C(e.url)?e.url:b(e.baseURL,e.url),{a:e.data,b:e.params},I);let t=r.get(o);return!t||R?(t=l(this,null,function*(){try{let a=yield u(e);return a&&(a.cacheKey=o),a}catch(a){throw"delete"in r?r.delete(o):r.del(o),a}}),r.set(o,t),t):t.then(a=>(a.fromCache=!0,a.cacheTime=Date.now(),o&&(a.cacheKey=o),a))}return u(e)})}}export{O as default};