@ahooksjs/use-request
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("React")):"function"==typeof define&&define.amd?define(["React"],e):"object"==typeof exports?exports.ahooksUseRequest=e(require("React")):t.ahooksUseRequest=e(t.React)}(this,(function(t){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=4)}([function(e,n){e.exports=t},function(t,e){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(t){"object"==typeof window&&(n=window)}t.exports=n},function(t,e,n){(function(e){var n=/^\s+|\s+$/g,r=/^[-+]0x[0-9a-f]+$/i,i=/^0b[01]+$/i,o=/^0o[0-7]+$/i,u=parseInt,a="object"==typeof e&&e&&e.Object===Object&&e,c="object"==typeof self&&self&&self.Object===Object&&self,s=a||c||Function("return this")(),f=Object.prototype.toString,l=Math.max,d=Math.min,h=function(){return s.Date.now()};function v(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function p(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==f.call(t)}(t))return NaN;if(v(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=v(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(n,"");var a=i.test(t);return a||o.test(t)?u(t.slice(2),a?2:8):r.test(t)?NaN:+t}t.exports=function(t,e,n){var r,i,o,u,a,c,s=0,f=!1,g=!1,b=!0;if("function"!=typeof t)throw new TypeError("Expected a function");function y(e){var n=r,o=i;return r=i=void 0,s=e,u=t.apply(o,n)}function m(t){return s=t,a=setTimeout(j,e),f?y(t):u}function O(t){var n=t-c;return void 0===c||n>=e||n<0||g&&t-s>=o}function j(){var t=h();if(O(t))return w(t);a=setTimeout(j,function(t){var n=e-(t-c);return g?d(n,o-(t-s)):n}(t))}function w(t){return a=void 0,b&&r?y(t):(r=i=void 0,u)}function T(){var t=h(),n=O(t);if(r=arguments,i=this,c=t,n){if(void 0===a)return m(c);if(g)return a=setTimeout(j,e),y(c)}return void 0===a&&(a=setTimeout(j,e)),u}return e=p(e)||0,v(n)&&(f=!!n.leading,o=(g="maxWait"in n)?l(p(n.maxWait)||0,e):o,b="trailing"in n?!!n.trailing:b),T.cancel=function(){void 0!==a&&clearTimeout(a),s=0,r=c=i=a=void 0},T.flush=function(){return void 0===a?u:w(h())},T}}).call(this,n(1))},function(t,e,n){(function(e){var n=/^\s+|\s+$/g,r=/^[-+]0x[0-9a-f]+$/i,i=/^0b[01]+$/i,o=/^0o[0-7]+$/i,u=parseInt,a="object"==typeof e&&e&&e.Object===Object&&e,c="object"==typeof self&&self&&self.Object===Object&&self,s=a||c||Function("return this")(),f=Object.prototype.toString,l=Math.max,d=Math.min,h=function(){return s.Date.now()};function v(t,e,n){var r,i,o,u,a,c,s=0,f=!1,v=!1,b=!0;if("function"!=typeof t)throw new TypeError("Expected a function");function y(e){var n=r,o=i;return r=i=void 0,s=e,u=t.apply(o,n)}function m(t){return s=t,a=setTimeout(j,e),f?y(t):u}function O(t){var n=t-c;return void 0===c||n>=e||n<0||v&&t-s>=o}function j(){var t=h();if(O(t))return w(t);a=setTimeout(j,function(t){var n=e-(t-c);return v?d(n,o-(t-s)):n}(t))}function w(t){return a=void 0,b&&r?y(t):(r=i=void 0,u)}function T(){var t=h(),n=O(t);if(r=arguments,i=this,c=t,n){if(void 0===a)return m(c);if(v)return a=setTimeout(j,e),y(c)}return void 0===a&&(a=setTimeout(j,e)),u}return e=g(e)||0,p(n)&&(f=!!n.leading,o=(v="maxWait"in n)?l(g(n.maxWait)||0,e):o,b="trailing"in n?!!n.trailing:b),T.cancel=function(){void 0!==a&&clearTimeout(a),s=0,r=c=i=a=void 0},T.flush=function(){return void 0===a?u:w(h())},T}function p(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function g(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==f.call(t)}(t))return NaN;if(p(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=p(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(n,"");var a=i.test(t);return a||o.test(t)?u(t.slice(2),a?2:8):r.test(t)?NaN:+t}t.exports=function(t,e,n){var r=!0,i=!0;if("function"!=typeof t)throw new TypeError("Expected a function");return p(n)&&(r="leading"in n?!!n.leading:r,i="trailing"in n?!!n.trailing:i),v(t,e,{leading:r,maxWait:e,trailing:i})}}).call(this,n(1))},function(t,e,n){"use strict";n.r(e),n.d(e,"useAsync",(function(){return T})),n.d(e,"usePaginated",(function(){return S})),n.d(e,"useLoadMore",(function(){return E})),n.d(e,"UseRequestProvider",(function(){return D}));function r(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(n[r[i]]=t[r[i]])}return n}var i=n(0),o=n.n(i),u=n(2),a=n.n(u),c=n(3),s=n.n(c);function f(){return"undefined"==typeof document||void 0===document.visibilityState||"hidden"!==document.visibilityState}var l={};var d=function(t,e){void 0===e&&(e=[]);var n=Object(i.useRef)((function(){throw new Error("Cannot call an event handler while rendering.")}));Object(i.useEffect)((function(){n.current=t}),[t].concat(e));var r=Object(i.useCallback)((function(){var t=n.current;if(t)return t.apply(void 0,arguments)}),[n]);if("function"==typeof t)return r},h=function(t,e){var n=Object(i.useRef)(!1);Object(i.useEffect)((function(){if(n.current)return t();n.current=!0}),e)},v=[];var p=!1;if("undefined"!=typeof window&&window.addEventListener&&!p){var g=function(){if(f()&&(void 0===navigator.onLine||navigator.onLine))for(var t=0;t<v.length;t++){(0,v[t])()}};window.addEventListener("visibilitychange",g,!1),window.addEventListener("focus",g,!1),p=!0}var b=function(t){return v.push(t),function(){var e=v.indexOf(t);v.splice(e,1)}},y=[];var m=!1;if("undefined"!=typeof window&&window.addEventListener&&!m){window.addEventListener("visibilitychange",(function(){if(f())for(var t=0;t<y.length;t++){(0,y[t])()}}),!1),m=!0}var O=function(t){return y.push(t),function(){var e=y.indexOf(t);y.splice(e,1)}},j="AHOOKS_USE_REQUEST_DEFAULT_KEY",w=function(){function t(t,e,n,r){var i,o,u;this.count=0,this.unmountedFlag=!1,this.pollingWhenVisibleFlag=!1,this.pollingTimer=void 0,this.loadingDelayTimer=void 0,this.unsubscribe=[],this.that=this,this.state={loading:!1,params:[],data:void 0,error:void 0,run:this.run.bind(this.that),mutate:this.mutate.bind(this.that),refresh:this.refresh.bind(this.that),cancel:this.cancel.bind(this.that),unmount:this.unmount.bind(this.that)},this.service=t,this.config=e,this.subscribe=n,r&&(this.state=Object.assign(Object.assign({},this.state),r)),this.debounceRun=this.config.debounceInterval?a()(this._run,this.config.debounceInterval):void 0,this.throttleRun=this.config.throttleInterval?s()(this._run,this.config.throttleInterval):void 0,this.limitRefresh=(i=this.refresh.bind(this),o=this.config.focusTimespan,u=!1,function(){u||(u=!0,i.apply(void 0,arguments),setTimeout((function(){u=!1}),o))}),this.config.pollingInterval&&this.unsubscribe.push(O(this.rePolling.bind(this))),this.config.refreshOnWindowFocus&&this.unsubscribe.push(b(this.limitRefresh.bind(this)))}var e=t.prototype;return e.setState=function(t){void 0===t&&(t={}),this.state=Object.assign(Object.assign({},this.state),t),this.subscribe(this.state)},e._run=function(){for(var t=this,e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];this.pollingTimer&&clearTimeout(this.pollingTimer),this.loadingDelayTimer&&clearTimeout(this.loadingDelayTimer),this.count+=1;var i=this.count;return this.setState({loading:!this.config.loadingDelay,params:n}),this.config.loadingDelay&&(this.loadingDelayTimer=setTimeout((function(){t.setState({loading:!0})}),this.config.loadingDelay)),this.service.apply(this,n).then((function(e){if(!t.unmountedFlag&&i===t.count){t.loadingDelayTimer&&clearTimeout(t.loadingDelayTimer);var r=t.config.formatResult?t.config.formatResult(e):e;return t.setState({data:r,error:void 0,loading:!1}),t.config.onSuccess&&t.config.onSuccess(r,n),r}})).catch((function(e){if(!t.unmountedFlag&&i===t.count){if(t.loadingDelayTimer&&clearTimeout(t.loadingDelayTimer),t.setState({data:void 0,error:e,loading:!1}),t.config.onError&&t.config.onError(e,n),t.config.throwOnError)throw e;console.error(e)}})).finally((function(){if(!t.unmountedFlag&&i===t.count&&t.config.pollingInterval){if(!f()&&!t.config.pollingWhenHidden)return void(t.pollingWhenVisibleFlag=!0);t.pollingTimer=setTimeout((function(){t._run.apply(t,n)}),t.config.pollingInterval)}}))},e.run=function(){return this.debounceRun?(this.debounceRun.apply(this,arguments),Promise.resolve(null)):this.throttleRun?(this.throttleRun.apply(this,arguments),Promise.resolve(null)):this._run.apply(this,arguments)},e.cancel=function(){this.debounceRun&&this.debounceRun.cancel(),this.throttleRun&&this.throttleRun.cancel(),this.loadingDelayTimer&&clearTimeout(this.loadingDelayTimer),this.pollingTimer&&clearTimeout(this.pollingTimer),this.pollingWhenVisibleFlag=!1,this.count+=1,this.setState({loading:!1})},e.refresh=function(){return this.run.apply(this,this.state.params)},e.rePolling=function(){this.pollingWhenVisibleFlag&&(this.pollingWhenVisibleFlag=!1,this.refresh())},e.mutate=function(t){"function"==typeof t?this.setState({data:t(this.state.data)||{}}):this.setState({data:t})},e.unmount=function(){this.unmountedFlag=!0,this.cancel(),this.unsubscribe.forEach((function(t){t()}))},t}();var T=function(t,e){var n,r=e||{},o=r.refreshDeps,u=void 0===o?[]:o,a=r.manual,c=void 0!==a&&a,s=r.onSuccess,f=void 0===s?function(){}:s,v=r.onError,p=void 0===v?function(){}:v,g=r.defaultLoading,b=void 0!==g&&g,y=r.loadingDelay,m=r.pollingInterval,O=void 0===m?0:m,T=r.pollingWhenHidden,E=void 0===T||T,S=r.defaultParams,R=void 0===S?[]:S,x=r.refreshOnWindowFocus,D=void 0!==x&&x,P=r.focusTimespan,M=void 0===P?5e3:P,C=r.fetchKey,F=r.cacheKey,k=r.debounceInterval,I=r.throttleInterval,W=r.initialData,_=r.ready,K=void 0===_||_,L=r.throwOnError,N=void 0!==L&&L,z=Object(i.useRef)(j),q=d(t),$=d(f),U=d(p),A=d(C);"formatResult"in r&&(n=r.formatResult);var H={formatResult:d(n),onSuccess:$,onError:U,loadingDelay:y,pollingInterval:O,pollingWhenHidden:E,refreshOnWindowFocus:D,focusTimespan:M,debounceInterval:k,throttleInterval:I,throwOnError:N},V=d((function(t,e){B((function(n){return n[t]=e,Object.assign({},n)}))}),[]),Y=Object(i.useState)((function(){if(F){var t=null===(n=l[F])||void 0===n?void 0:n.data;if(t){z.current=t.newstFetchKey;var e={};return Object.keys(t.fetches).forEach((function(n){var r=t.fetches[n],i=new w(q,H,V.bind(null,n),{loading:r.loading,params:r.params,data:r.data,error:r.error});e[n]=i.state})),e}}var n;return[]})),Q=Y[0],B=Y[1],G=Object(i.useRef)(Q);G.current=Q;var J=Object(i.useRef)(),X=Object(i.useCallback)((function(){for(var t,e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];if(K){if(A){var i=A.apply(void 0,n);z.current=void 0===i?j:i}var o=z.current,u=G.current[o];if(!u){var a=new w(q,H,V.bind(null,o),{data:W});u=a.state,B((function(t){return t[o]=u,Object.assign({},t)}))}return(t=u).run.apply(t,n)}J.current=n}),[C,V,K]),Z=Object(i.useRef)(X);Z.current=X,Object(i.useEffect)((function(){F&&function(t,e){l[t]&&clearTimeout(l[t].timer);var n=setTimeout((function(){delete l[t]}),3e5);l[t]={data:e,timer:n}}(F,{fetches:Q,newstFetchKey:z.current})}),[F,Q]);var tt=Object(i.useRef)(!1);h((function(){K&&(!tt.current&&J.current&&Z.current.apply(Z,J.current),tt.current=!0)}),[K]),Object(i.useEffect)((function(){c||(Object.keys(Q).length>0?Object.values(Q).forEach((function(t){t.refresh()})):Z.current.apply(Z,R))}),[]);var et=Object(i.useCallback)((function(){Object.values(G.current).forEach((function(t){t.unmount()})),z.current=j,B({}),G.current={}}),[B]);h((function(){c||Object.values(G.current).forEach((function(t){t.refresh()}))}),[].concat(u)),Object(i.useEffect)((function(){return function(){Object.values(G.current).forEach((function(t){t.unmount()}))}}),[]);var nt=Object(i.useCallback)((function(t){return function(){throw new Error("Cannot call "+t+" when service not executed once.")}}),[]);return Object.assign(Object.assign({loading:K&&!c||b,data:W,error:void 0,params:[],cancel:nt("cancel"),refresh:nt("refresh"),mutate:nt("mutate")},Q[z.current]||{}),{run:X,fetches:Q,reset:et})};var E=function(t,e){var n=e.refreshDeps,o=void 0===n?[]:n,u=e.ref,a=e.isNoMore,c=e.threshold,s=void 0===c?100:c,f=(e.fetchKey,r(e,["refreshDeps","ref","isNoMore","threshold","fetchKey"])),l=Object(i.useState)(!1),d=l[0],v=l[1];Object(i.useEffect)((function(){e.fetchKey&&console.warn("useRequest loadMore mode don't need fetchKey!")}),[]);var p=T(t,Object.assign(Object.assign({},f),{fetchKey:function(t){var e;return(null===(e=null==t?void 0:t.list)||void 0===e?void 0:e.length)||0},onSuccess:function(){v(!1),e.onSuccess&&e.onSuccess.apply(e,arguments)}})),g=p.data,b=p.run,y=p.params,m=p.reset,O=p.loading,j=p.fetches,w=Object(i.useCallback)((function(){m();var t=y.slice(1);b.apply(void 0,[void 0].concat(t))}),[b,m,y]),E=Object(i.useRef)(w);E.current=w,h((function(){e.manual||E.current()}),[].concat(o));var S=Object(i.useMemo)((function(){var t=[],e=g;return Object.values(j).forEach((function(n){var r,i;(null===(r=n.data)||void 0===r?void 0:r.list)&&(t=t.concat(null===(i=n.data)||void 0===i?void 0:i.list)),n.loading||(e=n.data)})),Object.assign(Object.assign({},e),{list:t})}),[j,g]),R=!!a&&(!O&&!d&&a(S)),x=Object(i.useCallback)((function(){if(!R){v(!0);var t=y.slice(1);b.apply(void 0,[S].concat(t))}}),[R,b,S,y]),D=Object(i.useCallback)((function(){!O&&!d&&u&&u.current&&u.current.scrollHeight-u.current.scrollTop<=u.current.clientHeight+s&&x()}),[O,u,x]);return Object(i.useEffect)((function(){return u&&u.current?(u.current.addEventListener("scroll",D),function(){u&&u.current&&u.current.removeEventListener("scroll",D)}):function(){}}),[D]),Object.assign(Object.assign({},p),{data:S,reload:w,loading:O&&0===S.list.length,loadMore:x,loadingMore:d,noMore:R})};var S=function(t,e){e.paginated;var n=e.defaultPageSize,o=void 0===n?10:n,u=e.refreshDeps,a=void 0===u?[]:u,c=e.fetchKey,s=r(e,["paginated","defaultPageSize","refreshDeps","fetchKey"]);Object(i.useEffect)((function(){c&&console.error("useRequest pagination's fetchKey will not work!")}),[]);var f=T(t,Object.assign({defaultParams:[{current:1,pageSize:o}]},s)),l=f.data,d=f.params,v=f.run,p=f.loading,g=r(f,["data","params","run","loading"]),b=d&&d[0]?d[0]:{},y=b.current,m=void 0===y?1:y,O=b.pageSize,j=void 0===O?o:O,w=b.sorter,E=void 0===w?{}:w,S=b.filters,R=void 0===S?{}:S,x=Object(i.useCallback)((function(t){var e=d[0],n=d.slice(1);v.apply(void 0,[Object.assign(Object.assign({},e),t)].concat(n))}),[v,d]),D=(null==l?void 0:l.total)||0,P=Object(i.useMemo)((function(){return Math.ceil(D/j)}),[j,D]),M=Object(i.useCallback)((function(t,e){var n=t<=0?1:t,r=e<=0?1:e,i=Math.ceil(D/r);n>i&&(n=i),x({current:t,pageSize:e})}),[D,x]),C=Object(i.useCallback)((function(t){M(t,j)}),[M,j]),F=Object(i.useCallback)((function(t){M(m,t)}),[M,m]),k=Object(i.useRef)(C);k.current=C,h((function(){e.manual||k.current(1)}),[].concat(a));var I=Object(i.useCallback)((function(t,e,n){x({current:t.current,pageSize:t.pageSize||o,filters:e,sorter:n})}),[R,E,x]);return Object.assign({loading:p,data:l,params:d,run:v,pagination:{current:m,pageSize:j,total:D,totalPage:P,onChange:M,changeCurrent:C,changePageSize:F},tableProps:{dataSource:(null==l?void 0:l.list)||[],loading:p,onChange:I,pagination:{current:m,pageSize:j,total:D}},sorter:E,filters:R},g)},R=o.a.createContext({});R.displayName="UseRequestConfigContext";var x=R;var D=x.Provider;e.default=function(t,e){void 0===e&&(e={});var n=Object(i.useContext)(x),o=Object.assign(Object.assign({},n),e),u=o.paginated,a=o.loadMore,c=o.requestMethod,s=Object(i.useRef)(u),f=Object(i.useRef)(a);if(s.current!==u)throw Error("You should not modify the paginated of options");if(f.current!==a)throw Error("You should not modify the loadMore of options");s.current=u,f.current=a;var l,d=function(){return fetch.apply(void 0,arguments).then((function(t){if(t.ok)return t.json();throw new Error(t.statusText)}))},h=c||d;if("string"==typeof t)l=function(){return h(t)};else if("object"==typeof t){var v=t.url,p=r(t,["url"]);l=function(){return c?c(t):d(v,p)}}else l=function(){for(var e=arguments.length,n=new Array(e),i=0;i<e;i++)n[i]=arguments[i];return new Promise((function(e,i){var o=t.apply(void 0,n);if(o.then)o.then((function(t){return e(t)})).catch((function(t){return i(t)}));else if("string"==typeof o)h(o).then((function(t){e(t)})).catch((function(t){return i(t)}));else if("object"==typeof o)if(c)c(o).then((function(t){e(t)})).catch((function(t){return i(t)}));else{var u=o.url,a=r(o,["url"]);d(u,a).then((function(t){e(t)})).catch((function(t){return i(t)}))}}))};return a?E(l,o):u?S(l,o):T(l,o)}}])})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("React")):"function"==typeof define&&define.amd?define(["React"],e):"object"==typeof exports?exports.ahooksUseRequest=e(require("React")):t.ahooksUseRequest=e(t.React)}(this,(function(t){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=4)}([function(e,n){e.exports=t},function(t,e){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(t){"object"==typeof window&&(n=window)}t.exports=n},function(t,e,n){(function(e){var n=/^\s+|\s+$/g,r=/^[-+]0x[0-9a-f]+$/i,i=/^0b[01]+$/i,o=/^0o[0-7]+$/i,u=parseInt,a="object"==typeof e&&e&&e.Object===Object&&e,c="object"==typeof self&&self&&self.Object===Object&&self,s=a||c||Function("return this")(),f=Object.prototype.toString,l=Math.max,d=Math.min,h=function(){return s.Date.now()};function v(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function p(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==f.call(t)}(t))return NaN;if(v(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=v(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(n,"");var a=i.test(t);return a||o.test(t)?u(t.slice(2),a?2:8):r.test(t)?NaN:+t}t.exports=function(t,e,n){var r,i,o,u,a,c,s=0,f=!1,g=!1,b=!0;if("function"!=typeof t)throw new TypeError("Expected a function");function y(e){var n=r,o=i;return r=i=void 0,s=e,u=t.apply(o,n)}function m(t){return s=t,a=setTimeout(j,e),f?y(t):u}function O(t){var n=t-c;return void 0===c||n>=e||n<0||g&&t-s>=o}function j(){var t=h();if(O(t))return w(t);a=setTimeout(j,function(t){var n=e-(t-c);return g?d(n,o-(t-s)):n}(t))}function w(t){return a=void 0,b&&r?y(t):(r=i=void 0,u)}function T(){var t=h(),n=O(t);if(r=arguments,i=this,c=t,n){if(void 0===a)return m(c);if(g)return a=setTimeout(j,e),y(c)}return void 0===a&&(a=setTimeout(j,e)),u}return e=p(e)||0,v(n)&&(f=!!n.leading,o=(g="maxWait"in n)?l(p(n.maxWait)||0,e):o,b="trailing"in n?!!n.trailing:b),T.cancel=function(){void 0!==a&&clearTimeout(a),s=0,r=c=i=a=void 0},T.flush=function(){return void 0===a?u:w(h())},T}}).call(this,n(1))},function(t,e,n){(function(e){var n=/^\s+|\s+$/g,r=/^[-+]0x[0-9a-f]+$/i,i=/^0b[01]+$/i,o=/^0o[0-7]+$/i,u=parseInt,a="object"==typeof e&&e&&e.Object===Object&&e,c="object"==typeof self&&self&&self.Object===Object&&self,s=a||c||Function("return this")(),f=Object.prototype.toString,l=Math.max,d=Math.min,h=function(){return s.Date.now()};function v(t,e,n){var r,i,o,u,a,c,s=0,f=!1,v=!1,b=!0;if("function"!=typeof t)throw new TypeError("Expected a function");function y(e){var n=r,o=i;return r=i=void 0,s=e,u=t.apply(o,n)}function m(t){return s=t,a=setTimeout(j,e),f?y(t):u}function O(t){var n=t-c;return void 0===c||n>=e||n<0||v&&t-s>=o}function j(){var t=h();if(O(t))return w(t);a=setTimeout(j,function(t){var n=e-(t-c);return v?d(n,o-(t-s)):n}(t))}function w(t){return a=void 0,b&&r?y(t):(r=i=void 0,u)}function T(){var t=h(),n=O(t);if(r=arguments,i=this,c=t,n){if(void 0===a)return m(c);if(v)return a=setTimeout(j,e),y(c)}return void 0===a&&(a=setTimeout(j,e)),u}return e=g(e)||0,p(n)&&(f=!!n.leading,o=(v="maxWait"in n)?l(g(n.maxWait)||0,e):o,b="trailing"in n?!!n.trailing:b),T.cancel=function(){void 0!==a&&clearTimeout(a),s=0,r=c=i=a=void 0},T.flush=function(){return void 0===a?u:w(h())},T}function p(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function g(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==f.call(t)}(t))return NaN;if(p(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=p(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(n,"");var a=i.test(t);return a||o.test(t)?u(t.slice(2),a?2:8):r.test(t)?NaN:+t}t.exports=function(t,e,n){var r=!0,i=!0;if("function"!=typeof t)throw new TypeError("Expected a function");return p(n)&&(r="leading"in n?!!n.leading:r,i="trailing"in n?!!n.trailing:i),v(t,e,{leading:r,maxWait:e,trailing:i})}}).call(this,n(1))},function(t,e,n){"use strict";n.r(e),n.d(e,"useAsync",(function(){return T})),n.d(e,"usePaginated",(function(){return S})),n.d(e,"useLoadMore",(function(){return E})),n.d(e,"UseRequestProvider",(function(){return P})),n.d(e,"UseAPIProvider",(function(){return D}));function r(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(n[r[i]]=t[r[i]])}return n}var i=n(0),o=n.n(i),u=n(2),a=n.n(u),c=n(3),s=n.n(c);function f(){return"undefined"==typeof document||void 0===document.visibilityState||"hidden"!==document.visibilityState}var l={};var d=function(t){var e=Object(i.useRef)((function(){throw new Error("Cannot call an event handler while rendering.")}));e.current=t;var n=Object(i.useCallback)((function(){var t=e.current;if(t)return t.apply(void 0,arguments)}),[e]);if("function"==typeof t)return n},h=function(t,e){var n=Object(i.useRef)(!1);Object(i.useEffect)((function(){if(n.current)return t();n.current=!0}),e)},v=[];var p=!1;if("undefined"!=typeof window&&window.addEventListener&&!p){var g=function(){if(f()&&(void 0===navigator.onLine||navigator.onLine))for(var t=0;t<v.length;t++){(0,v[t])()}};window.addEventListener("visibilitychange",g,!1),window.addEventListener("focus",g,!1),p=!0}var b=function(t){return v.push(t),function(){var e=v.indexOf(t);v.splice(e,1)}},y=[];var m=!1;if("undefined"!=typeof window&&window.addEventListener&&!m){window.addEventListener("visibilitychange",(function(){if(f())for(var t=0;t<y.length;t++){(0,y[t])()}}),!1),m=!0}var O=function(t){return y.push(t),function(){var e=y.indexOf(t);y.splice(e,1)}},j="AHOOKS_USE_REQUEST_DEFAULT_KEY",w=function(){function t(t,e,n,r){var i,o,u;this.count=0,this.unmountedFlag=!1,this.pollingWhenVisibleFlag=!1,this.pollingTimer=void 0,this.loadingDelayTimer=void 0,this.unsubscribe=[],this.that=this,this.state={loading:!1,params:[],data:void 0,error:void 0,run:this.run.bind(this.that),mutate:this.mutate.bind(this.that),refresh:this.refresh.bind(this.that),cancel:this.cancel.bind(this.that),unmount:this.unmount.bind(this.that)},this.service=t,this.config=e,this.subscribe=n,r&&(this.state=Object.assign(Object.assign({},this.state),r)),this.debounceRun=this.config.debounceInterval?a()(this._run,this.config.debounceInterval):void 0,this.throttleRun=this.config.throttleInterval?s()(this._run,this.config.throttleInterval):void 0,this.limitRefresh=(i=this.refresh.bind(this),o=this.config.focusTimespan,u=!1,function(){u||(u=!0,i.apply(void 0,arguments),setTimeout((function(){u=!1}),o))}),this.config.pollingInterval&&this.unsubscribe.push(O(this.rePolling.bind(this))),this.config.refreshOnWindowFocus&&this.unsubscribe.push(b(this.limitRefresh.bind(this)))}var e=t.prototype;return e.setState=function(t){void 0===t&&(t={}),this.state=Object.assign(Object.assign({},this.state),t),this.subscribe(this.state)},e._run=function(){for(var t=this,e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];this.pollingTimer&&clearTimeout(this.pollingTimer),this.loadingDelayTimer&&clearTimeout(this.loadingDelayTimer),this.count+=1;var i=this.count;return this.setState({loading:!this.config.loadingDelay,params:n}),this.config.loadingDelay&&(this.loadingDelayTimer=setTimeout((function(){t.setState({loading:!0})}),this.config.loadingDelay)),this.service.apply(this,n).then((function(e){if(!t.unmountedFlag&&i===t.count){t.loadingDelayTimer&&clearTimeout(t.loadingDelayTimer);var r=t.config.formatResult?t.config.formatResult(e):e;return t.setState({data:r,error:void 0,loading:!1}),t.config.onSuccess&&t.config.onSuccess(r,n),r}})).catch((function(e){if(!t.unmountedFlag&&i===t.count){if(t.loadingDelayTimer&&clearTimeout(t.loadingDelayTimer),t.setState({data:void 0,error:e,loading:!1}),t.config.onError&&t.config.onError(e,n),t.config.throwOnError)throw e;console.error(e)}})).finally((function(){if(!t.unmountedFlag&&i===t.count&&t.config.pollingInterval){if(!f()&&!t.config.pollingWhenHidden)return void(t.pollingWhenVisibleFlag=!0);t.pollingTimer=setTimeout((function(){t._run.apply(t,n)}),t.config.pollingInterval)}}))},e.run=function(){return this.debounceRun?(this.debounceRun.apply(this,arguments),Promise.resolve(null)):this.throttleRun?(this.throttleRun.apply(this,arguments),Promise.resolve(null)):this._run.apply(this,arguments)},e.cancel=function(){this.debounceRun&&this.debounceRun.cancel(),this.throttleRun&&this.throttleRun.cancel(),this.loadingDelayTimer&&clearTimeout(this.loadingDelayTimer),this.pollingTimer&&clearTimeout(this.pollingTimer),this.pollingWhenVisibleFlag=!1,this.count+=1,this.setState({loading:!1})},e.refresh=function(){return this.run.apply(this,this.state.params)},e.rePolling=function(){this.pollingWhenVisibleFlag&&(this.pollingWhenVisibleFlag=!1,this.refresh())},e.mutate=function(t){"function"==typeof t?this.setState({data:t(this.state.data)||{}}):this.setState({data:t})},e.unmount=function(){this.unmountedFlag=!0,this.cancel(),this.unsubscribe.forEach((function(t){t()}))},t}();var T=function(t,e){var n,r=e||{},o=r.refreshDeps,u=void 0===o?[]:o,a=r.manual,c=void 0!==a&&a,s=r.onSuccess,f=void 0===s?function(){}:s,v=r.onError,p=void 0===v?function(){}:v,g=r.defaultLoading,b=void 0!==g&&g,y=r.loadingDelay,m=r.pollingInterval,O=void 0===m?0:m,T=r.pollingWhenHidden,E=void 0===T||T,S=r.defaultParams,R=void 0===S?[]:S,x=r.refreshOnWindowFocus,P=void 0!==x&&x,D=r.focusTimespan,M=void 0===D?5e3:D,C=r.fetchKey,F=r.cacheKey,k=r.debounceInterval,I=r.throttleInterval,W=r.initialData,_=r.ready,K=void 0===_||_,L=r.throwOnError,N=void 0!==L&&L,z=Object(i.useRef)(j),q=d(t),U=d(f),$=d(p),A=d(C);"formatResult"in r&&(n=r.formatResult);var H={formatResult:d(n),onSuccess:U,onError:$,loadingDelay:y,pollingInterval:O,pollingWhenHidden:E,refreshOnWindowFocus:P,focusTimespan:M,debounceInterval:k,throttleInterval:I,throwOnError:N},V=d((function(t,e){B((function(n){return n[t]=e,Object.assign({},n)}))})),Y=Object(i.useState)((function(){if(F){var t=null===(n=l[F])||void 0===n?void 0:n.data;if(t){z.current=t.newstFetchKey;var e={};return Object.keys(t.fetches).forEach((function(n){var r=t.fetches[n],i=new w(q,H,V.bind(null,n),{loading:r.loading,params:r.params,data:r.data,error:r.error});e[n]=i.state})),e}}var n;return[]})),Q=Y[0],B=Y[1],G=Object(i.useRef)(Q);G.current=Q;var J=Object(i.useRef)(),X=Object(i.useCallback)((function(){for(var t,e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];if(K){if(A){var i=A.apply(void 0,n);z.current=void 0===i?j:i}var o=z.current,u=G.current[o];if(!u){var a=new w(q,H,V.bind(null,o),{data:W});u=a.state,B((function(t){return t[o]=u,Object.assign({},t)}))}return(t=u).run.apply(t,n)}J.current=n}),[C,V,K]),Z=Object(i.useRef)(X);Z.current=X,Object(i.useEffect)((function(){F&&function(t,e){l[t]&&clearTimeout(l[t].timer);var n=setTimeout((function(){delete l[t]}),3e5);l[t]={data:e,timer:n}}(F,{fetches:Q,newstFetchKey:z.current})}),[F,Q]);var tt=Object(i.useRef)(!1);h((function(){K&&(!tt.current&&J.current&&Z.current.apply(Z,J.current),tt.current=!0)}),[K]),Object(i.useEffect)((function(){c||(Object.keys(Q).length>0?Object.values(Q).forEach((function(t){t.refresh()})):Z.current.apply(Z,R))}),[]);var et=Object(i.useCallback)((function(){Object.values(G.current).forEach((function(t){t.unmount()})),z.current=j,B({}),G.current={}}),[B]);h((function(){c||Object.values(G.current).forEach((function(t){t.refresh()}))}),[].concat(u)),Object(i.useEffect)((function(){return function(){Object.values(G.current).forEach((function(t){t.unmount()}))}}),[]);var nt=Object(i.useCallback)((function(t){return function(){throw new Error("Cannot call "+t+" when service not executed once.")}}),[]);return Object.assign(Object.assign({loading:K&&!c||b,data:W,error:void 0,params:[],cancel:nt("cancel"),refresh:nt("refresh"),mutate:nt("mutate")},Q[z.current]||{}),{run:X,fetches:Q,reset:et})};var E=function(t,e){var n=e.refreshDeps,o=void 0===n?[]:n,u=e.ref,a=e.isNoMore,c=e.threshold,s=void 0===c?100:c,f=(e.fetchKey,r(e,["refreshDeps","ref","isNoMore","threshold","fetchKey"])),l=Object(i.useState)(!1),d=l[0],v=l[1];Object(i.useEffect)((function(){e.fetchKey&&console.warn("useRequest loadMore mode don't need fetchKey!")}),[]);var p=T(t,Object.assign(Object.assign({},f),{fetchKey:function(t){var e;return(null===(e=null==t?void 0:t.list)||void 0===e?void 0:e.length)||0},onSuccess:function(){v(!1),e.onSuccess&&e.onSuccess.apply(e,arguments)}})),g=p.data,b=p.run,y=p.params,m=p.reset,O=p.loading,j=p.fetches,w=Object(i.useCallback)((function(){m();var t=y.slice(1);b.apply(void 0,[void 0].concat(t))}),[b,m,y]),E=Object(i.useRef)(w);E.current=w,h((function(){e.manual||E.current()}),[].concat(o));var S=Object(i.useMemo)((function(){var t=[],e=g;return Object.values(j).forEach((function(n){var r,i;(null===(r=n.data)||void 0===r?void 0:r.list)&&(t=t.concat(null===(i=n.data)||void 0===i?void 0:i.list)),n.loading||(e=n.data)})),Object.assign(Object.assign({},e),{list:t})}),[j,g]),R=!!a&&(!O&&!d&&a(S)),x=Object(i.useCallback)((function(){if(!R){v(!0);var t=y.slice(1);b.apply(void 0,[S].concat(t))}}),[R,b,S,y]),P=Object(i.useCallback)((function(){!O&&!d&&u&&u.current&&u.current.scrollHeight-u.current.scrollTop<=u.current.clientHeight+s&&x()}),[O,u,x]);return Object(i.useEffect)((function(){return u&&u.current?(u.current.addEventListener("scroll",P),function(){u&&u.current&&u.current.removeEventListener("scroll",P)}):function(){}}),[P]),Object.assign(Object.assign({},p),{data:S,reload:w,loading:O&&0===S.list.length,loadMore:x,loadingMore:d,noMore:R})};var S=function(t,e){e.paginated;var n=e.defaultPageSize,o=void 0===n?10:n,u=e.refreshDeps,a=void 0===u?[]:u,c=e.fetchKey,s=r(e,["paginated","defaultPageSize","refreshDeps","fetchKey"]);Object(i.useEffect)((function(){c&&console.error("useRequest pagination's fetchKey will not work!")}),[]);var f=T(t,Object.assign({defaultParams:[{current:1,pageSize:o}]},s)),l=f.data,d=f.params,v=f.run,p=f.loading,g=r(f,["data","params","run","loading"]),b=d&&d[0]?d[0]:{},y=b.current,m=void 0===y?1:y,O=b.pageSize,j=void 0===O?o:O,w=b.sorter,E=void 0===w?{}:w,S=b.filters,R=void 0===S?{}:S,x=Object(i.useCallback)((function(t){var e=d[0],n=d.slice(1);v.apply(void 0,[Object.assign(Object.assign({},e),t)].concat(n))}),[v,d]),P=(null==l?void 0:l.total)||0,D=Object(i.useMemo)((function(){return Math.ceil(P/j)}),[j,P]),M=Object(i.useCallback)((function(t,e){var n=t<=0?1:t,r=e<=0?1:e,i=Math.ceil(P/r);n>i&&(n=i),x({current:t,pageSize:e})}),[P,x]),C=Object(i.useCallback)((function(t){M(t,j)}),[M,j]),F=Object(i.useCallback)((function(t){M(m,t)}),[M,m]),k=Object(i.useRef)(C);k.current=C,h((function(){e.manual||k.current(1)}),[].concat(a));var I=Object(i.useCallback)((function(t,e,n){x({current:t.current,pageSize:t.pageSize||o,filters:e,sorter:n})}),[R,E,x]);return Object.assign({loading:p,data:l,params:d,run:v,pagination:{current:m,pageSize:j,total:P,totalPage:D,onChange:M,changeCurrent:C,changePageSize:F},tableProps:{dataSource:(null==l?void 0:l.list)||[],loading:p,onChange:I,pagination:{current:m,pageSize:j,total:P}},sorter:E,filters:R},g)},R=o.a.createContext({});R.displayName="UseRequestConfigContext";var x=R;var P=x.Provider,D=P;e.default=function(t,e){void 0===e&&(e={});var n=Object(i.useContext)(x),o=Object.assign(Object.assign({},n),e),u=o.paginated,a=o.loadMore,c=o.requestMethod,s=Object(i.useRef)(u),f=Object(i.useRef)(a);if(s.current!==u)throw Error("You should not modify the paginated of options");if(f.current!==a)throw Error("You should not modify the loadMore of options");s.current=u,f.current=a;var l,d=function(){return fetch.apply(void 0,arguments).then((function(t){if(t.ok)return t.json();throw new Error(t.statusText)}))},h=c||d;if("string"==typeof t)l=function(){return h(t)};else if("object"==typeof t){var v=t.url,p=r(t,["url"]);l=function(){return c?c(t):d(v,p)}}else l=function(){for(var e=arguments.length,n=new Array(e),i=0;i<e;i++)n[i]=arguments[i];return new Promise((function(e,i){var o=t.apply(void 0,n);if(o.then)o.then((function(t){return e(t)})).catch((function(t){return i(t)}));else if("string"==typeof o)h(o).then((function(t){e(t)})).catch((function(t){return i(t)}));else if("object"==typeof o)if(c)c(o).then((function(t){e(t)})).catch((function(t){return i(t)}));else{var u=o.url,a=r(o,["url"]);d(u,a).then((function(t){e(t)})).catch((function(t){return i(t)}))}}))};return a?E(l,o):u?S(l,o):T(l,o)}}])})); |
@@ -13,3 +13,4 @@ /// <reference types="react" /> | ||
declare const UseRequestProvider: import("react").Provider<BaseOptions<any, any> | OptionsWithFormat<any, any, any, any> | BasePaginatedOptions<any> | LoadMoreOptions<any>>; | ||
export { useAsync, usePaginated, useLoadMore, UseRequestProvider }; | ||
declare const UseAPIProvider: import("react").Provider<BaseOptions<any, any> | OptionsWithFormat<any, any, any, any> | BasePaginatedOptions<any> | LoadMoreOptions<any>>; | ||
export { useAsync, usePaginated, useLoadMore, UseRequestProvider, UseAPIProvider }; | ||
export default useRequest; |
import { __rest } from "tslib"; | ||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
import { useRef, useContext } from 'react'; | ||
@@ -33,9 +35,11 @@ import useAsync from './useAsync'; | ||
var fetchProxy = function fetchProxy() { | ||
return fetch.apply(void 0, arguments).then(function (res) { | ||
if (res.ok) { | ||
return res.json(); | ||
} | ||
return (// @ts-ignore | ||
fetch.apply(void 0, arguments).then(function (res) { | ||
if (res.ok) { | ||
return res.json(); | ||
} | ||
throw new Error(res.statusText); | ||
}); | ||
throw new Error(res.statusText); | ||
}) | ||
); | ||
}; | ||
@@ -112,4 +116,6 @@ | ||
var UseRequestProvider = ConfigContext.Provider; | ||
export { useAsync, usePaginated, useLoadMore, UseRequestProvider }; | ||
var UseRequestProvider = ConfigContext.Provider; // UseAPIProvider 已经废弃,此处为了兼容 umijs 插件 plugin-request | ||
var UseAPIProvider = UseRequestProvider; | ||
export { useAsync, usePaginated, useLoadMore, UseRequestProvider, UseAPIProvider }; | ||
export default useRequest; |
@@ -7,3 +7,3 @@ import { DependencyList, RefObject } from 'react'; | ||
export declare type Subscribe<R, P extends any[]> = (data: FetchResult<R, P>) => void; | ||
export declare type Mutate<R> = (x: (R | undefined | ((data: R) => R))) => void; | ||
export declare type Mutate<R> = (x: R | undefined | ((data: R) => R)) => void; | ||
export declare type RequestService = string | { | ||
@@ -10,0 +10,0 @@ [key: string]: any; |
@@ -302,3 +302,3 @@ import debounce from 'lodash.debounce'; | ||
}); | ||
}, []); | ||
}); | ||
@@ -305,0 +305,0 @@ var _useState = useState(function () { |
@@ -17,3 +17,3 @@ import { __rest } from "tslib"; | ||
if (fetchKey) { | ||
console.error('useRequest pagination\'s fetchKey will not work!'); | ||
console.error("useRequest pagination's fetchKey will not work!"); | ||
} | ||
@@ -20,0 +20,0 @@ }, []); |
@@ -1,2 +0,2 @@ | ||
declare function usePersistFn(fn: any, dependencies?: any): (...args: any[]) => any; | ||
declare function usePersistFn(fn: any): (...args: any[]) => any; | ||
export default usePersistFn; |
@@ -1,14 +0,8 @@ | ||
import { useCallback, useRef, useEffect } from 'react'; | ||
import { useCallback, useRef } from 'react'; | ||
function usePersistFn(fn, dependencies) { | ||
if (dependencies === void 0) { | ||
dependencies = []; | ||
} | ||
function usePersistFn(fn) { | ||
var ref = useRef(function () { | ||
throw new Error('Cannot call an event handler while rendering.'); | ||
}); | ||
useEffect(function () { | ||
ref.current = fn; | ||
}, [fn].concat(dependencies)); | ||
ref.current = fn; | ||
var persist = useCallback(function () { | ||
@@ -15,0 +9,0 @@ var refFn = ref.current; |
@@ -13,3 +13,4 @@ /// <reference types="react" /> | ||
declare const UseRequestProvider: import("react").Provider<BaseOptions<any, any> | OptionsWithFormat<any, any, any, any> | BasePaginatedOptions<any> | LoadMoreOptions<any>>; | ||
export { useAsync, usePaginated, useLoadMore, UseRequestProvider }; | ||
declare const UseAPIProvider: import("react").Provider<BaseOptions<any, any> | OptionsWithFormat<any, any, any, any> | BasePaginatedOptions<any> | LoadMoreOptions<any>>; | ||
export { useAsync, usePaginated, useLoadMore, UseRequestProvider, UseAPIProvider }; | ||
export default useRequest; |
@@ -6,6 +6,8 @@ "use strict"; | ||
}); | ||
exports.UseRequestProvider = exports.useLoadMore = exports.usePaginated = exports.useAsync = void 0; | ||
exports.UseAPIProvider = exports.UseRequestProvider = exports.useLoadMore = exports.usePaginated = exports.useAsync = void 0; | ||
var tslib_1 = require("tslib"); | ||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
var react_1 = require("react"); | ||
@@ -52,9 +54,11 @@ | ||
var fetchProxy = function fetchProxy() { | ||
return fetch.apply(void 0, arguments).then(function (res) { | ||
if (res.ok) { | ||
return res.json(); | ||
} | ||
return (// @ts-ignore | ||
fetch.apply(void 0, arguments).then(function (res) { | ||
if (res.ok) { | ||
return res.json(); | ||
} | ||
throw new Error(res.statusText); | ||
}); | ||
throw new Error(res.statusText); | ||
}) | ||
); | ||
}; | ||
@@ -132,3 +136,6 @@ | ||
var UseRequestProvider = configContext_1["default"].Provider; | ||
exports.UseRequestProvider = UseRequestProvider; | ||
exports.UseRequestProvider = UseRequestProvider; // UseAPIProvider 已经废弃,此处为了兼容 umijs 插件 plugin-request | ||
var UseAPIProvider = UseRequestProvider; | ||
exports.UseAPIProvider = UseAPIProvider; | ||
exports["default"] = useRequest; |
@@ -7,3 +7,3 @@ import { DependencyList, RefObject } from 'react'; | ||
export declare type Subscribe<R, P extends any[]> = (data: FetchResult<R, P>) => void; | ||
export declare type Mutate<R> = (x: (R | undefined | ((data: R) => R))) => void; | ||
export declare type Mutate<R> = (x: R | undefined | ((data: R) => R)) => void; | ||
export declare type RequestService = string | { | ||
@@ -10,0 +10,0 @@ [key: string]: any; |
@@ -320,3 +320,3 @@ "use strict"; | ||
}); | ||
}, []); | ||
}); | ||
@@ -323,0 +323,0 @@ var _react_1$useState = react_1.useState(function () { |
@@ -26,3 +26,3 @@ "use strict"; | ||
if (fetchKey) { | ||
console.error('useRequest pagination\'s fetchKey will not work!'); | ||
console.error("useRequest pagination's fetchKey will not work!"); | ||
} | ||
@@ -29,0 +29,0 @@ }, []); |
@@ -1,2 +0,2 @@ | ||
declare function usePersistFn(fn: any, dependencies?: any): (...args: any[]) => any; | ||
declare function usePersistFn(fn: any): (...args: any[]) => any; | ||
export default usePersistFn; |
@@ -9,13 +9,7 @@ "use strict"; | ||
function usePersistFn(fn, dependencies) { | ||
if (dependencies === void 0) { | ||
dependencies = []; | ||
} | ||
function usePersistFn(fn) { | ||
var ref = react_1.useRef(function () { | ||
throw new Error('Cannot call an event handler while rendering.'); | ||
}); | ||
react_1.useEffect(function () { | ||
ref.current = fn; | ||
}, [fn].concat(dependencies)); | ||
ref.current = fn; | ||
var persist = react_1.useCallback(function () { | ||
@@ -22,0 +16,0 @@ var refFn = ref.current; |
{ | ||
"name": "@ahooksjs/use-request", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "React Hooks for fetching, caching and updating asynchronous data", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
103946
2227