@cytools/vue-query
Version:
Introducing elegant way of making api requests and asynchronous functions with the composition api in Vue
16 lines (13 loc) • 15.5 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vue-demi"),require("lodash")):"function"==typeof define&&define.amd?define(["exports","vue-demi","lodash"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).VueQuery={},e["vue-demi"],e.lodash)}(this,(function(e,t,r){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */var n=function(e,t){return(n=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 u,a=function(){return(a=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 o(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 i(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 c(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 l(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 s(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(l(arguments[t]));return e}!function(e){e.IDLE="idle",e.ERROR="error",e.LOADING="loading",e.SUCCESS="success"}(u||(u={})),e.queryDataClone=r.cloneDeep;var v=function(){function r(r){var n=void 0===r?{}:r,a=n.data,o=void 0===a?null:a,i=n.error,c=void 0===i?null:i,l=n.isFetching,s=void 0!==l&&l,v=n.status,f=void 0===v?u.IDLE:v;this.queryData=t.reactive({value:{data:e.queryDataClone(o),error:c,status:f,isFetching:s}})}return r.prototype.update=function(t){return this.queryData.value=a(a(a({},this.queryData.value),t),{data:e.queryDataClone(t.data||this.queryData.value.data)}),this},r.prototype.updateData=function(e){return this.update({data:e(this.data)})},Object.defineProperty(r.prototype,"data",{get:function(){return this.queryData.value.data},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"status",{get:function(){return this.queryData.value.status},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"error",{get:function(){return this.queryData.value.error},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"isIdle",{get:function(){return this.queryData.value.status===u.IDLE},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"isError",{get:function(){return this.queryData.value.status===u.ERROR},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"isLoading",{get:function(){return this.queryData.value.status===u.LOADING},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"isSuccess",{get:function(){return this.queryData.value.status===u.SUCCESS||this.queryData.value.data},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"isFetching",{get:function(){return this.queryData.value.isFetching},enumerable:!1,configurable:!0}),r}(),f=function(){function e(e){this.cache=e.cache}return e.prototype.addQuery=function(e,t){e=this.convertKey(e);var r=this.getQuery(e);return r.value?r:(this.cache.put(e,new v(t)),this.getQuery(e))},e.prototype.removeQuery=function(e){return this.cache.remove(this.convertKey(e)),this},e.prototype.getQuery=function(e){return this.cache.get(this.convertKey(e))},e.prototype.getQueriesWithStartingKey=function(e){var r=this,n=this.convertKey(e);return t.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}))}))},e.prototype.updateQueryDataForQueriesWithStartingKey=function(e,t){this.getQueriesWithStartingKey(e).value.forEach((function(e){return e.query.updateData(t)}))},e.prototype.reset=function(){return this.cache.clear()},e.prototype.convertKey=function(e){return Array.isArray(e)?JSON.stringify(e):e},e}(),d=function(e){function r(r){void 0===r&&(r={});var n=e.call(this)||this;return n.cache=t.reactive({value:r}),n}return function(e,t){function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}(r,e),r.prototype.put=function(e,t){return this.updateCache(e,t),this},r.prototype.get=function(e){var r=this;return t.computed((function(){return r.cache.value[e]||null}))},r.prototype.remove=function(e){return this.put(e,null),this},r.prototype.count=function(){var e=this;return Object.keys(this.cache.value).reduce((function(t,r){return e.cache.value[r]?++t:t}),0)},r.prototype.clear=function(){return this.cache.value={},this},r.prototype.getCacheKeys=function(){return Object.keys(this.cache.value)},r.prototype.updateCache=function(e,t){var r;this.cache.value=a(a({},this.cache.value),((r={})[e]=t,r))},r}((function(){})),h=null;function p(){return{queryClient:h=h||new f({cache:new d})}}var y,b=function(e,t){var r,n;try{for(var u=c(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 g(e){var n=e.key,u=void 0===n?"":n,a=e.waitTime,o=void 0===a?500:a,i=e.keysNotToWait,s=void 0===i?[]:i,v=e.keysNotToWatch,f=void 0===v?[]:v,d=e.callback,h=void 0===d?function(){}:d;if(!u)throw new Error("No key has been provided!");var p=[],y=[],g=function(){var e=t.getCurrentInstance();return r.get(e,"proxy",e)}(),m=function(e){var n,u,a,i,v,d;if(Array.isArray(e)){var g=[],w=[],D=function(e){return function(t){y[e]=t,h()}};try{for(var O=c(e),S=O.next();!S.done;S=O.next()){var E=S.value;if(t.isRef(E))w.push({index:y.push(E.value)-1,reactiveValue:E});else if(r.isObject(E))try{for(var C=(a=void 0,c(Object.entries(E))),R=C.next();!R.done;R=C.next()){var P=l(R.value,2),x=P[0],k=P[1];f.includes(x)||(t.isRef(k)?(g.push(x),w.push({index:y.push(k.value)-1,reactiveValue:k})):m(k))}}catch(e){a={error:e}}finally{try{R&&!R.done&&(i=C.return)&&i.call(C)}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=c(w),j=q.next();!j.done;j=q.next()){var I=j.value;o>0&&!b(s,g)?p.push(t.watch(I.reactiveValue,r.debounce(D(I.index),o))):p.push(t.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&&t.onUnmounted((function(){p.forEach((function(e){return e()})),p=[],y=[]})),{variables:y}}function m(e,r,n){var a=this;void 0===r&&(r=null);var c=void 0===n?{}:n,l=c.manual,v=void 0!==l&&l,f=c.onError,d=void 0===f?function(){}:f,h=c.keysNotToWait,y=void 0===h?[]:h,b=c.defaultData,m=void 0===b?null:b,w=c.onSuccess,D=void 0===w?function(){}:w,O=c.keepPreviousData,S=void 0!==O&&O,E=c.timesToRetryOnError,C=void 0===E?3:E,R=c.onDataReceive,P=void 0===R?function(){}:R,x=c.keyChangeRefetchWaitTime,k=void 0===x?500:x,q=c.timeToWaitBeforeRetryingOnError,j=void 0===q?2e3:q,I=!1,T=t.ref(null),L=p().queryClient,N=t.reactive({value:{}}),Q=function(t,n){return void 0===t&&(t=[]),void 0===n&&(n=!0),o(a,void 0,void 0,(function(){var u,a,o;return i(this,(function(i){switch(i.label){case 0:return N.value=L.addQuery(e,{data:m}),!(null===(u=N.value)||void 0===u?void 0:u.isIdle)&&(null===(a=N.value)||void 0===a?void 0:a.data)||!r?[3,3]:n?[4,W(t)]:[3,2];case 1:i.sent(),i.label=2;case 2:return[3,4];case 3:P(null===(o=N.value)||void 0===o?void 0:o.data),I=!0,i.label=4;case 4:return[2,N.value.data]}}))}))},A=function(){return Q(F,!v)},F=g({key:e,keysNotToWait:y,callback:A,waitTime:k}).variables,K=function(e,t){return void 0===e&&(e=[]),void 0===t&&(t=0),o(a,void 0,void 0,(function(){var n,a,o,c,l,v,f,h;return i(this,(function(i){switch(i.label){case 0:if(!r)return[2];if((null===(h=N.value)||void 0===h?void 0:h.isFetching)&&0===t)return[2];if(n=N.value,e=e.length?e:F,n.update({isFetching:!0}),!((a=r.apply(void 0,s(e)))instanceof Promise))return o=new Error("The provided callback doesn't return a promise!"),n.update({error:o,status:u.ERROR}),d(o),[2];i.label=1;case 1:return i.trys.push([1,3,5,6]),l=(c=n).update,f={},[4,a];case 2:return l.apply(c,[(f.data=i.sent(),f.status=u.SUCCESS,f)]),D(n.data),P(n.data),[3,6];case 3:return v=i.sent(),t>=C?(n.update({error:v,status:u.ERROR}),d(v),[2]):[4,(p=j,y=null,b=new Promise((function(e){y=setTimeout(e,p)})),b.cancel=function(){return y&&clearTimeout(y)},b)];case 4:return i.sent(),K(e,++t),[3,6];case 5:return n.update({isFetching:!1}),[7];case 6:return[2]}var p,y,b}))}))},W=function(e){return void 0===e&&(e=[]),o(a,void 0,void 0,(function(){var t,r;return i(this,(function(n){switch(n.label){case 0:return S&&I||null===(r=N.value)||void 0===r||r.update({status:u.LOADING}),[4,K(e)];case 1:return t=n.sent(),I=!0,[2,t]}}))}))};return A(),t.watch(N,(function(e){var t,r;(null===(t=null==e?void 0:e.value)||void 0===t?void 0:t.isSuccess)&&(T.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=N.value)||void 0===t?void 0:t.updateData(e)},data:t.computed((function(){return T.value})),status:t.computed((function(){var e;return null===(e=N.value)||void 0===e?void 0:e.status})),error:t.computed((function(){var e;return null===(e=N.value)||void 0===e?void 0:e.error})),isIdle:t.computed((function(){var e;return null===(e=N.value)||void 0===e?void 0:e.isIdle})),isError:t.computed((function(){var e;return null===(e=N.value)||void 0===e?void 0:e.isError})),isLoading:t.computed((function(){var e;return null===(e=N.value)||void 0===e?void 0:e.isLoading})),isSuccess:t.computed((function(){var e;return null===(e=N.value)||void 0===e?void 0:e.isSuccess})),isFetching:t.computed((function(){var e;return null===(e=N.value)||void 0===e?void 0:e.isFetching}))}}!function(e){e.IDLE="idle",e.ERROR="error",e.LOADING="loading",e.SUCCESS="success"}(y||(y={})),e.changeQueryDataCloneMethod=function(t){return e.queryDataClone=t},e.useMutation=function(e,r){var n=this,u=void 0===r?{}:r,c=u.onError,l=void 0===c?function(){return null}:c,v=u.onSuccess,f=void 0===v?function(){return null}:v,d=u.onMutate,h=void 0===d?function(){return o(n,void 0,void 0,(function(){return i(this,(function(e){return[2,null]}))}))}:d,p=t.ref(null),b=t.ref(null),g=t.ref(y.IDLE),m=t.ref(null),w=function(){return{data:t.computed((function(){return p.value})),error:t.computed((function(){return b.value})),isIdle:t.computed((function(){return g.value===y.IDLE})),isError:t.computed((function(){return g.value===y.ERROR})),isSuccess:t.computed((function(){return g.value===y.SUCCESS})),isLoading:t.computed((function(){return g.value===y.LOADING}))}};return a({mutate:function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return o(n,void 0,void 0,(function(){var r,n,u,a;return i(this,(function(o){switch(o.label){case 0:if(g.value===y.LOADING)return[2];b.value=null,g.value=y.LOADING,o.label=1;case 1:return o.trys.push([1,4,,5]),r=m,[4,h.apply(void 0,s(t))];case 2:if(r.value=o.sent(),m.value&&(p.value=m.value),!((n=e.apply(void 0,s(t)))instanceof Promise))throw new Error("The provided callback doesn't return a promise!");return u=p,[4,n];case 3:return u.value=o.sent(),g.value=y.SUCCESS,f(p.value,t,m.value),[3,5];case 4:return a=o.sent(),b.value=a,g.value=y.ERROR,l(a,t,m.value),[3,5];case 5:return[2,w()]}}))}))},status:t.computed((function(){return g.value}))},w())},e.usePaginateQuery=function(r,n,u){var c=this;void 0===n&&(n=null),void 0===u&&(u={});var l=t.ref(1),v=t.ref(u.defaultData||null),f=t.ref({1:!1}),d=function(){return p.fetchFromCacheOrRefetch(s([l.value],h))},h=g({key:r=Array.isArray(r)?s(r.slice(0,1),[{page:l}],r.slice(1)):[r,{page:l}],callback:function(){return o(c,void 0,void 0,(function(){return i(this,(function(e){switch(e.label){case 0:return l.value=1,[4,d()];case 1:return e.sent(),[2]}}))}))},keysNotToWatch:["page"]}).variables,p=m(r,n,a(a({},u),{manual:!0,keepPreviousData:!0})),y=t.computed((function(){return f.value[l.value]})),b=function(t){var r,n=void 0===t?{}:t,u=n.data,o=void 0===u?null:u,i=n.hasNextPage,c=void 0===i||i;o&&(v.value=e.queryDataClone(o)),f.value=a(a({},f.value),((r={})[l.value]=p.isSuccess.value&&c,r))};return d(),b(p.data.value||{}),t.watch(p.data,b),a(a({},p),{fetchPrevPage:function(){return o(c,void 0,void 0,(function(){return i(this,(function(e){switch(e.label){case 0:return p.isFetching.value||l.value<=1?[2]:(l.value=l.value-1,[4,d()]);case 1:return e.sent(),[2]}}))}))},fetchNextPage:function(){return o(c,void 0,void 0,(function(){return i(this,(function(e){switch(e.label){case 0:return p.isFetching.value||!f.value[l.value]?[2]:(l.value=l.value+1,[4,d()]);case 1:return e.sent(),[2]}}))}))},hasMorePages:y,data:t.computed((function(){return v.value})),currentPage:t.computed((function(){return l.value})),isNextButtonActive:t.computed((function(){return y.value})),isPrevButtonActive:t.computed((function(){return l.value>1})),isSuccess:t.computed((function(){return p.isSuccess||v.value})),canShowPaginationButtons:t.computed((function(){return f.value[1]}))})},e.useQuery=m,e.useQueryClient=p,Object.defineProperty(e,"__esModule",{value:!0})}));