@cytools/vue-query
Version:
Introducing elegant way of making api requests and asynchronous functions with the composition api in Vue
2 lines (1 loc) • 14.4 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("vue-demi"),t=require("lodash"),r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])})(e,t)};var n,u=function(){return(u=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var u in t=arguments[r])Object.prototype.hasOwnProperty.call(t,u)&&(e[u]=t[u]);return e}).apply(this,arguments)};function a(e,t,r,n){return new(r||(r=Promise))((function(u,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function i(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?u(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,i)}c((n=n.apply(e,t||[])).next())}))}function o(e,t){var r,n,u,a,o={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(a){return function(i){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;o;)try{if(r=1,n&&(u=2&a[0]?n.return:a[0]?n.throw||((u=n.return)&&u.call(n),0):n.next)&&!(u=u.call(n,a[1])).done)return u;switch(n=0,u&&(a=[2&a[0],u.value]),a[0]){case 0:case 1:u=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,n=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(u=o.trys,(u=u.length>0&&u[u.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!u||a[1]>u[0]&&a[1]<u[3])){o.label=a[1];break}if(6===a[0]&&o.label<u[1]){o.label=u[1],u=a;break}if(u&&o.label<u[2]){o.label=u[2],o.ops.push(a);break}u[2]&&o.ops.pop(),o.trys.pop();continue}a=t.call(e,o)}catch(e){a=[6,e],n=0}finally{r=u=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,i])}}}function i(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function c(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,u,a=r.call(e),o=[];try{for(;(void 0===t||t-- >0)&&!(n=a.next()).done;)o.push(n.value)}catch(e){u={error:e}}finally{try{n&&!n.done&&(r=a.return)&&r.call(a)}finally{if(u)throw u.error}}return o}function l(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(c(arguments[t]));return e}!function(e){e.IDLE="idle",e.ERROR="error",e.LOADING="loading",e.SUCCESS="success"}(n||(n={})),exports.queryDataClone=t.cloneDeep;var s=function(){function t(t){var r=void 0===t?{}:t,u=r.data,a=void 0===u?null:u,o=r.error,i=void 0===o?null:o,c=r.isFetching,l=void 0!==c&&c,s=r.status,v=void 0===s?n.IDLE:s;this.queryData=e.reactive({value:{data:exports.queryDataClone(a),error:i,status:v,isFetching:l}})}return t.prototype.update=function(e){return this.queryData.value=u(u(u({},this.queryData.value),e),{data:exports.queryDataClone(e.data||this.queryData.value.data)}),this},t.prototype.updateData=function(e){return this.update({data:e(this.data)})},Object.defineProperty(t.prototype,"data",{get:function(){return this.queryData.value.data},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"status",{get:function(){return this.queryData.value.status},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"error",{get:function(){return this.queryData.value.error},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isIdle",{get:function(){return this.queryData.value.status===n.IDLE},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isError",{get:function(){return this.queryData.value.status===n.ERROR},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isLoading",{get:function(){return this.queryData.value.status===n.LOADING},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isSuccess",{get:function(){return this.queryData.value.status===n.SUCCESS||this.queryData.value.data},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isFetching",{get:function(){return this.queryData.value.isFetching},enumerable:!1,configurable:!0}),t}(),v=function(){function t(e){this.cache=e.cache}return t.prototype.addQuery=function(e,t){e=this.convertKey(e);var r=this.getQuery(e);return r.value?r:(this.cache.put(e,new s(t)),this.getQuery(e))},t.prototype.removeQuery=function(e){return this.cache.remove(this.convertKey(e)),this},t.prototype.getQuery=function(e){return this.cache.get(this.convertKey(e))},t.prototype.getQueriesWithStartingKey=function(t){var r=this,n=this.convertKey(t);return e.computed((function(){return r.cache.getCacheKeys().map((function(e){return e.includes(n)?{key:e,query:r.getQuery(e).value}:null})).filter((function(e){return e}))}))},t.prototype.updateQueryDataForQueriesWithStartingKey=function(e,t){this.getQueriesWithStartingKey(e).value.forEach((function(e){return e.query.updateData(t)}))},t.prototype.reset=function(){return this.cache.clear()},t.prototype.convertKey=function(e){return Array.isArray(e)?JSON.stringify(e):e},t}(),f=function(t){function n(r){void 0===r&&(r={});var n=t.call(this)||this;return n.cache=e.reactive({value:r}),n}return function(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}(n,t),n.prototype.put=function(e,t){return this.updateCache(e,t),this},n.prototype.get=function(t){var r=this;return e.computed((function(){return r.cache.value[t]||null}))},n.prototype.remove=function(e){return this.put(e,null),this},n.prototype.count=function(){var e=this;return Object.keys(this.cache.value).reduce((function(t,r){return e.cache.value[r]?++t:t}),0)},n.prototype.clear=function(){return this.cache.value={},this},n.prototype.getCacheKeys=function(){return Object.keys(this.cache.value)},n.prototype.updateCache=function(e,t){var r;this.cache.value=u(u({},this.cache.value),((r={})[e]=t,r))},n}((function(){})),d=null;function p(){return{queryClient:d=d||new v({cache:new f})}}var h,y=function(e,t){var r,n;try{for(var u=i(t),a=u.next();!a.done;a=u.next()){var o=a.value;if(e.includes(o))return!0}}catch(e){r={error:e}}finally{try{a&&!a.done&&(n=u.return)&&n.call(u)}finally{if(r)throw r.error}}return!1};function b(r){var n=r.key,u=void 0===n?"":n,a=r.waitTime,o=void 0===a?500:a,l=r.keysNotToWait,s=void 0===l?[]:l,v=r.keysNotToWatch,f=void 0===v?[]:v,d=r.callback,p=void 0===d?function(){}:d;if(!u)throw new Error("No key has been provided!");var h=[],b=[],g=function(){var r=e.getCurrentInstance();return t.get(r,"proxy",r)}(),m=function(r){var n,u,a,l,v,d;if(Array.isArray(r)){var g=[],w=[],D=function(e){return function(t){b[e]=t,p()}};try{for(var O=i(r),S=O.next();!S.done;S=O.next()){var x=S.value;if(e.isRef(x))w.push({index:b.push(x.value)-1,reactiveValue:x});else if(t.isObject(x))try{for(var E=(a=void 0,i(Object.entries(x))),C=E.next();!C.done;C=E.next()){var R=c(C.value,2),P=R[0],k=R[1];f.includes(P)||(e.isRef(k)?(g.push(P),w.push({index:b.push(k.value)-1,reactiveValue:k})):m(k))}}catch(e){a={error:e}}finally{try{C&&!C.done&&(l=E.return)&&l.call(E)}finally{if(a)throw a.error}}}}catch(e){n={error:e}}finally{try{S&&!S.done&&(u=O.return)&&u.call(O)}finally{if(n)throw n.error}}try{for(var q=i(w),j=q.next();!j.done;j=q.next()){var I=j.value;o>0&&!y(s,g)?h.push(e.watch(I.reactiveValue,t.debounce(D(I.index),o))):h.push(e.watch(I.reactiveValue,D(I.index)))}}catch(e){v={error:e}}finally{try{j&&!j.done&&(d=q.return)&&d.call(q)}finally{if(v)throw v.error}}}};return m(u),g&&e.onUnmounted((function(){h.forEach((function(e){return e()})),h=[],b=[]})),{variables:b}}function g(t,r,u){var i=this;void 0===r&&(r=null);var c=void 0===u?{}:u,s=c.manual,v=void 0!==s&&s,f=c.onError,d=void 0===f?function(){}:f,h=c.keysNotToWait,y=void 0===h?[]:h,g=c.defaultData,m=void 0===g?null:g,w=c.onSuccess,D=void 0===w?function(){}:w,O=c.keepPreviousData,S=void 0!==O&&O,x=c.timesToRetryOnError,E=void 0===x?3:x,C=c.onDataReceive,R=void 0===C?function(){}:C,P=c.keyChangeRefetchWaitTime,k=void 0===P?500:P,q=c.timeToWaitBeforeRetryingOnError,j=void 0===q?2e3:q,I=!1,L=e.ref(null),N=p().queryClient,A=e.reactive({value:{}}),Q=function(e,n){return void 0===e&&(e=[]),void 0===n&&(n=!0),a(i,void 0,void 0,(function(){var u,a,i;return o(this,(function(o){switch(o.label){case 0:return A.value=N.addQuery(t,{data:m}),!(null===(u=A.value)||void 0===u?void 0:u.isIdle)&&(null===(a=A.value)||void 0===a?void 0:a.data)||!r?[3,3]:n?[4,W(e)]:[3,2];case 1:o.sent(),o.label=2;case 2:return[3,4];case 3:R(null===(i=A.value)||void 0===i?void 0:i.data),I=!0,o.label=4;case 4:return[2,A.value.data]}}))}))},T=function(){return Q(F,!v)},F=b({key:t,keysNotToWait:y,callback:T,waitTime:k}).variables,K=function(e,t){return void 0===e&&(e=[]),void 0===t&&(t=0),a(i,void 0,void 0,(function(){var u,a,i,c,s,v,f,p;return o(this,(function(o){switch(o.label){case 0:if(!r)return[2];if((null===(p=A.value)||void 0===p?void 0:p.isFetching)&&0===t)return[2];if(u=A.value,e=e.length?e:F,u.update({isFetching:!0}),!((a=r.apply(void 0,l(e)))instanceof Promise))return i=new Error("The provided callback doesn't return a promise!"),u.update({error:i,status:n.ERROR}),d(i),[2];o.label=1;case 1:return o.trys.push([1,3,5,6]),s=(c=u).update,f={},[4,a];case 2:return s.apply(c,[(f.data=o.sent(),f.status=n.SUCCESS,f)]),D(u.data),R(u.data),[3,6];case 3:return v=o.sent(),t>=E?(u.update({error:v,status:n.ERROR}),d(v),[2]):[4,(h=j,y=null,b=new Promise((function(e){y=setTimeout(e,h)})),b.cancel=function(){return y&&clearTimeout(y)},b)];case 4:return o.sent(),K(e,++t),[3,6];case 5:return u.update({isFetching:!1}),[7];case 6:return[2]}var h,y,b}))}))},W=function(e){return void 0===e&&(e=[]),a(i,void 0,void 0,(function(){var t,r;return o(this,(function(u){switch(u.label){case 0:return S&&I||null===(r=A.value)||void 0===r||r.update({status:n.LOADING}),[4,K(e)];case 1:return t=u.sent(),I=!0,[2,t]}}))}))};return T(),e.watch(A,(function(e){var t,r;(null===(t=null==e?void 0:e.value)||void 0===t?void 0:t.isSuccess)&&(L.value=null===(r=null==e?void 0:e.value)||void 0===r?void 0:r.data)}),{immediate:!0}),{refetch:W,fetchFromCacheOrRefetch:Q,updateQueryData:function(e){var t;return null===(t=A.value)||void 0===t?void 0:t.updateData(e)},data:e.computed((function(){return L.value})),status:e.computed((function(){var e;return null===(e=A.value)||void 0===e?void 0:e.status})),error:e.computed((function(){var e;return null===(e=A.value)||void 0===e?void 0:e.error})),isIdle:e.computed((function(){var e;return null===(e=A.value)||void 0===e?void 0:e.isIdle})),isError:e.computed((function(){var e;return null===(e=A.value)||void 0===e?void 0:e.isError})),isLoading:e.computed((function(){var e;return null===(e=A.value)||void 0===e?void 0:e.isLoading})),isSuccess:e.computed((function(){var e;return null===(e=A.value)||void 0===e?void 0:e.isSuccess})),isFetching:e.computed((function(){var e;return null===(e=A.value)||void 0===e?void 0:e.isFetching}))}}!function(e){e.IDLE="idle",e.ERROR="error",e.LOADING="loading",e.SUCCESS="success"}(h||(h={})),exports.changeQueryDataCloneMethod=function(e){return exports.queryDataClone=e},exports.useMutation=function(t,r){var n=this,i=void 0===r?{}:r,c=i.onError,s=void 0===c?function(){return null}:c,v=i.onSuccess,f=void 0===v?function(){return null}:v,d=i.onMutate,p=void 0===d?function(){return a(n,void 0,void 0,(function(){return o(this,(function(e){return[2,null]}))}))}:d,y=e.ref(null),b=e.ref(null),g=e.ref(h.IDLE),m=e.ref(null),w=function(){return{data:e.computed((function(){return y.value})),error:e.computed((function(){return b.value})),isIdle:e.computed((function(){return g.value===h.IDLE})),isError:e.computed((function(){return g.value===h.ERROR})),isSuccess:e.computed((function(){return g.value===h.SUCCESS})),isLoading:e.computed((function(){return g.value===h.LOADING}))}};return u({mutate:function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return a(n,void 0,void 0,(function(){var r,n,u,a;return o(this,(function(o){switch(o.label){case 0:if(g.value===h.LOADING)return[2];b.value=null,g.value=h.LOADING,o.label=1;case 1:return o.trys.push([1,4,,5]),r=m,[4,p.apply(void 0,l(e))];case 2:if(r.value=o.sent(),m.value&&(y.value=m.value),!((n=t.apply(void 0,l(e)))instanceof Promise))throw new Error("The provided callback doesn't return a promise!");return u=y,[4,n];case 3:return u.value=o.sent(),g.value=h.SUCCESS,f(y.value,e,m.value),[3,5];case 4:return a=o.sent(),b.value=a,g.value=h.ERROR,s(a,e,m.value),[3,5];case 5:return[2,w()]}}))}))},status:e.computed((function(){return g.value}))},w())},exports.usePaginateQuery=function(t,r,n){var i=this;void 0===r&&(r=null),void 0===n&&(n={});var c=e.ref(1),s=e.ref(n.defaultData||null),v=e.ref({1:!1}),f=function(){return p.fetchFromCacheOrRefetch(l([c.value],d))},d=b({key:t=Array.isArray(t)?l(t.slice(0,1),[{page:c}],t.slice(1)):[t,{page:c}],callback:function(){return a(i,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return c.value=1,[4,f()];case 1:return e.sent(),[2]}}))}))},keysNotToWatch:["page"]}).variables,p=g(t,r,u(u({},n),{manual:!0,keepPreviousData:!0})),h=e.computed((function(){return v.value[c.value]})),y=function(e){var t,r=void 0===e?{}:e,n=r.data,a=void 0===n?null:n,o=r.hasNextPage,i=void 0===o||o;a&&(s.value=exports.queryDataClone(a)),v.value=u(u({},v.value),((t={})[c.value]=p.isSuccess.value&&i,t))};return f(),y(p.data.value||{}),e.watch(p.data,y),u(u({},p),{fetchPrevPage:function(){return a(i,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return p.isFetching.value||c.value<=1?[2]:(c.value=c.value-1,[4,f()]);case 1:return e.sent(),[2]}}))}))},fetchNextPage:function(){return a(i,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return p.isFetching.value||!v.value[c.value]?[2]:(c.value=c.value+1,[4,f()]);case 1:return e.sent(),[2]}}))}))},hasMorePages:h,data:e.computed((function(){return s.value})),currentPage:e.computed((function(){return c.value})),isNextButtonActive:e.computed((function(){return h.value})),isPrevButtonActive:e.computed((function(){return c.value>1})),isSuccess:e.computed((function(){return p.isSuccess||s.value})),canShowPaginationButtons:e.computed((function(){return v.value[1]}))})},exports.useQuery=g,exports.useQueryClient=p;