vue3-request 1.0.24 → 1.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,36 +1,33 @@
1
- 更多内容请查看文档 -> [**文档**](https://fslflame.github.io/vue3-request/)
1
+ 更多内容请查看<a href="https://Flame-00.github.io/vue3-request/" target="_blank">文档</a>
2
2
 
3
- ## 核心解决方案
3
+ [演练场](https://codesandbox.io/p/devbox/yrxqp8)
4
+
5
+ ## 核心优势
4
6
 
5
7
  Vue3-Request 是一个专为 Vue 3 设计的异步请求处理库,它通过统一的 useRequest Hook 和强大的插件生态,彻底简化了异步操作和 API 调用的复杂性:
6
8
 
7
9
  ✨ 统一状态管理
8
10
 
9
11
  - 🚀 所有数据都具有响应式
10
- - 📊 自动管理 loading、data、error、signal、params 等状态
12
+ - 📊 自动管理 loading、data、res、error、signal、params 等状态
11
13
  - 🔄 提供 run、runAsync、refresh、cancel、abort 等便捷方法
12
14
  - 🎛️ 支持手动和自动执行模式,满足不同业务场景
13
15
 
14
16
  🧩 强大的插件生态 内置 9 大核心插件,开箱即用:
15
17
 
16
- - <img src="https://fslflame.github.io/vue3-request/cancel.svg" alt="取消请求" width="20" height="20" /> 请求中止 - 自动生成 AbortSignal 中止过期请求,避免状态污染和竞态条件问题
17
- - 🔄 错误重试 - 智能重试机制,提升请求成功率
18
- - ⚡ 防抖节流 - 优化用户交互,减少无效请求
19
- - 💾 智能缓存 - 提升用户体验,减少服务器压力
20
- - 🔁 轮询请求 - 实时数据同步,支持动态控制
21
- - 👁️ 窗口聚焦刷新 - 用户回到页面时自动刷新数据
22
- - 📦 依赖刷新 - 响应式依赖变化,自动触发请求
23
- - ⏳ 准备状态控制 - 条件请求执行,精确控制时机
24
- - 💻 完整 TypeScript 支持 - 类型安全,智能提示
25
- - 🔌 框架无关 - 兼容 Axios、Fetch 等任意请求库
26
- - 📝 丰富的生命周期 - onBefore、onSuccess、onError、onFinally
27
- - 🎨 可扩展架构 - 支持自定义插件,满足特殊需求
28
- - 📚 完善文档 - 详尽示例,快速上手
29
-
30
- ## 为什么选择 Vue3-Request?
31
-
32
- 在一个充满挑战的异步世界中,Vue3-Request 不仅仅是一个工具库,更是你的开发伙伴。它让复杂的异步逻辑变得简单优雅,让重复的样板代码成为历史,让你可以专注于真正重要的业务逻辑。
33
- 从简单的数据获取到复杂的业务场景,从性能优化到用户体验,Vue3-Request 都为你提供了最佳实践的解决方案。让我们一起拥抱更高效、更优雅的异步编程方式!
18
+ - <img src="https://Flame-00.github.io/vue3-request/cancel.svg" alt="取消请求" width="20" height="20" /> 请求中止
19
+ - 🔄 错误重试
20
+ - ⚡ 防抖节流
21
+ - 💾 智能缓存
22
+ - 🔁 轮询请求
23
+ - 👁️ 窗口聚焦刷新
24
+ - 📦 依赖刷新
25
+ - ⏳ 准备状态控制
26
+ - 💻 完整 TypeScript 支持
27
+ - 🔌 框架无关
28
+ - 📝 丰富的生命周期
29
+ - 🎨 可扩展架构
30
+ - 📚 完善文档
34
31
 
35
32
  ## ⚡ 快速安装
36
33
 
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("vue"),b=e=>e,C=e=>e==null,S=typeof window>"u";function M(e){return C(e)?e:typeof e=="number"&&!isNaN(e)}function p(e,t){return M(e)===void 0?{is:!1,value:0}:M(e)?t&&e===-1?{is:!0,value:-1}:{is:!0,value:e===0||e&&e<0?0:e}:{is:!1,value:0}}const q=(e,t)=>{let n=t;for(let s=e.length;s-- >0;){const r=e[s];n=r(n)}return n?.()},F=()=>new Promise(()=>{}),j=e=>e instanceof Function,y=new Map,N=(e,t,{data:n,params:s,time:r})=>{let u;const a=L(e),{is:o,value:l}=p(t,!0);o&&(a?.timer&&clearTimeout(a.timer),l!==-1&&(u=setTimeout(()=>{E(e)},l)),y.set(e,{data:n,params:s,time:r,timer:u}))},L=e=>y.get(e),E=e=>{e&&typeof e=="string"?y.delete(e):y.clear()};class U{constructor(t,n){this.service=t,this.options=n,this.state=i.reactive({data:void 0,error:void 0,params:n?.defaultParams||[],loading:!1,isFinished:!1,isAborted:!1,signal:new AbortController().signal})}currentRequestId=0;pluginImpls=[];state;abort=()=>{};setState=t=>{Object.assign(this.state,t)};executePlugin=(t,...n)=>{if(t==="onRequest")return{servicePromise:q(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const s=this.pluginImpls.map(r=>r[t]?.(...n)).filter(Boolean);return Object.assign({},...s)}};loading=t=>{this.setState({loading:t,isFinished:!t})};onFinished=()=>{this.executePlugin("onFinally",this.state.params,this.state.data,this.state.error),this.loading(!1),this.options.onFinally?.(this.state.params,this.state.data,this.state.error)};runAsync=async(...t)=>{const n=++this.currentRequestId,{isReturn:s,isReady:r,...u}=this.executePlugin("onBefore",t);if(!r)return F();if(this.setState({params:t,...u}),this.loading(!0),s)return this.loading(!1),u.data;this.options.onBefore?.(t);try{const a=()=>this.service(...t);let{servicePromise:o}=this.executePlugin("onRequest",a);o||(o=a());const l=await o;return n!==this.currentRequestId?F():(this.setState({data:l,error:void 0}),this.executePlugin("onSuccess",l,t),this.options.onSuccess?.(l,t),this.onFinished(),l)}catch(a){if(n!==this.currentRequestId)return F();const o=a;throw this.setState({data:void 0,error:o}),this.executePlugin("onError",o,t),this.options.onError?.(o,t),this.onFinished(),o}};run=(...t)=>{this.runAsync(...t).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);mutate=t=>{j(t)?t(this.state.data):this.setState({data:t}),this.executePlugin("onMutate",this.state.data)};cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function D(e,t,n){const s={manual:!1,...t},r=new U(e,s);r.pluginImpls=n.map(m=>m(r,s)),i.onMounted(()=>{if(!s.manual){const m=r.state.params;r.run(...m)}}),i.onUnmounted(r.cancel);const{run:u,cancel:a,refresh:o,runAsync:l,refreshAsync:v,abort:f,mutate:g}=r;return{...i.toRefs(r.state),run:u,cancel:a,refresh:o,runAsync:l,mutate:g,abort:f,refreshAsync:v,clearCache:E}}const W=b((e,{abortPrevious:t=!0})=>{let n=null;const s=()=>{n=new AbortController,e.setState({signal:n.signal,isAborted:n.signal.aborted})};return e.abort=()=>{n&&!n.signal.aborted&&!e.state.isFinished&&(n.abort(),e.setState({isAborted:n.signal.aborted}))},{onBefore:()=>{typeof t=="boolean"&&t&&e.abort(),s()},onRequest:r=>()=>r(),onCancel:()=>{e.abort(),n=null}}}),R=new Map,_=(e,{data:t,params:n,time:s})=>{R.has(e)&&R.get(e)?.forEach(u=>u({data:t,params:n,time:s}))},V=(e,t)=>(R.has(e)?R.get(e)?.add(t):R.set(e,new Set),()=>{const n=R.get(e);n?.delete(t),n?.size===0&&R.delete(e)}),P=new Map,H=(e,t)=>{P.set(e,t),t.then(()=>{}).catch(()=>{}).finally(()=>{z(e)})},$=e=>P.get(e),z=e=>{e&&typeof e=="string"?P.delete(e):P.clear()},J=b((e,{cacheKey:t,cacheTime:n=3e5,staleTime:s=0,setCache:r,getCache:u})=>{const a=i.ref(null);let o=null;const{is:l,value:v}=p(s,!0);if(!l)return{};const f=typeof t=="function"?t:()=>t,g=(d,c)=>{r?r(d,c):N(d,n,c),_(d,c)},m=d=>u?u(d):L(d);function w(){const d=f();if(!d)return;const c=m(d);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),a.value=V(d,h=>{e.setState({data:h.data})})}return w(),i.onUnmounted(()=>{a.value?.()}),{onBefore:d=>{const c=f(d);if(!c)return null;const h=m(c);return!h||!Reflect.has(h,"data")?{}:v===-1||Date.now()-h.time<v?{data:h.data,error:void 0,isReturn:!0}:{data:h.data,error:void 0}},onRequest:d=>{const c=f(e.state.params);if(!c)return d;let h=$(c);return h&&h!==o?()=>h:(h=d(),o=h,H(c,h),()=>h)},onSuccess:(d,c)=>{const h=f(c);h&&(a.value?.(),g(h,{data:d,params:c,time:Date.now()}),a.value=V(h,O=>{e.setState({data:O.data})}))},onMutate(d){const c=f(e.state.params);c&&(a.value?.(),g(c,{data:d,params:e.state.params,time:Date.now()}),a.value=V(c,h=>{e.setState({data:h.data})}))}}}),Q=b((e,{errorRetryCount:t=0,errorRetryInterval:n})=>{const s=i.ref(),r=i.ref(0),u=i.ref(!1),a=i.computed(()=>1e3*Math.pow(2,r.value)),o=()=>{let f=null;r.value++;const{value:g}=p(i.toValue(t),!0),m=g===-1,w=r.value<=g;if(m||w){let{is:d,value:c}=p(i.toValue(n));d||(c=i.toValue(Math.min(a.value,3e4))),f=setTimeout(()=>{u.value=!0,e.refresh()},c)}return()=>{f&&clearTimeout(f)}},l=()=>{s.value?.()},{is:v}=p(i.toValue(t),!0);return v?{onBefore:()=>{u.value||(r.value=0),u.value=!1,l()},onSuccess:()=>{r.value=0},onError:()=>{s.value=o()},onCancel:()=>{r.value=0,l()}}:{}}),x=()=>S||C(window.document?.visibilityState)?!0:document.visibilityState==="visible",T=new Set,X=e=>(T.add(e),()=>{T.delete(e)}),Y=()=>{x()&&T.forEach(e=>{e()})};!S&&window?.addEventListener&&window.addEventListener("visibilitychange",Y,!1);const Z=b((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:s})=>{const r=i.ref(null),u=i.ref(),a=()=>{let o;const{value:l}=p(i.toValue(s),!0);if(e.state.error&&l!==0)return;const{is:v,value:f}=p(i.toValue(t));return v?(o=setTimeout(()=>{!i.toValue(n)&&!x()?r.value=X(e.refresh):e.refresh()},f),()=>{o&&clearTimeout(o),r.value?.()}):void 0};return i.watch([()=>i.toValue(t),()=>i.toValue(n)],()=>{u.value?.(),u.value=a()}),i.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{u.value?.()},onCancel:()=>{u.value?.()},onFinally:()=>{u.value=a()}}}),G=b((e,{manual:t,refreshDeps:n,refreshDepsAction:s})=>{if(C(n)||typeof n!="object"&&typeof n!="function")return{};const r=i.watch(n,()=>{t||(s?s():e.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),k=()=>(!S&&window.navigator?.onLine)??!0,A=new Set,I=e=>(A.add(e),()=>{A.delete(e)}),B=()=>{!x()||!k()||A.forEach(e=>{e()})};!S&&window?.addEventListener&&(window.addEventListener("visibilitychange",B,!1),window.addEventListener("focus",B,!1));const K=b((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const s=i.ref(),{is:r}=p(i.toValue(t));if(!r||C(i.toValue(n))||typeof i.toValue(n)!="boolean")return{};const u=(a,o)=>{let l=!1;return()=>{l||(l=!0,a(),setTimeout(()=>{l=!1},o))}};return i.watchEffect(()=>{s.value?.(),i.toValue(n)&&(s.value=I(u(e.refresh,i.toValue(t))))}),i.onUnmounted(()=>{s.value?.()}),{}}),ee=b((e,{manual:t,ready:n=!0,defaultParams:s=[]})=>{const r=i.watch(()=>i.toValue(n),u=>{!t&&u&&e.run(...s)},{flush:"sync"});return{onBefore:()=>({isReady:i.toValue(n)}),onCancel:()=>{r()}}});function te(e,t,n){var s=null,r=null,u=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),a=!1,o=null,l=u.leading,v=u.trailing,f=function(){s=null,r=null},g=function(){a=!0,e.apply(r,s),f()},m=function(){if(l===!0&&(o=null),a){f();return}if(v===!0){g();return}f()},w=function(){var c=o!==null;return c&&clearTimeout(o),f(),o=null,a=!1,c},d=function(){a=!1,s=arguments,r=this,o===null?l===!0&&g():clearTimeout(o),o=setTimeout(m,t)};return d.cancel=w,d}const ne=b((e,{debounceWait:t,debounceOptions:n,manual:s})=>{const r=i.ref(!1);let u=null;const a=e.runAsync;return s||(r.value=!0),i.watchEffect(()=>{const{is:o,value:l}=p(i.toValue(t));if(!o)return;const v=i.toValue(n);u=te(f=>f(),l,v),e.runAsync=(...f)=>new Promise((g,m)=>{r.value?(r.value=!1,a(...f).then(g).catch(m)):u(()=>{a(...f).then(g).catch(m)})}),i.onWatcherCleanup(()=>{u?.cancel(),e.runAsync=a})}),{onCancel:()=>{u?.cancel()}}});function re(e,t,n){var s=null,r=null,u=!1,a=null,o=Object.assign({leading:!0,trailing:!0},n),l=o.leading,v=o.trailing,f=function(){s=null,r=null},g=function(){u=!0,e.apply(r,s),a=setTimeout(m,t),f()},m=function(){if(a=null,u){f();return}if(v===!0){g();return}f()},w=function(){var c=a!==null;return c&&clearTimeout(a),f(),a=null,u=!1,c},d=function(){if(s=arguments,r=this,u=!1,a===null&&l===!0){g();return}v===!0&&(a=setTimeout(m,t))};return d.cancel=w,d}const se=b((e,{throttleWait:t,throttleOptions:n})=>{let s=null;const r=e.runAsync;return i.watchEffect(()=>{const{is:u,value:a}=p(i.toValue(t));if(!u)return;const o=i.toValue(n);s=re(l=>l(),a,o),e.runAsync=(...l)=>new Promise((v,f)=>{s(()=>{r(...l).then(v).catch(f)})}),i.onWatcherCleanup(()=>{s?.cancel(),e.runAsync=r})}),{onCancel:()=>{s?.cancel()}}}),ie=[Q,Z,G,K,W,ee,ne,se,J];function ae(e,t,n){return D(e,t||{},[...n||[],...ie])}exports.clearCache=E;exports.definePlugin=b;exports.useRequest=ae;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("vue"),p=e=>e,y=e=>e==null,S=typeof window>"u";function x(e){return y(e)?e:typeof e=="number"&&!isNaN(e)}function b(e,t){return x(e)===void 0?{is:!1,value:0}:x(e)?t&&e===-1?{is:!0,value:-1}:{is:!0,value:e===0||e&&e<0?0:e}:{is:!1,value:0}}const M=(e,t)=>{let n=t;for(let i=e.length;i-- >0;){const r=e[i];n=r(n)}return n?.()},V=()=>new Promise(()=>{}),P=new Map,O=(e,t,{data:n,params:i,time:r})=>{let a;const u=L(e),{is:o,value:f}=b(t,!0);o&&(u?.timer&&clearTimeout(u.timer),f!==-1&&(a=setTimeout(()=>{A(e)},f)),P.set(e,{data:n,params:i,time:r,timer:a}))},L=e=>P.get(e),A=e=>{e&&typeof e=="string"?P.delete(e):P.clear()};class j{constructor(t,n){this.service=t,this.options=n,this.state=s.reactive({data:void 0,res:void 0,error:void 0,params:n?.defaultParams||[],loading:!1,isFinished:!1,isAborted:!1,signal:new AbortController().signal})}currentRequestId=0;pluginImpls=[];state;abort=()=>{};setState=t=>{if(Object.assign(this.state,t),t.data&&typeof t.data=="object"&&!y(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)&&(this.state.res=t.data[n])}};executePlugin=(t,...n)=>{if(t==="onRequest")return{servicePromise:M(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const i=this.pluginImpls.map(r=>r[t]?.(...n)).filter(Boolean);return Object.assign({},...i)}};loading=t=>{this.setState({loading:t,isFinished:!t})};onFinished=()=>{this.executePlugin("onFinally",this.state.params),this.loading(!1),this.options.onFinally?.(this.state.params)};runAsync=async(...t)=>{const n=++this.currentRequestId,{isReturn:i,isReady:r,...a}=this.executePlugin("onBefore",t);if(!r)return V();if(this.setState({params:t,...a}),this.loading(!0),i)return this.loading(!1),a.data;this.options.onBefore?.(t);try{const u=()=>this.service(...t);let{servicePromise:o}=this.executePlugin("onRequest",u);return o||(o=u()),n!==this.currentRequestId?V():(this.setState({data:await o,error:void 0}),this.executePlugin("onSuccess",t),this.options.onSuccess?.(t),this.onFinished(),this.state.data)}catch(u){if(n!==this.currentRequestId)return V();throw this.setState({data:void 0,res:void 0,error:u}),this.executePlugin("onError",t),this.options.onError?.(t),this.onFinished(),this.state.error}};run=(...t)=>{this.runAsync(...t).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function N(e,t,n){const i={manual:!1,...t},r=new j(e,i);r.pluginImpls=n.map(h=>h(r,i)),s.onMounted(()=>{if(!i.manual){const h=r.state.params;r.run(...h)}}),s.onUnmounted(r.cancel);const{run:a,cancel:u,refresh:o,runAsync:f,refreshAsync:v,abort:l}=r;return{...s.toRefs(r.state),run:a,cancel:u,refresh:o,runAsync:f,abort:l,refreshAsync:v,clearCache:A}}const U=p((e,{abortPrevious:t=!0})=>{let n=null;const i=()=>{n=new AbortController,e.setState({signal:n.signal,isAborted:n.signal.aborted})};return e.abort=()=>{n&&!n.signal.aborted&&!e.state.isFinished&&(n.abort(),e.setState({isAborted:n.signal.aborted}))},{onBefore:()=>{typeof t=="boolean"&&t&&e.abort(),i()},onRequest:r=>()=>r(),onCancel:()=>{e.abort(),n=null}}}),R=new Map,W=(e,{data:t,params:n,time:i})=>{R.has(e)&&R.get(e)?.forEach(a=>a({data:t,params:n,time:i}))},B=(e,t)=>(R.has(e)?R.get(e)?.add(t):R.set(e,new Set),()=>{const n=R.get(e);n?.delete(t),n?.size===0&&R.delete(e)}),C=new Map,_=(e,t)=>{C.set(e,t),t.then(()=>{}).catch(()=>{}).finally(()=>{H(e)})},D=e=>C.get(e),H=e=>{e&&typeof e=="string"?C.delete(e):C.clear()},$=p((e,{cacheKey:t,cacheTime:n=3e5,staleTime:i=0,setCache:r,getCache:a})=>{const u=s.ref(null);let o=null;const{is:f,value:v}=b(i,!0);if(!f)return{};const l=typeof t=="function"?t:()=>t,h=(d,c)=>{r?r(d,c):O(d,n,c),W(d,c)},m=d=>a?a(d):L(d);function w(){const d=l();if(!d)return;const c=m(d);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),u.value=B(d,g=>{e.setState({data:g.data})})}return w(),s.onUnmounted(()=>{u.value?.()}),{onBefore:d=>{const c=l(d);if(!c)return null;const g=m(c);return!g||!Reflect.has(g,"data")?{}:v===-1||Date.now()-g.time<v?{data:g.data,error:void 0,isReturn:!0}:{data:g.data,error:void 0}},onRequest:d=>{const c=l(e.state.params);if(!c)return d;let g=D(c);return g&&g!==o?()=>g:(g=d(),o=g,_(c,g),()=>g)},onSuccess:d=>{const c=l(d);c&&(u.value?.(),h(c,{data:e.state.data,params:d,time:Date.now()}),u.value=B(c,g=>{e.setState({data:g.data})}))}}}),z=p((e,{errorRetryCount:t=0,errorRetryInterval:n})=>{const i=s.ref(),r=s.ref(0),a=s.ref(!1),u=s.computed(()=>1e3*Math.pow(2,r.value)),o=()=>{let l=null;r.value++;const{value:h}=b(s.toValue(t),!0),m=h===-1,w=r.value<=h;if(m||w){let{is:d,value:c}=b(s.toValue(n));d||(c=s.toValue(Math.min(u.value,3e4))),l=setTimeout(()=>{a.value=!0,e.refresh()},c)}return()=>{l&&clearTimeout(l)}},f=()=>{i.value?.()},{is:v}=b(s.toValue(t),!0);return v?{onBefore:()=>{a.value||(r.value=0),a.value=!1,f()},onSuccess:()=>{r.value=0},onError:()=>{i.value=o()},onCancel:()=>{r.value=0,f()}}:{}}),E=()=>S||y(window.document?.visibilityState)?!0:document.visibilityState==="visible",T=new Set,J=e=>(T.add(e),()=>{T.delete(e)}),Q=()=>{E()&&T.forEach(e=>{e()})};!S&&window?.addEventListener&&window.addEventListener("visibilitychange",Q,!1);const X=p((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:i})=>{const r=s.ref(null),a=s.ref(),u=()=>{let o;const{value:f}=b(s.toValue(i),!0);if(e.state.error&&f!==0)return;const{is:v,value:l}=b(s.toValue(t));return v?(o=setTimeout(()=>{!s.toValue(n)&&!E()?r.value=J(e.refresh):e.refresh()},l),()=>{o&&clearTimeout(o),r.value?.()}):void 0};return s.watch([()=>s.toValue(t),()=>s.toValue(n)],()=>{a.value?.(),a.value=u()}),s.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{a.value?.()},onCancel:()=>{a.value?.()},onFinally:()=>{a.value=u()}}}),Y=p((e,{manual:t,refreshDeps:n,refreshDepsAction:i})=>{if(y(n)||typeof n!="object"&&typeof n!="function")return{};const r=s.watch(n,()=>{t||(i?i():e.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),Z=()=>(!S&&window.navigator?.onLine)??!0,F=new Set,G=e=>(F.add(e),()=>{F.delete(e)}),q=()=>{!E()||!Z()||F.forEach(e=>{e()})};!S&&window?.addEventListener&&(window.addEventListener("visibilitychange",q,!1),window.addEventListener("focus",q,!1));const I=p((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const i=s.ref(),{is:r}=b(s.toValue(t));if(!r||y(s.toValue(n))||typeof s.toValue(n)!="boolean")return{};const a=(u,o)=>{let f=!1;return()=>{f||(f=!0,u(),setTimeout(()=>{f=!1},o))}};return s.watchEffect(()=>{i.value?.(),s.toValue(n)&&(i.value=G(a(e.refresh,s.toValue(t))))}),s.onUnmounted(()=>{i.value?.()}),{}}),k=p((e,{manual:t,ready:n=!0,defaultParams:i=[]})=>{const r=s.watch(()=>s.toValue(n),a=>{!t&&a&&e.run(...i)},{flush:"sync"});return{onBefore:()=>({isReady:s.toValue(n)}),onCancel:()=>{r()}}});function K(e,t,n){var i=null,r=null,a=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),u=!1,o=null,f=a.leading,v=a.trailing,l=function(){i=null,r=null},h=function(){u=!0,e.apply(r,i),l()},m=function(){if(f===!0&&(o=null),u){l();return}if(v===!0){h();return}l()},w=function(){var c=o!==null;return c&&clearTimeout(o),l(),o=null,u=!1,c},d=function(){u=!1,i=arguments,r=this,o===null?f===!0&&h():clearTimeout(o),o=setTimeout(m,t)};return d.cancel=w,d}const ee=p((e,{debounceWait:t,debounceOptions:n,manual:i})=>{const r=s.ref(!1);let a=null;const u=e.runAsync;return i||(r.value=!0),s.watchEffect(()=>{const{is:o,value:f}=b(s.toValue(t));if(!o)return;const v=s.toValue(n);a=K(l=>l(),f,v),e.runAsync=(...l)=>new Promise((h,m)=>{r.value?(r.value=!1,u(...l).then(h).catch(m)):a(()=>{u(...l).then(h).catch(m)})}),s.onWatcherCleanup(()=>{a?.cancel(),e.runAsync=u})}),{onCancel:()=>{a?.cancel()}}});function te(e,t,n){var i=null,r=null,a=!1,u=null,o=Object.assign({leading:!0,trailing:!0},n),f=o.leading,v=o.trailing,l=function(){i=null,r=null},h=function(){a=!0,e.apply(r,i),u=setTimeout(m,t),l()},m=function(){if(u=null,a){l();return}if(v===!0){h();return}l()},w=function(){var c=u!==null;return c&&clearTimeout(u),l(),u=null,a=!1,c},d=function(){if(i=arguments,r=this,a=!1,u===null&&f===!0){h();return}v===!0&&(u=setTimeout(m,t))};return d.cancel=w,d}const ne=p((e,{throttleWait:t,throttleOptions:n})=>{let i=null;const r=e.runAsync;return s.watchEffect(()=>{const{is:a,value:u}=b(s.toValue(t));if(!a)return;const o=s.toValue(n);i=te(f=>f(),u,o),e.runAsync=(...f)=>new Promise((v,l)=>{i(()=>{r(...f).then(v).catch(l)})}),s.onWatcherCleanup(()=>{i?.cancel(),e.runAsync=r})}),{onCancel:()=>{i?.cancel()}}}),re=[z,X,Y,I,U,k,ee,ne,$];function ie(e,t,n){return N(e,t||{},[...n||[],...re])}exports.clearCache=A;exports.definePlugin=p;exports.useRequest=ie;
@@ -3,11 +3,11 @@ import { Ref } from 'vue';
3
3
  import { ToRefs } from 'vue';
4
4
  import { WatchSource } from 'vue';
5
5
 
6
- declare type BaseOptions<D, P extends any[]> = Partial<{
6
+ declare type BaseOptions<D, P extends any[], K = "data"> = Partial<{
7
7
  onBefore: (params: P) => void;
8
- onSuccess: (data: D, params: P) => void;
9
- onFinally: (params: P, data?: D, error?: Error) => void;
10
- onError: (error: Error, params: P) => void;
8
+ onSuccess: (params: P) => void;
9
+ onFinally: (params: P) => void;
10
+ onError: (params: P) => void;
11
11
  manual: boolean;
12
12
  defaultParams: P;
13
13
  refreshDeps: WatchSource | WatchSource[] | object;
@@ -29,10 +29,12 @@ declare type BaseOptions<D, P extends any[]> = Partial<{
29
29
  throttleWait: Ref<number> | number;
30
30
  throttleOptions: Reactive<ThrottleOptionsType> | ThrottleOptionsType;
31
31
  abortPrevious: boolean;
32
- }>;
32
+ }> & (D extends object ? {
33
+ resKey?: K;
34
+ } : {});
33
35
 
34
36
  declare type CacheParamsType<D = any, P = any> = {
35
- data?: D;
37
+ data: D;
36
38
  params: P;
37
39
  time: number;
38
40
  } & {
@@ -48,12 +50,15 @@ declare type DebounceOptionsType = {
48
50
 
49
51
  export declare const definePlugin: <D = any, P extends any[] = any, O = {}>(plugin: Plugin_2<D, P, O>) => Plugin_2<D, P, O>;
50
52
 
51
- declare interface IState<D, P extends any[]> {
52
- data?: D;
53
+ declare type ExtractFieldType<D, K> = D extends object ? K extends keyof D ? D[K] : "data" extends keyof D ? D["data"] : never : never;
54
+
55
+ declare interface IState<D, P extends any[], K = "data"> {
56
+ data: D;
57
+ res: ExtractFieldType<D, K>;
53
58
  loading: boolean;
54
59
  isFinished: boolean;
55
60
  isAborted: boolean;
56
- error?: Error;
61
+ error: Error;
57
62
  params: P;
58
63
  signal: AbortSignal;
59
64
  }
@@ -66,27 +71,30 @@ declare type PluginMethodsReturn<D, P extends any[]> = Partial<{
66
71
  signal: AbortSignal;
67
72
  isReturn: boolean;
68
73
  isReady: boolean;
69
- data?: D;
74
+ data: D;
70
75
  }>;
71
76
 
72
77
  declare type PluginReturn<D, P extends any[]> = Partial<{
73
78
  onBefore: (params: P) => void;
74
- onSuccess: (data: D, params: P) => void;
75
- onFinally: (params: P, data: D, error: Error) => void;
76
- onError: (error: Error, params: P) => void;
79
+ onSuccess: (params: P) => void;
80
+ onError: (params: P) => void;
81
+ onFinally: (params: P) => void;
77
82
  onCancel: () => void;
78
- onMutate: (data: D) => void;
79
83
  onRequest: (service: ServiceType<D, P>) => ServiceType<D, P>;
80
84
  }>;
81
85
 
82
86
  declare class Request_2<D, P extends any[]> {
83
87
  service: ServiceType<D, P>;
84
- options: BaseOptions<D, P>;
88
+ options: BaseOptions<D, P> & {
89
+ resKey: keyof D;
90
+ };
85
91
  currentRequestId: number;
86
92
  pluginImpls: PluginReturn<D, P>[];
87
93
  state: IState<D, P>;
88
94
  abort: () => void;
89
- constructor(service: ServiceType<D, P>, options: BaseOptions<D, P>);
95
+ constructor(service: ServiceType<D, P>, options: BaseOptions<D, P> & {
96
+ resKey: keyof D;
97
+ });
90
98
  setState: (s: Partial<IState<D, P>>) => void;
91
99
  executePlugin: (event: keyof PluginReturn<D, P>, ...rest: any[]) => PluginMethodsReturn<D, P>;
92
100
  loading: (loading: boolean) => void;
@@ -95,7 +103,6 @@ declare class Request_2<D, P extends any[]> {
95
103
  run: (...params: P) => void;
96
104
  refresh: () => void;
97
105
  refreshAsync: () => Promise<D>;
98
- mutate: (data: D | ((data: D) => D)) => void;
99
106
  cancel: () => void;
100
107
  }
101
108
 
@@ -108,11 +115,17 @@ declare type ThrottleOptionsType = {
108
115
 
109
116
  declare type Timeout = ReturnType<typeof setTimeout>;
110
117
 
111
- export declare function useRequest<D, P extends any[] = any[]>(service: ServiceType<D, P>, options?: BaseOptions<D, P>): UseRequestReturnType<D, P>;
118
+ export declare function useRequest<D extends object, K extends keyof D, P extends any[] = any[]>(service: ServiceType<D, P>, options: BaseOptions<D, P, K> & {
119
+ resKey: K;
120
+ }): UseRequestReturnType<D, P, K>;
121
+
122
+ export declare function useRequest<D extends object, P extends any[] = any[]>(service: ServiceType<D, P>, options?: BaseOptions<D, P, "data">): UseRequestReturnType<D, P, "data">;
123
+
124
+ export declare function useRequest<D, P extends any[] = any[]>(service: ServiceType<D, P>, options?: BaseOptions<D, P, never>): UseRequestReturnType<D, P, never>;
112
125
 
113
126
  export declare function useRequest<D, P extends any[] = any[], O = {}>(service: ServiceType<D, P>, options: BaseOptions<D, P> & O, plugins: Plugin_2<D, P, O>[]): UseRequestReturnType<D, P>;
114
127
 
115
- declare interface UseRequestReturnType<D, P extends any[]> extends ToRefs<IState<D, P>> {
128
+ declare interface UseRequestReturnType<D, P extends any[], K = "data"> extends ToRefs<IState<D, P, K>> {
116
129
  run: (...args: P) => void;
117
130
  cancel: () => void;
118
131
  refresh: () => void;
@@ -120,7 +133,6 @@ declare interface UseRequestReturnType<D, P extends any[]> extends ToRefs<IState
120
133
  abort: () => void;
121
134
  refreshAsync: () => Promise<D>;
122
135
  clearCache: (key?: string) => void;
123
- mutate: (data: D | ((data: D) => D)) => void;
124
136
  }
125
137
 
126
138
  export { }
@@ -1,13 +1,13 @@
1
- import { reactive as H, onMounted as W, onUnmounted as S, toRefs as $, ref as w, computed as z, toValue as h, watch as M, watchEffect as L, onWatcherCleanup as N } from "vue";
2
- const b = (e) => e, F = (e) => e == null, T = typeof window > "u";
3
- function j(e) {
4
- return F(e) ? e : typeof e == "number" && !isNaN(e);
1
+ import { reactive as D, onMounted as H, onUnmounted as F, toRefs as W, ref as R, computed as $, toValue as d, watch as B, watchEffect as L, onWatcherCleanup as N } from "vue";
2
+ const b = (e) => e, P = (e) => e == null, T = typeof window > "u";
3
+ function O(e) {
4
+ return P(e) ? e : typeof e == "number" && !isNaN(e);
5
5
  }
6
6
  function p(e, t) {
7
- return j(e) === void 0 ? {
7
+ return O(e) === void 0 ? {
8
8
  is: !1,
9
9
  value: 0
10
- } : j(e) ? t && e === -1 ? {
10
+ } : O(e) ? t && e === -1 ? {
11
11
  is: !0,
12
12
  value: -1
13
13
  } : {
@@ -18,27 +18,28 @@ function p(e, t) {
18
18
  value: 0
19
19
  };
20
20
  }
21
- const U = (e, t) => {
21
+ const z = (e, t) => {
22
22
  let n = t;
23
- for (let s = e.length; s-- > 0; ) {
24
- const r = e[s];
23
+ for (let i = e.length; i-- > 0; ) {
24
+ const r = e[i];
25
25
  n = r(n);
26
26
  }
27
27
  return n?.();
28
28
  }, A = () => new Promise(() => {
29
- }), J = (e) => e instanceof Function, P = /* @__PURE__ */ new Map(), Q = (e, t, { data: n, params: s, time: r }) => {
30
- let a;
31
- const i = V(e), { is: u, value: o } = p(t, !0);
32
- u && (i?.timer && clearTimeout(i.timer), o !== -1 && (a = setTimeout(() => {
33
- D(e);
34
- }, o)), P.set(e, { data: n, params: s, time: r, timer: a }));
35
- }, V = (e) => P.get(e), D = (e) => {
36
- e && typeof e == "string" ? P.delete(e) : P.clear();
29
+ }), C = /* @__PURE__ */ new Map(), U = (e, t, { data: n, params: i, time: r }) => {
30
+ let s;
31
+ const a = V(e), { is: u, value: c } = p(t, !0);
32
+ u && (a?.timer && clearTimeout(a.timer), c !== -1 && (s = setTimeout(() => {
33
+ _(e);
34
+ }, c)), C.set(e, { data: n, params: i, time: r, timer: s }));
35
+ }, V = (e) => C.get(e), _ = (e) => {
36
+ e && typeof e == "string" ? C.delete(e) : C.clear();
37
37
  };
38
- class X {
38
+ class J {
39
39
  constructor(t, n) {
40
- this.service = t, this.options = n, this.state = H({
40
+ this.service = t, this.options = n, this.state = D({
41
41
  data: void 0,
42
+ res: void 0,
42
43
  error: void 0,
43
44
  params: n?.defaultParams || [],
44
45
  loading: !1,
@@ -53,59 +54,57 @@ class X {
53
54
  abort = () => {
54
55
  };
55
56
  setState = (t) => {
56
- Object.assign(this.state, t);
57
+ if (Object.assign(this.state, t), t.data && typeof t.data == "object" && !P(t.data)) {
58
+ const n = this.options.resKey || "data";
59
+ Reflect.has(t.data, n) && (this.state.res = t.data[n]);
60
+ }
57
61
  };
58
62
  executePlugin = (t, ...n) => {
59
63
  if (t === "onRequest")
60
64
  return {
61
- servicePromise: U(
65
+ servicePromise: z(
62
66
  this.pluginImpls.map((r) => r.onRequest).filter(Boolean),
63
67
  n[0]
64
68
  )
65
69
  };
66
70
  {
67
- const s = this.pluginImpls.map((r) => r[t]?.(...n)).filter(Boolean);
68
- return Object.assign({}, ...s);
71
+ const i = this.pluginImpls.map((r) => r[t]?.(...n)).filter(Boolean);
72
+ return Object.assign({}, ...i);
69
73
  }
70
74
  };
71
- // 设置加载状态
72
75
  loading = (t) => {
73
76
  this.setState({ loading: t, isFinished: !t });
74
77
  };
75
- // 请求完成
76
78
  onFinished = () => {
77
79
  this.executePlugin(
78
80
  "onFinally",
79
- this.state.params,
80
- this.state.data,
81
- this.state.error
81
+ this.state.params
82
+ // this.state.data,
83
+ // this.state.error
82
84
  ), this.loading(!1), this.options.onFinally?.(
83
- this.state.params,
84
- this.state.data,
85
- this.state.error
85
+ this.state.params
86
+ // this.state.data,
87
+ // this.state.error
86
88
  );
87
89
  };
88
90
  runAsync = async (...t) => {
89
- const n = ++this.currentRequestId, { isReturn: s, isReady: r, ...a } = this.executePlugin(
91
+ const n = ++this.currentRequestId, { isReturn: i, isReady: r, ...s } = this.executePlugin(
90
92
  "onBefore",
91
93
  t
92
94
  );
93
95
  if (!r)
94
96
  return A();
95
- if (this.setState({ params: t, ...a }), this.loading(!0), s)
96
- return this.loading(!1), a.data;
97
+ if (this.setState({ params: t, ...s }), this.loading(!0), i)
98
+ return this.loading(!1), s.data;
97
99
  this.options.onBefore?.(t);
98
100
  try {
99
- const i = () => this.service(...t);
100
- let { servicePromise: u } = this.executePlugin("onRequest", i);
101
- u || (u = i());
102
- const o = await u;
103
- return n !== this.currentRequestId ? A() : (this.setState({ data: o, error: void 0 }), this.executePlugin("onSuccess", o, t), this.options.onSuccess?.(o, t), this.onFinished(), o);
104
- } catch (i) {
101
+ const a = () => this.service(...t);
102
+ let { servicePromise: u } = this.executePlugin("onRequest", a);
103
+ return u || (u = a()), n !== this.currentRequestId ? A() : (this.setState({ data: await u, error: void 0 }), this.executePlugin("onSuccess", t), this.options.onSuccess?.(t), this.onFinished(), this.state.data);
104
+ } catch (a) {
105
105
  if (n !== this.currentRequestId)
106
106
  return A();
107
- const u = i;
108
- throw this.setState({ data: void 0, error: u }), this.executePlugin("onError", u, t), this.options.onError?.(u, t), this.onFinished(), u;
107
+ throw this.setState({ data: void 0, res: void 0, error: a }), this.executePlugin("onError", t), this.options.onError?.(t), this.onFinished(), this.state.error;
109
108
  }
110
109
  };
111
110
  run = (...t) => {
@@ -117,42 +116,38 @@ class X {
117
116
  this.run(...this.state.params);
118
117
  };
119
118
  refreshAsync = async () => await this.runAsync(...this.state.params);
120
- mutate = (t) => {
121
- J(t) ? t(this.state.data) : this.setState({ data: t }), this.executePlugin("onMutate", this.state.data);
122
- };
123
119
  cancel = () => {
124
120
  this.executePlugin("onCancel"), this.currentRequestId++, this.loading(!1);
125
121
  };
126
122
  }
127
- function Y(e, t, n) {
128
- const s = {
123
+ function Q(e, t, n) {
124
+ const i = {
129
125
  manual: !1,
130
126
  ...t
131
- }, r = new X(e, s);
127
+ }, r = new J(e, i);
132
128
  r.pluginImpls = n.map(
133
- (m) => m(r, s)
134
- ), W(() => {
135
- if (!s.manual) {
136
- const m = r.state.params;
137
- r.run(...m);
129
+ (h) => h(r, i)
130
+ ), H(() => {
131
+ if (!i.manual) {
132
+ const h = r.state.params;
133
+ r.run(...h);
138
134
  }
139
- }), S(r.cancel);
140
- const { run: a, cancel: i, refresh: u, runAsync: o, refreshAsync: v, abort: c, mutate: g } = r;
135
+ }), F(r.cancel);
136
+ const { run: s, cancel: a, refresh: u, runAsync: c, refreshAsync: v, abort: o } = r;
141
137
  return {
142
- ...$(r.state),
143
- run: a,
144
- cancel: i,
138
+ ...W(r.state),
139
+ run: s,
140
+ cancel: a,
145
141
  refresh: u,
146
- runAsync: o,
147
- mutate: g,
148
- abort: c,
142
+ runAsync: c,
143
+ abort: o,
149
144
  refreshAsync: v,
150
- clearCache: D
145
+ clearCache: _
151
146
  };
152
147
  }
153
- const Z = b((e, { abortPrevious: t = !0 }) => {
148
+ const X = b((e, { abortPrevious: t = !0 }) => {
154
149
  let n = null;
155
- const s = () => {
150
+ const i = () => {
156
151
  n = new AbortController(), e.setState({
157
152
  signal: n.signal,
158
153
  isAborted: n.signal.aborted
@@ -164,189 +159,177 @@ const Z = b((e, { abortPrevious: t = !0 }) => {
164
159
  }));
165
160
  }, {
166
161
  onBefore: () => {
167
- typeof t == "boolean" && t && e.abort(), s();
162
+ typeof t == "boolean" && t && e.abort(), i();
168
163
  },
169
164
  onRequest: (r) => () => r(),
170
165
  onCancel: () => {
171
166
  e.abort(), n = null;
172
167
  }
173
168
  };
174
- }), y = /* @__PURE__ */ new Map(), G = (e, { data: t, params: n, time: s }) => {
175
- y.has(e) && y.get(e)?.forEach((a) => a({ data: t, params: n, time: s }));
176
- }, E = (e, t) => (y.has(e) ? y.get(e)?.add(t) : y.set(e, /* @__PURE__ */ new Set()), () => {
169
+ }), y = /* @__PURE__ */ new Map(), Y = (e, { data: t, params: n, time: i }) => {
170
+ y.has(e) && y.get(e)?.forEach((s) => s({ data: t, params: n, time: i }));
171
+ }, q = (e, t) => (y.has(e) ? y.get(e)?.add(t) : y.set(e, /* @__PURE__ */ new Set()), () => {
177
172
  const n = y.get(e);
178
173
  n?.delete(t), n?.size === 0 && y.delete(e);
179
- }), C = /* @__PURE__ */ new Map(), k = (e, t) => {
180
- C.set(e, t), t.then(() => {
174
+ }), S = /* @__PURE__ */ new Map(), Z = (e, t) => {
175
+ S.set(e, t), t.then(() => {
181
176
  }).catch(() => {
182
177
  }).finally(() => {
183
- K(e);
178
+ I(e);
184
179
  });
185
- }, I = (e) => C.get(e), K = (e) => {
186
- e && typeof e == "string" ? C.delete(e) : C.clear();
187
- }, ee = b(
180
+ }, G = (e) => S.get(e), I = (e) => {
181
+ e && typeof e == "string" ? S.delete(e) : S.clear();
182
+ }, k = b(
188
183
  (e, {
189
184
  cacheKey: t,
190
185
  cacheTime: n = 3e5,
191
- staleTime: s = 0,
186
+ staleTime: i = 0,
192
187
  setCache: r,
193
- getCache: a
188
+ getCache: s
194
189
  }) => {
195
- const i = w(null);
190
+ const a = R(null);
196
191
  let u = null;
197
- const { is: o, value: v } = p(s, !0);
198
- if (!o)
192
+ const { is: c, value: v } = p(i, !0);
193
+ if (!c)
199
194
  return {};
200
- const c = typeof t == "function" ? t : () => t, g = (f, l) => {
201
- r ? r(f, l) : Q(f, n, l), G(f, l);
202
- }, m = (f) => a ? a(f) : V(f);
203
- function R() {
204
- const f = c();
195
+ const o = typeof t == "function" ? t : () => t, h = (f, l) => {
196
+ r ? r(f, l) : U(f, n, l), Y(f, l);
197
+ }, m = (f) => s ? s(f) : V(f);
198
+ function w() {
199
+ const f = o();
205
200
  if (!f) return;
206
201
  const l = m(f);
207
202
  l && Reflect.has(l, "data") && e.setState({
208
203
  data: l.data,
209
204
  params: l.params
210
- }), i.value = E(f, (d) => {
205
+ }), a.value = q(f, (g) => {
211
206
  e.setState({
212
- data: d.data
207
+ data: g.data
213
208
  });
214
209
  });
215
210
  }
216
- return R(), S(() => {
217
- i.value?.();
211
+ return w(), F(() => {
212
+ a.value?.();
218
213
  }), {
219
214
  onBefore: (f) => {
220
- const l = c(f);
215
+ const l = o(f);
221
216
  if (!l) return null;
222
- const d = m(l);
223
- return !d || !Reflect.has(d, "data") ? {} : v === -1 || Date.now() - d.time < v ? {
224
- data: d.data,
217
+ const g = m(l);
218
+ return !g || !Reflect.has(g, "data") ? {} : v === -1 || Date.now() - g.time < v ? {
219
+ data: g.data,
225
220
  error: void 0,
226
221
  isReturn: !0
227
222
  } : {
228
- data: d.data,
223
+ data: g.data,
229
224
  error: void 0
230
225
  };
231
226
  },
232
227
  onRequest: (f) => {
233
- const l = c(e.state.params);
228
+ const l = o(e.state.params);
234
229
  if (!l) return f;
235
- let d = I(l);
236
- return d && d !== u ? () => d : (d = f(), u = d, k(l, d), () => d);
230
+ let g = G(l);
231
+ return g && g !== u ? () => g : (g = f(), u = g, Z(l, g), () => g);
237
232
  },
238
- onSuccess: (f, l) => {
239
- const d = c(l);
240
- d && (i.value?.(), g(d, {
241
- data: f,
242
- params: l,
233
+ onSuccess: (f) => {
234
+ const l = o(f);
235
+ l && (a.value?.(), h(l, {
236
+ data: e.state.data,
237
+ params: f,
243
238
  time: Date.now()
244
- }), i.value = E(d, (_) => {
239
+ }), a.value = q(l, (g) => {
245
240
  e.setState({
246
- data: _.data
247
- });
248
- }));
249
- },
250
- onMutate(f) {
251
- const l = c(e.state.params);
252
- l && (i.value?.(), g(l, {
253
- data: f,
254
- params: e.state.params,
255
- time: Date.now()
256
- }), i.value = E(l, (d) => {
257
- e.setState({
258
- data: d.data
241
+ data: g.data
259
242
  });
260
243
  }));
261
244
  }
262
245
  };
263
246
  }
264
- ), te = b((e, { errorRetryCount: t = 0, errorRetryInterval: n }) => {
265
- const s = w(), r = w(0), a = w(!1), i = z(
247
+ ), K = b((e, { errorRetryCount: t = 0, errorRetryInterval: n }) => {
248
+ const i = R(), r = R(0), s = R(!1), a = $(
266
249
  () => 1e3 * Math.pow(2, r.value)
267
250
  ), u = () => {
268
- let c = null;
251
+ let o = null;
269
252
  r.value++;
270
- const { value: g } = p(h(t), !0), m = g === -1, R = r.value <= g;
271
- if (m || R) {
272
- let { is: f, value: l } = p(h(n));
273
- f || (l = h(Math.min(i.value, 3e4))), c = setTimeout(() => {
274
- a.value = !0, e.refresh();
253
+ const { value: h } = p(d(t), !0), m = h === -1, w = r.value <= h;
254
+ if (m || w) {
255
+ let { is: f, value: l } = p(d(n));
256
+ f || (l = d(Math.min(a.value, 3e4))), o = setTimeout(() => {
257
+ s.value = !0, e.refresh();
275
258
  }, l);
276
259
  }
277
260
  return () => {
278
- c && clearTimeout(c);
261
+ o && clearTimeout(o);
279
262
  };
280
- }, o = () => {
281
- s.value?.();
282
- }, { is: v } = p(h(t), !0);
263
+ }, c = () => {
264
+ i.value?.();
265
+ }, { is: v } = p(d(t), !0);
283
266
  return v ? {
284
267
  onBefore: () => {
285
- a.value || (r.value = 0), a.value = !1, o();
268
+ s.value || (r.value = 0), s.value = !1, c();
286
269
  },
287
270
  onSuccess: () => {
288
271
  r.value = 0;
289
272
  },
290
273
  onError: () => {
291
- s.value = u();
274
+ i.value = u();
292
275
  },
293
276
  onCancel: () => {
294
- r.value = 0, o();
277
+ r.value = 0, c();
295
278
  }
296
279
  } : {};
297
- }), O = () => T || F(window.document?.visibilityState) ? !0 : document.visibilityState === "visible", x = /* @__PURE__ */ new Set(), ne = (e) => (x.add(e), () => {
298
- x.delete(e);
299
- }), re = () => {
300
- O() && x.forEach((e) => {
280
+ }), M = () => T || P(window.document?.visibilityState) ? !0 : document.visibilityState === "visible", E = /* @__PURE__ */ new Set(), ee = (e) => (E.add(e), () => {
281
+ E.delete(e);
282
+ }), te = () => {
283
+ M() && E.forEach((e) => {
301
284
  e();
302
285
  });
303
286
  };
304
- !T && window?.addEventListener && window.addEventListener("visibilitychange", re, !1);
305
- const se = b(
306
- (e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount: s }) => {
307
- const r = w(null), a = w(), i = () => {
287
+ !T && window?.addEventListener && window.addEventListener("visibilitychange", te, !1);
288
+ const ne = b(
289
+ (e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount: i }) => {
290
+ const r = R(null), s = R(), a = () => {
308
291
  let u;
309
- const { value: o } = p(
310
- h(s),
292
+ const { value: c } = p(
293
+ d(i),
311
294
  !0
312
295
  );
313
- if (e.state.error && o !== 0) return;
314
- const { is: v, value: c } = p(
315
- h(t)
296
+ if (e.state.error && c !== 0) return;
297
+ const { is: v, value: o } = p(
298
+ d(t)
316
299
  );
317
300
  return v ? (u = setTimeout(() => {
318
- !h(n) && !O() ? r.value = ne(e.refresh) : e.refresh();
319
- }, c), () => {
301
+ !d(n) && !M() ? r.value = ee(e.refresh) : e.refresh();
302
+ }, o), () => {
320
303
  u && clearTimeout(u), r.value?.();
321
304
  }) : void 0;
322
305
  };
323
- return M(
324
- [() => h(t), () => h(n)],
306
+ return B(
307
+ [() => d(t), () => d(n)],
325
308
  () => {
326
- a.value?.(), a.value = i();
309
+ s.value?.(), s.value = a();
327
310
  }
328
- ), S(() => {
311
+ ), F(() => {
329
312
  r.value?.();
330
313
  }), {
331
314
  onBefore: () => {
332
- a.value?.();
315
+ s.value?.();
333
316
  },
334
317
  onCancel: () => {
335
- a.value?.();
318
+ s.value?.();
336
319
  },
337
320
  onFinally: () => {
338
- a.value = i();
321
+ s.value = a();
339
322
  }
340
323
  };
341
324
  }
342
- ), ie = b(
343
- (e, { manual: t, refreshDeps: n, refreshDepsAction: s }) => {
344
- if (F(n) || typeof n != "object" && typeof n != "function")
325
+ ), re = b(
326
+ (e, { manual: t, refreshDeps: n, refreshDepsAction: i }) => {
327
+ if (P(n) || typeof n != "object" && typeof n != "function")
345
328
  return {};
346
- const r = M(
329
+ const r = B(
347
330
  n,
348
331
  () => {
349
- t || (s ? s() : e.refresh());
332
+ t || (i ? i() : e.refresh());
350
333
  },
351
334
  {
352
335
  deep: !0
@@ -358,175 +341,175 @@ const se = b(
358
341
  }
359
342
  };
360
343
  }
361
- ), ae = () => (!T && window.navigator?.onLine) ?? !0, B = /* @__PURE__ */ new Set(), ue = (e) => (B.add(e), () => {
362
- B.delete(e);
363
- }), q = () => {
364
- !O() || !ae() || B.forEach((e) => {
344
+ ), ie = () => (!T && window.navigator?.onLine) ?? !0, x = /* @__PURE__ */ new Set(), se = (e) => (x.add(e), () => {
345
+ x.delete(e);
346
+ }), j = () => {
347
+ !M() || !ie() || x.forEach((e) => {
365
348
  e();
366
349
  });
367
350
  };
368
- !T && window?.addEventListener && (window.addEventListener("visibilitychange", q, !1), window.addEventListener("focus", q, !1));
369
- const oe = b(
351
+ !T && window?.addEventListener && (window.addEventListener("visibilitychange", j, !1), window.addEventListener("focus", j, !1));
352
+ const ae = b(
370
353
  (e, { refocusTimespan: t = 5e3, refreshOnWindowFocus: n = !1 }) => {
371
- const s = w(), { is: r } = p(h(t));
372
- if (!r || F(h(n)) || typeof h(n) != "boolean")
354
+ const i = R(), { is: r } = p(d(t));
355
+ if (!r || P(d(n)) || typeof d(n) != "boolean")
373
356
  return {};
374
- const a = (i, u) => {
375
- let o = !1;
357
+ const s = (a, u) => {
358
+ let c = !1;
376
359
  return () => {
377
- o || (o = !0, i(), setTimeout(() => {
378
- o = !1;
360
+ c || (c = !0, a(), setTimeout(() => {
361
+ c = !1;
379
362
  }, u));
380
363
  };
381
364
  };
382
365
  return L(() => {
383
- s.value?.(), h(n) && (s.value = ue(
384
- a(e.refresh, h(t))
366
+ i.value?.(), d(n) && (i.value = se(
367
+ s(e.refresh, d(t))
385
368
  ));
386
- }), S(() => {
387
- s.value?.();
369
+ }), F(() => {
370
+ i.value?.();
388
371
  }), {};
389
372
  }
390
- ), le = b(
391
- (e, { manual: t, ready: n = !0, defaultParams: s = [] }) => {
392
- const r = M(
393
- () => h(n),
394
- (a) => {
395
- !t && a && e.run(...s);
373
+ ), ue = b(
374
+ (e, { manual: t, ready: n = !0, defaultParams: i = [] }) => {
375
+ const r = B(
376
+ () => d(n),
377
+ (s) => {
378
+ !t && s && e.run(...i);
396
379
  },
397
380
  {
398
381
  flush: "sync"
399
382
  }
400
383
  );
401
384
  return {
402
- onBefore: () => ({ isReady: h(n) }),
385
+ onBefore: () => ({ isReady: d(n) }),
403
386
  onCancel: () => {
404
387
  r();
405
388
  }
406
389
  };
407
390
  }
408
391
  );
409
- function ce(e, t, n) {
410
- var s = null, r = null, a = typeof n == "boolean" ? { leading: n, trailing: !n } : Object.assign({ leading: !1, trailing: !0 }, n), i = !1, u = null, o = a.leading, v = a.trailing, c = function() {
411
- s = null, r = null;
412
- }, g = function() {
413
- i = !0, e.apply(r, s), c();
392
+ function oe(e, t, n) {
393
+ var i = null, r = null, s = typeof n == "boolean" ? { leading: n, trailing: !n } : Object.assign({ leading: !1, trailing: !0 }, n), a = !1, u = null, c = s.leading, v = s.trailing, o = function() {
394
+ i = null, r = null;
395
+ }, h = function() {
396
+ a = !0, e.apply(r, i), o();
414
397
  }, m = function() {
415
- if (o === !0 && (u = null), i) {
416
- c();
398
+ if (c === !0 && (u = null), a) {
399
+ o();
417
400
  return;
418
401
  }
419
402
  if (v === !0) {
420
- g();
403
+ h();
421
404
  return;
422
405
  }
423
- c();
424
- }, R = function() {
406
+ o();
407
+ }, w = function() {
425
408
  var l = u !== null;
426
- return l && clearTimeout(u), c(), u = null, i = !1, l;
409
+ return l && clearTimeout(u), o(), u = null, a = !1, l;
427
410
  }, f = function() {
428
- i = !1, s = arguments, r = this, u === null ? o === !0 && g() : clearTimeout(u), u = setTimeout(m, t);
411
+ a = !1, i = arguments, r = this, u === null ? c === !0 && h() : clearTimeout(u), u = setTimeout(m, t);
429
412
  };
430
- return f.cancel = R, f;
413
+ return f.cancel = w, f;
431
414
  }
432
- const fe = b(
433
- (e, { debounceWait: t, debounceOptions: n, manual: s }) => {
434
- const r = w(!1);
435
- let a = null;
436
- const i = e.runAsync;
437
- return s || (r.value = !0), L(() => {
438
- const { is: u, value: o } = p(h(t));
415
+ const le = b(
416
+ (e, { debounceWait: t, debounceOptions: n, manual: i }) => {
417
+ const r = R(!1);
418
+ let s = null;
419
+ const a = e.runAsync;
420
+ return i || (r.value = !0), L(() => {
421
+ const { is: u, value: c } = p(d(t));
439
422
  if (!u) return;
440
- const v = h(n);
441
- a = ce(
442
- (c) => c(),
443
- o,
423
+ const v = d(n);
424
+ s = oe(
425
+ (o) => o(),
426
+ c,
444
427
  v
445
- ), e.runAsync = (...c) => new Promise((g, m) => {
446
- r.value ? (r.value = !1, i(...c).then(g).catch(m)) : a(() => {
447
- i(...c).then(g).catch(m);
428
+ ), e.runAsync = (...o) => new Promise((h, m) => {
429
+ r.value ? (r.value = !1, a(...o).then(h).catch(m)) : s(() => {
430
+ a(...o).then(h).catch(m);
448
431
  });
449
432
  }), N(() => {
450
- a?.cancel(), e.runAsync = i;
433
+ s?.cancel(), e.runAsync = a;
451
434
  });
452
435
  }), {
453
436
  onCancel: () => {
454
- a?.cancel();
437
+ s?.cancel();
455
438
  }
456
439
  };
457
440
  }
458
441
  );
459
- function de(e, t, n) {
460
- var s = null, r = null, a = !1, i = null, u = Object.assign({ leading: !0, trailing: !0 }, n), o = u.leading, v = u.trailing, c = function() {
461
- s = null, r = null;
462
- }, g = function() {
463
- a = !0, e.apply(r, s), i = setTimeout(m, t), c();
442
+ function ce(e, t, n) {
443
+ var i = null, r = null, s = !1, a = null, u = Object.assign({ leading: !0, trailing: !0 }, n), c = u.leading, v = u.trailing, o = function() {
444
+ i = null, r = null;
445
+ }, h = function() {
446
+ s = !0, e.apply(r, i), a = setTimeout(m, t), o();
464
447
  }, m = function() {
465
- if (i = null, a) {
466
- c();
448
+ if (a = null, s) {
449
+ o();
467
450
  return;
468
451
  }
469
452
  if (v === !0) {
470
- g();
453
+ h();
471
454
  return;
472
455
  }
473
- c();
474
- }, R = function() {
475
- var l = i !== null;
476
- return l && clearTimeout(i), c(), i = null, a = !1, l;
456
+ o();
457
+ }, w = function() {
458
+ var l = a !== null;
459
+ return l && clearTimeout(a), o(), a = null, s = !1, l;
477
460
  }, f = function() {
478
- if (s = arguments, r = this, a = !1, i === null && o === !0) {
479
- g();
461
+ if (i = arguments, r = this, s = !1, a === null && c === !0) {
462
+ h();
480
463
  return;
481
464
  }
482
- v === !0 && (i = setTimeout(m, t));
465
+ v === !0 && (a = setTimeout(m, t));
483
466
  };
484
- return f.cancel = R, f;
467
+ return f.cancel = w, f;
485
468
  }
486
- const he = b(
469
+ const fe = b(
487
470
  (e, { throttleWait: t, throttleOptions: n }) => {
488
- let s = null;
471
+ let i = null;
489
472
  const r = e.runAsync;
490
473
  return L(() => {
491
- const { is: a, value: i } = p(h(t));
492
- if (!a) return;
493
- const u = h(n);
494
- s = de(
495
- (o) => o(),
496
- i,
474
+ const { is: s, value: a } = p(d(t));
475
+ if (!s) return;
476
+ const u = d(n);
477
+ i = ce(
478
+ (c) => c(),
479
+ a,
497
480
  u
498
- ), e.runAsync = (...o) => new Promise((v, c) => {
499
- s(() => {
500
- r(...o).then(v).catch(c);
481
+ ), e.runAsync = (...c) => new Promise((v, o) => {
482
+ i(() => {
483
+ r(...c).then(v).catch(o);
501
484
  });
502
485
  }), N(() => {
503
- s?.cancel(), e.runAsync = r;
486
+ i?.cancel(), e.runAsync = r;
504
487
  });
505
488
  }), {
506
489
  onCancel: () => {
507
- s?.cancel();
490
+ i?.cancel();
508
491
  }
509
492
  };
510
493
  }
511
- ), ve = [
512
- te,
513
- se,
514
- ie,
515
- oe,
516
- Z,
494
+ ), de = [
495
+ K,
496
+ ne,
497
+ re,
498
+ ae,
499
+ X,
500
+ ue,
517
501
  le,
518
502
  fe,
519
- he,
520
- ee
503
+ k
521
504
  ];
522
- function me(e, t, n) {
523
- return Y(e, t || {}, [
505
+ function ve(e, t, n) {
506
+ return Q(e, t || {}, [
524
507
  ...n || [],
525
- ...ve
508
+ ...de
526
509
  ]);
527
510
  }
528
511
  export {
529
- D as clearCache,
512
+ _ as clearCache,
530
513
  b as definePlugin,
531
- me as useRequest
514
+ ve as useRequest
532
515
  };
@@ -1 +1 @@
1
- (function(w,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(w=typeof globalThis<"u"?globalThis:w||self,i(w.Vue3Request={},w.Vue))})(this,function(w,i){"use strict";const p=e=>e,P=e=>e==null,C=typeof window>"u";function B(e){return P(e)?e:typeof e=="number"&&!isNaN(e)}function b(e,t){return B(e)===void 0?{is:!1,value:0}:B(e)?t&&e===-1?{is:!0,value:-1}:{is:!0,value:e===0||e&&e<0?0:e}:{is:!1,value:0}}const q=(e,t)=>{let n=t;for(let s=e.length;s-- >0;){const r=e[s];n=r(n)}return n?.()},T=()=>new Promise(()=>{}),j=e=>e instanceof Function,S=new Map,N=(e,t,{data:n,params:s,time:r})=>{let o;const a=L(e),{is:l,value:u}=b(t,!0);l&&(a?.timer&&clearTimeout(a.timer),u!==-1&&(o=setTimeout(()=>{F(e)},u)),S.set(e,{data:n,params:s,time:r,timer:o}))},L=e=>S.get(e),F=e=>{e&&typeof e=="string"?S.delete(e):S.clear()};class U{constructor(t,n){this.service=t,this.options=n,this.state=i.reactive({data:void 0,error:void 0,params:n?.defaultParams||[],loading:!1,isFinished:!1,isAborted:!1,signal:new AbortController().signal})}currentRequestId=0;pluginImpls=[];state;abort=()=>{};setState=t=>{Object.assign(this.state,t)};executePlugin=(t,...n)=>{if(t==="onRequest")return{servicePromise:q(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const s=this.pluginImpls.map(r=>r[t]?.(...n)).filter(Boolean);return Object.assign({},...s)}};loading=t=>{this.setState({loading:t,isFinished:!t})};onFinished=()=>{this.executePlugin("onFinally",this.state.params,this.state.data,this.state.error),this.loading(!1),this.options.onFinally?.(this.state.params,this.state.data,this.state.error)};runAsync=async(...t)=>{const n=++this.currentRequestId,{isReturn:s,isReady:r,...o}=this.executePlugin("onBefore",t);if(!r)return T();if(this.setState({params:t,...o}),this.loading(!0),s)return this.loading(!1),o.data;this.options.onBefore?.(t);try{const a=()=>this.service(...t);let{servicePromise:l}=this.executePlugin("onRequest",a);l||(l=a());const u=await l;return n!==this.currentRequestId?T():(this.setState({data:u,error:void 0}),this.executePlugin("onSuccess",u,t),this.options.onSuccess?.(u,t),this.onFinished(),u)}catch(a){if(n!==this.currentRequestId)return T();const l=a;throw this.setState({data:void 0,error:l}),this.executePlugin("onError",l,t),this.options.onError?.(l,t),this.onFinished(),l}};run=(...t)=>{this.runAsync(...t).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);mutate=t=>{j(t)?t(this.state.data):this.setState({data:t}),this.executePlugin("onMutate",this.state.data)};cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function D(e,t,n){const s={manual:!1,...t},r=new U(e,s);r.pluginImpls=n.map(v=>v(r,s)),i.onMounted(()=>{if(!s.manual){const v=r.state.params;r.run(...v)}}),i.onUnmounted(r.cancel);const{run:o,cancel:a,refresh:l,runAsync:u,refreshAsync:g,abort:f,mutate:m}=r;return{...i.toRefs(r.state),run:o,cancel:a,refresh:l,runAsync:u,mutate:m,abort:f,refreshAsync:g,clearCache:F}}const W=p((e,{abortPrevious:t=!0})=>{let n=null;const s=()=>{n=new AbortController,e.setState({signal:n.signal,isAborted:n.signal.aborted})};return e.abort=()=>{n&&!n.signal.aborted&&!e.state.isFinished&&(n.abort(),e.setState({isAborted:n.signal.aborted}))},{onBefore:()=>{typeof t=="boolean"&&t&&e.abort(),s()},onRequest:r=>()=>r(),onCancel:()=>{e.abort(),n=null}}}),R=new Map,_=(e,{data:t,params:n,time:s})=>{R.has(e)&&R.get(e)?.forEach(o=>o({data:t,params:n,time:s}))},A=(e,t)=>(R.has(e)?R.get(e)?.add(t):R.set(e,new Set),()=>{const n=R.get(e);n?.delete(t),n?.size===0&&R.delete(e)}),V=new Map,H=(e,t)=>{V.set(e,t),t.then(()=>{}).catch(()=>{}).finally(()=>{z(e)})},$=e=>V.get(e),z=e=>{e&&typeof e=="string"?V.delete(e):V.clear()},J=p((e,{cacheKey:t,cacheTime:n=3e5,staleTime:s=0,setCache:r,getCache:o})=>{const a=i.ref(null);let l=null;const{is:u,value:g}=b(s,!0);if(!u)return{};const f=typeof t=="function"?t:()=>t,m=(d,c)=>{r?r(d,c):N(d,n,c),_(d,c)},v=d=>o?o(d):L(d);function y(){const d=f();if(!d)return;const c=v(d);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),a.value=A(d,h=>{e.setState({data:h.data})})}return y(),i.onUnmounted(()=>{a.value?.()}),{onBefore:d=>{const c=f(d);if(!c)return null;const h=v(c);return!h||!Reflect.has(h,"data")?{}:g===-1||Date.now()-h.time<g?{data:h.data,error:void 0,isReturn:!0}:{data:h.data,error:void 0}},onRequest:d=>{const c=f(e.state.params);if(!c)return d;let h=$(c);return h&&h!==l?()=>h:(h=d(),l=h,H(c,h),()=>h)},onSuccess:(d,c)=>{const h=f(c);h&&(a.value?.(),m(h,{data:d,params:c,time:Date.now()}),a.value=A(h,oe=>{e.setState({data:oe.data})}))},onMutate(d){const c=f(e.state.params);c&&(a.value?.(),m(c,{data:d,params:e.state.params,time:Date.now()}),a.value=A(c,h=>{e.setState({data:h.data})}))}}}),Q=p((e,{errorRetryCount:t=0,errorRetryInterval:n})=>{const s=i.ref(),r=i.ref(0),o=i.ref(!1),a=i.computed(()=>1e3*Math.pow(2,r.value)),l=()=>{let f=null;r.value++;const{value:m}=b(i.toValue(t),!0),v=m===-1,y=r.value<=m;if(v||y){let{is:d,value:c}=b(i.toValue(n));d||(c=i.toValue(Math.min(a.value,3e4))),f=setTimeout(()=>{o.value=!0,e.refresh()},c)}return()=>{f&&clearTimeout(f)}},u=()=>{s.value?.()},{is:g}=b(i.toValue(t),!0);return g?{onBefore:()=>{o.value||(r.value=0),o.value=!1,u()},onSuccess:()=>{r.value=0},onError:()=>{s.value=l()},onCancel:()=>{r.value=0,u()}}:{}}),E=()=>C||P(window.document?.visibilityState)?!0:document.visibilityState==="visible",x=new Set,X=e=>(x.add(e),()=>{x.delete(e)}),Y=()=>{E()&&x.forEach(e=>{e()})};!C&&window?.addEventListener&&window.addEventListener("visibilitychange",Y,!1);const Z=p((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:s})=>{const r=i.ref(null),o=i.ref(),a=()=>{let l;const{value:u}=b(i.toValue(s),!0);if(e.state.error&&u!==0)return;const{is:g,value:f}=b(i.toValue(t));return g?(l=setTimeout(()=>{!i.toValue(n)&&!E()?r.value=X(e.refresh):e.refresh()},f),()=>{l&&clearTimeout(l),r.value?.()}):void 0};return i.watch([()=>i.toValue(t),()=>i.toValue(n)],()=>{o.value?.(),o.value=a()}),i.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{o.value?.()},onCancel:()=>{o.value?.()},onFinally:()=>{o.value=a()}}}),G=p((e,{manual:t,refreshDeps:n,refreshDepsAction:s})=>{if(P(n)||typeof n!="object"&&typeof n!="function")return{};const r=i.watch(n,()=>{t||(s?s():e.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),k=()=>(!C&&window.navigator?.onLine)??!0,M=new Set,I=e=>(M.add(e),()=>{M.delete(e)}),O=()=>{!E()||!k()||M.forEach(e=>{e()})};!C&&window?.addEventListener&&(window.addEventListener("visibilitychange",O,!1),window.addEventListener("focus",O,!1));const K=p((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const s=i.ref(),{is:r}=b(i.toValue(t));if(!r||P(i.toValue(n))||typeof i.toValue(n)!="boolean")return{};const o=(a,l)=>{let u=!1;return()=>{u||(u=!0,a(),setTimeout(()=>{u=!1},l))}};return i.watchEffect(()=>{s.value?.(),i.toValue(n)&&(s.value=I(o(e.refresh,i.toValue(t))))}),i.onUnmounted(()=>{s.value?.()}),{}}),ee=p((e,{manual:t,ready:n=!0,defaultParams:s=[]})=>{const r=i.watch(()=>i.toValue(n),o=>{!t&&o&&e.run(...s)},{flush:"sync"});return{onBefore:()=>({isReady:i.toValue(n)}),onCancel:()=>{r()}}});function te(e,t,n){var s=null,r=null,o=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),a=!1,l=null,u=o.leading,g=o.trailing,f=function(){s=null,r=null},m=function(){a=!0,e.apply(r,s),f()},v=function(){if(u===!0&&(l=null),a){f();return}if(g===!0){m();return}f()},y=function(){var c=l!==null;return c&&clearTimeout(l),f(),l=null,a=!1,c},d=function(){a=!1,s=arguments,r=this,l===null?u===!0&&m():clearTimeout(l),l=setTimeout(v,t)};return d.cancel=y,d}const ne=p((e,{debounceWait:t,debounceOptions:n,manual:s})=>{const r=i.ref(!1);let o=null;const a=e.runAsync;return s||(r.value=!0),i.watchEffect(()=>{const{is:l,value:u}=b(i.toValue(t));if(!l)return;const g=i.toValue(n);o=te(f=>f(),u,g),e.runAsync=(...f)=>new Promise((m,v)=>{r.value?(r.value=!1,a(...f).then(m).catch(v)):o(()=>{a(...f).then(m).catch(v)})}),i.onWatcherCleanup(()=>{o?.cancel(),e.runAsync=a})}),{onCancel:()=>{o?.cancel()}}});function re(e,t,n){var s=null,r=null,o=!1,a=null,l=Object.assign({leading:!0,trailing:!0},n),u=l.leading,g=l.trailing,f=function(){s=null,r=null},m=function(){o=!0,e.apply(r,s),a=setTimeout(v,t),f()},v=function(){if(a=null,o){f();return}if(g===!0){m();return}f()},y=function(){var c=a!==null;return c&&clearTimeout(a),f(),a=null,o=!1,c},d=function(){if(s=arguments,r=this,o=!1,a===null&&u===!0){m();return}g===!0&&(a=setTimeout(v,t))};return d.cancel=y,d}const ie=p((e,{throttleWait:t,throttleOptions:n})=>{let s=null;const r=e.runAsync;return i.watchEffect(()=>{const{is:o,value:a}=b(i.toValue(t));if(!o)return;const l=i.toValue(n);s=re(u=>u(),a,l),e.runAsync=(...u)=>new Promise((g,f)=>{s(()=>{r(...u).then(g).catch(f)})}),i.onWatcherCleanup(()=>{s?.cancel(),e.runAsync=r})}),{onCancel:()=>{s?.cancel()}}}),se=[Q,Z,G,K,W,ee,ne,ie,J];function ae(e,t,n){return D(e,t||{},[...n||[],...se])}w.clearCache=F,w.definePlugin=p,w.useRequest=ae,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
1
+ (function(w,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(w=typeof globalThis<"u"?globalThis:w||self,i(w.Vue3Request={},w.Vue))})(this,function(w,i){"use strict";const p=e=>e,P=e=>e==null,C=typeof window>"u";function q(e){return P(e)?e:typeof e=="number"&&!isNaN(e)}function b(e,t){return q(e)===void 0?{is:!1,value:0}:q(e)?t&&e===-1?{is:!0,value:-1}:{is:!0,value:e===0||e&&e<0?0:e}:{is:!1,value:0}}const O=(e,t)=>{let n=t;for(let s=e.length;s-- >0;){const r=e[s];n=r(n)}return n?.()},T=()=>new Promise(()=>{}),V=new Map,j=(e,t,{data:n,params:s,time:r})=>{let a;const o=B(e),{is:l,value:f}=b(t,!0);l&&(o?.timer&&clearTimeout(o.timer),f!==-1&&(a=setTimeout(()=>{F(e)},f)),V.set(e,{data:n,params:s,time:r,timer:a}))},B=e=>V.get(e),F=e=>{e&&typeof e=="string"?V.delete(e):V.clear()};class N{constructor(t,n){this.service=t,this.options=n,this.state=i.reactive({data:void 0,res:void 0,error:void 0,params:n?.defaultParams||[],loading:!1,isFinished:!1,isAborted:!1,signal:new AbortController().signal})}currentRequestId=0;pluginImpls=[];state;abort=()=>{};setState=t=>{if(Object.assign(this.state,t),t.data&&typeof t.data=="object"&&!P(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)&&(this.state.res=t.data[n])}};executePlugin=(t,...n)=>{if(t==="onRequest")return{servicePromise:O(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const s=this.pluginImpls.map(r=>r[t]?.(...n)).filter(Boolean);return Object.assign({},...s)}};loading=t=>{this.setState({loading:t,isFinished:!t})};onFinished=()=>{this.executePlugin("onFinally",this.state.params),this.loading(!1),this.options.onFinally?.(this.state.params)};runAsync=async(...t)=>{const n=++this.currentRequestId,{isReturn:s,isReady:r,...a}=this.executePlugin("onBefore",t);if(!r)return T();if(this.setState({params:t,...a}),this.loading(!0),s)return this.loading(!1),a.data;this.options.onBefore?.(t);try{const o=()=>this.service(...t);let{servicePromise:l}=this.executePlugin("onRequest",o);return l||(l=o()),n!==this.currentRequestId?T():(this.setState({data:await l,error:void 0}),this.executePlugin("onSuccess",t),this.options.onSuccess?.(t),this.onFinished(),this.state.data)}catch(o){if(n!==this.currentRequestId)return T();throw this.setState({data:void 0,res:void 0,error:o}),this.executePlugin("onError",t),this.options.onError?.(t),this.onFinished(),this.state.error}};run=(...t)=>{this.runAsync(...t).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function U(e,t,n){const s={manual:!1,...t},r=new N(e,s);r.pluginImpls=n.map(h=>h(r,s)),i.onMounted(()=>{if(!s.manual){const h=r.state.params;r.run(...h)}}),i.onUnmounted(r.cancel);const{run:a,cancel:o,refresh:l,runAsync:f,refreshAsync:g,abort:u}=r;return{...i.toRefs(r.state),run:a,cancel:o,refresh:l,runAsync:f,abort:u,refreshAsync:g,clearCache:F}}const W=p((e,{abortPrevious:t=!0})=>{let n=null;const s=()=>{n=new AbortController,e.setState({signal:n.signal,isAborted:n.signal.aborted})};return e.abort=()=>{n&&!n.signal.aborted&&!e.state.isFinished&&(n.abort(),e.setState({isAborted:n.signal.aborted}))},{onBefore:()=>{typeof t=="boolean"&&t&&e.abort(),s()},onRequest:r=>()=>r(),onCancel:()=>{e.abort(),n=null}}}),R=new Map,_=(e,{data:t,params:n,time:s})=>{R.has(e)&&R.get(e)?.forEach(a=>a({data:t,params:n,time:s}))},L=(e,t)=>(R.has(e)?R.get(e)?.add(t):R.set(e,new Set),()=>{const n=R.get(e);n?.delete(t),n?.size===0&&R.delete(e)}),S=new Map,D=(e,t)=>{S.set(e,t),t.then(()=>{}).catch(()=>{}).finally(()=>{$(e)})},H=e=>S.get(e),$=e=>{e&&typeof e=="string"?S.delete(e):S.clear()},z=p((e,{cacheKey:t,cacheTime:n=3e5,staleTime:s=0,setCache:r,getCache:a})=>{const o=i.ref(null);let l=null;const{is:f,value:g}=b(s,!0);if(!f)return{};const u=typeof t=="function"?t:()=>t,h=(d,c)=>{r?r(d,c):j(d,n,c),_(d,c)},v=d=>a?a(d):B(d);function y(){const d=u();if(!d)return;const c=v(d);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),o.value=L(d,m=>{e.setState({data:m.data})})}return y(),i.onUnmounted(()=>{o.value?.()}),{onBefore:d=>{const c=u(d);if(!c)return null;const m=v(c);return!m||!Reflect.has(m,"data")?{}:g===-1||Date.now()-m.time<g?{data:m.data,error:void 0,isReturn:!0}:{data:m.data,error:void 0}},onRequest:d=>{const c=u(e.state.params);if(!c)return d;let m=H(c);return m&&m!==l?()=>m:(m=d(),l=m,D(c,m),()=>m)},onSuccess:d=>{const c=u(d);c&&(o.value?.(),h(c,{data:e.state.data,params:d,time:Date.now()}),o.value=L(c,m=>{e.setState({data:m.data})}))}}}),J=p((e,{errorRetryCount:t=0,errorRetryInterval:n})=>{const s=i.ref(),r=i.ref(0),a=i.ref(!1),o=i.computed(()=>1e3*Math.pow(2,r.value)),l=()=>{let u=null;r.value++;const{value:h}=b(i.toValue(t),!0),v=h===-1,y=r.value<=h;if(v||y){let{is:d,value:c}=b(i.toValue(n));d||(c=i.toValue(Math.min(o.value,3e4))),u=setTimeout(()=>{a.value=!0,e.refresh()},c)}return()=>{u&&clearTimeout(u)}},f=()=>{s.value?.()},{is:g}=b(i.toValue(t),!0);return g?{onBefore:()=>{a.value||(r.value=0),a.value=!1,f()},onSuccess:()=>{r.value=0},onError:()=>{s.value=l()},onCancel:()=>{r.value=0,f()}}:{}}),A=()=>C||P(window.document?.visibilityState)?!0:document.visibilityState==="visible",E=new Set,Q=e=>(E.add(e),()=>{E.delete(e)}),X=()=>{A()&&E.forEach(e=>{e()})};!C&&window?.addEventListener&&window.addEventListener("visibilitychange",X,!1);const Y=p((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:s})=>{const r=i.ref(null),a=i.ref(),o=()=>{let l;const{value:f}=b(i.toValue(s),!0);if(e.state.error&&f!==0)return;const{is:g,value:u}=b(i.toValue(t));return g?(l=setTimeout(()=>{!i.toValue(n)&&!A()?r.value=Q(e.refresh):e.refresh()},u),()=>{l&&clearTimeout(l),r.value?.()}):void 0};return i.watch([()=>i.toValue(t),()=>i.toValue(n)],()=>{a.value?.(),a.value=o()}),i.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{a.value?.()},onCancel:()=>{a.value?.()},onFinally:()=>{a.value=o()}}}),Z=p((e,{manual:t,refreshDeps:n,refreshDepsAction:s})=>{if(P(n)||typeof n!="object"&&typeof n!="function")return{};const r=i.watch(n,()=>{t||(s?s():e.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),G=()=>(!C&&window.navigator?.onLine)??!0,x=new Set,I=e=>(x.add(e),()=>{x.delete(e)}),M=()=>{!A()||!G()||x.forEach(e=>{e()})};!C&&window?.addEventListener&&(window.addEventListener("visibilitychange",M,!1),window.addEventListener("focus",M,!1));const k=p((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const s=i.ref(),{is:r}=b(i.toValue(t));if(!r||P(i.toValue(n))||typeof i.toValue(n)!="boolean")return{};const a=(o,l)=>{let f=!1;return()=>{f||(f=!0,o(),setTimeout(()=>{f=!1},l))}};return i.watchEffect(()=>{s.value?.(),i.toValue(n)&&(s.value=I(a(e.refresh,i.toValue(t))))}),i.onUnmounted(()=>{s.value?.()}),{}}),K=p((e,{manual:t,ready:n=!0,defaultParams:s=[]})=>{const r=i.watch(()=>i.toValue(n),a=>{!t&&a&&e.run(...s)},{flush:"sync"});return{onBefore:()=>({isReady:i.toValue(n)}),onCancel:()=>{r()}}});function ee(e,t,n){var s=null,r=null,a=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),o=!1,l=null,f=a.leading,g=a.trailing,u=function(){s=null,r=null},h=function(){o=!0,e.apply(r,s),u()},v=function(){if(f===!0&&(l=null),o){u();return}if(g===!0){h();return}u()},y=function(){var c=l!==null;return c&&clearTimeout(l),u(),l=null,o=!1,c},d=function(){o=!1,s=arguments,r=this,l===null?f===!0&&h():clearTimeout(l),l=setTimeout(v,t)};return d.cancel=y,d}const te=p((e,{debounceWait:t,debounceOptions:n,manual:s})=>{const r=i.ref(!1);let a=null;const o=e.runAsync;return s||(r.value=!0),i.watchEffect(()=>{const{is:l,value:f}=b(i.toValue(t));if(!l)return;const g=i.toValue(n);a=ee(u=>u(),f,g),e.runAsync=(...u)=>new Promise((h,v)=>{r.value?(r.value=!1,o(...u).then(h).catch(v)):a(()=>{o(...u).then(h).catch(v)})}),i.onWatcherCleanup(()=>{a?.cancel(),e.runAsync=o})}),{onCancel:()=>{a?.cancel()}}});function ne(e,t,n){var s=null,r=null,a=!1,o=null,l=Object.assign({leading:!0,trailing:!0},n),f=l.leading,g=l.trailing,u=function(){s=null,r=null},h=function(){a=!0,e.apply(r,s),o=setTimeout(v,t),u()},v=function(){if(o=null,a){u();return}if(g===!0){h();return}u()},y=function(){var c=o!==null;return c&&clearTimeout(o),u(),o=null,a=!1,c},d=function(){if(s=arguments,r=this,a=!1,o===null&&f===!0){h();return}g===!0&&(o=setTimeout(v,t))};return d.cancel=y,d}const re=p((e,{throttleWait:t,throttleOptions:n})=>{let s=null;const r=e.runAsync;return i.watchEffect(()=>{const{is:a,value:o}=b(i.toValue(t));if(!a)return;const l=i.toValue(n);s=ne(f=>f(),o,l),e.runAsync=(...f)=>new Promise((g,u)=>{s(()=>{r(...f).then(g).catch(u)})}),i.onWatcherCleanup(()=>{s?.cancel(),e.runAsync=r})}),{onCancel:()=>{s?.cancel()}}}),ie=[J,Y,Z,k,W,K,te,re,z];function se(e,t,n){return U(e,t||{},[...n||[],...ie])}w.clearCache=F,w.definePlugin=p,w.useRequest=se,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue3-request",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "type": "module",
5
5
  "description": "A Vue3 asynchronous request processing library, designed to simplify your asynchronous operations and API calls.",
6
6
  "main": "dist/vue3-request.es.js",