New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@better-typed/react-hyper-fetch

Package Overview
Dependencies
Maintainers
2
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@better-typed/react-hyper-fetch - npm Package Compare versions

Comparing version 0.0.11 to 0.0.12

2

dist/index.cjs.js

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("@better-typed/react-lifecycle-hooks"),t=require("@better-typed/hyper-fetch");function n(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function s(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,s,a=[],i=!0,u=!1;try{for(t=t.call(e);!(i=(n=t.next()).done)&&(a.push(n.value),!r||a.length!==r);i=!0);}catch(e){u=!0,s=e}finally{try{i||null==t.return||t.return()}finally{if(u)throw s}}return a}}(e,r)||function(e,r){if(e){if("string"==typeof e)return n(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}var i={data:null,error:null,loading:!1,status:null,refreshError:null,retryError:null,isRefreshed:!1,retries:0,timestamp:null,isOnline:!0,isFocused:!0};function u(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function o(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?u(Object(t),!0).forEach((function(r){a(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):u(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}function l(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function c(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?l(Object(t),!0).forEach((function(r){a(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):l(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var d=(t,n)=>{var a=t.builder,u=t.cache,l=t.cacheKey,d=s(e.useState(+new Date),2)[1],f=e.useRef(((e,r)=>{var t,n,s,a;return o(o({},i),{},{data:(null==r||null===(t=r.response)||void 0===t?void 0:t[0])||i.data,error:(null==r||null===(n=r.response)||void 0===n?void 0:n[1])||i.error,status:(null==r||null===(s=r.response)||void 0===s?void 0:s[2])||i.status,retries:(null==r?void 0:r.retries)||i.retries,timestamp:(a=(null==r?void 0:r.timestamp)||i.timestamp,a?new Date(a):null),isOnline:e.builder.appManager.isOnline,isFocused:e.builder.appManager.isFocused,loading:!!e.builder.fetchQueue.get(e.queueKey)||i.loading})})(t,n)),v=e.useRef([]),h=e=>{v.current.find((r=>e.includes(r)))&&d(+new Date)};r.useDidMount((()=>{var e=a.appManager.events.onFocus((()=>{f.current.isFocused=!0,h(["isFocused"])})),r=a.appManager.events.onBlur((()=>{f.current.isFocused=!1,h(["isFocused"])})),t=a.appManager.events.onOnline((()=>{f.current.isOnline=!0,h(["isOnline"])})),n=a.appManager.events.onOffline((()=>{f.current.isOnline=!1,h(["isOnline"])}));return()=>{e(),r(),t(),n()}}));var p={setCacheData:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r)a.cache.set(c({cache:u,cacheKey:l},e));else{var t={data:e.response[0],error:e.response[1],status:e.response[2],retries:e.retries,timestamp:new Date(e.timestamp),retryError:e.retryError,refreshError:e.refreshError,isRefreshed:e.isRefreshed,loading:!1};f.current=c(c({},f.current),t),h(Object.keys(t))}},setData:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[e,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed})}else f.current.data=e,h(["data"])},setError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,e,t.status],retries:t.retries,isRefreshed:t.isRefreshed})}else f.current.error=e,h(["error"])},setLoading:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];r?a.fetchQueue.events.setLoading(l,{isLoading:e,isRetry:!1,isRefreshed:!1,isRevalidated:!1}):(f.current.loading=e,h(["loading"]))},setStatus:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,t.error,e],retries:t.retries,isRefreshed:t.isRefreshed})}else f.current.status=e,h(["status"])},setRefreshed:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:e})}else f.current.isRefreshed=e,h(["isRefreshed"])},setRefreshError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,e,t.status],retries:t.retries,isRefreshed:!0})}else f.current.refreshError=e,h(["refreshError"])},setRetryError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,e,t.status],retries:t.retries,isRefreshed:!0})}else f.current.retryError=e,h(["retryError"])},setRetries:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,t.error,t.status],retries:e,isRefreshed:t.isRefreshed})}else f.current.retries=e,h(["retries"])},setTimestamp:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed,timestamp:e?+e:void 0})}else f.current.timestamp=e,h(["timestamp"])}};return[f.current,p,e=>{v.current.push(e)}]},f=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e.useRef({time:t,timer:null});n.current.time=t;var s=()=>{n.current.timer&&clearTimeout(n.current.timer),n.current.timer=null},a=e=>{s(),n.current.timer=setTimeout((()=>{n.current.timer=null,e()}),n.current.time)};return r.useWillUnmount(s),{debounce:a,resetDebounce:s,active:!!n.current.timer}},v=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e.useRef({time:t,timer:null});n.current.time=t;var s=()=>{n.current.timer&&clearInterval(n.current.timer),n.current.timer=null},a=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;s(),n.current.timer=setInterval((()=>{e()}),r<=0?n.current.time:r)};return r.useWillUnmount(s),{interval:a,resetInterval:s,active:!!n.current.timer}},h=(e,r,t)=>{if(r&&e&&t&&+e.timestamp+t>+new Date)return e},p=e=>e?{response:e,retries:0,timestamp:+new Date,isRefreshed:!1}:null,g=(e,r)=>!r||!!e&&+new Date>+r+e,m={dependencies:[],disabled:!1,dependencyTracking:!0,cacheOnMount:!0,revalidateOnMount:!0,initialData:null,refresh:!1,refreshTime:t.DateInterval.hour,refreshBlurred:!1,refreshOnTabBlur:!1,refreshOnTabFocus:!1,refreshOnReconnect:!1,cancelable:!1,debounce:!1,debounceTime:400,deepCompareFn:void 0,responseDataModifierFn:null,shouldThrow:!1},b={disabled:!1,dependencyTracking:!0,cacheOnMount:!0,initialData:null,debounce:!1,debounceTime:400,suspense:!1,shouldThrow:!1,responseDataModifierFn:null,invalidate:[]};exports.useFetch=function(n){var a,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:m,u=i.dependencies,o=void 0===u?m.dependencies:u,l=i.disabled,c=void 0===l?m.disabled:l,b=i.dependencyTracking,y=void 0===b?m.dependencyTracking:b,R=i.revalidateOnMount,O=void 0===R?m.revalidateOnMount:R,D=i.initialData,w=void 0===D?m.initialData:D,F=i.refresh,E=void 0===F?m.refresh:F,S=i.refreshTime,T=void 0===S?m.refreshTime:S,j=i.refreshBlurred,P=void 0===j?m.refreshBlurred:j,M=i.refreshOnTabBlur,q=void 0===M?m.refreshOnTabBlur:M,K=i.refreshOnTabFocus,k=void 0===K?m.refreshOnTabFocus:K,C=i.refreshOnReconnect,I=void 0===C?m.refreshOnReconnect:C,L=i.debounce,U=void 0===L?m.debounce:L,B=i.debounceTime,A=void 0===B?m.debounceTime:B,x=i.responseDataModifierFn,Q=void 0===x?m.responseDataModifierFn:x,W=i.shouldThrow,_=void 0===W?m.shouldThrow:W,$=n.cacheTime,z=n.cacheKey,G=n.queueKey,H=n.builder,J=f(A),N=v(T),V=H.cache,X=H.fetchQueue,Y=H.appManager,Z=H.commandManager,ee=H.logger,re=e.useRef(h(V.get(z),n.cache,$)),te=e.useRef(g($,null===(a=re.current)||void 0===a?void 0:a.timestamp)),ne=e.useRef(te.current?p(w):re.current),se=d(n,ne.current),ae=s(se,3),ie=ae[0],ue=ae[1],oe=ae[2],le=e.useState(!te.current),ce=s(le,2),de=ce[0],fe=ce[1],ve=e.useRef(null),he=e.useRef(null),pe=e.useRef(null),ge=e.useRef(null),me=e.useRef(null),be=e.useRef(null),ye=e.useRef(null),Re=e.useRef(null),Oe=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:ie.isRefreshed,r=arguments.length>1&&void 0!==arguments[1]&&arguments[1],t=g($,ie.timestamp);c||!t&&de&&!e&&!r?ee.debug("[useFetch] Cannot add to queue",Object.entries({isRefreshed:e,isRevalidated:r,disabled:c,isStale:t,hasCacheData:de})):(ee.debug("[useFetch] Adding request to queue",Object.entries({isRefreshed:e,isRevalidated:r})),X.add(n,{isRefreshed:e,isRevalidated:r}))},De=()=>{N.resetInterval();var e=ie.timestamp,r=e?Math.max(+e+T-+new Date):0;E&&(ee.debug("[useFetch] Starting refresh counter, request will be send in ".concat(r,"ms")),N.interval((()=>{var e=X.get(G),r=!n.builder.appManager.isFocused&&P||n.builder.appManager.isFocused;!e&&r&&(ee.debug("[useFetch] Performing refresh request"),Oe(!0,!1),N.resetInterval())}),r))},we=e=>{if(e){var r,t,n;if(e[0])null==he||null===(t=he.current)||void 0===t||t.call(he,e[0]);if(e[1])if(null==pe||null===(n=pe.current)||void 0===n||n.call(pe,e[1]),_)throw{message:"Fetching Error.",error:e[1]};null==ge||null===(r=ge.current)||void 0===r||r.call(ge,e)}},Fe=e=>{ue.setCacheData(e,!1),we(e.response)},Ee=(e,r)=>{if(!de){var t=V.get(z);t&&(ue.setCacheData(t,!1),fe(!0))}ue.setRefreshed(e,!1),ue.setTimestamp(new Date(r),!1),we([ie.data,ie.error,ie.status])},Se=e=>{var r,t=e.isLoading,n=e.isRefreshed,s=e.isRetry,a=e.isRevalidated;ue.setLoading(t,!1),null===(r=ve.current)||void 0===r||r.call(ve,{isRefreshed:n,isRetry:s,isRevalidated:a})},Te=()=>{Oe(!1,!0)},je=e=>{e&&"string"==typeof e?n.builder.cache.events.revalidate(e):e&&e instanceof t.FetchCommand?n.builder.cache.events.revalidate(t.getCommandKey(e)):Te()},Pe=e=>{var r;null==ye||null===(r=ye.current)||void 0===r||r.call(ye,e)},Me=e=>{var r;null==Re||null===(r=Re.current)||void 0===r||r.call(Re,e)},qe=e=>{var r;null==me||null===(r=me.current)||void 0===r||r.call(me,e)},Ke=e=>{var r;null==me||null===(r=me.current)||void 0===r||r.call(me,e)},ke=()=>{var e=Y.events.onFocus((()=>k&&Oe(!0))),r=Y.events.onBlur((()=>q&&Oe(!0))),t=Y.events.onOnline((()=>I&&Oe(!0))),n=Z.events.onDownloadProgress(G,Pe),s=Z.events.onUploadProgress(G,Me),a=Z.events.onRequestStart(G,qe),i=Z.events.onResponseStart(G,Ke),u=X.events.getLoading(G,Se),o=V.events.get(z,Fe),l=V.events.getEqualData(z,Ee),c=V.events.onRevalidate(z,Te);return()=>{e(),r(),t(),n(),s(),a(),i(),u(),o(),l(),c()}},Ce=()=>Q&&ie.data?Q(ie.data):ie.data,Ie=()=>{y||Object.keys(ie).forEach((e=>oe(e)))};return r.useDidMount((()=>{var e;return we(null===(e=ne.current)||void 0===e?void 0:e.response),Ie(),ke()})),r.useDidUpdate((()=>{de&&U?J.debounce((()=>Oe(!1,O))):Oe(!1,O)}),[...o,c],!0),r.useDidUpdate((()=>{De()}),[E,T,ie.timestamp],!0),{get data(){return oe("data"),Ce()},get error(){return oe("error"),ie.error},get loading(){return oe("loading"),ie.loading},get status(){return oe("status"),ie.status},get retryError(){return oe("retryError"),ie.retryError},get refreshError(){return oe("refreshError"),ie.refreshError},get isRefreshed(){return oe("isRefreshed"),ie.isRefreshed},get retries(){return oe("retries"),ie.retries},get timestamp(){return oe("timestamp"),ie.timestamp},get isOnline(){return oe("isOnline"),ie.isOnline},get isFocused(){return oe("isFocused"),ie.isFocused},get isStale(){return g($,ie.timestamp)},actions:ue,onRequest:e=>{ve.current=e},onSuccess:e=>{he.current=e},onError:e=>{pe.current=e},onFinished:e=>{ge.current=e},onRequestStart:e=>{me.current=e},onResponseStart:e=>{be.current=e},onDownloadProgress:e=>{ye.current=e},onUploadProgress:e=>{Re.current=e},isRefreshingError:!!ie.error&&ie.isRefreshed,isDebouncing:J.active,refresh:je}},exports.useSubmit=function(t){var n,a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:b,i=a.disabled,u=void 0===i?b.disabled:i,o=a.dependencyTracking,l=void 0===o?b.dependencyTracking:o,c=a.cacheOnMount,f=void 0===c?b.cacheOnMount:c,v=a.initialData,m=void 0===v?b.initialData:v,y=a.shouldThrow,R=void 0===y?b.shouldThrow:y,O=a.responseDataModifierFn,D=void 0===O?b.responseDataModifierFn:O,w=t.cacheTime,F=t.cacheKey,E=t.queueKey,S=t.builder,T=S.cache,j=S.submitQueue,P=S.commandManager,M=e.useRef(h(T.get(F),f,w)),q=e.useRef(g(w,null===(n=M.current)||void 0===n?void 0:n.timestamp)),K=e.useRef(q.current?p(m):M.current),k=d(t,K.current),C=s(k,3),I=C[0],L=C[1],U=C[2],B=e.useRef(null),A=e.useRef(null),x=e.useRef(null),Q=e.useRef(null),W=e.useRef(null),_=e.useRef(null),$=e.useRef(null),z=e.useRef(null),G=function(){var e=t,r=arguments.length<=0?void 0:arguments[0];null!=r&&r.data&&(e=e.setData(r.data)),null!=r&&r.params&&(e=e.setParams(r.params)),null!=r&&r.queryParams&&(e=e.setQueryParams(r.queryParams)),u||j.add(e)},H=e=>{if(e){var r,t,n;if(e[0])null==A||null===(t=A.current)||void 0===t||t.call(A,e[0]);if(e[1])if(null==x||null===(n=x.current)||void 0===n||n.call(x,e[1]),R)throw{message:"Fetching Error.",error:e[1]};null==Q||null===(r=Q.current)||void 0===r||r.call(Q,e)}},J=e=>{L.setCacheData(e,!1),H(e.response)},N=(e,r)=>{L.setRefreshed(e,!1),L.setTimestamp(new Date(r),!1),H([I.data,I.error,I.status])},V=e=>{var r,t=e.isLoading,n=e.isRetry;L.setLoading(t,!1),null===(r=B.current)||void 0===r||r.call(B,{isRetry:n})},X=e=>{var r;null==$||null===(r=$.current)||void 0===r||r.call($,e)},Y=e=>{var r;null==z||null===(r=z.current)||void 0===r||r.call(z,e)},Z=e=>{var r;null==W||null===(r=W.current)||void 0===r||r.call(W,e)},ee=e=>{var r;null==W||null===(r=W.current)||void 0===r||r.call(W,e)},re=()=>{var e=P.events.onDownloadProgress(E,X),r=P.events.onUploadProgress(E,Y),t=P.events.onRequestStart(E,Z),n=P.events.onResponseStart(E,ee),s=j.events.getLoading(E,V),a=T.events.get(E,J),i=T.events.getEqualData(F,N);return()=>{e(),r(),t(),n(),s(),a(),i()}},te=()=>D&&I.data?D(I.data):I.data,ne=()=>{l||Object.keys(I).forEach((e=>U(e)))};return r.useDidMount((()=>(ne(),re()))),{submit:G,get data(){return U("data"),te()},get error(){return U("error"),I.error},get submitting(){return U("loading"),I.loading},get status(){return U("status"),I.status},get retryError(){return U("retryError"),I.retryError},get retries(){return U("retries"),I.retries},get timestamp(){return U("timestamp"),I.timestamp},get isOnline(){return U("isOnline"),I.isOnline},get isFocused(){return U("isFocused"),I.isFocused},get isStale(){return g(w,I.timestamp)},actions:L,isDebouncing:!1,isRefreshed:!1,onSubmitRequest:e=>{B.current=e},onSubmitSuccess:e=>{A.current=e},onSubmitError:e=>{x.current=e},onSubmitFinished:e=>{Q.current=e},onSubmitRequestStart:e=>{W.current=e},onSubmitResponseStart:e=>{_.current=e},onSubmitDownloadProgress:e=>{$.current=e},onSubmitUploadProgress:e=>{z.current=e}}};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("@better-typed/react-lifecycle-hooks"),t=require("@better-typed/hyper-fetch");function n(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function s(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,s,a=[],i=!0,u=!1;try{for(t=t.call(e);!(i=(n=t.next()).done)&&(a.push(n.value),!r||a.length!==r);i=!0);}catch(e){u=!0,s=e}finally{try{i||null==t.return||t.return()}finally{if(u)throw s}}return a}}(e,r)||function(e,r){if(e){if("string"==typeof e)return n(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}var i={data:null,error:null,loading:!1,status:null,refreshError:null,retryError:null,isRefreshed:!1,retries:0,timestamp:null,isOnline:!0,isFocused:!0};function u(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function o(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?u(Object(t),!0).forEach((function(r){a(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):u(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}function l(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function c(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?l(Object(t),!0).forEach((function(r){a(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):l(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var d=(t,n)=>{var a=t.builder,u=t.cache,l=t.cacheKey,d=s(e.useState(+new Date),2)[1],f=e.useRef(((e,r)=>{var t,n,s,a;return o(o({},i),{},{data:(null==r||null===(t=r.response)||void 0===t?void 0:t[0])||i.data,error:(null==r||null===(n=r.response)||void 0===n?void 0:n[1])||i.error,status:(null==r||null===(s=r.response)||void 0===s?void 0:s[2])||i.status,retries:(null==r?void 0:r.retries)||i.retries,timestamp:(a=(null==r?void 0:r.timestamp)||i.timestamp,a?new Date(a):null),isOnline:e.builder.appManager.isOnline,isFocused:e.builder.appManager.isFocused,loading:!!e.builder.fetchQueue.get(e.queueKey)||i.loading})})(t,n)),v=e.useRef([]),h=e=>{v.current.find((r=>e.includes(r)))&&d(+new Date)};r.useDidMount((()=>{var e=a.appManager.events.onFocus((()=>{f.current.isFocused=!0,h(["isFocused"])})),r=a.appManager.events.onBlur((()=>{f.current.isFocused=!1,h(["isFocused"])})),t=a.appManager.events.onOnline((()=>{f.current.isOnline=!0,h(["isOnline"])})),n=a.appManager.events.onOffline((()=>{f.current.isOnline=!1,h(["isOnline"])}));return()=>{e(),r(),t(),n()}}));var p={setCacheData:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r)a.cache.set(c({cache:u,cacheKey:l},e));else{var t={data:e.response[0],error:e.response[1],status:e.response[2],retries:e.retries,timestamp:new Date(e.timestamp),retryError:e.retryError,refreshError:e.refreshError,isRefreshed:e.isRefreshed,loading:!1};f.current=c(c({},f.current),t),h(Object.keys(t))}},setData:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[e,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed})}else f.current.data=e,h(["data"])},setError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,e,t.status],retries:t.retries,isRefreshed:t.isRefreshed})}else f.current.error=e,h(["error"])},setLoading:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];r?a.fetchQueue.events.setLoading(l,{isLoading:e,isRetry:!1,isRefreshed:!1,isRevalidated:!1}):(f.current.loading=e,h(["loading"]))},setStatus:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,t.error,e],retries:t.retries,isRefreshed:t.isRefreshed})}else f.current.status=e,h(["status"])},setRefreshed:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:e})}else f.current.isRefreshed=e,h(["isRefreshed"])},setRefreshError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,e,t.status],retries:t.retries,isRefreshed:!0})}else f.current.refreshError=e,h(["refreshError"])},setRetryError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,e,t.status],retries:t.retries,isRefreshed:!0})}else f.current.retryError=e,h(["retryError"])},setRetries:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,t.error,t.status],retries:e,isRefreshed:t.isRefreshed})}else f.current.retries=e,h(["retries"])},setTimestamp:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=f.current;a.cache.set({cache:u,cacheKey:l,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed,timestamp:e?+e:void 0})}else f.current.timestamp=e,h(["timestamp"])}};return[f.current,p,e=>{v.current.push(e)}]},f=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e.useRef({time:t,timer:null});n.current.time=t;var s=()=>{n.current.timer&&clearTimeout(n.current.timer),n.current.timer=null},a=e=>{s(),n.current.timer=setTimeout((()=>{n.current.timer=null,e()}),n.current.time)};return r.useWillUnmount(s),{debounce:a,resetDebounce:s,active:!!n.current.timer}},v=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,n=e.useRef({time:t,timer:null});n.current.time=t;var s=()=>{n.current.timer&&clearInterval(n.current.timer),n.current.timer=null},a=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;s(),n.current.timer=setInterval((()=>{e()}),r<=0?n.current.time:r)};return r.useWillUnmount(s),{interval:a,resetInterval:s,active:!!n.current.timer}},h=(e,r,t)=>{if(r&&e&&t&&+e.timestamp+t>+new Date)return e},p=e=>e?{response:e,retries:0,timestamp:+new Date,isRefreshed:!1}:null,g=(e,r)=>!r||!!e&&+new Date>+r+e,m={dependencies:[],disabled:!1,dependencyTracking:!0,cacheOnMount:!0,revalidateOnMount:!0,initialData:null,refresh:!1,refreshTime:t.DateInterval.hour,refreshBlurred:!1,refreshOnTabBlur:!1,refreshOnTabFocus:!1,refreshOnReconnect:!1,cancelable:!1,debounce:!1,debounceTime:400,deepCompareFn:void 0,responseDataModifierFn:null,shouldThrow:!1},b={disabled:!1,dependencyTracking:!0,cacheOnMount:!0,initialData:null,debounce:!1,debounceTime:400,suspense:!1,shouldThrow:!1,responseDataModifierFn:null,invalidate:[]};exports.useFetch=function(n){var a,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:m,u=i.dependencies,o=void 0===u?m.dependencies:u,l=i.disabled,c=void 0===l?m.disabled:l,b=i.dependencyTracking,y=void 0===b?m.dependencyTracking:b,R=i.revalidateOnMount,O=void 0===R?m.revalidateOnMount:R,D=i.initialData,w=void 0===D?m.initialData:D,E=i.refresh,S=void 0===E?m.refresh:E,F=i.refreshTime,T=void 0===F?m.refreshTime:F,P=i.refreshBlurred,j=void 0===P?m.refreshBlurred:P,M=i.refreshOnTabBlur,q=void 0===M?m.refreshOnTabBlur:M,K=i.refreshOnTabFocus,k=void 0===K?m.refreshOnTabFocus:K,C=i.refreshOnReconnect,I=void 0===C?m.refreshOnReconnect:C,L=i.debounce,U=void 0===L?m.debounce:L,B=i.debounceTime,A=void 0===B?m.debounceTime:B,x=i.responseDataModifierFn,Q=void 0===x?m.responseDataModifierFn:x,W=i.shouldThrow,_=void 0===W?m.shouldThrow:W,$=n.cacheTime,z=n.cacheKey,G=n.queueKey,H=n.builder,J=f(A),N=v(T),V=H.cache,X=H.fetchQueue,Y=H.appManager,Z=H.commandManager,ee=H.logger,re=e.useRef(h(V.get(z),n.cache,$)),te=e.useRef(g($,null===(a=re.current)||void 0===a?void 0:a.timestamp)),ne=e.useRef(te.current?p(w):re.current),se=d(n,ne.current),ae=s(se,3),ie=ae[0],ue=ae[1],oe=ae[2],le=e.useState(!te.current),ce=s(le,2),de=ce[0],fe=ce[1],ve=e.useRef(null),he=e.useRef(null),pe=e.useRef(null),ge=e.useRef(null),me=e.useRef(null),be=e.useRef(null),ye=e.useRef(null),Re=e.useRef(null),Oe=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:ie.isRefreshed,r=arguments.length>1&&void 0!==arguments[1]&&arguments[1],t=g($,ie.timestamp);c||!t&&de&&!e&&!r?ee.debug("Cannot add to queue",{isRefreshed:e,isRevalidated:r,disabled:c,isStale:t,hasCacheData:de}):(ee.debug("Adding request to queue",{isRefreshed:e,isRevalidated:r}),X.add(n,{isRefreshed:e,isRevalidated:r}))},De=()=>{N.resetInterval();var e=ie.timestamp,r=e?Math.max(+e+T-+new Date):0;S&&(ee.debug("Starting refresh counter, request will be send in ".concat(r,"ms")),N.interval((()=>{var e=X.get(G),r=!n.builder.appManager.isFocused&&j||n.builder.appManager.isFocused;!e&&r&&(ee.debug("Performing refresh request"),Oe(!0,!1),N.resetInterval())}),r))},we=e=>{if(e){var r,t,n;if(e[0])null==he||null===(t=he.current)||void 0===t||t.call(he,e[0]);if(e[1])if(null==pe||null===(n=pe.current)||void 0===n||n.call(pe,e[1]),_)throw{message:"Fetching Error.",error:e[1]};null==ge||null===(r=ge.current)||void 0===r||r.call(ge,e)}},Ee=e=>{ue.setCacheData(e,!1),we(e.response)},Se=(e,r)=>{if(!de){var t=V.get(z);t&&(ue.setCacheData(t,!1),fe(!0))}ue.setRefreshed(e,!1),ue.setTimestamp(new Date(r),!1),we([ie.data,ie.error,ie.status])},Fe=e=>{var r,t=e.isLoading,n=e.isRefreshed,s=e.isRetry,a=e.isRevalidated;ue.setLoading(t,!1),null===(r=ve.current)||void 0===r||r.call(ve,{isRefreshed:n,isRetry:s,isRevalidated:a})},Te=()=>{Oe(!1,!0)},Pe=e=>{e&&"string"==typeof e?n.builder.cache.events.revalidate(e):e&&e instanceof t.FetchCommand?n.builder.cache.events.revalidate(t.getCommandKey(e)):Te()},je=e=>{var r;null==ye||null===(r=ye.current)||void 0===r||r.call(ye,e)},Me=e=>{var r;null==Re||null===(r=Re.current)||void 0===r||r.call(Re,e)},qe=e=>{var r;null==me||null===(r=me.current)||void 0===r||r.call(me,e)},Ke=e=>{var r;null==me||null===(r=me.current)||void 0===r||r.call(me,e)},ke=()=>{var e=Y.events.onFocus((()=>k&&Oe(!0))),r=Y.events.onBlur((()=>q&&Oe(!0))),t=Y.events.onOnline((()=>I&&Oe(!0))),n=Z.events.onDownloadProgress(G,je),s=Z.events.onUploadProgress(G,Me),a=Z.events.onRequestStart(G,qe),i=Z.events.onResponseStart(G,Ke),u=X.events.getLoading(G,Fe),o=V.events.get(z,Ee),l=V.events.getEqualData(z,Se),c=V.events.onRevalidate(z,Te);return()=>{e(),r(),t(),n(),s(),a(),i(),u(),o(),l(),c()}},Ce=()=>Q&&ie.data?Q(ie.data):ie.data,Ie=()=>{y||Object.keys(ie).forEach((e=>oe(e)))};return r.useDidMount((()=>{var e;return we(null===(e=ne.current)||void 0===e?void 0:e.response),Ie(),ke()})),r.useDidUpdate((()=>{de&&U?J.debounce((()=>Oe(!1,O))):Oe(!1,O)}),[...o,c],!0),r.useDidUpdate((()=>{De()}),[S,T,ie.timestamp],!0),{get data(){return oe("data"),Ce()},get error(){return oe("error"),ie.error},get loading(){return oe("loading"),ie.loading},get status(){return oe("status"),ie.status},get retryError(){return oe("retryError"),ie.retryError},get refreshError(){return oe("refreshError"),ie.refreshError},get isRefreshed(){return oe("isRefreshed"),ie.isRefreshed},get retries(){return oe("retries"),ie.retries},get timestamp(){return oe("timestamp"),ie.timestamp},get isOnline(){return oe("isOnline"),ie.isOnline},get isFocused(){return oe("isFocused"),ie.isFocused},get isStale(){return g($,ie.timestamp)},actions:ue,onRequest:e=>{ve.current=e},onSuccess:e=>{he.current=e},onError:e=>{pe.current=e},onFinished:e=>{ge.current=e},onRequestStart:e=>{me.current=e},onResponseStart:e=>{be.current=e},onDownloadProgress:e=>{ye.current=e},onUploadProgress:e=>{Re.current=e},isRefreshingError:!!ie.error&&ie.isRefreshed,isDebouncing:J.active,refresh:Pe}},exports.useSubmit=function(t){var n,a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:b,i=a.disabled,u=void 0===i?b.disabled:i,o=a.dependencyTracking,l=void 0===o?b.dependencyTracking:o,c=a.cacheOnMount,f=void 0===c?b.cacheOnMount:c,v=a.initialData,m=void 0===v?b.initialData:v,y=a.shouldThrow,R=void 0===y?b.shouldThrow:y,O=a.responseDataModifierFn,D=void 0===O?b.responseDataModifierFn:O,w=t.cacheTime,E=t.cacheKey,S=t.queueKey,F=t.builder,T=F.cache,P=F.submitQueue,j=F.commandManager,M=e.useRef(h(T.get(E),f,w)),q=e.useRef(g(w,null===(n=M.current)||void 0===n?void 0:n.timestamp)),K=e.useRef(q.current?p(m):M.current),k=d(t,K.current),C=s(k,3),I=C[0],L=C[1],U=C[2],B=e.useRef(null),A=e.useRef(null),x=e.useRef(null),Q=e.useRef(null),W=e.useRef(null),_=e.useRef(null),$=e.useRef(null),z=e.useRef(null),G=function(){var e=t,r=arguments.length<=0?void 0:arguments[0];null!=r&&r.data&&(e=e.setData(r.data)),null!=r&&r.params&&(e=e.setParams(r.params)),null!=r&&r.queryParams&&(e=e.setQueryParams(r.queryParams)),u||P.add(e)},H=e=>{if(e){var r,t,n;if(e[0])null==A||null===(t=A.current)||void 0===t||t.call(A,e[0]);if(e[1])if(null==x||null===(n=x.current)||void 0===n||n.call(x,e[1]),R)throw{message:"Fetching Error.",error:e[1]};null==Q||null===(r=Q.current)||void 0===r||r.call(Q,e)}},J=e=>{L.setCacheData(e,!1),H(e.response)},N=(e,r)=>{L.setRefreshed(e,!1),L.setTimestamp(new Date(r),!1),H([I.data,I.error,I.status])},V=e=>{var r,t=e.isLoading,n=e.isRetry;L.setLoading(t,!1),null===(r=B.current)||void 0===r||r.call(B,{isRetry:n})},X=e=>{var r;null==$||null===(r=$.current)||void 0===r||r.call($,e)},Y=e=>{var r;null==z||null===(r=z.current)||void 0===r||r.call(z,e)},Z=e=>{var r;null==W||null===(r=W.current)||void 0===r||r.call(W,e)},ee=e=>{var r;null==W||null===(r=W.current)||void 0===r||r.call(W,e)},re=()=>{var e=j.events.onDownloadProgress(S,X),r=j.events.onUploadProgress(S,Y),t=j.events.onRequestStart(S,Z),n=j.events.onResponseStart(S,ee),s=P.events.getLoading(S,V),a=T.events.get(S,J),i=T.events.getEqualData(E,N);return()=>{e(),r(),t(),n(),s(),a(),i()}},te=()=>D&&I.data?D(I.data):I.data,ne=()=>{l||Object.keys(I).forEach((e=>U(e)))};return r.useDidMount((()=>(ne(),re()))),{submit:G,get data(){return U("data"),te()},get error(){return U("error"),I.error},get submitting(){return U("loading"),I.loading},get status(){return U("status"),I.status},get retryError(){return U("retryError"),I.retryError},get retries(){return U("retries"),I.retries},get timestamp(){return U("timestamp"),I.timestamp},get isOnline(){return U("isOnline"),I.isOnline},get isFocused(){return U("isFocused"),I.isFocused},get isStale(){return g(w,I.timestamp)},actions:L,isDebouncing:!1,isRefreshed:!1,onSubmitRequest:e=>{B.current=e},onSubmitSuccess:e=>{A.current=e},onSubmitError:e=>{x.current=e},onSubmitFinished:e=>{Q.current=e},onSubmitRequestStart:e=>{W.current=e},onSubmitResponseStart:e=>{_.current=e},onSubmitDownloadProgress:e=>{$.current=e},onSubmitUploadProgress:e=>{z.current=e}}};
//# sourceMappingURL=index.cjs.js.map

