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