@reause/integrations 0.1.2
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/LICENSE +21 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.iife.js +1621 -0
- package/dist/index.iife.min.js +1 -0
- package/dist/index.js +13 -0
- package/dist/useAsyncValidator.d.ts +83 -0
- package/dist/useAsyncValidator.iife.js +192 -0
- package/dist/useAsyncValidator.iife.min.js +1 -0
- package/dist/useAsyncValidator.js +168 -0
- package/dist/useAxios.d.ts +125 -0
- package/dist/useAxios.iife.js +288 -0
- package/dist/useAxios.iife.min.js +1 -0
- package/dist/useAxios.js +265 -0
- package/dist/useChangeCase.d.ts +57 -0
- package/dist/useChangeCase.iife.js +91 -0
- package/dist/useChangeCase.iife.min.js +1 -0
- package/dist/useChangeCase.js +68 -0
- package/dist/useCookies.d.ts +117 -0
- package/dist/useCookies.iife.js +142 -0
- package/dist/useCookies.iife.min.js +1 -0
- package/dist/useCookies.js +117 -0
- package/dist/useDrauu.d.ts +152 -0
- package/dist/useDrauu.iife.js +221 -0
- package/dist/useDrauu.iife.min.js +1 -0
- package/dist/useDrauu.js +221 -0
- package/dist/useFocusTrap.d.ts +116 -0
- package/dist/useFocusTrap.iife.js +125 -0
- package/dist/useFocusTrap.iife.min.js +1 -0
- package/dist/useFocusTrap.js +125 -0
- package/dist/useFuse.d.ts +86 -0
- package/dist/useFuse.iife.js +95 -0
- package/dist/useFuse.iife.min.js +1 -0
- package/dist/useFuse.js +72 -0
- package/dist/useIDBKeyval.d.ts +139 -0
- package/dist/useIDBKeyval.iife.js +175 -0
- package/dist/useIDBKeyval.iife.min.js +1 -0
- package/dist/useIDBKeyval.js +174 -0
- package/dist/useJwt.d.ts +52 -0
- package/dist/useJwt.iife.js +54 -0
- package/dist/useJwt.iife.min.js +1 -0
- package/dist/useJwt.js +53 -0
- package/dist/useNProgress.d.ts +115 -0
- package/dist/useNProgress.iife.js +148 -0
- package/dist/useNProgress.iife.min.js +1 -0
- package/dist/useNProgress.js +125 -0
- package/dist/useQRCode.d.ts +38 -0
- package/dist/useQRCode.iife.js +78 -0
- package/dist/useQRCode.iife.min.js +1 -0
- package/dist/useQRCode.js +55 -0
- package/dist/useSortable.d.ts +143 -0
- package/dist/useSortable.iife.js +199 -0
- package/dist/useSortable.iife.min.js +1 -0
- package/dist/useSortable.js +173 -0
- package/package.json +92 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t,n,r,i,a,o,s,c,l,u,d,f,p,m){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var h=Object.create,g=Object.defineProperty,_=Object.getOwnPropertyDescriptor,v=Object.getOwnPropertyNames,y=Object.getPrototypeOf,b=Object.prototype.hasOwnProperty,x=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=v(t),a=0,o=i.length,s;a<o;a++)s=i[a],!b.call(e,s)&&s!==n&&g(e,s,{get:(e=>t[e]).bind(null,s),enumerable:!(r=_(t,s))||r.enumerable});return e},S=(e,t,n)=>(n=e==null?{}:h(y(e)),x(t||!e||!e.__esModule||!b.call(e,`default`)?g(n,`default`,{value:e,enumerable:!0}):n,e));t=S(t,1),i=S(i,1),a=S(a,1),o=S(o,1),l=S(l,1),f=S(f,1),p=S(p,1),m=S(m,1);let C=t.default.default||t.default;function w(e,t,r){let{validateOption:i={},immediate:a=!0,manual:o=!1}=r||{},s=e,c=t,[l,u]=(0,n.useState)(null),[d,f]=(0,n.useState)(!0),[p,m]=(0,n.useState)(!a||o),h=(0,n.useMemo)(()=>new C(c),[c]),g=(0,n.useRef)(e);g.current=e;let _=(0,n.useRef)(h);_.current=h;let v=(0,n.useRef)(i);v.current=i;let y=(0,n.useRef)({pass:p,isFinished:d,errorInfo:l});y.current.pass=p,y.current.isFinished=d,y.current.errorInfo=l;let b=(0,n.useRef)(!0),x=(0,n.useRef)(d),S=(0,n.useRef)([]),w=(0,n.useCallback)(async()=>{x.current=!1,y.current.pass=!1,y.current.isFinished=!1,m(!1),f(!1);let e=!1,t=null;try{await _.current.validate(g.current,v.current),e=!0,t=null}catch(e){t=e}let n={pass:e,errorInfo:t,errors:(t==null?void 0:t.errors)||[],errorFields:(t==null?void 0:t.fields)||{}};return b.current?(x.current=!0,y.current.pass=e,y.current.isFinished=!0,y.current.errorInfo=t,m(e),u(t),f(!0),n):n},[]),T=(0,n.useRef)(s),E=(0,n.useRef)(c),D=(0,n.useRef)(!1);(0,n.useEffect)(()=>{if(o)return;let e=!D.current;(e||T.current!==s||E.current!==c)&&(D.current=!0,T.current=s,E.current=c,(!e||a)&&w())}),(0,n.useEffect)(()=>(b.current=!0,()=>{b.current=!1}),[]);let O=(0,n.useRef)(void 0),k={get pass(){return y.current.pass},get isFinished(){return y.current.isFinished},get errorInfo(){return y.current.errorInfo},get errors(){var e;return((e=y.current.errorInfo)==null?void 0:e.errors)||[]},get errorFields(){var e;return((e=y.current.errorInfo)==null?void 0:e.fields)||{}},execute:w};O.current=k;let A=(0,n.useCallback)(()=>new Promise(e=>{if(x.current){e(O.current);return}S.current.push({resolve:e})}),[]);(0,n.useEffect)(()=>{if(!d)return;let e=S.current;S.current=[],e.forEach(e=>e.resolve(O.current))},[d]);let j=Object.create(k);return j.then=function(e,t){return A().then(e,t)},j}function T(...e){let t=typeof e[0]==`string`?e[0]:void 0,a=+(typeof t==`string`),o={immediate:!!a,shallow:!0,abortPrevious:!0},s={},c=i.default,l=o,u=e=>!!(e!=null&&e.request);e.length>0+a&&(u(e[0+a])?c=e[0+a]:s=e[0+a]),e.length>1+a&&u(e[1+a])&&(c=e[1+a]),(e.length===2+a&&!u(e[1+a])||e.length===3+a)&&(l=e[e.length-1]||o);let{onSuccess:d=r.noop,onError:f=r.noop,immediate:p,resetOnExecute:m=!1}=l,h=l.initialData,g=(0,n.useRef)(t);g.current=t;let _=(0,n.useRef)(s);_.current=s;let v=(0,n.useRef)(c);v.current=c;let y=(0,n.useRef)(l);y.current=l;let b=(0,n.useRef)(p);b.current=p;let x=(0,n.useRef)(m);x.current=m;let S=(0,n.useRef)(h);S.current=h;let C=(0,n.useRef)(d);C.current=d;let w=(0,n.useRef)(f);w.current=f;let T=(0,n.useRef)(l.onFinish);T.current=l.onFinish;let[E,D]=(0,n.useState)(void 0),[O,k]=(0,n.useState)(h),[A,j]=(0,n.useState)(!1),[M,N]=(0,n.useState)(!1),[P,F]=(0,n.useState)(!1),[I,L]=(0,n.useState)(void 0),R=(0,n.useRef)({response:E,data:O,isFinished:A,isLoading:M,isAborted:P,error:I}),z=(0,n.useRef)(null);z.current===null&&(z.current=new AbortController);let B=(0,n.useRef)(0),V=(0,n.useRef)([]),H=(0,n.useRef)(null),U=(0,n.useRef)(null),W=(0,n.useCallback)(e=>{var t;!R.current.isFinished&&R.current.isLoading&&((t=z.current)==null||t.abort(e),z.current=new AbortController,R.current.isAborted=!0,R.current.isLoading=!1,R.current.isFinished=!1,F(!0),N(!1),j(!1))},[]),G=(0,n.useCallback)(e=>{R.current.isLoading=e,R.current.isFinished=!e,N(e),j(!e)},[]),K=(0,n.useCallback)(()=>{x.current&&(R.current.data=S.current,k(S.current))},[]),q=(0,n.useCallback)(()=>{let e=V.current;if(e.length===0)return;V.current=[];let t=R.current.error,n=H.current;e.forEach(({resolve:e,reject:r})=>{t?r(t):e(n)})},[]),J=(0,n.useCallback)((e=g.current,t={})=>{var n,r;R.current.error=void 0,L(void 0);let a=typeof e==`string`?e:(n=g.current)==null?t.url:n;if(a===void 0){let e=new i.AxiosError(i.AxiosError.ERR_INVALID_URL);return R.current.error=e,R.current.isFinished=!0,L(e),j(!0),U.current}K(),y.current.abortPrevious!==!1&&W(),G(!0),B.current+=1;let o=B.current;return R.current.isAborted=!1,F(!1),v.current(a,{..._.current,...typeof e==`object`?e:t,signal:(r=z.current)==null?void 0:r.signal}).then(e=>{if(R.current.isAborted)return;R.current.response=e,D(e);let t=e==null?void 0:e.data;R.current.data=t,k(t),C.current(t)}).catch(e=>{R.current.error=e,L(e),w.current(e)}).finally(()=>{var e;(e=T.current)==null||e.call(T),o===B.current&&(G(!1),q())}),U.current},[W,q,G,K]),Y={get response(){return R.current.response},get data(){return R.current.data},get isFinished(){return R.current.isFinished},get isLoading(){return R.current.isLoading},get isAborted(){return R.current.isAborted},get isCanceled(){return R.current.isAborted},get error(){return R.current.error},abort:W,cancel:W,execute:J};H.current=Y;let X=(0,n.useCallback)(()=>new Promise((e,t)=>{if(!R.current.isLoading){let n=R.current.error;n?t(n):e(H.current);return}V.current.push({resolve:e,reject:t})}),[]);(0,n.useEffect)(()=>{b.current&&g.current&&J().catch(r.noop)},[J]),(0,n.useEffect)(()=>()=>{var e;(e=z.current)==null||e.abort()},[]),(0,n.useEffect)(()=>{M||q()},[M,q]);let Z=Object.create(Y);return Z.then=function(e,t){return X().then(e,t)},Z.catch=function(e){return X().catch(e)},U.current=Z,Z}let E=Object.entries(a).filter(e=>{let[t,n]=e;return typeof n==`function`&&t.endsWith(`Case`)}).reduce((e,[t,n])=>(e[t]=n,e),{});function D(e,t,i){let[a,o]=(0,n.useState)(e),s=(0,n.useRef)(e);(0,n.useEffect)(()=>{Object.is(e,s.current)||(s.current=e,o(e))});let c=(0,r.toValue)(t),l=(0,r.toValue)(i);return[(0,n.useMemo)(()=>{let e=E[c];if(!e)throw Error(`Invalid change case type "${c}"`);return e(a,l)},[a,c,l]),o]}function O(e){var t,n;let r=new o.default(typeof e==`string`?e:(t=e==null||(n=e.headers)==null?void 0:n.cookie)==null?null:t);return(e,{doNotParse:t=!1,autoUpdateDependencies:n=!1}={})=>k(e,{doNotParse:t,autoUpdateDependencies:n},r)}function k(e,{doNotParse:t=!1,autoUpdateDependencies:r=!1}={},i=new o.default){let a=(0,n.useRef)(r?[...e||[]]:e),s=(0,n.useRef)(null);s.current===null&&(s.current=i.getAll({doNotParse:!0}));let[c,l]=(0,n.useState)(0),u=(0,n.useRef)(e);(0,n.useEffect)(()=>{u.current!==e&&(u.current=e,r||(a.current=e))},[e,r]);let d=(0,n.useCallback)(()=>{var e;let t=i.getAll({doNotParse:!0}),n=s.current;if(n===null){s.current=t;return}A((e=a.current)==null?null:e,t,n)&&l(e=>e+1),s.current=t},[i]);return(0,n.useEffect)(()=>(i.addChangeListener(d),()=>{i.removeChangeListener(d)}),[i,d]),{get:(e,n)=>{let o=a.current;return r&&o&&!o.includes(e)&&o.push(e),i.get(e,{doNotParse:t,...n})},getAll:e=>i.getAll({doNotParse:t,...e}),set:(e,t,n)=>i.set(e,t,n),remove:(e,t)=>i.remove(e,t),addChangeListener:e=>i.addChangeListener(e),removeChangeListener:e=>i.removeChangeListener(e)}}function A(e,t,n){if(!e)return!0;for(let r of e)if(t[r]!==n[r])return!0;return!1}function j(e){let t=(0,r.toValue)(e);return(0,r.isRefLike)(t)&&(t=(0,r.toValue)(t)),typeof t==`object`&&t&&(t instanceof HTMLElement||t instanceof SVGElement)?t:null}let M={color:`black`,size:3,arrowEnd:!1,cornerRadius:0,dasharray:void 0,fill:`transparent`,mode:`draw`};function N(){let e=(0,n.useRef)(new Set);return{on:(0,n.useCallback)(t=>(e.current.add(t),{off:()=>{e.current.delete(t)}}),[]),trigger:(0,n.useCallback)((...t)=>{Array.from(e.current).forEach(e=>e(...t))},[])}}function P(e,t){let[r,i]=(0,n.useState)(void 0),[a,o]=(0,n.useState)(!1),[c,l]=(0,n.useState)(!1),[u,d]=(0,n.useState)(()=>({...M,...t==null?void 0:t.brush})),f=(0,n.useRef)(null),p=(0,n.useRef)([]),m=(0,n.useRef)(null),h=(0,n.useRef)(e);h.current=e;let g=(0,n.useRef)(t);g.current=t;let _=(0,n.useRef)(u);_.current=u;let{on:v,trigger:y}=N(),{on:b,trigger:x}=N(),{on:S,trigger:C}=N(),{on:w,trigger:T}=N(),{on:E,trigger:D}=N(),O=(0,n.useCallback)(()=>{let e=f.current;e&&(o(e.canUndo()),l(e.canRedo()))},[]),k=(0,n.useCallback)(()=>{let e=f.current;e&&(p.current.forEach(e=>e()),p.current=[],e.unmount(),f.current=null),i(void 0),o(!1),l(!1)},[]);return(0,n.useEffect)(()=>{let e=j(h.current),t=typeof SVGSVGElement>`u`||!(e instanceof SVGSVGElement)?null:e;if(t===m.current||(m.current=t,k(),!t))return;let n=(0,s.createDrauu)({...g.current,el:t,brush:_.current});f.current=n,p.current=[n.on(`canceled`,()=>D()),n.on(`committed`,e=>x(e)),n.on(`start`,()=>C()),n.on(`end`,()=>T()),n.on(`changed`,()=>{O(),y()})],O(),i(n)}),(0,n.useEffect)(()=>()=>{k(),m.current=null},[k]),{drauuInstance:r,load:(0,n.useCallback)(e=>{var t;(t=f.current)==null||t.load(e)},[]),dump:(0,n.useCallback)(()=>{var e;return(e=f.current)==null?void 0:e.dump()},[]),clear:(0,n.useCallback)(()=>{var e;(e=f.current)==null||e.clear()},[]),cancel:(0,n.useCallback)(()=>{var e;(e=f.current)==null||e.cancel()},[]),undo:(0,n.useCallback)(()=>{var e;return(e=f.current)==null?void 0:e.undo()},[]),redo:(0,n.useCallback)(()=>{var e;return(e=f.current)==null?void 0:e.redo()},[]),canUndo:a,canRedo:c,brush:u,setBrush:(0,n.useCallback)(e=>{let t=typeof e==`function`?e(_.current):e;_.current=t,d(t);let n=f.current;n&&(n.brush=t,t.mode&&(n.mode=t.mode))},[]),onChanged:v,onCommitted:b,onStart:S,onEnd:w,onCanceled:E}}function F(e){let t=(0,r.toValue)(e);return typeof t!=`string`&&(0,r.isRefLike)(t)&&(t=(0,r.toValue)(t)),typeof t==`string`||typeof t==`object`&&t&&(t instanceof HTMLElement||t instanceof SVGElement)?t:null}function I(e,t={}){let[i,a]=(0,n.useState)(!1),[o,s]=(0,n.useState)(!1),l=(0,n.useRef)(null),u=(0,n.useRef)(e);u.current=e;let d=(0,n.useRef)(t);d.current=t;let f=(0,n.useRef)(null);return(0,n.useEffect)(()=>{let e=(0,r.toArray)((0,r.toValue)(u.current)).map(F).filter(e=>e!=null),t=f.current;if(f.current=e,t&&e.length===t.length&&e.every((e,n)=>e===t[n])||!e.length)return;let n=l.current;if(n){let t=n.active;n.updateContainerElements(e),!t&&d.current.immediate&&n.activate()}else{let{immediate:t,...n}=d.current;l.current=(0,c.createFocusTrap)(e,{...n,onActivate(e){var t,n;a(!0),(t=(n=d.current).onActivate)==null||t.call(n,e)},onDeactivate(e){var t,n;a(!1),(t=(n=d.current).onDeactivate)==null||t.call(n,e)}}),t&&l.current.activate()}}),(0,n.useEffect)(()=>()=>{var e;(e=l.current)==null||e.deactivate(),l.current=null},[]),{hasFocus:i,isPaused:o,activate:(0,n.useCallback)(e=>{var t;(t=l.current)==null||t.activate(e)},[]),deactivate:(0,n.useCallback)(e=>{var t;(t=l.current)==null||t.deactivate(e)},[]),pause:(0,n.useCallback)(()=>{l.current&&(l.current.pause(),s(!0))},[]),unpause:(0,n.useCallback)(()=>{l.current&&(l.current.unpause(),s(!1))},[])}}function L(e,t,i){let a=t,o=(0,r.toValue)(i),s=(0,n.useMemo)(()=>new l.default(a,o==null?void 0:o.fuseOptions),[a,o==null?void 0:o.fuseOptions]),c=e;return{fuse:s,results:(0,n.useMemo)(()=>{if(o!=null&&o.matchAllWhenSearchEmpty&&!c)return a.map((e,t)=>({item:e,refIndex:t}));let e=o==null?void 0:o.resultLimit;return s.search(c,e?{limit:e}:void 0)},[a,s,o,c])}}function R(e){console.error(e)}function z(){return{read:e=>e,write:e=>e}}function B(e,t,r={}){var i;let a=(0,n.useRef)(r),o=(0,n.useRef)(void 0);o.current!=null||(o.current={value:t});let[s,c]=(0,n.useState)(()=>t==null?null:t),[l,d]=(0,n.useState)(!1),f=(0,n.useRef)(s),p=(0,n.useRef)(void 0),m=(0,n.useRef)(!0),h=(i=a.current.window)==null?typeof window<`u`?window:void 0:i,g=!!h&&`BroadcastChannel`in h,_=(0,n.useRef)(e);_.current=e;let v=(0,n.useCallback)(()=>{var e;return(e=a.current.serializer)==null?z():e},[]),y=(0,n.useCallback)(async()=>{let{onError:t=R,writeDefaults:n=!0}=a.current,r=o.current.value,i=e;try{let e=await(0,u.get)(i);if(!m.current||_.current!==i)return;if(e===void 0)r!=null&&n&&await(0,u.set)(i,v().write(r));else{let t=v().read(e);f.current=t,c(t)}}catch(e){m.current&&t(e)}m.current&&d(!0)},[e,v]),b=(0,n.useCallback)(async t=>{let{onError:n=R}=a.current;try{if(t==null){var r;await(0,u.del)(e),(r=p.current)==null||r.postMessage({type:`delete`})}else{var i;let n=v().write(t);await(0,u.update)(e,()=>n),(i=p.current)==null||i.postMessage({type:`set`,value:n})}}catch(e){n(e)}},[e,v]),x=(0,n.useCallback)(async e=>{f.current=e,m.current&&c(e),await b(e)},[b]);return(0,n.useEffect)(()=>(m.current=!0,y(),()=>{m.current=!1}),[y]),(0,n.useEffect)(()=>{let{onError:t=R,listenToStorageChanges:n=!0}=a.current;if(!n||!g)return;let r=new BroadcastChannel(`vueuse-idb-${JSON.stringify(e)}`);p.current=r;let i=e=>{var n;let{type:r,value:i}=(n=e.data)==null?{}:n;try{if(r===`delete`){var a;let e=(a=o.current.value)==null?null:a;f.current=e,c(e)}else if(r===`set`){let e=v().read(i);f.current=e,c(e)}}catch(e){t(e)}};return r.addEventListener(`message`,i,{passive:!0}),()=>{r.removeEventListener(`message`,i),p.current===r&&(p.current=void 0),r.close()}},[e,g,v]),[s,x,{isFinished:l,isSupported:g}]}function V(e,t={}){let{onError:r,fallbackValue:i=null}=t,a=(0,n.useRef)(r);a.current=r;let o=(0,n.useRef)(i);o.current=i;let s=e,c=(e,t)=>{try{return(0,d.jwtDecode)(e,t)}catch(e){var n;return(n=a.current)==null||n.call(a,e),o.current}};return{header:(0,n.useMemo)(()=>c(s,{header:!0}),[s]),payload:(0,n.useMemo)(()=>c(s),[s])}}function H(e=null,t){let[i,a]=(0,n.useState)(e),o=(0,n.useRef)(e),s=(0,n.useCallback)(e=>{o.current=e,a(e)},[]),c=(0,n.useRef)(e);(0,n.useEffect)(()=>{Object.is(e,c.current)||(c.current=e,s(e))});let l=(0,n.useRef)(t);l.current=t,(0,n.useEffect)(()=>{l.current&&f.default.configure(l.current)},[]),(0,n.useEffect)(()=>{typeof i==`number`&&r.isClient&&f.default.set(i)},[i]),(0,n.useEffect)(()=>()=>{f.default.remove()},[]);let u=(0,n.useCallback)(e=>{let t=typeof e==`function`?e(o.current):e;s(t)},[s]),d=(0,n.useCallback)(()=>{let e=f.default.isStarted(),t=f.default.start();return e||s(0),t},[s]),p=(0,n.useCallback)(e=>{let t=f.default.status,n=f.default.done(e);return(e||t)&&s(1),n},[s]),m=(0,n.useCallback)(e=>{(typeof e==`function`?e(typeof o.current==`number`&&o.current<1):e)?d():p()},[d,p]),h=(0,n.useCallback)(()=>{s(null),f.default.remove()},[s]);return{progress:i,setProgress:u,isLoading:typeof i==`number`&&i<1,setIsLoading:m,start:d,done:p,remove:h}}function U(e,t){let i=e,[a,o]=(0,n.useState)(``);return(0,n.useEffect)(()=>{if(!i||!r.isClient)return;let e=!1;return p.toDataURL(i,t).then(t=>{e||o(t)}),()=>{e=!0}},[i,t]),a}function W(e){let t=(0,r.toValue)(e);return(0,r.isRefLike)(t)&&(t=(0,r.toValue)(t)),typeof t==`object`&&t&&(t instanceof HTMLElement||t instanceof SVGElement)?t:null}function G(e,t,r={}){let{document:i=globalThis.document,watchElement:a=!1,onUpdate:o,...s}=r,c=(0,n.useRef)(void 0),l=(0,n.useRef)(t),u=(0,n.useRef)(o),d=(0,n.useRef)(s);l.current=t,u.current=o,d.current=s;let f=(0,n.useCallback)(()=>{var e;(e=c.current)==null||e.destroy(),c.current=void 0},[]),p=(0,n.useCallback)(e=>{e&&c.current===void 0&&(c.current=new m.default(e,{onUpdate:e=>{var t;let n=J(l.current,e.oldIndex,e.newIndex,e);(t=u.current)==null||t.call(u,n,e)},...d.current}))},[]),h=(0,n.useCallback)(()=>{let t=typeof e==`string`?i==null?void 0:i.querySelector(e):W(e);t&&p(t)},[i,e,p]),g=(0,n.useCallback)(()=>{f()},[f]),_=(0,n.useCallback)((e,t)=>{var n,r;if(t!==void 0)(n=c.current)==null||n.option(e,t);else return(r=c.current)==null?void 0:r.option(e)},[]),v=typeof e==`string`?e:W(e);return(0,n.useEffect)(()=>{if(a&&typeof v!=`string`)return f(),v&&p(v),f},[f,p,v,a]),(0,n.useEffect)(()=>{if(!(a&&typeof e!=`string`))return h(),f},[f,h,a,e]),{start:h,stop:g,option:_}}function K(e,t,n){let r=e.children[n];e.insertBefore(t,r)}function q(e){e.parentNode&&e.parentNode.removeChild(e)}function J(e,t,n,r){r!=null&&(q(r.item),K(r.from,r.item,t));let i=[...e];if(n>=0&&n<i.length){let e=i.splice(t,1)[0];i.splice(n,0,e)}return i}e.createCookies=O,e.insertNodeAt=K,e.moveArrayElement=J,e.removeNode=q,e.useAsyncValidator=w,e.useAxios=T,e.useChangeCase=D,e.useCookies=k,e.useDrauu=P,e.useFocusTrap=I,e.useFuse=L,e.useIDBKeyval=B,e.useJwt=V,e.useNProgress=H,e.useQRCode=U,e.useSortable=G})(this.reause=this.reause||{},AsyncValidator,React,reause,axios,changeCase,UniversalCookie,Drauu,focusTrap,Fuse,idbKeyval,jwt_decode,nprogress,QRCode,Sortable);
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useAsyncValidator } from "./useAsyncValidator.js";
|
|
2
|
+
import { useAxios } from "./useAxios.js";
|
|
3
|
+
import { useChangeCase } from "./useChangeCase.js";
|
|
4
|
+
import { createCookies, useCookies } from "./useCookies.js";
|
|
5
|
+
import { useDrauu } from "./useDrauu.js";
|
|
6
|
+
import { useFocusTrap } from "./useFocusTrap.js";
|
|
7
|
+
import { useFuse } from "./useFuse.js";
|
|
8
|
+
import { useIDBKeyval } from "./useIDBKeyval.js";
|
|
9
|
+
import { useJwt } from "./useJwt.js";
|
|
10
|
+
import { useNProgress } from "./useNProgress.js";
|
|
11
|
+
import { useQRCode } from "./useQRCode.js";
|
|
12
|
+
import { insertNodeAt, moveArrayElement, removeNode, useSortable } from "./useSortable.js";
|
|
13
|
+
export { createCookies, insertNodeAt, moveArrayElement, removeNode, useAsyncValidator, useAxios, useChangeCase, useCookies, useDrauu, useFocusTrap, useFuse, useIDBKeyval, useJwt, useNProgress, useQRCode, useSortable };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Rules, ValidateError, ValidateOption } from "async-validator";
|
|
2
|
+
//#region useAsyncValidator/index.d.ts
|
|
3
|
+
export type AsyncValidatorError = Error & {
|
|
4
|
+
errors: ValidateError[];
|
|
5
|
+
fields: Record<string, ValidateError[]>;
|
|
6
|
+
};
|
|
7
|
+
export interface UseAsyncValidatorExecuteReturn {
|
|
8
|
+
pass: boolean;
|
|
9
|
+
errors: ValidateError[];
|
|
10
|
+
errorInfo: AsyncValidatorError | null;
|
|
11
|
+
errorFields: Record<string, ValidateError[]>;
|
|
12
|
+
}
|
|
13
|
+
export interface UseAsyncValidatorReturn {
|
|
14
|
+
pass: boolean;
|
|
15
|
+
isFinished: boolean;
|
|
16
|
+
errors: ValidateError[];
|
|
17
|
+
errorInfo: AsyncValidatorError | null;
|
|
18
|
+
errorFields: Record<string, ValidateError[]>;
|
|
19
|
+
execute: () => Promise<UseAsyncValidatorExecuteReturn>;
|
|
20
|
+
}
|
|
21
|
+
export interface UseAsyncValidatorOptions {
|
|
22
|
+
/**
|
|
23
|
+
* @see https://github.com/yiminghe/async-validator#options
|
|
24
|
+
*/
|
|
25
|
+
validateOption?: ValidateOption;
|
|
26
|
+
/**
|
|
27
|
+
* The validation will be triggered right away for the first time.
|
|
28
|
+
* Only works when `manual` is not set to true.
|
|
29
|
+
*
|
|
30
|
+
* @default true
|
|
31
|
+
*/
|
|
32
|
+
immediate?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* If set to true, the validation will not be triggered automatically.
|
|
35
|
+
*/
|
|
36
|
+
manual?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* React port of VueUse's `useAsyncValidator` — a wrapper around
|
|
40
|
+
* [`async-validator`](https://github.com/yiminghe/async-validator).
|
|
41
|
+
*
|
|
42
|
+
* Map from @vueuse/integrations `useAsyncValidator`
|
|
43
|
+
* (`source/vueuse/packages/integrations/useAsyncValidator/`). The upstream
|
|
44
|
+
* object return is preserved (`{ pass, isFinished, errors, errorInfo,
|
|
45
|
+
* errorFields, execute }`) with the refs unwrapped: every member is a plain
|
|
46
|
+
* value (no `.value`), `pass` starts as `!immediate || manual`, `execute()`
|
|
47
|
+
* flips `isFinished` to `false`, awaits
|
|
48
|
+
* `validator.validate(value, validateOption)` and settles with
|
|
49
|
+
* `pass`, `errorInfo`, `errors` and `errorFields`; `errors` is
|
|
50
|
+
* `errorInfo?.errors || []` and `errorFields` is `errorInfo?.fields || {}`.
|
|
51
|
+
* `value` and `rules` are the hook's **read-only value sources** and take
|
|
52
|
+
* plain values (`Record<string, any>` and `Rules`; upstream:
|
|
53
|
+
* `MaybeRefOrGetter`); the returned
|
|
54
|
+
* object is promise-like (`await useAsyncValidator(...)` resolves with the
|
|
55
|
+
* current snapshot), mirroring `useAsyncState`.
|
|
56
|
+
*
|
|
57
|
+
* Adjustment for React:
|
|
58
|
+
* - upstream re-runs validation from
|
|
59
|
+
* `watch([valueRef, validator], execute, { immediate, deep: true })`. React
|
|
60
|
+
* has no deep observation, so this port re-runs from an effect keyed on the
|
|
61
|
+
* identity of `value` / `rules` and skips it entirely when
|
|
62
|
+
* `manual` is `true`. Consequence: mutating the SAME object in place does NOT
|
|
63
|
+
* re-trigger validation — pass a new object (or new `rules`) or call
|
|
64
|
+
* `execute()` yourself. In React StrictMode dev builds the initial
|
|
65
|
+
* validation may run twice (the effect is double-invoked); the extra run is
|
|
66
|
+
* idempotent.
|
|
67
|
+
* - `errors` and `errorFields` are derived during render from `errorInfo`
|
|
68
|
+
* (upstream uses `computed`); `execute` is stable and ignores results that
|
|
69
|
+
* arrive after unmount.
|
|
70
|
+
*
|
|
71
|
+
* @__NO_SIDE_EFFECTS__
|
|
72
|
+
* @example
|
|
73
|
+
* const { pass, isFinished, errors, errorFields, execute } = useAsyncValidator(
|
|
74
|
+
* form,
|
|
75
|
+
* { name: { type: 'string', min: 5, max: 20 }, age: { type: 'number' } },
|
|
76
|
+
* )
|
|
77
|
+
* // `pass` becomes true once the initial validation resolves
|
|
78
|
+
*
|
|
79
|
+
* @see https://vueuse.org/useAsyncValidator
|
|
80
|
+
* @see https://github.com/yiminghe/async-validator
|
|
81
|
+
*/
|
|
82
|
+
export declare function useAsyncValidator(value: Record<string, any>, rules: Rules, options?: UseAsyncValidatorOptions): UseAsyncValidatorReturn & PromiseLike<UseAsyncValidatorReturn>;
|
|
83
|
+
//#endregion
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
(function(exports, async_validator, react) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
//#region \0rolldown/runtime.js
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
21
|
+
value: mod,
|
|
22
|
+
enumerable: true
|
|
23
|
+
}) : target, mod));
|
|
24
|
+
//#endregion
|
|
25
|
+
async_validator = __toESM(async_validator, 1);
|
|
26
|
+
//#region useAsyncValidator/index.tsx
|
|
27
|
+
const AsyncValidatorSchema = async_validator.default.default || async_validator.default;
|
|
28
|
+
/**
|
|
29
|
+
* React port of VueUse's `useAsyncValidator` — a wrapper around
|
|
30
|
+
* [`async-validator`](https://github.com/yiminghe/async-validator).
|
|
31
|
+
*
|
|
32
|
+
* Map from @vueuse/integrations `useAsyncValidator`
|
|
33
|
+
* (`source/vueuse/packages/integrations/useAsyncValidator/`). The upstream
|
|
34
|
+
* object return is preserved (`{ pass, isFinished, errors, errorInfo,
|
|
35
|
+
* errorFields, execute }`) with the refs unwrapped: every member is a plain
|
|
36
|
+
* value (no `.value`), `pass` starts as `!immediate || manual`, `execute()`
|
|
37
|
+
* flips `isFinished` to `false`, awaits
|
|
38
|
+
* `validator.validate(value, validateOption)` and settles with
|
|
39
|
+
* `pass`, `errorInfo`, `errors` and `errorFields`; `errors` is
|
|
40
|
+
* `errorInfo?.errors || []` and `errorFields` is `errorInfo?.fields || {}`.
|
|
41
|
+
* `value` and `rules` are the hook's **read-only value sources** and take
|
|
42
|
+
* plain values (`Record<string, any>` and `Rules`; upstream:
|
|
43
|
+
* `MaybeRefOrGetter`); the returned
|
|
44
|
+
* object is promise-like (`await useAsyncValidator(...)` resolves with the
|
|
45
|
+
* current snapshot), mirroring `useAsyncState`.
|
|
46
|
+
*
|
|
47
|
+
* Adjustment for React:
|
|
48
|
+
* - upstream re-runs validation from
|
|
49
|
+
* `watch([valueRef, validator], execute, { immediate, deep: true })`. React
|
|
50
|
+
* has no deep observation, so this port re-runs from an effect keyed on the
|
|
51
|
+
* identity of `value` / `rules` and skips it entirely when
|
|
52
|
+
* `manual` is `true`. Consequence: mutating the SAME object in place does NOT
|
|
53
|
+
* re-trigger validation — pass a new object (or new `rules`) or call
|
|
54
|
+
* `execute()` yourself. In React StrictMode dev builds the initial
|
|
55
|
+
* validation may run twice (the effect is double-invoked); the extra run is
|
|
56
|
+
* idempotent.
|
|
57
|
+
* - `errors` and `errorFields` are derived during render from `errorInfo`
|
|
58
|
+
* (upstream uses `computed`); `execute` is stable and ignores results that
|
|
59
|
+
* arrive after unmount.
|
|
60
|
+
*
|
|
61
|
+
* @__NO_SIDE_EFFECTS__
|
|
62
|
+
* @example
|
|
63
|
+
* const { pass, isFinished, errors, errorFields, execute } = useAsyncValidator(
|
|
64
|
+
* form,
|
|
65
|
+
* { name: { type: 'string', min: 5, max: 20 }, age: { type: 'number' } },
|
|
66
|
+
* )
|
|
67
|
+
* // `pass` becomes true once the initial validation resolves
|
|
68
|
+
*
|
|
69
|
+
* @see https://vueuse.org/useAsyncValidator
|
|
70
|
+
* @see https://github.com/yiminghe/async-validator
|
|
71
|
+
*/
|
|
72
|
+
function useAsyncValidator(value, rules, options) {
|
|
73
|
+
const { validateOption = {}, immediate = true, manual = false } = options || {};
|
|
74
|
+
const valueValue = value;
|
|
75
|
+
const rulesValue = rules;
|
|
76
|
+
const [errorInfo, setErrorInfo] = (0, react.useState)(null);
|
|
77
|
+
const [isFinished, setIsFinished] = (0, react.useState)(true);
|
|
78
|
+
const [pass, setPass] = (0, react.useState)(!immediate || manual);
|
|
79
|
+
const validator = (0, react.useMemo)(() => new AsyncValidatorSchema(rulesValue), [rulesValue]);
|
|
80
|
+
const valueRef = (0, react.useRef)(value);
|
|
81
|
+
valueRef.current = value;
|
|
82
|
+
const validatorRef = (0, react.useRef)(validator);
|
|
83
|
+
validatorRef.current = validator;
|
|
84
|
+
const validateOptionRef = (0, react.useRef)(validateOption);
|
|
85
|
+
validateOptionRef.current = validateOption;
|
|
86
|
+
const liveRef = (0, react.useRef)({
|
|
87
|
+
pass,
|
|
88
|
+
isFinished,
|
|
89
|
+
errorInfo
|
|
90
|
+
});
|
|
91
|
+
liveRef.current.pass = pass;
|
|
92
|
+
liveRef.current.isFinished = isFinished;
|
|
93
|
+
liveRef.current.errorInfo = errorInfo;
|
|
94
|
+
const mountedRef = (0, react.useRef)(true);
|
|
95
|
+
const finishedRef = (0, react.useRef)(isFinished);
|
|
96
|
+
const waitersRef = (0, react.useRef)([]);
|
|
97
|
+
const execute = (0, react.useCallback)(async () => {
|
|
98
|
+
finishedRef.current = false;
|
|
99
|
+
liveRef.current.pass = false;
|
|
100
|
+
liveRef.current.isFinished = false;
|
|
101
|
+
setPass(false);
|
|
102
|
+
setIsFinished(false);
|
|
103
|
+
let nextPass = false;
|
|
104
|
+
let nextErrorInfo = null;
|
|
105
|
+
try {
|
|
106
|
+
await validatorRef.current.validate(valueRef.current, validateOptionRef.current);
|
|
107
|
+
nextPass = true;
|
|
108
|
+
nextErrorInfo = null;
|
|
109
|
+
} catch (err) {
|
|
110
|
+
nextErrorInfo = err;
|
|
111
|
+
}
|
|
112
|
+
const result = {
|
|
113
|
+
pass: nextPass,
|
|
114
|
+
errorInfo: nextErrorInfo,
|
|
115
|
+
errors: (nextErrorInfo === null || nextErrorInfo === void 0 ? void 0 : nextErrorInfo.errors) || [],
|
|
116
|
+
errorFields: (nextErrorInfo === null || nextErrorInfo === void 0 ? void 0 : nextErrorInfo.fields) || {}
|
|
117
|
+
};
|
|
118
|
+
if (!mountedRef.current) return result;
|
|
119
|
+
finishedRef.current = true;
|
|
120
|
+
liveRef.current.pass = nextPass;
|
|
121
|
+
liveRef.current.isFinished = true;
|
|
122
|
+
liveRef.current.errorInfo = nextErrorInfo;
|
|
123
|
+
setPass(nextPass);
|
|
124
|
+
setErrorInfo(nextErrorInfo);
|
|
125
|
+
setIsFinished(true);
|
|
126
|
+
return result;
|
|
127
|
+
}, []);
|
|
128
|
+
const lastValueRef = (0, react.useRef)(valueValue);
|
|
129
|
+
const lastRulesRef = (0, react.useRef)(rulesValue);
|
|
130
|
+
const hasAutoRunRef = (0, react.useRef)(false);
|
|
131
|
+
(0, react.useEffect)(() => {
|
|
132
|
+
if (manual) return;
|
|
133
|
+
const first = !hasAutoRunRef.current;
|
|
134
|
+
if (!(first || lastValueRef.current !== valueValue || lastRulesRef.current !== rulesValue)) return;
|
|
135
|
+
hasAutoRunRef.current = true;
|
|
136
|
+
lastValueRef.current = valueValue;
|
|
137
|
+
lastRulesRef.current = rulesValue;
|
|
138
|
+
if (first && !immediate) return;
|
|
139
|
+
execute();
|
|
140
|
+
});
|
|
141
|
+
(0, react.useEffect)(() => {
|
|
142
|
+
mountedRef.current = true;
|
|
143
|
+
return () => {
|
|
144
|
+
mountedRef.current = false;
|
|
145
|
+
};
|
|
146
|
+
}, []);
|
|
147
|
+
const baseRef = (0, react.useRef)(void 0);
|
|
148
|
+
const base = {
|
|
149
|
+
get pass() {
|
|
150
|
+
return liveRef.current.pass;
|
|
151
|
+
},
|
|
152
|
+
get isFinished() {
|
|
153
|
+
return liveRef.current.isFinished;
|
|
154
|
+
},
|
|
155
|
+
get errorInfo() {
|
|
156
|
+
return liveRef.current.errorInfo;
|
|
157
|
+
},
|
|
158
|
+
get errors() {
|
|
159
|
+
var _liveRef$current$erro;
|
|
160
|
+
return ((_liveRef$current$erro = liveRef.current.errorInfo) === null || _liveRef$current$erro === void 0 ? void 0 : _liveRef$current$erro.errors) || [];
|
|
161
|
+
},
|
|
162
|
+
get errorFields() {
|
|
163
|
+
var _liveRef$current$erro2;
|
|
164
|
+
return ((_liveRef$current$erro2 = liveRef.current.errorInfo) === null || _liveRef$current$erro2 === void 0 ? void 0 : _liveRef$current$erro2.fields) || {};
|
|
165
|
+
},
|
|
166
|
+
execute
|
|
167
|
+
};
|
|
168
|
+
baseRef.current = base;
|
|
169
|
+
const waitUntilFinished = (0, react.useCallback)(() => {
|
|
170
|
+
return new Promise((resolve) => {
|
|
171
|
+
if (finishedRef.current) {
|
|
172
|
+
resolve(baseRef.current);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
waitersRef.current.push({ resolve });
|
|
176
|
+
});
|
|
177
|
+
}, []);
|
|
178
|
+
(0, react.useEffect)(() => {
|
|
179
|
+
if (!isFinished) return;
|
|
180
|
+
const waiters = waitersRef.current;
|
|
181
|
+
waitersRef.current = [];
|
|
182
|
+
waiters.forEach((waiter) => waiter.resolve(baseRef.current));
|
|
183
|
+
}, [isFinished]);
|
|
184
|
+
const shell = Object.create(base);
|
|
185
|
+
shell.then = function then(onFulfilled, onRejected) {
|
|
186
|
+
return waitUntilFinished().then(onFulfilled, onRejected);
|
|
187
|
+
};
|
|
188
|
+
return shell;
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
exports.useAsyncValidator = useAsyncValidator;
|
|
192
|
+
})(this.reause = this.reause || {}, AsyncValidator, React);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var r=Object.create,i=Object.defineProperty,a=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.getPrototypeOf,c=Object.prototype.hasOwnProperty,l=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var s=o(t),l=0,u=s.length,d;l<u;l++)d=s[l],!c.call(e,d)&&d!==n&&i(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=a(t,d))||r.enumerable});return e};t=((e,t,n)=>(n=e==null?{}:r(s(e)),l(t||!e||!e.__esModule||!c.call(e,`default`)?i(n,`default`,{value:e,enumerable:!0}):n,e)))(t,1);let u=t.default.default||t.default;function d(e,t,r){let{validateOption:i={},immediate:a=!0,manual:o=!1}=r||{},s=e,c=t,[l,d]=(0,n.useState)(null),[f,p]=(0,n.useState)(!0),[m,h]=(0,n.useState)(!a||o),g=(0,n.useMemo)(()=>new u(c),[c]),_=(0,n.useRef)(e);_.current=e;let v=(0,n.useRef)(g);v.current=g;let y=(0,n.useRef)(i);y.current=i;let b=(0,n.useRef)({pass:m,isFinished:f,errorInfo:l});b.current.pass=m,b.current.isFinished=f,b.current.errorInfo=l;let x=(0,n.useRef)(!0),S=(0,n.useRef)(f),C=(0,n.useRef)([]),w=(0,n.useCallback)(async()=>{S.current=!1,b.current.pass=!1,b.current.isFinished=!1,h(!1),p(!1);let e=!1,t=null;try{await v.current.validate(_.current,y.current),e=!0,t=null}catch(e){t=e}let n={pass:e,errorInfo:t,errors:(t==null?void 0:t.errors)||[],errorFields:(t==null?void 0:t.fields)||{}};return x.current?(S.current=!0,b.current.pass=e,b.current.isFinished=!0,b.current.errorInfo=t,h(e),d(t),p(!0),n):n},[]),T=(0,n.useRef)(s),E=(0,n.useRef)(c),D=(0,n.useRef)(!1);(0,n.useEffect)(()=>{if(o)return;let e=!D.current;(e||T.current!==s||E.current!==c)&&(D.current=!0,T.current=s,E.current=c,(!e||a)&&w())}),(0,n.useEffect)(()=>(x.current=!0,()=>{x.current=!1}),[]);let O=(0,n.useRef)(void 0),k={get pass(){return b.current.pass},get isFinished(){return b.current.isFinished},get errorInfo(){return b.current.errorInfo},get errors(){var e;return((e=b.current.errorInfo)==null?void 0:e.errors)||[]},get errorFields(){var e;return((e=b.current.errorInfo)==null?void 0:e.fields)||{}},execute:w};O.current=k;let A=(0,n.useCallback)(()=>new Promise(e=>{if(S.current){e(O.current);return}C.current.push({resolve:e})}),[]);(0,n.useEffect)(()=>{if(!f)return;let e=C.current;C.current=[],e.forEach(e=>e.resolve(O.current))},[f]);let j=Object.create(k);return j.then=function(e,t){return A().then(e,t)},j}e.useAsyncValidator=d})(this.reause=this.reause||{},AsyncValidator,React);
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import Schema from "async-validator";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
//#region useAsyncValidator/index.tsx
|
|
4
|
+
const AsyncValidatorSchema = Schema.default || Schema;
|
|
5
|
+
/**
|
|
6
|
+
* React port of VueUse's `useAsyncValidator` — a wrapper around
|
|
7
|
+
* [`async-validator`](https://github.com/yiminghe/async-validator).
|
|
8
|
+
*
|
|
9
|
+
* Map from @vueuse/integrations `useAsyncValidator`
|
|
10
|
+
* (`source/vueuse/packages/integrations/useAsyncValidator/`). The upstream
|
|
11
|
+
* object return is preserved (`{ pass, isFinished, errors, errorInfo,
|
|
12
|
+
* errorFields, execute }`) with the refs unwrapped: every member is a plain
|
|
13
|
+
* value (no `.value`), `pass` starts as `!immediate || manual`, `execute()`
|
|
14
|
+
* flips `isFinished` to `false`, awaits
|
|
15
|
+
* `validator.validate(value, validateOption)` and settles with
|
|
16
|
+
* `pass`, `errorInfo`, `errors` and `errorFields`; `errors` is
|
|
17
|
+
* `errorInfo?.errors || []` and `errorFields` is `errorInfo?.fields || {}`.
|
|
18
|
+
* `value` and `rules` are the hook's **read-only value sources** and take
|
|
19
|
+
* plain values (`Record<string, any>` and `Rules`; upstream:
|
|
20
|
+
* `MaybeRefOrGetter`); the returned
|
|
21
|
+
* object is promise-like (`await useAsyncValidator(...)` resolves with the
|
|
22
|
+
* current snapshot), mirroring `useAsyncState`.
|
|
23
|
+
*
|
|
24
|
+
* Adjustment for React:
|
|
25
|
+
* - upstream re-runs validation from
|
|
26
|
+
* `watch([valueRef, validator], execute, { immediate, deep: true })`. React
|
|
27
|
+
* has no deep observation, so this port re-runs from an effect keyed on the
|
|
28
|
+
* identity of `value` / `rules` and skips it entirely when
|
|
29
|
+
* `manual` is `true`. Consequence: mutating the SAME object in place does NOT
|
|
30
|
+
* re-trigger validation — pass a new object (or new `rules`) or call
|
|
31
|
+
* `execute()` yourself. In React StrictMode dev builds the initial
|
|
32
|
+
* validation may run twice (the effect is double-invoked); the extra run is
|
|
33
|
+
* idempotent.
|
|
34
|
+
* - `errors` and `errorFields` are derived during render from `errorInfo`
|
|
35
|
+
* (upstream uses `computed`); `execute` is stable and ignores results that
|
|
36
|
+
* arrive after unmount.
|
|
37
|
+
*
|
|
38
|
+
* @__NO_SIDE_EFFECTS__
|
|
39
|
+
* @example
|
|
40
|
+
* const { pass, isFinished, errors, errorFields, execute } = useAsyncValidator(
|
|
41
|
+
* form,
|
|
42
|
+
* { name: { type: 'string', min: 5, max: 20 }, age: { type: 'number' } },
|
|
43
|
+
* )
|
|
44
|
+
* // `pass` becomes true once the initial validation resolves
|
|
45
|
+
*
|
|
46
|
+
* @see https://vueuse.org/useAsyncValidator
|
|
47
|
+
* @see https://github.com/yiminghe/async-validator
|
|
48
|
+
*/
|
|
49
|
+
function useAsyncValidator(value, rules, options) {
|
|
50
|
+
const { validateOption = {}, immediate = true, manual = false } = options || {};
|
|
51
|
+
const valueValue = value;
|
|
52
|
+
const rulesValue = rules;
|
|
53
|
+
const [errorInfo, setErrorInfo] = useState(null);
|
|
54
|
+
const [isFinished, setIsFinished] = useState(true);
|
|
55
|
+
const [pass, setPass] = useState(!immediate || manual);
|
|
56
|
+
const validator = useMemo(() => new AsyncValidatorSchema(rulesValue), [rulesValue]);
|
|
57
|
+
const valueRef = useRef(value);
|
|
58
|
+
valueRef.current = value;
|
|
59
|
+
const validatorRef = useRef(validator);
|
|
60
|
+
validatorRef.current = validator;
|
|
61
|
+
const validateOptionRef = useRef(validateOption);
|
|
62
|
+
validateOptionRef.current = validateOption;
|
|
63
|
+
const liveRef = useRef({
|
|
64
|
+
pass,
|
|
65
|
+
isFinished,
|
|
66
|
+
errorInfo
|
|
67
|
+
});
|
|
68
|
+
liveRef.current.pass = pass;
|
|
69
|
+
liveRef.current.isFinished = isFinished;
|
|
70
|
+
liveRef.current.errorInfo = errorInfo;
|
|
71
|
+
const mountedRef = useRef(true);
|
|
72
|
+
const finishedRef = useRef(isFinished);
|
|
73
|
+
const waitersRef = useRef([]);
|
|
74
|
+
const execute = useCallback(async () => {
|
|
75
|
+
finishedRef.current = false;
|
|
76
|
+
liveRef.current.pass = false;
|
|
77
|
+
liveRef.current.isFinished = false;
|
|
78
|
+
setPass(false);
|
|
79
|
+
setIsFinished(false);
|
|
80
|
+
let nextPass = false;
|
|
81
|
+
let nextErrorInfo = null;
|
|
82
|
+
try {
|
|
83
|
+
await validatorRef.current.validate(valueRef.current, validateOptionRef.current);
|
|
84
|
+
nextPass = true;
|
|
85
|
+
nextErrorInfo = null;
|
|
86
|
+
} catch (err) {
|
|
87
|
+
nextErrorInfo = err;
|
|
88
|
+
}
|
|
89
|
+
const result = {
|
|
90
|
+
pass: nextPass,
|
|
91
|
+
errorInfo: nextErrorInfo,
|
|
92
|
+
errors: (nextErrorInfo === null || nextErrorInfo === void 0 ? void 0 : nextErrorInfo.errors) || [],
|
|
93
|
+
errorFields: (nextErrorInfo === null || nextErrorInfo === void 0 ? void 0 : nextErrorInfo.fields) || {}
|
|
94
|
+
};
|
|
95
|
+
if (!mountedRef.current) return result;
|
|
96
|
+
finishedRef.current = true;
|
|
97
|
+
liveRef.current.pass = nextPass;
|
|
98
|
+
liveRef.current.isFinished = true;
|
|
99
|
+
liveRef.current.errorInfo = nextErrorInfo;
|
|
100
|
+
setPass(nextPass);
|
|
101
|
+
setErrorInfo(nextErrorInfo);
|
|
102
|
+
setIsFinished(true);
|
|
103
|
+
return result;
|
|
104
|
+
}, []);
|
|
105
|
+
const lastValueRef = useRef(valueValue);
|
|
106
|
+
const lastRulesRef = useRef(rulesValue);
|
|
107
|
+
const hasAutoRunRef = useRef(false);
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
if (manual) return;
|
|
110
|
+
const first = !hasAutoRunRef.current;
|
|
111
|
+
if (!(first || lastValueRef.current !== valueValue || lastRulesRef.current !== rulesValue)) return;
|
|
112
|
+
hasAutoRunRef.current = true;
|
|
113
|
+
lastValueRef.current = valueValue;
|
|
114
|
+
lastRulesRef.current = rulesValue;
|
|
115
|
+
if (first && !immediate) return;
|
|
116
|
+
execute();
|
|
117
|
+
});
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
mountedRef.current = true;
|
|
120
|
+
return () => {
|
|
121
|
+
mountedRef.current = false;
|
|
122
|
+
};
|
|
123
|
+
}, []);
|
|
124
|
+
const baseRef = useRef(void 0);
|
|
125
|
+
const base = {
|
|
126
|
+
get pass() {
|
|
127
|
+
return liveRef.current.pass;
|
|
128
|
+
},
|
|
129
|
+
get isFinished() {
|
|
130
|
+
return liveRef.current.isFinished;
|
|
131
|
+
},
|
|
132
|
+
get errorInfo() {
|
|
133
|
+
return liveRef.current.errorInfo;
|
|
134
|
+
},
|
|
135
|
+
get errors() {
|
|
136
|
+
var _liveRef$current$erro;
|
|
137
|
+
return ((_liveRef$current$erro = liveRef.current.errorInfo) === null || _liveRef$current$erro === void 0 ? void 0 : _liveRef$current$erro.errors) || [];
|
|
138
|
+
},
|
|
139
|
+
get errorFields() {
|
|
140
|
+
var _liveRef$current$erro2;
|
|
141
|
+
return ((_liveRef$current$erro2 = liveRef.current.errorInfo) === null || _liveRef$current$erro2 === void 0 ? void 0 : _liveRef$current$erro2.fields) || {};
|
|
142
|
+
},
|
|
143
|
+
execute
|
|
144
|
+
};
|
|
145
|
+
baseRef.current = base;
|
|
146
|
+
const waitUntilFinished = useCallback(() => {
|
|
147
|
+
return new Promise((resolve) => {
|
|
148
|
+
if (finishedRef.current) {
|
|
149
|
+
resolve(baseRef.current);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
waitersRef.current.push({ resolve });
|
|
153
|
+
});
|
|
154
|
+
}, []);
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
if (!isFinished) return;
|
|
157
|
+
const waiters = waitersRef.current;
|
|
158
|
+
waitersRef.current = [];
|
|
159
|
+
waiters.forEach((waiter) => waiter.resolve(baseRef.current));
|
|
160
|
+
}, [isFinished]);
|
|
161
|
+
const shell = Object.create(base);
|
|
162
|
+
shell.then = function then(onFulfilled, onRejected) {
|
|
163
|
+
return waitUntilFinished().then(onFulfilled, onRejected);
|
|
164
|
+
};
|
|
165
|
+
return shell;
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
export { useAsyncValidator };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
|
|
2
|
+
//#region useAxios/index.d.ts
|
|
3
|
+
export interface UseAxiosReturn<T, R = AxiosResponse<T>, _D = any, O extends UseAxiosOptions = UseAxiosOptions<T>> {
|
|
4
|
+
/**
|
|
5
|
+
* Axios Response
|
|
6
|
+
*/
|
|
7
|
+
response: R | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Axios response data
|
|
10
|
+
*
|
|
11
|
+
* `O extends UseAxiosOptionsWithInitialData<T>` is `T` (upstream's
|
|
12
|
+
* `Ref<T>` branch), otherwise `T | undefined`.
|
|
13
|
+
*/
|
|
14
|
+
data: O extends UseAxiosOptionsWithInitialData<T> ? T : T | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Indicates if the request has finished
|
|
17
|
+
*/
|
|
18
|
+
isFinished: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Indicates if the request is currently loading
|
|
21
|
+
*/
|
|
22
|
+
isLoading: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Indicates if the request was canceled
|
|
25
|
+
*/
|
|
26
|
+
isAborted: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Any errors that may have occurred
|
|
29
|
+
*/
|
|
30
|
+
error: unknown | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Aborts the current request
|
|
33
|
+
*/
|
|
34
|
+
abort: (message?: string | undefined) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Alias to `abort`
|
|
37
|
+
*/
|
|
38
|
+
cancel: (message?: string | undefined) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Alias to `isAborted`
|
|
41
|
+
*/
|
|
42
|
+
isCanceled: boolean;
|
|
43
|
+
}
|
|
44
|
+
export interface StrictUseAxiosReturn<T, R, D, O extends UseAxiosOptions = UseAxiosOptions<T>> extends UseAxiosReturn<T, R, D, O> {
|
|
45
|
+
/**
|
|
46
|
+
* Manually call the axios request — returns the shared thenable shell
|
|
47
|
+
* (upstream `return promise`): `await execute()` resolves with the shell
|
|
48
|
+
* once the request finished, rejecting with the request error on failure;
|
|
49
|
+
* a bare unawaited call never settles eagerly, so it cannot produce an
|
|
50
|
+
* unhandled rejection.
|
|
51
|
+
*/
|
|
52
|
+
execute: (url?: string | AxiosRequestConfig<D>, config?: AxiosRequestConfig<D>) => Promise<StrictUseAxiosReturn<T, R, D, O>>;
|
|
53
|
+
}
|
|
54
|
+
export interface EasyUseAxiosReturn<T, R, D> extends UseAxiosReturn<T, R, D> {
|
|
55
|
+
/**
|
|
56
|
+
* Manually call the axios request — returns the shared thenable shell
|
|
57
|
+
* (upstream `return promise`): `await execute(url)` resolves with the shell
|
|
58
|
+
* once the request finished, rejecting with the request error on failure;
|
|
59
|
+
* a bare unawaited call never settles eagerly, so it cannot produce an
|
|
60
|
+
* unhandled rejection.
|
|
61
|
+
*/
|
|
62
|
+
execute: (url: string, config?: AxiosRequestConfig<D>) => Promise<EasyUseAxiosReturn<T, R, D>>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The thenable half of the returned shell — upstream's
|
|
66
|
+
* `promise = { then, catch }` object. `then`/`catch` settle once the latest
|
|
67
|
+
* request finished, resolving with the shell itself (so
|
|
68
|
+
* `const { data } = await useAxios(...)` works) or rejecting with the request
|
|
69
|
+
* error.
|
|
70
|
+
*/
|
|
71
|
+
export interface UseAxiosThenable<X> extends PromiseLike<X> {
|
|
72
|
+
catch: <TResult = never>(onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null) => PromiseLike<X | TResult>;
|
|
73
|
+
}
|
|
74
|
+
export interface UseAxiosOptionsBase<T = any> {
|
|
75
|
+
/**
|
|
76
|
+
* Will automatically run axios request when `useAxios` is used
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
immediate?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Use shallowRef.
|
|
82
|
+
*
|
|
83
|
+
* @default true
|
|
84
|
+
*/
|
|
85
|
+
shallow?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Abort previous request when a new request is made.
|
|
88
|
+
*
|
|
89
|
+
* @default true
|
|
90
|
+
*/
|
|
91
|
+
abortPrevious?: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Callback when error is caught.
|
|
94
|
+
*/
|
|
95
|
+
onError?: (e: unknown) => void;
|
|
96
|
+
/**
|
|
97
|
+
* Callback when success is caught.
|
|
98
|
+
*/
|
|
99
|
+
onSuccess?: (data: T) => void;
|
|
100
|
+
/**
|
|
101
|
+
* Sets the state to initialState before executing the promise.
|
|
102
|
+
*/
|
|
103
|
+
resetOnExecute?: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Callback when request is finished.
|
|
106
|
+
*/
|
|
107
|
+
onFinish?: () => void;
|
|
108
|
+
}
|
|
109
|
+
export interface UseAxiosOptionsWithInitialData<T> extends UseAxiosOptionsBase<T> {
|
|
110
|
+
/**
|
|
111
|
+
* Initial data
|
|
112
|
+
*/
|
|
113
|
+
initialData: T;
|
|
114
|
+
}
|
|
115
|
+
export type UseAxiosOptions<T = any> = UseAxiosOptionsBase<T> | UseAxiosOptionsWithInitialData<T>;
|
|
116
|
+
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any, O extends UseAxiosOptionsWithInitialData<T> = UseAxiosOptionsWithInitialData<T>>(url: string, config?: AxiosRequestConfig<D>, options?: O): StrictUseAxiosReturn<T, R, D, O> & UseAxiosThenable<StrictUseAxiosReturn<T, R, D, O>>;
|
|
117
|
+
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any, O extends UseAxiosOptionsWithInitialData<T> = UseAxiosOptionsWithInitialData<T>>(url: string, instance?: AxiosInstance, options?: O): StrictUseAxiosReturn<T, R, D, O> & UseAxiosThenable<StrictUseAxiosReturn<T, R, D, O>>;
|
|
118
|
+
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any, O extends UseAxiosOptionsWithInitialData<T> = UseAxiosOptionsWithInitialData<T>>(url: string, config: AxiosRequestConfig<D>, instance: AxiosInstance, options?: O): StrictUseAxiosReturn<T, R, D, O> & UseAxiosThenable<StrictUseAxiosReturn<T, R, D, O>>;
|
|
119
|
+
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any, O extends UseAxiosOptionsBase<T> = UseAxiosOptionsBase<T>>(url: string, config?: AxiosRequestConfig<D>, options?: O): StrictUseAxiosReturn<T, R, D, O> & UseAxiosThenable<StrictUseAxiosReturn<T, R, D, O>>;
|
|
120
|
+
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any, O extends UseAxiosOptionsBase<T> = UseAxiosOptionsBase<T>>(url: string, instance?: AxiosInstance, options?: O): StrictUseAxiosReturn<T, R, D, O> & UseAxiosThenable<StrictUseAxiosReturn<T, R, D, O>>;
|
|
121
|
+
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any, O extends UseAxiosOptionsBase<T> = UseAxiosOptionsBase<T>>(url: string, config: AxiosRequestConfig<D>, instance: AxiosInstance, options?: O): StrictUseAxiosReturn<T, R, D, O> & UseAxiosThenable<StrictUseAxiosReturn<T, R, D, O>>;
|
|
122
|
+
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(config?: AxiosRequestConfig<D>): EasyUseAxiosReturn<T, R, D> & UseAxiosThenable<EasyUseAxiosReturn<T, R, D>>;
|
|
123
|
+
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(instance?: AxiosInstance): EasyUseAxiosReturn<T, R, D> & UseAxiosThenable<EasyUseAxiosReturn<T, R, D>>;
|
|
124
|
+
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(config?: AxiosRequestConfig<D>, instance?: AxiosInstance): EasyUseAxiosReturn<T, R, D> & UseAxiosThenable<EasyUseAxiosReturn<T, R, D>>;
|
|
125
|
+
//#endregion
|