vue3-request 1.0.23 → 1.0.25

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,7 +1,44 @@
1
+ 更多内容请查看<a href="https://Flame-00.github.io/vue3-request/" target="_blank">文档</a>
2
+
3
+ [演练场](https://codesandbox.io/p/devbox/yrxqp8)
4
+
5
+ ## 核心优势
6
+
7
+ Vue3-Request 是一个专为 Vue 3 设计的异步请求处理库,它通过统一的 useRequest Hook 和强大的插件生态,彻底简化了异步操作和 API 调用的复杂性:
8
+
9
+ ✨ 统一状态管理
10
+
11
+ - 🚀 所有数据都具有响应式
12
+ - 📊 自动管理 loading、data、res、error、signal、params 等状态
13
+ - 🔄 提供 run、runAsync、refresh、cancel、abort 等便捷方法
14
+ - 🎛️ 支持手动和自动执行模式,满足不同业务场景
15
+
16
+ 🧩 强大的插件生态 内置 9 大核心插件,开箱即用:
17
+
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
+ - 📚 完善文档
31
+
1
32
  ## ⚡ 快速安装
2
33
 
3
34
  ```bash [pnpm]
4
35
  pnpm add vue3-request
36
+ # or
37
+ npm i vue3-request
38
+ # or
39
+ yarn add vue3-request
40
+ # or
41
+ cnpm i vue3-request
5
42
  ```
6
43
 
7
44
  ## 🚀 5 分钟上手
@@ -28,7 +65,6 @@ const getUserInfo = async () => {
28
65
  };
29
66
 
30
67
  // 一行代码搞定状态管理
31
- const { data, error, loading, signal, refresh, abort } =
32
- useRequest(getUserInfo);
68
+ const { data, error, loading, signal, refresh, abort } = useRequest(getUserInfo);
33
69
  </script>
34
70
  ```
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("vue"),p=e=>e,C=e=>e==null,S=typeof window>"u";function E(e){return C(e)?e:typeof e=="number"&&!isNaN(e)}function b(e,t){return E(e)===void 0?{is:!1,value:0}:E(e)?t&&e===-1?{is:!0,value:-1}:{is:!0,value:e===0||e&&e<0?0:e}:{is:!1,value:0}}const L=(e,t)=>{let n=t;for(let s=e.length;s-- >0;){const r=e[s];n=r(n)}return n?.()},F=()=>new Promise(()=>{}),O=e=>e instanceof Function,R=new Map,j=(e,t,{data:n,params:s,time:r})=>{let u;const a=B(e),{is:o,value:l}=b(t,!0);o&&(a?.timer&&clearTimeout(a.timer),l!==-1&&(u=setTimeout(()=>{T(e)},l)),R.set(e,{data:n,params:s,time:r,timer:u}))},B=e=>R.get(e),T=e=>{e&&typeof e=="string"?R.delete(e):R.clear()};class N{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:L(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}),this.loading(!0),s)return this.loading(u.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=>{O(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 U(e,t,n){const s={manual:!1,...t},r=new N(e,s);r.pluginImpls=n.map(g=>g(r,s)),i.onMounted(()=>{if(!s.manual){const g=r.state.params;r.run(...g)}}),i.onUnmounted(r.cancel);const{run:u,cancel:a,refresh:o,runAsync:l,refreshAsync:v,abort:f,mutate:h}=r;return{...i.toRefs(r.state),run:u,cancel:a,refresh:o,runAsync:l,mutate:h,abort:f,refreshAsync:v,clearCache:T}}const D=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}}}),w=new Map,W=(e,{data:t,params:n,time:s})=>{w.has(e)&&w.get(e)?.forEach(u=>u({data:t,params:n,time:s}))},x=(e,t)=>(w.has(e)?w.get(e)?.add(t):w.set(e,new Set),()=>{const n=w.get(e);n?.delete(t),n?.size===0&&w.delete(e)}),P=new Map,H=(e,t)=>{P.set(e,t),t.then(n=>{}).catch(()=>{}).finally(()=>{$(e)})},_=e=>P.get(e),$=e=>{e&&typeof e=="string"?P.delete(e):P.clear()},z=p((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}=b(s,!0);if(!l)return{};const f=(c,d)=>{r?r(c,d):j(c,n,d),W(c,d)},h=c=>u?u(c):B(c);function g(){if(!t)return;const c=h(t);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),a.value=x(t,d=>{e.setState({data:d.data})})}return g(),i.onUnmounted(()=>{a.value?.()}),{onBefore:()=>{if(!t)return null;const c=h(t);return!c||!Reflect.has(c,"data")?{}:v===-1||Date.now()-c.time<v?{loading:!1,data:c.data,error:void 0,isReturn:!0}:{data:c.data,error:void 0}},onRequest:c=>{if(!t)return c;let d=_(t);return d&&d!==o?()=>d:(d=c(),o=d,H(t,d),()=>d)},onSuccess:(c,d)=>{if(!t)return;const m={data:c,params:d,time:Date.now()};a.value?.(),f(t,m),a.value=x(t,M=>{e.setState({data:M.data})})}}}),I=p((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:h}=b(i.toValue(t),!0),g=h===-1,c=r.value<=h;if(g||c){let{is:d,value:m}=b(i.toValue(n));d||(m=i.toValue(Math.min(a.value,3e4))),f=setTimeout(()=>{u.value=!0,e.refresh()},m)}return()=>{f&&clearTimeout(f)}},l=()=>{s.value?.()},{is:v}=b(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()}}:{}}),A=()=>S||C(window.document?.visibilityState)?!0:document.visibilityState==="visible",V=new Set,J=e=>(V.add(e),()=>{V.delete(e)}),Q=()=>{A()&&V.forEach(e=>{e()})};!S&&window?.addEventListener&&window.addEventListener("visibilitychange",Q,!1);const X=p((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:s})=>{const r=i.ref(null),u=i.ref(),a=()=>{let o;const{value:l}=b(i.toValue(s),!0);if(e.state.error&&l!==0)return;const{is:v,value:f}=b(i.toValue(t));return v?(o=setTimeout(()=>{!i.toValue(n)&&!A()?r.value=J(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()}}}),Y=p((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()}}}),Z=()=>(!S&&window.navigator?.onLine)??!0,y=new Set,G=e=>(y.add(e),()=>{y.delete(e)}),q=()=>{!A()||!Z()||y.forEach(e=>{e()})};!S&&window?.addEventListener&&(window.addEventListener("visibilitychange",q,!1),window.addEventListener("focus",q,!1));const k=p((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const s=i.ref(),{is:r}=b(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=G(u(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),u=>{!t&&u&&e.run(...s)},{flush:"sync"});return{onBefore:()=>({isReady:i.toValue(n)}),onCancel:()=>{r()}}});function ee(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},h=function(){a=!0,e.apply(r,s),f()},g=function(){if(l===!0&&(o=null),a){f();return}if(v===!0){h();return}f()},c=function(){var m=o!==null;return m&&clearTimeout(o),f(),o=null,a=!1,m},d=function(){a=!1,s=arguments,r=this,o===null?l===!0&&h():clearTimeout(o),o=setTimeout(g,t)};return d.cancel=c,d}const te=p((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}=b(i.toValue(t));if(!o)return;const v=i.toValue(n);u=ee(f=>f(),l,v),e.runAsync=(...f)=>new Promise((h,g)=>{r.value?(r.value=!1,a(...f).then(h).catch(g)):u(()=>{a(...f).then(h).catch(g)})}),i.onWatcherCleanup(()=>{u?.cancel(),e.runAsync=a})}),{onCancel:()=>{u?.cancel()}}});function ne(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},h=function(){u=!0,e.apply(r,s),a=setTimeout(g,t),f()},g=function(){if(a=null,u){f();return}if(v===!0){h();return}f()},c=function(){var m=a!==null;return m&&clearTimeout(a),f(),a=null,u=!1,m},d=function(){if(s=arguments,r=this,u=!1,a===null&&l===!0){h();return}v===!0&&(a=setTimeout(g,t))};return d.cancel=c,d}const re=p((e,{throttleWait:t,throttleOptions:n})=>{let s=null;const r=e.runAsync;return i.watchEffect(()=>{const{is:u,value:a}=b(i.toValue(t));if(!u)return;const o=i.toValue(n);s=ne(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()}}}),se=[I,X,Y,k,D,K,te,re,z];function ie(e,t,n){return U(e,t||{},[...n||[],...se])}exports.clearCache=T;exports.definePlugin=p;exports.useRequest=ie;
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()),this.setState({data:await o,error:void 0}),n!==this.currentRequestId?V():(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;
@@ -18,7 +18,7 @@ declare type BaseOptions<D, P extends any[]> = Partial<{
18
18
  errorRetryInterval: number | Ref<number>;
19
19
  refreshOnWindowFocus: boolean | Ref<boolean>;
20
20
  refocusTimespan: number | Ref<number>;
21
- cacheKey: string;
21
+ cacheKey: string | ((params?: P) => string);
22
22
  cacheTime: number;
23
23
  staleTime: number;
24
24
  setCache: (cacheKey: string, cacheData: CacheParamsType) => void;
@@ -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,28 +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;
70
- loading: boolean;
74
+ data: D;
71
75
  }>;
72
76
 
73
77
  declare type PluginReturn<D, P extends any[]> = Partial<{
74
78
  onBefore: (params: P) => void;
75
- onSuccess: (data: D, params: P) => void;
76
- onFinally: (params: P, data: D, error: Error) => void;
77
- onError: (error: Error, params: P) => void;
79
+ onSuccess: (params: P) => void;
80
+ onError: (params: P) => void;
81
+ onFinally: (params: P) => void;
78
82
  onCancel: () => void;
79
- onMutate: (data: D | ((data: D) => D)) => void;
80
83
  onRequest: (service: ServiceType<D, P>) => ServiceType<D, P>;
81
84
  }>;
82
85
 
83
86
  declare class Request_2<D, P extends any[]> {
84
87
  service: ServiceType<D, P>;
85
- options: BaseOptions<D, P>;
88
+ options: BaseOptions<D, P> & {
89
+ resKey: keyof D;
90
+ };
86
91
  currentRequestId: number;
87
92
  pluginImpls: PluginReturn<D, P>[];
88
93
  state: IState<D, P>;
89
94
  abort: () => void;
90
- constructor(service: ServiceType<D, P>, options: BaseOptions<D, P>);
95
+ constructor(service: ServiceType<D, P>, options: BaseOptions<D, P> & {
96
+ resKey: keyof D;
97
+ });
91
98
  setState: (s: Partial<IState<D, P>>) => void;
92
99
  executePlugin: (event: keyof PluginReturn<D, P>, ...rest: any[]) => PluginMethodsReturn<D, P>;
93
100
  loading: (loading: boolean) => void;
@@ -96,7 +103,6 @@ declare class Request_2<D, P extends any[]> {
96
103
  run: (...params: P) => void;
97
104
  refresh: () => void;
98
105
  refreshAsync: () => Promise<D>;
99
- mutate: (data: D | ((data: D) => D)) => void;
100
106
  cancel: () => void;
101
107
  }
102
108
 
@@ -109,11 +115,17 @@ declare type ThrottleOptionsType = {
109
115
 
110
116
  declare type Timeout = ReturnType<typeof setTimeout>;
111
117
 
112
- 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>;
113
125
 
114
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>;
115
127
 
116
- 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>> {
117
129
  run: (...args: P) => void;
118
130
  cancel: () => void;
119
131
  refresh: () => void;
@@ -121,7 +133,6 @@ declare interface UseRequestReturnType<D, P extends any[]> extends ToRefs<IState
121
133
  abort: () => void;
122
134
  refreshAsync: () => Promise<D>;
123
135
  clearCache: (key?: string) => void;
124
- mutate: (data: D | ((data: D) => D)) => void;
125
136
  }
126
137
 
127
138
  export { }
@@ -1,13 +1,13 @@
1
- import { reactive as H, onMounted as W, onUnmounted as F, toRefs as _, ref as R, computed as $, toValue as d, watch as x, watchEffect as B, onWatcherCleanup as j } from "vue";
2
- const p = (e) => e, S = (e) => e == null, T = typeof window > "u";
3
- function L(e) {
4
- return S(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
- function b(e, t) {
7
- return L(e) === void 0 ? {
6
+ function p(e, t) {
7
+ return O(e) === void 0 ? {
8
8
  is: !1,
9
9
  value: 0
10
- } : L(e) ? t && e === -1 ? {
10
+ } : O(e) ? t && e === -1 ? {
11
11
  is: !0,
12
12
  value: -1
13
13
  } : {
@@ -20,25 +20,26 @@ function b(e, t) {
20
20
  }
21
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
- }, y = () => new Promise(() => {
29
- }), I = (e) => e instanceof Function, P = /* @__PURE__ */ new Map(), U = (e, t, { data: n, params: s, time: r }) => {
30
- let i;
31
- const u = N(e), { is: a, value: o } = b(t, !0);
32
- a && (u?.timer && clearTimeout(u.timer), o !== -1 && (i = setTimeout(() => {
33
- V(e);
34
- }, o)), P.set(e, { data: n, params: s, time: r, timer: i }));
35
- }, N = (e) => P.get(e), V = (e) => {
36
- e && typeof e == "string" ? P.delete(e) : P.clear();
28
+ }, A = () => new Promise(() => {
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
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,7 +54,10 @@ class J {
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")
@@ -64,48 +68,43 @@ class J {
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, ...i } = 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
- return y();
95
- if (this.setState({ params: t }), this.loading(!0), s)
96
- return this.loading(i.loading || !1), i.data;
96
+ return A();
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 u = () => this.service(...t);
100
- let { servicePromise: a } = this.executePlugin("onRequest", u);
101
- a || (a = u());
102
- const o = await a;
103
- return n !== this.currentRequestId ? y() : (this.setState({ data: o, error: void 0 }), this.executePlugin("onSuccess", o, t), this.options.onSuccess?.(o, t), this.onFinished(), o);
104
- } catch (u) {
101
+ const a = () => this.service(...t);
102
+ let { servicePromise: u } = this.executePlugin("onRequest", a);
103
+ return u || (u = a()), this.setState({ data: await u, error: void 0 }), n !== this.currentRequestId ? A() : (this.executePlugin("onSuccess", t), this.options.onSuccess?.(t), this.onFinished(), this.state.data);
104
+ } catch (a) {
105
105
  if (n !== this.currentRequestId)
106
- return y();
107
- const a = u;
108
- throw this.setState({ data: void 0, error: a }), this.executePlugin("onError", a, t), this.options.onError?.(a, t), this.onFinished(), a;
106
+ return A();
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,47 @@ class J {
117
116
  this.run(...this.state.params);
118
117
  };
119
118
  refreshAsync = async () => await this.runAsync(...this.state.params);
120
- mutate = (t) => {
121
- I(t) ? t(this.state.data) : this.setState({ data: t }), this.executePlugin("onMutate", this.state.data);
122
- };
119
+ // mutate = (data: D | ((data: D) => D)) => {
120
+ // if (isFunction(data)) {
121
+ // this.setState({ data: data(this.state.data!) });
122
+ // } else {
123
+ // this.setState({ data });
124
+ // }
125
+ // this.executePlugin("onMutate", this.state.data);
126
+ // };
123
127
  cancel = () => {
124
128
  this.executePlugin("onCancel"), this.currentRequestId++, this.loading(!1);
125
129
  };
126
130
  }
127
131
  function Q(e, t, n) {
128
- const s = {
132
+ const i = {
129
133
  manual: !1,
130
134
  ...t
131
- }, r = new J(e, s);
135
+ }, r = new J(e, i);
132
136
  r.pluginImpls = n.map(
133
- (g) => g(r, s)
134
- ), W(() => {
135
- if (!s.manual) {
136
- const g = r.state.params;
137
- r.run(...g);
137
+ (h) => h(r, i)
138
+ ), H(() => {
139
+ if (!i.manual) {
140
+ const h = r.state.params;
141
+ r.run(...h);
138
142
  }
139
143
  }), F(r.cancel);
140
- const { run: i, cancel: u, refresh: a, runAsync: o, refreshAsync: v, abort: c, mutate: h } = r;
144
+ const { run: s, cancel: a, refresh: u, runAsync: c, refreshAsync: v, abort: o } = r;
141
145
  return {
142
- ..._(r.state),
143
- run: i,
144
- cancel: u,
145
- refresh: a,
146
- runAsync: o,
147
- mutate: h,
148
- abort: c,
146
+ ...W(r.state),
147
+ run: s,
148
+ cancel: a,
149
+ refresh: u,
150
+ runAsync: c,
151
+ // mutate,
152
+ abort: o,
149
153
  refreshAsync: v,
150
- clearCache: V
154
+ clearCache: _
151
155
  };
152
156
  }
153
- const X = p((e, { abortPrevious: t = !0 }) => {
157
+ const X = b((e, { abortPrevious: t = !0 }) => {
154
158
  let n = null;
155
- const s = () => {
159
+ const i = () => {
156
160
  n = new AbortController(), e.setState({
157
161
  signal: n.signal,
158
162
  isAborted: n.signal.aborted
@@ -164,176 +168,192 @@ const X = p((e, { abortPrevious: t = !0 }) => {
164
168
  }));
165
169
  }, {
166
170
  onBefore: () => {
167
- typeof t == "boolean" && t && e.abort(), s();
171
+ typeof t == "boolean" && t && e.abort(), i();
168
172
  },
169
173
  onRequest: (r) => () => r(),
170
174
  onCancel: () => {
171
175
  e.abort(), n = null;
172
176
  }
173
177
  };
174
- }), w = /* @__PURE__ */ new Map(), Y = (e, { data: t, params: n, time: s }) => {
175
- w.has(e) && w.get(e)?.forEach((i) => i({ data: t, params: n, time: s }));
176
- }, M = (e, t) => (w.has(e) ? w.get(e)?.add(t) : w.set(e, /* @__PURE__ */ new Set()), () => {
177
- const n = w.get(e);
178
- n?.delete(t), n?.size === 0 && w.delete(e);
179
- }), C = /* @__PURE__ */ new Map(), Z = (e, t) => {
180
- C.set(e, t), t.then((n) => {
178
+ }), y = /* @__PURE__ */ new Map(), Y = (e, { data: t, params: n, time: i }) => {
179
+ y.has(e) && y.get(e)?.forEach((s) => s({ data: t, params: n, time: i }));
180
+ }, q = (e, t) => (y.has(e) ? y.get(e)?.add(t) : y.set(e, /* @__PURE__ */ new Set()), () => {
181
+ const n = y.get(e);
182
+ n?.delete(t), n?.size === 0 && y.delete(e);
183
+ }), S = /* @__PURE__ */ new Map(), Z = (e, t) => {
184
+ S.set(e, t), t.then(() => {
181
185
  }).catch(() => {
182
186
  }).finally(() => {
183
- k(e);
187
+ I(e);
184
188
  });
185
- }, G = (e) => C.get(e), k = (e) => {
186
- e && typeof e == "string" ? C.delete(e) : C.clear();
187
- }, K = p(
189
+ }, G = (e) => S.get(e), I = (e) => {
190
+ e && typeof e == "string" ? S.delete(e) : S.clear();
191
+ }, k = b(
188
192
  (e, {
189
193
  cacheKey: t,
190
194
  cacheTime: n = 3e5,
191
- staleTime: s = 0,
195
+ staleTime: i = 0,
192
196
  setCache: r,
193
- getCache: i
197
+ getCache: s
194
198
  }) => {
195
- const u = R(null);
196
- let a = null;
197
- const { is: o, value: v } = b(s, !0);
198
- if (!o)
199
+ const a = R(null);
200
+ let u = null;
201
+ const { is: c, value: v } = p(i, !0);
202
+ if (!c)
199
203
  return {};
200
- const c = (l, f) => {
201
- r ? r(l, f) : U(l, n, f), Y(l, f);
202
- }, h = (l) => i ? i(l) : N(l);
203
- function g() {
204
- if (!t) return;
205
- const l = h(t);
204
+ const o = typeof t == "function" ? t : () => t, h = (f, l) => {
205
+ r ? r(f, l) : U(f, n, l), Y(f, l);
206
+ }, m = (f) => s ? s(f) : V(f);
207
+ function w() {
208
+ const f = o();
209
+ if (!f) return;
210
+ const l = m(f);
206
211
  l && Reflect.has(l, "data") && e.setState({
207
212
  data: l.data,
208
213
  params: l.params
209
- }), u.value = M(t, (f) => {
214
+ }), a.value = q(f, (g) => {
210
215
  e.setState({
211
- data: f.data
216
+ data: g.data
212
217
  });
213
218
  });
214
219
  }
215
- return g(), F(() => {
216
- u.value?.();
220
+ return w(), F(() => {
221
+ a.value?.();
217
222
  }), {
218
- onBefore: () => {
219
- if (!t) return null;
220
- const l = h(t);
221
- return !l || !Reflect.has(l, "data") ? {} : v === -1 || Date.now() - l.time < v ? {
222
- loading: !1,
223
- data: l.data,
223
+ onBefore: (f) => {
224
+ const l = o(f);
225
+ if (!l) return null;
226
+ const g = m(l);
227
+ return !g || !Reflect.has(g, "data") ? {} : v === -1 || Date.now() - g.time < v ? {
228
+ data: g.data,
224
229
  error: void 0,
225
230
  isReturn: !0
226
231
  } : {
227
- data: l.data,
232
+ data: g.data,
228
233
  error: void 0
229
234
  };
230
235
  },
231
- onRequest: (l) => {
232
- if (!t) return l;
233
- let f = G(t);
234
- return f && f !== a ? () => f : (f = l(), a = f, Z(t, f), () => f);
236
+ onRequest: (f) => {
237
+ const l = o(e.state.params);
238
+ if (!l) return f;
239
+ let g = G(l);
240
+ return g && g !== u ? () => g : (g = f(), u = g, Z(l, g), () => g);
235
241
  },
236
- onSuccess: (l, f) => {
237
- if (!t) return;
238
- const m = {
239
- data: l,
242
+ onSuccess: (f) => {
243
+ const l = o(f);
244
+ l && (a.value?.(), h(l, {
245
+ data: e.state.data,
240
246
  params: f,
241
247
  time: Date.now()
242
- };
243
- u.value?.(), c(t, m), u.value = M(t, (D) => {
248
+ }), a.value = q(l, (g) => {
244
249
  e.setState({
245
- data: D.data
250
+ data: g.data
246
251
  });
247
- });
252
+ }));
248
253
  }
254
+ // onMutate(data) {
255
+ // const cacheKey = _getCacheKey(requestInstance.state.params);
256
+ // if (!cacheKey) return;
257
+ // unSubscribe.value?.();
258
+ // _setCache(cacheKey, {
259
+ // data,
260
+ // params: requestInstance.state.params,
261
+ // time: Date.now(),
262
+ // });
263
+ // unSubscribe.value = on(cacheKey, (cacheData) => {
264
+ // requestInstance.setState({
265
+ // data: cacheData.data,
266
+ // });
267
+ // });
268
+ // },
249
269
  };
250
270
  }
251
- ), ee = p((e, { errorRetryCount: t = 0, errorRetryInterval: n }) => {
252
- const s = R(), r = R(0), i = R(!1), u = $(
271
+ ), K = b((e, { errorRetryCount: t = 0, errorRetryInterval: n }) => {
272
+ const i = R(), r = R(0), s = R(!1), a = $(
253
273
  () => 1e3 * Math.pow(2, r.value)
254
- ), a = () => {
255
- let c = null;
274
+ ), u = () => {
275
+ let o = null;
256
276
  r.value++;
257
- const { value: h } = b(d(t), !0), g = h === -1, l = r.value <= h;
258
- if (g || l) {
259
- let { is: f, value: m } = b(d(n));
260
- f || (m = d(Math.min(u.value, 3e4))), c = setTimeout(() => {
261
- i.value = !0, e.refresh();
262
- }, m);
277
+ const { value: h } = p(d(t), !0), m = h === -1, w = r.value <= h;
278
+ if (m || w) {
279
+ let { is: f, value: l } = p(d(n));
280
+ f || (l = d(Math.min(a.value, 3e4))), o = setTimeout(() => {
281
+ s.value = !0, e.refresh();
282
+ }, l);
263
283
  }
264
284
  return () => {
265
- c && clearTimeout(c);
285
+ o && clearTimeout(o);
266
286
  };
267
- }, o = () => {
268
- s.value?.();
269
- }, { is: v } = b(d(t), !0);
287
+ }, c = () => {
288
+ i.value?.();
289
+ }, { is: v } = p(d(t), !0);
270
290
  return v ? {
271
291
  onBefore: () => {
272
- i.value || (r.value = 0), i.value = !1, o();
292
+ s.value || (r.value = 0), s.value = !1, c();
273
293
  },
274
294
  onSuccess: () => {
275
295
  r.value = 0;
276
296
  },
277
297
  onError: () => {
278
- s.value = a();
298
+ i.value = u();
279
299
  },
280
300
  onCancel: () => {
281
- r.value = 0, o();
301
+ r.value = 0, c();
282
302
  }
283
303
  } : {};
284
- }), q = () => T || S(window.document?.visibilityState) ? !0 : document.visibilityState === "visible", A = /* @__PURE__ */ new Set(), te = (e) => (A.add(e), () => {
285
- A.delete(e);
286
- }), ne = () => {
287
- q() && A.forEach((e) => {
304
+ }), M = () => T || P(window.document?.visibilityState) ? !0 : document.visibilityState === "visible", E = /* @__PURE__ */ new Set(), ee = (e) => (E.add(e), () => {
305
+ E.delete(e);
306
+ }), te = () => {
307
+ M() && E.forEach((e) => {
288
308
  e();
289
309
  });
290
310
  };
291
- !T && window?.addEventListener && window.addEventListener("visibilitychange", ne, !1);
292
- const re = p(
293
- (e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount: s }) => {
294
- const r = R(null), i = R(), u = () => {
295
- let a;
296
- const { value: o } = b(
297
- d(s),
311
+ !T && window?.addEventListener && window.addEventListener("visibilitychange", te, !1);
312
+ const ne = b(
313
+ (e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount: i }) => {
314
+ const r = R(null), s = R(), a = () => {
315
+ let u;
316
+ const { value: c } = p(
317
+ d(i),
298
318
  !0
299
319
  );
300
- if (e.state.error && o !== 0) return;
301
- const { is: v, value: c } = b(
320
+ if (e.state.error && c !== 0) return;
321
+ const { is: v, value: o } = p(
302
322
  d(t)
303
323
  );
304
- return v ? (a = setTimeout(() => {
305
- !d(n) && !q() ? r.value = te(e.refresh) : e.refresh();
306
- }, c), () => {
307
- a && clearTimeout(a), r.value?.();
324
+ return v ? (u = setTimeout(() => {
325
+ !d(n) && !M() ? r.value = ee(e.refresh) : e.refresh();
326
+ }, o), () => {
327
+ u && clearTimeout(u), r.value?.();
308
328
  }) : void 0;
309
329
  };
310
- return x(
330
+ return B(
311
331
  [() => d(t), () => d(n)],
312
332
  () => {
313
- i.value?.(), i.value = u();
333
+ s.value?.(), s.value = a();
314
334
  }
315
335
  ), F(() => {
316
336
  r.value?.();
317
337
  }), {
318
338
  onBefore: () => {
319
- i.value?.();
339
+ s.value?.();
320
340
  },
321
341
  onCancel: () => {
322
- i.value?.();
342
+ s.value?.();
323
343
  },
324
344
  onFinally: () => {
325
- i.value = u();
345
+ s.value = a();
326
346
  }
327
347
  };
328
348
  }
329
- ), se = p(
330
- (e, { manual: t, refreshDeps: n, refreshDepsAction: s }) => {
331
- if (S(n) || typeof n != "object" && typeof n != "function")
349
+ ), re = b(
350
+ (e, { manual: t, refreshDeps: n, refreshDepsAction: i }) => {
351
+ if (P(n) || typeof n != "object" && typeof n != "function")
332
352
  return {};
333
- const r = x(
353
+ const r = B(
334
354
  n,
335
355
  () => {
336
- t || (s ? s() : e.refresh());
356
+ t || (i ? i() : e.refresh());
337
357
  },
338
358
  {
339
359
  deep: !0
@@ -345,41 +365,41 @@ const re = p(
345
365
  }
346
366
  };
347
367
  }
348
- ), ie = () => (!T && window.navigator?.onLine) ?? !0, E = /* @__PURE__ */ new Set(), ue = (e) => (E.add(e), () => {
349
- E.delete(e);
350
- }), O = () => {
351
- !q() || !ie() || E.forEach((e) => {
368
+ ), ie = () => (!T && window.navigator?.onLine) ?? !0, x = /* @__PURE__ */ new Set(), se = (e) => (x.add(e), () => {
369
+ x.delete(e);
370
+ }), j = () => {
371
+ !M() || !ie() || x.forEach((e) => {
352
372
  e();
353
373
  });
354
374
  };
355
- !T && window?.addEventListener && (window.addEventListener("visibilitychange", O, !1), window.addEventListener("focus", O, !1));
356
- const ae = p(
375
+ !T && window?.addEventListener && (window.addEventListener("visibilitychange", j, !1), window.addEventListener("focus", j, !1));
376
+ const ae = b(
357
377
  (e, { refocusTimespan: t = 5e3, refreshOnWindowFocus: n = !1 }) => {
358
- const s = R(), { is: r } = b(d(t));
359
- if (!r || S(d(n)) || typeof d(n) != "boolean")
378
+ const i = R(), { is: r } = p(d(t));
379
+ if (!r || P(d(n)) || typeof d(n) != "boolean")
360
380
  return {};
361
- const i = (u, a) => {
362
- let o = !1;
381
+ const s = (a, u) => {
382
+ let c = !1;
363
383
  return () => {
364
- o || (o = !0, u(), setTimeout(() => {
365
- o = !1;
366
- }, a));
384
+ c || (c = !0, a(), setTimeout(() => {
385
+ c = !1;
386
+ }, u));
367
387
  };
368
388
  };
369
- return B(() => {
370
- s.value?.(), d(n) && (s.value = ue(
371
- i(e.refresh, d(t))
389
+ return L(() => {
390
+ i.value?.(), d(n) && (i.value = se(
391
+ s(e.refresh, d(t))
372
392
  ));
373
393
  }), F(() => {
374
- s.value?.();
394
+ i.value?.();
375
395
  }), {};
376
396
  }
377
- ), oe = p(
378
- (e, { manual: t, ready: n = !0, defaultParams: s = [] }) => {
379
- const r = x(
397
+ ), ue = b(
398
+ (e, { manual: t, ready: n = !0, defaultParams: i = [] }) => {
399
+ const r = B(
380
400
  () => d(n),
381
- (i) => {
382
- !t && i && e.run(...s);
401
+ (s) => {
402
+ !t && s && e.run(...i);
383
403
  },
384
404
  {
385
405
  flush: "sync"
@@ -393,127 +413,127 @@ const ae = p(
393
413
  };
394
414
  }
395
415
  );
396
- function le(e, t, n) {
397
- var s = null, r = null, i = typeof n == "boolean" ? { leading: n, trailing: !n } : Object.assign({ leading: !1, trailing: !0 }, n), u = !1, a = null, o = i.leading, v = i.trailing, c = function() {
398
- s = null, r = null;
416
+ function oe(e, t, n) {
417
+ 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() {
418
+ i = null, r = null;
399
419
  }, h = function() {
400
- u = !0, e.apply(r, s), c();
401
- }, g = function() {
402
- if (o === !0 && (a = null), u) {
403
- c();
420
+ a = !0, e.apply(r, i), o();
421
+ }, m = function() {
422
+ if (c === !0 && (u = null), a) {
423
+ o();
404
424
  return;
405
425
  }
406
426
  if (v === !0) {
407
427
  h();
408
428
  return;
409
429
  }
410
- c();
411
- }, l = function() {
412
- var m = a !== null;
413
- return m && clearTimeout(a), c(), a = null, u = !1, m;
430
+ o();
431
+ }, w = function() {
432
+ var l = u !== null;
433
+ return l && clearTimeout(u), o(), u = null, a = !1, l;
414
434
  }, f = function() {
415
- u = !1, s = arguments, r = this, a === null ? o === !0 && h() : clearTimeout(a), a = setTimeout(g, t);
435
+ a = !1, i = arguments, r = this, u === null ? c === !0 && h() : clearTimeout(u), u = setTimeout(m, t);
416
436
  };
417
- return f.cancel = l, f;
437
+ return f.cancel = w, f;
418
438
  }
419
- const ce = p(
420
- (e, { debounceWait: t, debounceOptions: n, manual: s }) => {
439
+ const le = b(
440
+ (e, { debounceWait: t, debounceOptions: n, manual: i }) => {
421
441
  const r = R(!1);
422
- let i = null;
423
- const u = e.runAsync;
424
- return s || (r.value = !0), B(() => {
425
- const { is: a, value: o } = b(d(t));
426
- if (!a) return;
442
+ let s = null;
443
+ const a = e.runAsync;
444
+ return i || (r.value = !0), L(() => {
445
+ const { is: u, value: c } = p(d(t));
446
+ if (!u) return;
427
447
  const v = d(n);
428
- i = le(
429
- (c) => c(),
430
- o,
448
+ s = oe(
449
+ (o) => o(),
450
+ c,
431
451
  v
432
- ), e.runAsync = (...c) => new Promise((h, g) => {
433
- r.value ? (r.value = !1, u(...c).then(h).catch(g)) : i(() => {
434
- u(...c).then(h).catch(g);
452
+ ), e.runAsync = (...o) => new Promise((h, m) => {
453
+ r.value ? (r.value = !1, a(...o).then(h).catch(m)) : s(() => {
454
+ a(...o).then(h).catch(m);
435
455
  });
436
- }), j(() => {
437
- i?.cancel(), e.runAsync = u;
456
+ }), N(() => {
457
+ s?.cancel(), e.runAsync = a;
438
458
  });
439
459
  }), {
440
460
  onCancel: () => {
441
- i?.cancel();
461
+ s?.cancel();
442
462
  }
443
463
  };
444
464
  }
445
465
  );
446
- function fe(e, t, n) {
447
- var s = null, r = null, i = !1, u = null, a = Object.assign({ leading: !0, trailing: !0 }, n), o = a.leading, v = a.trailing, c = function() {
448
- s = null, r = null;
466
+ function ce(e, t, n) {
467
+ 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() {
468
+ i = null, r = null;
449
469
  }, h = function() {
450
- i = !0, e.apply(r, s), u = setTimeout(g, t), c();
451
- }, g = function() {
452
- if (u = null, i) {
453
- c();
470
+ s = !0, e.apply(r, i), a = setTimeout(m, t), o();
471
+ }, m = function() {
472
+ if (a = null, s) {
473
+ o();
454
474
  return;
455
475
  }
456
476
  if (v === !0) {
457
477
  h();
458
478
  return;
459
479
  }
460
- c();
461
- }, l = function() {
462
- var m = u !== null;
463
- return m && clearTimeout(u), c(), u = null, i = !1, m;
480
+ o();
481
+ }, w = function() {
482
+ var l = a !== null;
483
+ return l && clearTimeout(a), o(), a = null, s = !1, l;
464
484
  }, f = function() {
465
- if (s = arguments, r = this, i = !1, u === null && o === !0) {
485
+ if (i = arguments, r = this, s = !1, a === null && c === !0) {
466
486
  h();
467
487
  return;
468
488
  }
469
- v === !0 && (u = setTimeout(g, t));
489
+ v === !0 && (a = setTimeout(m, t));
470
490
  };
471
- return f.cancel = l, f;
491
+ return f.cancel = w, f;
472
492
  }
473
- const de = p(
493
+ const fe = b(
474
494
  (e, { throttleWait: t, throttleOptions: n }) => {
475
- let s = null;
495
+ let i = null;
476
496
  const r = e.runAsync;
477
- return B(() => {
478
- const { is: i, value: u } = b(d(t));
479
- if (!i) return;
480
- const a = d(n);
481
- s = fe(
482
- (o) => o(),
483
- u,
484
- a
485
- ), e.runAsync = (...o) => new Promise((v, c) => {
486
- s(() => {
487
- r(...o).then(v).catch(c);
497
+ return L(() => {
498
+ const { is: s, value: a } = p(d(t));
499
+ if (!s) return;
500
+ const u = d(n);
501
+ i = ce(
502
+ (c) => c(),
503
+ a,
504
+ u
505
+ ), e.runAsync = (...c) => new Promise((v, o) => {
506
+ i(() => {
507
+ r(...c).then(v).catch(o);
488
508
  });
489
- }), j(() => {
490
- s?.cancel(), e.runAsync = r;
509
+ }), N(() => {
510
+ i?.cancel(), e.runAsync = r;
491
511
  });
492
512
  }), {
493
513
  onCancel: () => {
494
- s?.cancel();
514
+ i?.cancel();
495
515
  }
496
516
  };
497
517
  }
498
- ), ve = [
499
- ee,
518
+ ), de = [
519
+ K,
520
+ ne,
500
521
  re,
501
- se,
502
522
  ae,
503
523
  X,
504
- oe,
505
- ce,
506
- de,
507
- K
524
+ ue,
525
+ le,
526
+ fe,
527
+ k
508
528
  ];
509
- function ge(e, t, n) {
529
+ function ve(e, t, n) {
510
530
  return Q(e, t || {}, [
511
531
  ...n || [],
512
- ...ve
532
+ ...de
513
533
  ]);
514
534
  }
515
535
  export {
516
- V as clearCache,
517
- p as definePlugin,
518
- ge as useRequest
536
+ _ as clearCache,
537
+ b as definePlugin,
538
+ ve as useRequest
519
539
  };
@@ -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 v=t=>t,P=t=>t==null,C=typeof window>"u";function q(t){return P(t)?t:typeof t=="number"&&!isNaN(t)}function p(t,e){return q(t)===void 0?{is:!1,value:0}:q(t)?e&&t===-1?{is:!0,value:-1}:{is:!0,value:t===0||t&&t<0?0:t}:{is:!1,value:0}}const L=(t,e)=>{let n=e;for(let s=t.length;s-- >0;){const r=t[s];n=r(n)}return n?.()},y=()=>new Promise(()=>{}),O=t=>t instanceof Function,S=new Map,j=(t,e,{data:n,params:s,time:r})=>{let a;const o=x(t),{is:l,value:u}=p(e,!0);l&&(o?.timer&&clearTimeout(o.timer),u!==-1&&(a=setTimeout(()=>{T(t)},u)),S.set(t,{data:n,params:s,time:r,timer:a}))},x=t=>S.get(t),T=t=>{t&&typeof t=="string"?S.delete(t):S.clear()};class N{constructor(e,n){this.service=e,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=e=>{Object.assign(this.state,e)};executePlugin=(e,...n)=>{if(e==="onRequest")return{servicePromise:L(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const s=this.pluginImpls.map(r=>r[e]?.(...n)).filter(Boolean);return Object.assign({},...s)}};loading=e=>{this.setState({loading:e,isFinished:!e})};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(...e)=>{const n=++this.currentRequestId,{isReturn:s,isReady:r,...a}=this.executePlugin("onBefore",e);if(!r)return y();if(this.setState({params:e}),this.loading(!0),s)return this.loading(a.loading||!1),a.data;this.options.onBefore?.(e);try{const o=()=>this.service(...e);let{servicePromise:l}=this.executePlugin("onRequest",o);l||(l=o());const u=await l;return n!==this.currentRequestId?y():(this.setState({data:u,error:void 0}),this.executePlugin("onSuccess",u,e),this.options.onSuccess?.(u,e),this.onFinished(),u)}catch(o){if(n!==this.currentRequestId)return y();const l=o;throw this.setState({data:void 0,error:l}),this.executePlugin("onError",l,e),this.options.onError?.(l,e),this.onFinished(),l}};run=(...e)=>{this.runAsync(...e).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);mutate=e=>{O(e)?e(this.state.data):this.setState({data:e}),this.executePlugin("onMutate",this.state.data)};cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function U(t,e,n){const s={manual:!1,...e},r=new N(t,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:a,cancel:o,refresh:l,runAsync:u,refreshAsync:h,abort:f,mutate:g}=r;return{...i.toRefs(r.state),run:a,cancel:o,refresh:l,runAsync:u,mutate:g,abort:f,refreshAsync:h,clearCache:T}}const D=v((t,{abortPrevious:e=!0})=>{let n=null;const s=()=>{n=new AbortController,t.setState({signal:n.signal,isAborted:n.signal.aborted})};return t.abort=()=>{n&&!n.signal.aborted&&!t.state.isFinished&&(n.abort(),t.setState({isAborted:n.signal.aborted}))},{onBefore:()=>{typeof e=="boolean"&&e&&t.abort(),s()},onRequest:r=>()=>r(),onCancel:()=>{t.abort(),n=null}}}),R=new Map,W=(t,{data:e,params:n,time:s})=>{R.has(t)&&R.get(t)?.forEach(a=>a({data:e,params:n,time:s}))},B=(t,e)=>(R.has(t)?R.get(t)?.add(e):R.set(t,new Set),()=>{const n=R.get(t);n?.delete(e),n?.size===0&&R.delete(t)}),V=new Map,H=(t,e)=>{V.set(t,e),e.then(n=>{}).catch(()=>{}).finally(()=>{$(t)})},_=t=>V.get(t),$=t=>{t&&typeof t=="string"?V.delete(t):V.clear()},z=v((t,{cacheKey:e,cacheTime:n=3e5,staleTime:s=0,setCache:r,getCache:a})=>{const o=i.ref(null);let l=null;const{is:u,value:h}=p(s,!0);if(!u)return{};const f=(c,d)=>{r?r(c,d):j(c,n,d),W(c,d)},g=c=>a?a(c):x(c);function m(){if(!e)return;const c=g(e);c&&Reflect.has(c,"data")&&t.setState({data:c.data,params:c.params}),o.value=B(e,d=>{t.setState({data:d.data})})}return m(),i.onUnmounted(()=>{o.value?.()}),{onBefore:()=>{if(!e)return null;const c=g(e);return!c||!Reflect.has(c,"data")?{}:h===-1||Date.now()-c.time<h?{loading:!1,data:c.data,error:void 0,isReturn:!0}:{data:c.data,error:void 0}},onRequest:c=>{if(!e)return c;let d=_(e);return d&&d!==l?()=>d:(d=c(),l=d,H(e,d),()=>d)},onSuccess:(c,d)=>{if(!e)return;const b={data:c,params:d,time:Date.now()};o.value?.(),f(e,b),o.value=B(e,at=>{t.setState({data:at.data})})}}}),I=v((t,{errorRetryCount:e=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 f=null;r.value++;const{value:g}=p(i.toValue(e),!0),m=g===-1,c=r.value<=g;if(m||c){let{is:d,value:b}=p(i.toValue(n));d||(b=i.toValue(Math.min(o.value,3e4))),f=setTimeout(()=>{a.value=!0,t.refresh()},b)}return()=>{f&&clearTimeout(f)}},u=()=>{s.value?.()},{is:h}=p(i.toValue(e),!0);return h?{onBefore:()=>{a.value||(r.value=0),a.value=!1,u()},onSuccess:()=>{r.value=0},onError:()=>{s.value=l()},onCancel:()=>{r.value=0,u()}}:{}}),F=()=>C||P(window.document?.visibilityState)?!0:document.visibilityState==="visible",A=new Set,J=t=>(A.add(t),()=>{A.delete(t)}),Q=()=>{F()&&A.forEach(t=>{t()})};!C&&window?.addEventListener&&window.addEventListener("visibilitychange",Q,!1);const X=v((t,{pollingInterval:e,pollingWhenHidden:n=!0,errorRetryCount:s})=>{const r=i.ref(null),a=i.ref(),o=()=>{let l;const{value:u}=p(i.toValue(s),!0);if(t.state.error&&u!==0)return;const{is:h,value:f}=p(i.toValue(e));return h?(l=setTimeout(()=>{!i.toValue(n)&&!F()?r.value=J(t.refresh):t.refresh()},f),()=>{l&&clearTimeout(l),r.value?.()}):void 0};return i.watch([()=>i.toValue(e),()=>i.toValue(n)],()=>{a.value?.(),a.value=o()}),i.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{a.value?.()},onCancel:()=>{a.value?.()},onFinally:()=>{a.value=o()}}}),Y=v((t,{manual:e,refreshDeps:n,refreshDepsAction:s})=>{if(P(n)||typeof n!="object"&&typeof n!="function")return{};const r=i.watch(n,()=>{e||(s?s():t.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),Z=()=>(!C&&window.navigator?.onLine)??!0,E=new Set,G=t=>(E.add(t),()=>{E.delete(t)}),M=()=>{!F()||!Z()||E.forEach(t=>{t()})};!C&&window?.addEventListener&&(window.addEventListener("visibilitychange",M,!1),window.addEventListener("focus",M,!1));const k=v((t,{refocusTimespan:e=5e3,refreshOnWindowFocus:n=!1})=>{const s=i.ref(),{is:r}=p(i.toValue(e));if(!r||P(i.toValue(n))||typeof i.toValue(n)!="boolean")return{};const a=(o,l)=>{let u=!1;return()=>{u||(u=!0,o(),setTimeout(()=>{u=!1},l))}};return i.watchEffect(()=>{s.value?.(),i.toValue(n)&&(s.value=G(a(t.refresh,i.toValue(e))))}),i.onUnmounted(()=>{s.value?.()}),{}}),K=v((t,{manual:e,ready:n=!0,defaultParams:s=[]})=>{const r=i.watch(()=>i.toValue(n),a=>{!e&&a&&t.run(...s)},{flush:"sync"});return{onBefore:()=>({isReady:i.toValue(n)}),onCancel:()=>{r()}}});function tt(t,e,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,u=a.leading,h=a.trailing,f=function(){s=null,r=null},g=function(){o=!0,t.apply(r,s),f()},m=function(){if(u===!0&&(l=null),o){f();return}if(h===!0){g();return}f()},c=function(){var b=l!==null;return b&&clearTimeout(l),f(),l=null,o=!1,b},d=function(){o=!1,s=arguments,r=this,l===null?u===!0&&g():clearTimeout(l),l=setTimeout(m,e)};return d.cancel=c,d}const et=v((t,{debounceWait:e,debounceOptions:n,manual:s})=>{const r=i.ref(!1);let a=null;const o=t.runAsync;return s||(r.value=!0),i.watchEffect(()=>{const{is:l,value:u}=p(i.toValue(e));if(!l)return;const h=i.toValue(n);a=tt(f=>f(),u,h),t.runAsync=(...f)=>new Promise((g,m)=>{r.value?(r.value=!1,o(...f).then(g).catch(m)):a(()=>{o(...f).then(g).catch(m)})}),i.onWatcherCleanup(()=>{a?.cancel(),t.runAsync=o})}),{onCancel:()=>{a?.cancel()}}});function nt(t,e,n){var s=null,r=null,a=!1,o=null,l=Object.assign({leading:!0,trailing:!0},n),u=l.leading,h=l.trailing,f=function(){s=null,r=null},g=function(){a=!0,t.apply(r,s),o=setTimeout(m,e),f()},m=function(){if(o=null,a){f();return}if(h===!0){g();return}f()},c=function(){var b=o!==null;return b&&clearTimeout(o),f(),o=null,a=!1,b},d=function(){if(s=arguments,r=this,a=!1,o===null&&u===!0){g();return}h===!0&&(o=setTimeout(m,e))};return d.cancel=c,d}const rt=v((t,{throttleWait:e,throttleOptions:n})=>{let s=null;const r=t.runAsync;return i.watchEffect(()=>{const{is:a,value:o}=p(i.toValue(e));if(!a)return;const l=i.toValue(n);s=nt(u=>u(),o,l),t.runAsync=(...u)=>new Promise((h,f)=>{s(()=>{r(...u).then(h).catch(f)})}),i.onWatcherCleanup(()=>{s?.cancel(),t.runAsync=r})}),{onCancel:()=>{s?.cancel()}}}),it=[I,X,Y,k,D,K,et,rt,z];function st(t,e,n){return U(t,e||{},[...n||[],...it])}w.clearCache=T,w.definePlugin=v,w.useRequest=st,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()),this.setState({data:await l,error:void 0}),n!==this.currentRequestId?T():(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.23",
3
+ "version": "1.0.25",
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",