@@ -1,2 +0,2 @@

import{useState as e,useRef as r}from"react";import{useDidMount as t,useWillUnmount as n,useDidUpdate as s}from"@better-typed/react-lifecycle-hooks";import{DateInterval as a,FetchCommand as i,getCommandKey as o}from"@better-typed/hyper-fetch";function u(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function l(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,s,a=[],i=!0,o=!1;try{for(t=t.call(e);!(i=(n=t.next()).done)&&(a.push(n.value),!r||a.length!==r);i=!0);}catch(e){o=!0,s=e}finally{try{i||null==t.return||t.return()}finally{if(o)throw s}}return a}}(e,r)||function(e,r){if(e){if("string"==typeof e)return u(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?u(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function c(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}var d={data:null,error:null,loading:!1,status:null,refreshError:null,retryError:null,isRefreshed:!1,retries:0,timestamp:null,isOnline:!0,isFocused:!0};function v(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function h(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?v(Object(t),!0).forEach((function(r){c(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):v(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}function f(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function p(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?f(Object(t),!0).forEach((function(r){c(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):f(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var g=(n,s)=>{var a=n.builder,i=n.cache,o=n.cacheKey,u=l(e(+new Date),2)[1],c=r(((e,r)=>{var t,n,s,a;return h(h({},d),{},{data:(null==r||null===(t=r.response)||void 0===t?void 0:t[0])||d.data,error:(null==r||null===(n=r.response)||void 0===n?void 0:n[1])||d.error,status:(null==r||null===(s=r.response)||void 0===s?void 0:s[2])||d.status,retries:(null==r?void 0:r.retries)||d.retries,timestamp:(a=(null==r?void 0:r.timestamp)||d.timestamp,a?new Date(a):null),isOnline:e.builder.appManager.isOnline,isFocused:e.builder.appManager.isFocused,loading:!!e.builder.fetchQueue.get(e.queueKey)||d.loading})})(n,s)),v=r([]),f=e=>{v.current.find((r=>e.includes(r)))&&u(+new Date)};t((()=>{var e=a.appManager.events.onFocus((()=>{c.current.isFocused=!0,f(["isFocused"])})),r=a.appManager.events.onBlur((()=>{c.current.isFocused=!1,f(["isFocused"])})),t=a.appManager.events.onOnline((()=>{c.current.isOnline=!0,f(["isOnline"])})),n=a.appManager.events.onOffline((()=>{c.current.isOnline=!1,f(["isOnline"])}));return()=>{e(),r(),t(),n()}}));var g={setCacheData:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r)a.cache.set(p({cache:i,cacheKey:o},e));else{var t={data:e.response[0],error:e.response[1],status:e.response[2],retries:e.retries,timestamp:new Date(e.timestamp),retryError:e.retryError,refreshError:e.refreshError,isRefreshed:e.isRefreshed,loading:!1};c.current=p(p({},c.current),t),f(Object.keys(t))}},setData:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[e,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed})}else c.current.data=e,f(["data"])},setError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,e,t.status],retries:t.retries,isRefreshed:t.isRefreshed})}else c.current.error=e,f(["error"])},setLoading:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];r?a.fetchQueue.events.setLoading(o,{isLoading:e,isRetry:!1,isRefreshed:!1,isRevalidated:!1}):(c.current.loading=e,f(["loading"]))},setStatus:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,t.error,e],retries:t.retries,isRefreshed:t.isRefreshed})}else c.current.status=e,f(["status"])},setRefreshed:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:e})}else c.current.isRefreshed=e,f(["isRefreshed"])},setRefreshError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,e,t.status],retries:t.retries,isRefreshed:!0})}else c.current.refreshError=e,f(["refreshError"])},setRetryError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,e,t.status],retries:t.retries,isRefreshed:!0})}else c.current.retryError=e,f(["retryError"])},setRetries:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,t.error,t.status],retries:e,isRefreshed:t.isRefreshed})}else c.current.retries=e,f(["retries"])},setTimestamp:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed,timestamp:e?+e:void 0})}else c.current.timestamp=e,f(["timestamp"])}};return[c.current,g,e=>{v.current.push(e)}]},m=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,t=r({time:e,timer:null});t.current.time=e;var s=()=>{t.current.timer&&clearTimeout(t.current.timer),t.current.timer=null},a=e=>{s(),t.current.timer=setTimeout((()=>{t.current.timer=null,e()}),t.current.time)};return n(s),{debounce:a,resetDebounce:s,active:!!t.current.timer}},b=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,t=r({time:e,timer:null});t.current.time=e;var s=()=>{t.current.timer&&clearInterval(t.current.timer),t.current.timer=null},a=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;s(),t.current.timer=setInterval((()=>{e()}),r<=0?t.current.time:r)};return n(s),{interval:a,resetInterval:s,active:!!t.current.timer}},y=(e,r,t)=>{if(r&&e&&t&&+e.timestamp+t>+new Date)return e},O=e=>e?{response:e,retries:0,timestamp:+new Date,isRefreshed:!1}:null,R=(e,r)=>!r||!!e&&+new Date>+r+e,D={dependencies:[],disabled:!1,dependencyTracking:!0,cacheOnMount:!0,revalidateOnMount:!0,initialData:null,refresh:!1,refreshTime:a.hour,refreshBlurred:!1,refreshOnTabBlur:!1,refreshOnTabFocus:!1,refreshOnReconnect:!1,cancelable:!1,debounce:!1,debounceTime:400,deepCompareFn:void 0,responseDataModifierFn:null,shouldThrow:!1},w=function(n){var a,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:D,c=u.dependencies,d=void 0===c?D.dependencies:c,v=u.disabled,h=void 0===v?D.disabled:v,f=u.dependencyTracking,p=void 0===f?D.dependencyTracking:f,w=u.revalidateOnMount,E=void 0===w?D.revalidateOnMount:w,F=u.initialData,T=void 0===F?D.initialData:F,S=u.refresh,j=void 0===S?D.refresh:S,P=u.refreshTime,M=void 0===P?D.refreshTime:P,q=u.refreshBlurred,K=void 0===q?D.refreshBlurred:q,k=u.refreshOnTabBlur,L=void 0===k?D.refreshOnTabBlur:k,B=u.refreshOnTabFocus,C=void 0===B?D.refreshOnTabFocus:B,I=u.refreshOnReconnect,A=void 0===I?D.refreshOnReconnect:I,Q=u.debounce,U=void 0===Q?D.debounce:Q,x=u.debounceTime,$=void 0===x?D.debounceTime:x,z=u.responseDataModifierFn,G=void 0===z?D.responseDataModifierFn:z,H=u.shouldThrow,J=void 0===H?D.shouldThrow:H,N=n.cacheTime,V=n.cacheKey,W=n.queueKey,X=n.builder,Y=m($),Z=b(M),_=X.cache,ee=X.fetchQueue,re=X.appManager,te=X.commandManager,ne=X.logger,se=r(y(_.get(V),n.cache,N)),ae=r(R(N,null===(a=se.current)||void 0===a?void 0:a.timestamp)),ie=r(ae.current?O(T):se.current),oe=g(n,ie.current),ue=l(oe,3),le=ue[0],ce=ue[1],de=ue[2],ve=e(!ae.current),he=l(ve,2),fe=he[0],pe=he[1],ge=r(null),me=r(null),be=r(null),ye=r(null),Oe=r(null),Re=r(null),De=r(null),we=r(null),Ee=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:le.isRefreshed,r=arguments.length>1&&void 0!==arguments[1]&&arguments[1],t=R(N,le.timestamp);h||!t&&fe&&!e&&!r?ne.debug("[useFetch] Cannot add to queue",Object.entries({isRefreshed:e,isRevalidated:r,disabled:h,isStale:t,hasCacheData:fe})):(ne.debug("[useFetch] Adding request to queue",Object.entries({isRefreshed:e,isRevalidated:r})),ee.add(n,{isRefreshed:e,isRevalidated:r}))},Fe=()=>{Z.resetInterval();var e=le.timestamp,r=e?Math.max(+e+M-+new Date):0;j&&(ne.debug("[useFetch] Starting refresh counter, request will be send in ".concat(r,"ms")),Z.interval((()=>{var e=ee.get(W),r=!n.builder.appManager.isFocused&&K||n.builder.appManager.isFocused;!e&&r&&(ne.debug("[useFetch] Performing refresh request"),Ee(!0,!1),Z.resetInterval())}),r))},Te=e=>{if(e){var r,t,n;if(e[0])null==me||null===(t=me.current)||void 0===t||t.call(me,e[0]);if(e[1])if(null==be||null===(n=be.current)||void 0===n||n.call(be,e[1]),J)throw{message:"Fetching Error.",error:e[1]};null==ye||null===(r=ye.current)||void 0===r||r.call(ye,e)}},Se=e=>{ce.setCacheData(e,!1),Te(e.response)},je=(e,r)=>{if(!fe){var t=_.get(V);t&&(ce.setCacheData(t,!1),pe(!0))}ce.setRefreshed(e,!1),ce.setTimestamp(new Date(r),!1),Te([le.data,le.error,le.status])},Pe=e=>{var r,t=e.isLoading,n=e.isRefreshed,s=e.isRetry,a=e.isRevalidated;ce.setLoading(t,!1),null===(r=ge.current)||void 0===r||r.call(ge,{isRefreshed:n,isRetry:s,isRevalidated:a})},Me=()=>{Ee(!1,!0)},qe=e=>{e&&"string"==typeof e?n.builder.cache.events.revalidate(e):e&&e instanceof i?n.builder.cache.events.revalidate(o(e)):Me()},Ke=e=>{var r;null==De||null===(r=De.current)||void 0===r||r.call(De,e)},ke=e=>{var r;null==we||null===(r=we.current)||void 0===r||r.call(we,e)},Le=e=>{var r;null==Oe||null===(r=Oe.current)||void 0===r||r.call(Oe,e)},Be=e=>{var r;null==Oe||null===(r=Oe.current)||void 0===r||r.call(Oe,e)},Ce=()=>{var e=re.events.onFocus((()=>C&&Ee(!0))),r=re.events.onBlur((()=>L&&Ee(!0))),t=re.events.onOnline((()=>A&&Ee(!0))),n=te.events.onDownloadProgress(W,Ke),s=te.events.onUploadProgress(W,ke),a=te.events.onRequestStart(W,Le),i=te.events.onResponseStart(W,Be),o=ee.events.getLoading(W,Pe),u=_.events.get(V,Se),l=_.events.getEqualData(V,je),c=_.events.onRevalidate(V,Me);return()=>{e(),r(),t(),n(),s(),a(),i(),o(),u(),l(),c()}},Ie=()=>G&&le.data?G(le.data):le.data,Ae=()=>{p||Object.keys(le).forEach((e=>de(e)))};return t((()=>{var e;return Te(null===(e=ie.current)||void 0===e?void 0:e.response),Ae(),Ce()})),s((()=>{fe&&U?Y.debounce((()=>Ee(!1,E))):Ee(!1,E)}),[...d,h],!0),s((()=>{Fe()}),[j,M,le.timestamp],!0),{get data(){return de("data"),Ie()},get error(){return de("error"),le.error},get loading(){return de("loading"),le.loading},get status(){return de("status"),le.status},get retryError(){return de("retryError"),le.retryError},get refreshError(){return de("refreshError"),le.refreshError},get isRefreshed(){return de("isRefreshed"),le.isRefreshed},get retries(){return de("retries"),le.retries},get timestamp(){return de("timestamp"),le.timestamp},get isOnline(){return de("isOnline"),le.isOnline},get isFocused(){return de("isFocused"),le.isFocused},get isStale(){return R(N,le.timestamp)},actions:ce,onRequest:e=>{ge.current=e},onSuccess:e=>{me.current=e},onError:e=>{be.current=e},onFinished:e=>{ye.current=e},onRequestStart:e=>{Oe.current=e},onResponseStart:e=>{Re.current=e},onDownloadProgress:e=>{De.current=e},onUploadProgress:e=>{we.current=e},isRefreshingError:!!le.error&&le.isRefreshed,isDebouncing:Y.active,refresh:qe}},E={disabled:!1,dependencyTracking:!0,cacheOnMount:!0,initialData:null,debounce:!1,debounceTime:400,suspense:!1,shouldThrow:!1,responseDataModifierFn:null,invalidate:[]},F=function(e){var n,s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:E,a=s.disabled,i=void 0===a?E.disabled:a,o=s.dependencyTracking,u=void 0===o?E.dependencyTracking:o,c=s.cacheOnMount,d=void 0===c?E.cacheOnMount:c,v=s.initialData,h=void 0===v?E.initialData:v,f=s.shouldThrow,p=void 0===f?E.shouldThrow:f,m=s.responseDataModifierFn,b=void 0===m?E.responseDataModifierFn:m,D=e.cacheTime,w=e.cacheKey,F=e.queueKey,T=e.builder,S=T.cache,j=T.submitQueue,P=T.commandManager,M=r(y(S.get(w),d,D)),q=r(R(D,null===(n=M.current)||void 0===n?void 0:n.timestamp)),K=r(q.current?O(h):M.current),k=g(e,K.current),L=l(k,3),B=L[0],C=L[1],I=L[2],A=r(null),Q=r(null),U=r(null),x=r(null),$=r(null),z=r(null),G=r(null),H=r(null),J=function(){var r=e,t=arguments.length<=0?void 0:arguments[0];null!=t&&t.data&&(r=r.setData(t.data)),null!=t&&t.params&&(r=r.setParams(t.params)),null!=t&&t.queryParams&&(r=r.setQueryParams(t.queryParams)),i||j.add(r)},N=e=>{if(e){var r,t,n;if(e[0])null==Q||null===(t=Q.current)||void 0===t||t.call(Q,e[0]);if(e[1])if(null==U||null===(n=U.current)||void 0===n||n.call(U,e[1]),p)throw{message:"Fetching Error.",error:e[1]};null==x||null===(r=x.current)||void 0===r||r.call(x,e)}},V=e=>{C.setCacheData(e,!1),N(e.response)},W=(e,r)=>{C.setRefreshed(e,!1),C.setTimestamp(new Date(r),!1),N([B.data,B.error,B.status])},X=e=>{var r,t=e.isLoading,n=e.isRetry;C.setLoading(t,!1),null===(r=A.current)||void 0===r||r.call(A,{isRetry:n})},Y=e=>{var r;null==G||null===(r=G.current)||void 0===r||r.call(G,e)},Z=e=>{var r;null==H||null===(r=H.current)||void 0===r||r.call(H,e)},_=e=>{var r;null==$||null===(r=$.current)||void 0===r||r.call($,e)},ee=e=>{var r;null==$||null===(r=$.current)||void 0===r||r.call($,e)},re=()=>{var e=P.events.onDownloadProgress(F,Y),r=P.events.onUploadProgress(F,Z),t=P.events.onRequestStart(F,_),n=P.events.onResponseStart(F,ee),s=j.events.getLoading(F,X),a=S.events.get(F,V),i=S.events.getEqualData(w,W);return()=>{e(),r(),t(),n(),s(),a(),i()}},te=()=>b&&B.data?b(B.data):B.data,ne=()=>{u||Object.keys(B).forEach((e=>I(e)))};return t((()=>(ne(),re()))),{submit:J,get data(){return I("data"),te()},get error(){return I("error"),B.error},get submitting(){return I("loading"),B.loading},get status(){return I("status"),B.status},get retryError(){return I("retryError"),B.retryError},get retries(){return I("retries"),B.retries},get timestamp(){return I("timestamp"),B.timestamp},get isOnline(){return I("isOnline"),B.isOnline},get isFocused(){return I("isFocused"),B.isFocused},get isStale(){return R(D,B.timestamp)},actions:C,isDebouncing:!1,isRefreshed:!1,onSubmitRequest:e=>{A.current=e},onSubmitSuccess:e=>{Q.current=e},onSubmitError:e=>{U.current=e},onSubmitFinished:e=>{x.current=e},onSubmitRequestStart:e=>{$.current=e},onSubmitResponseStart:e=>{z.current=e},onSubmitDownloadProgress:e=>{G.current=e},onSubmitUploadProgress:e=>{H.current=e}}};export{w as useFetch,F as useSubmit};
import{useState as e,useRef as r}from"react";import{useDidMount as t,useWillUnmount as n,useDidUpdate as s}from"@better-typed/react-lifecycle-hooks";import{DateInterval as a,FetchCommand as i,getCommandKey as o}from"@better-typed/hyper-fetch";function u(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function l(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,s,a=[],i=!0,o=!1;try{for(t=t.call(e);!(i=(n=t.next()).done)&&(a.push(n.value),!r||a.length!==r);i=!0);}catch(e){o=!0,s=e}finally{try{i||null==t.return||t.return()}finally{if(o)throw s}}return a}}(e,r)||function(e,r){if(e){if("string"==typeof e)return u(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?u(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function c(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}var d={data:null,error:null,loading:!1,status:null,refreshError:null,retryError:null,isRefreshed:!1,retries:0,timestamp:null,isOnline:!0,isFocused:!0};function v(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function h(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?v(Object(t),!0).forEach((function(r){c(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):v(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}function f(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function p(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?f(Object(t),!0).forEach((function(r){c(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):f(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var g=(n,s)=>{var a=n.builder,i=n.cache,o=n.cacheKey,u=l(e(+new Date),2)[1],c=r(((e,r)=>{var t,n,s,a;return h(h({},d),{},{data:(null==r||null===(t=r.response)||void 0===t?void 0:t[0])||d.data,error:(null==r||null===(n=r.response)||void 0===n?void 0:n[1])||d.error,status:(null==r||null===(s=r.response)||void 0===s?void 0:s[2])||d.status,retries:(null==r?void 0:r.retries)||d.retries,timestamp:(a=(null==r?void 0:r.timestamp)||d.timestamp,a?new Date(a):null),isOnline:e.builder.appManager.isOnline,isFocused:e.builder.appManager.isFocused,loading:!!e.builder.fetchQueue.get(e.queueKey)||d.loading})})(n,s)),v=r([]),f=e=>{v.current.find((r=>e.includes(r)))&&u(+new Date)};t((()=>{var e=a.appManager.events.onFocus((()=>{c.current.isFocused=!0,f(["isFocused"])})),r=a.appManager.events.onBlur((()=>{c.current.isFocused=!1,f(["isFocused"])})),t=a.appManager.events.onOnline((()=>{c.current.isOnline=!0,f(["isOnline"])})),n=a.appManager.events.onOffline((()=>{c.current.isOnline=!1,f(["isOnline"])}));return()=>{e(),r(),t(),n()}}));var g={setCacheData:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r)a.cache.set(p({cache:i,cacheKey:o},e));else{var t={data:e.response[0],error:e.response[1],status:e.response[2],retries:e.retries,timestamp:new Date(e.timestamp),retryError:e.retryError,refreshError:e.refreshError,isRefreshed:e.isRefreshed,loading:!1};c.current=p(p({},c.current),t),f(Object.keys(t))}},setData:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[e,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed})}else c.current.data=e,f(["data"])},setError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,e,t.status],retries:t.retries,isRefreshed:t.isRefreshed})}else c.current.error=e,f(["error"])},setLoading:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];r?a.fetchQueue.events.setLoading(o,{isLoading:e,isRetry:!1,isRefreshed:!1,isRevalidated:!1}):(c.current.loading=e,f(["loading"]))},setStatus:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,t.error,e],retries:t.retries,isRefreshed:t.isRefreshed})}else c.current.status=e,f(["status"])},setRefreshed:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:e})}else c.current.isRefreshed=e,f(["isRefreshed"])},setRefreshError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,e,t.status],retries:t.retries,isRefreshed:!0})}else c.current.refreshError=e,f(["refreshError"])},setRetryError:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,e,t.status],retries:t.retries,isRefreshed:!0})}else c.current.retryError=e,f(["retryError"])},setRetries:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,t.error,t.status],retries:e,isRefreshed:t.isRefreshed})}else c.current.retries=e,f(["retries"])},setTimestamp:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(r){var t=c.current;a.cache.set({cache:i,cacheKey:o,response:[t.data,t.error,t.status],retries:t.retries,isRefreshed:t.isRefreshed,timestamp:e?+e:void 0})}else c.current.timestamp=e,f(["timestamp"])}};return[c.current,g,e=>{v.current.push(e)}]},m=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,t=r({time:e,timer:null});t.current.time=e;var s=()=>{t.current.timer&&clearTimeout(t.current.timer),t.current.timer=null},a=e=>{s(),t.current.timer=setTimeout((()=>{t.current.timer=null,e()}),t.current.time)};return n(s),{debounce:a,resetDebounce:s,active:!!t.current.timer}},b=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:600,t=r({time:e,timer:null});t.current.time=e;var s=()=>{t.current.timer&&clearInterval(t.current.timer),t.current.timer=null},a=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;s(),t.current.timer=setInterval((()=>{e()}),r<=0?t.current.time:r)};return n(s),{interval:a,resetInterval:s,active:!!t.current.timer}},y=(e,r,t)=>{if(r&&e&&t&&+e.timestamp+t>+new Date)return e},O=e=>e?{response:e,retries:0,timestamp:+new Date,isRefreshed:!1}:null,R=(e,r)=>!r||!!e&&+new Date>+r+e,D={dependencies:[],disabled:!1,dependencyTracking:!0,cacheOnMount:!0,revalidateOnMount:!0,initialData:null,refresh:!1,refreshTime:a.hour,refreshBlurred:!1,refreshOnTabBlur:!1,refreshOnTabFocus:!1,refreshOnReconnect:!1,cancelable:!1,debounce:!1,debounceTime:400,deepCompareFn:void 0,responseDataModifierFn:null,shouldThrow:!1},w=function(n){var a,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:D,c=u.dependencies,d=void 0===c?D.dependencies:c,v=u.disabled,h=void 0===v?D.disabled:v,f=u.dependencyTracking,p=void 0===f?D.dependencyTracking:f,w=u.revalidateOnMount,E=void 0===w?D.revalidateOnMount:w,T=u.initialData,S=void 0===T?D.initialData:T,F=u.refresh,P=void 0===F?D.refresh:F,j=u.refreshTime,M=void 0===j?D.refreshTime:j,q=u.refreshBlurred,K=void 0===q?D.refreshBlurred:q,k=u.refreshOnTabBlur,L=void 0===k?D.refreshOnTabBlur:k,B=u.refreshOnTabFocus,C=void 0===B?D.refreshOnTabFocus:B,I=u.refreshOnReconnect,A=void 0===I?D.refreshOnReconnect:I,Q=u.debounce,U=void 0===Q?D.debounce:Q,x=u.debounceTime,$=void 0===x?D.debounceTime:x,z=u.responseDataModifierFn,G=void 0===z?D.responseDataModifierFn:z,H=u.shouldThrow,J=void 0===H?D.shouldThrow:H,N=n.cacheTime,V=n.cacheKey,W=n.queueKey,X=n.builder,Y=m($),Z=b(M),_=X.cache,ee=X.fetchQueue,re=X.appManager,te=X.commandManager,ne=X.logger,se=r(y(_.get(V),n.cache,N)),ae=r(R(N,null===(a=se.current)||void 0===a?void 0:a.timestamp)),ie=r(ae.current?O(S):se.current),oe=g(n,ie.current),ue=l(oe,3),le=ue[0],ce=ue[1],de=ue[2],ve=e(!ae.current),he=l(ve,2),fe=he[0],pe=he[1],ge=r(null),me=r(null),be=r(null),ye=r(null),Oe=r(null),Re=r(null),De=r(null),we=r(null),Ee=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:le.isRefreshed,r=arguments.length>1&&void 0!==arguments[1]&&arguments[1],t=R(N,le.timestamp);h||!t&&fe&&!e&&!r?ne.debug("Cannot add to queue",{isRefreshed:e,isRevalidated:r,disabled:h,isStale:t,hasCacheData:fe}):(ne.debug("Adding request to queue",{isRefreshed:e,isRevalidated:r}),ee.add(n,{isRefreshed:e,isRevalidated:r}))},Te=()=>{Z.resetInterval();var e=le.timestamp,r=e?Math.max(+e+M-+new Date):0;P&&(ne.debug("Starting refresh counter, request will be send in ".concat(r,"ms")),Z.interval((()=>{var e=ee.get(W),r=!n.builder.appManager.isFocused&&K||n.builder.appManager.isFocused;!e&&r&&(ne.debug("Performing refresh request"),Ee(!0,!1),Z.resetInterval())}),r))},Se=e=>{if(e){var r,t,n;if(e[0])null==me||null===(t=me.current)||void 0===t||t.call(me,e[0]);if(e[1])if(null==be||null===(n=be.current)||void 0===n||n.call(be,e[1]),J)throw{message:"Fetching Error.",error:e[1]};null==ye||null===(r=ye.current)||void 0===r||r.call(ye,e)}},Fe=e=>{ce.setCacheData(e,!1),Se(e.response)},Pe=(e,r)=>{if(!fe){var t=_.get(V);t&&(ce.setCacheData(t,!1),pe(!0))}ce.setRefreshed(e,!1),ce.setTimestamp(new Date(r),!1),Se([le.data,le.error,le.status])},je=e=>{var r,t=e.isLoading,n=e.isRefreshed,s=e.isRetry,a=e.isRevalidated;ce.setLoading(t,!1),null===(r=ge.current)||void 0===r||r.call(ge,{isRefreshed:n,isRetry:s,isRevalidated:a})},Me=()=>{Ee(!1,!0)},qe=e=>{e&&"string"==typeof e?n.builder.cache.events.revalidate(e):e&&e instanceof i?n.builder.cache.events.revalidate(o(e)):Me()},Ke=e=>{var r;null==De||null===(r=De.current)||void 0===r||r.call(De,e)},ke=e=>{var r;null==we||null===(r=we.current)||void 0===r||r.call(we,e)},Le=e=>{var r;null==Oe||null===(r=Oe.current)||void 0===r||r.call(Oe,e)},Be=e=>{var r;null==Oe||null===(r=Oe.current)||void 0===r||r.call(Oe,e)},Ce=()=>{var e=re.events.onFocus((()=>C&&Ee(!0))),r=re.events.onBlur((()=>L&&Ee(!0))),t=re.events.onOnline((()=>A&&Ee(!0))),n=te.events.onDownloadProgress(W,Ke),s=te.events.onUploadProgress(W,ke),a=te.events.onRequestStart(W,Le),i=te.events.onResponseStart(W,Be),o=ee.events.getLoading(W,je),u=_.events.get(V,Fe),l=_.events.getEqualData(V,Pe),c=_.events.onRevalidate(V,Me);return()=>{e(),r(),t(),n(),s(),a(),i(),o(),u(),l(),c()}},Ie=()=>G&&le.data?G(le.data):le.data,Ae=()=>{p||Object.keys(le).forEach((e=>de(e)))};return t((()=>{var e;return Se(null===(e=ie.current)||void 0===e?void 0:e.response),Ae(),Ce()})),s((()=>{fe&&U?Y.debounce((()=>Ee(!1,E))):Ee(!1,E)}),[...d,h],!0),s((()=>{Te()}),[P,M,le.timestamp],!0),{get data(){return de("data"),Ie()},get error(){return de("error"),le.error},get loading(){return de("loading"),le.loading},get status(){return de("status"),le.status},get retryError(){return de("retryError"),le.retryError},get refreshError(){return de("refreshError"),le.refreshError},get isRefreshed(){return de("isRefreshed"),le.isRefreshed},get retries(){return de("retries"),le.retries},get timestamp(){return de("timestamp"),le.timestamp},get isOnline(){return de("isOnline"),le.isOnline},get isFocused(){return de("isFocused"),le.isFocused},get isStale(){return R(N,le.timestamp)},actions:ce,onRequest:e=>{ge.current=e},onSuccess:e=>{me.current=e},onError:e=>{be.current=e},onFinished:e=>{ye.current=e},onRequestStart:e=>{Oe.current=e},onResponseStart:e=>{Re.current=e},onDownloadProgress:e=>{De.current=e},onUploadProgress:e=>{we.current=e},isRefreshingError:!!le.error&&le.isRefreshed,isDebouncing:Y.active,refresh:qe}},E={disabled:!1,dependencyTracking:!0,cacheOnMount:!0,initialData:null,debounce:!1,debounceTime:400,suspense:!1,shouldThrow:!1,responseDataModifierFn:null,invalidate:[]},T=function(e){var n,s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:E,a=s.disabled,i=void 0===a?E.disabled:a,o=s.dependencyTracking,u=void 0===o?E.dependencyTracking:o,c=s.cacheOnMount,d=void 0===c?E.cacheOnMount:c,v=s.initialData,h=void 0===v?E.initialData:v,f=s.shouldThrow,p=void 0===f?E.shouldThrow:f,m=s.responseDataModifierFn,b=void 0===m?E.responseDataModifierFn:m,D=e.cacheTime,w=e.cacheKey,T=e.queueKey,S=e.builder,F=S.cache,P=S.submitQueue,j=S.commandManager,M=r(y(F.get(w),d,D)),q=r(R(D,null===(n=M.current)||void 0===n?void 0:n.timestamp)),K=r(q.current?O(h):M.current),k=g(e,K.current),L=l(k,3),B=L[0],C=L[1],I=L[2],A=r(null),Q=r(null),U=r(null),x=r(null),$=r(null),z=r(null),G=r(null),H=r(null),J=function(){var r=e,t=arguments.length<=0?void 0:arguments[0];null!=t&&t.data&&(r=r.setData(t.data)),null!=t&&t.params&&(r=r.setParams(t.params)),null!=t&&t.queryParams&&(r=r.setQueryParams(t.queryParams)),i||P.add(r)},N=e=>{if(e){var r,t,n;if(e[0])null==Q||null===(t=Q.current)||void 0===t||t.call(Q,e[0]);if(e[1])if(null==U||null===(n=U.current)||void 0===n||n.call(U,e[1]),p)throw{message:"Fetching Error.",error:e[1]};null==x||null===(r=x.current)||void 0===r||r.call(x,e)}},V=e=>{C.setCacheData(e,!1),N(e.response)},W=(e,r)=>{C.setRefreshed(e,!1),C.setTimestamp(new Date(r),!1),N([B.data,B.error,B.status])},X=e=>{var r,t=e.isLoading,n=e.isRetry;C.setLoading(t,!1),null===(r=A.current)||void 0===r||r.call(A,{isRetry:n})},Y=e=>{var r;null==G||null===(r=G.current)||void 0===r||r.call(G,e)},Z=e=>{var r;null==H||null===(r=H.current)||void 0===r||r.call(H,e)},_=e=>{var r;null==$||null===(r=$.current)||void 0===r||r.call($,e)},ee=e=>{var r;null==$||null===(r=$.current)||void 0===r||r.call($,e)},re=()=>{var e=j.events.onDownloadProgress(T,Y),r=j.events.onUploadProgress(T,Z),t=j.events.onRequestStart(T,_),n=j.events.onResponseStart(T,ee),s=P.events.getLoading(T,X),a=F.events.get(T,V),i=F.events.getEqualData(w,W);return()=>{e(),r(),t(),n(),s(),a(),i()}},te=()=>b&&B.data?b(B.data):B.data,ne=()=>{u||Object.keys(B).forEach((e=>I(e)))};return t((()=>(ne(),re()))),{submit:J,get data(){return I("data"),te()},get error(){return I("error"),B.error},get submitting(){return I("loading"),B.loading},get status(){return I("status"),B.status},get retryError(){return I("retryError"),B.retryError},get retries(){return I("retries"),B.retries},get timestamp(){return I("timestamp"),B.timestamp},get isOnline(){return I("isOnline"),B.isOnline},get isFocused(){return I("isFocused"),B.isFocused},get isStale(){return R(D,B.timestamp)},actions:C,isDebouncing:!1,isRefreshed:!1,onSubmitRequest:e=>{A.current=e},onSubmitSuccess:e=>{Q.current=e},onSubmitError:e=>{U.current=e},onSubmitFinished:e=>{x.current=e},onSubmitRequestStart:e=>{$.current=e},onSubmitResponseStart:e=>{z.current=e},onSubmitDownloadProgress:e=>{G.current=e},onSubmitUploadProgress:e=>{H.current=e}}};export{w as useFetch,T as useSubmit};
//# sourceMappingURL=index.esm.js.map

@@ -83,9 +83,6 @@ import { useRef, useState } from "react";

if (!disabled && (isStale || !hasCacheData || isRefreshed || isRevalidated)) {
logger.debug(`[useFetch] Adding request to queue`, Object.entries({ isRefreshed, isRevalidated }));
logger.debug(`Adding request to queue`, { isRefreshed, isRevalidated });
fetchQueue.add(command, { isRefreshed, isRevalidated });
} else {
logger.debug(
`[useFetch] Cannot add to queue`,
Object.entries({ isRefreshed, isRevalidated, disabled, isStale, hasCacheData }),
);
logger.debug(`Cannot add to queue`, { isRefreshed, isRevalidated, disabled, isStale, hasCacheData });
}

@@ -102,3 +99,3 @@ };

if (refresh) {
logger.debug(`[useFetch] Starting refresh counter, request will be send in ${timeLeft}ms`);
logger.debug(`Starting refresh counter, request will be send in ${timeLeft}ms`);
refreshInterval.interval(() => {

@@ -113,3 +110,3 @@ const currentRequest = fetchQueue.get(queueKey);

if (!currentRequest && canRefresh) {
logger.debug(`[useFetch] Performing refresh request`);
logger.debug(`Performing refresh request`);
handleFetch(true, false);

@@ -116,0 +113,0 @@ refreshInterval.resetInterval();

{
"name": "@better-typed/react-hyper-fetch",
"version": "0.0.11",
"version": "0.0.12",
"private": false,

@@ -5,0 +5,0 @@ "description": "React hooks and utils for the hyper-fetch",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc