UNPKG

vuex-xhr-state

Version:

Use Vuex to manage the state of you're ajax calls.

1 lines 15.1 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("vue-class-component"),require("vuex"),require("js-md5")):"function"==typeof define&&define.amd?define(["vue-class-component","vuex","js-md5"],e):"object"==typeof exports?exports["uw-vue-helpers"]=e(require("vue-class-component"),require("vuex"),require("js-md5")):t["uw-vue-helpers"]=e(t["vue-class-component"],t.vuex,t["js-md5"])}(window,function(t,e,n){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=3)}([function(e,n){e.exports=t},function(t,n){t.exports=e},function(t,e){t.exports=n},function(t,e,n){"use strict";var r,o,i;n.r(e);var a="globalXhrState",s="ADD_PENDING",c="END_PENDING",u="SET_ERROR",f={REQUEST:"REQUEST",RECEIVED:"END_PENDING",FAILED:"SET_ERROR",RESET_ERROR:"RESET_ERROR"},h={ANY_PENDING:"ANY_PENDING",ANY_ERROR:"ANY_ERROR",LAST_ERROR_RESPONSE:"LAST_ERROR_RESPONSE"},E=f,d=function(t){return t.filter(function(t,e,n){return e===n.indexOf(t)})},p={namespaced:!0,state:{activeLoaders:[],last_error:{}},getters:(r={},r[h.ANY_PENDING]=function(t){return t.activeLoaders.length>0},r[h.LAST_ERROR_RESPONSE]=function(t){return t.last_error},r),actions:(o={},o[E.REQUEST]=function(t,e){return(0,t.commit)(s,e)},o[E.RECEIVED]=function(t,e){return(0,t.commit)(c,e)},o[E.FAILED]=function(t,e){var n=t.commit;e.catched||n(u,e),n(c,e.key)},o),mutations:(i={},i[s]=function(t,e){t.activeLoaders.push(e),t.activeLoaders=d(t.activeLoaders)},i[c]=function(t,e){t.activeLoaders=d(t.activeLoaders).filter(function(t){return t!==e})},i[u]=function(t,e){t.last_error=e.response},i)},l=n(0),m=n(1),v=(R("computed",m.mapState),R("computed",m.mapGetters),R("methods",m.mapActions));R("methods",m.mapMutations);function R(t,e){function n(n,r){return Object(l.createDecorator)(function(o,i){o[t]||(o[t]={});var a,s=((a={})[i]=n,a);o[t][i]=void 0!==r?e(r,s)[i]:e(s)[i]})}return function(t,e){if("string"==typeof e){var r=e,o=t;return n(r,void 0)(o,r)}return n(t,function(t){var e=t&&t.namespace;if("string"==typeof e)return"/"!==e[e.length-1]?e+"/":e}(e))}}var _={REQUEST:"_xhr_request",RECEIVED:"_xhr_received",FAILED:"_xhr_failed",RESET:"_xhr_reset",INVALIDATE:"_xhr_invalidate"},O={PENDING:"_xhr_pending",HAS_ERROR:"_xhr_has_error",FETCHED:"_xhr_fetched",DATA:"_xhr_data",RESPONSE:"_xhr_response",PAYLOAD_KEYS:"_xhr_payload_keys"},y={SEND:"_xhr_send",FETCH:"_xhr_fetch",FORCE_FETCH:"_xhr_forceFetch",RESET:"_xhr_reset",INVALIDATE:"_xhr_invalidate",INVALIDATE_ALL:"_xhr_invalidate_all"};function N(t){return v(y.FETCH,{namespace:t.namespace})}function S(t){return v(y.SEND,{namespace:t.namespace})}function A(t){return v(y.FORCE_FETCH,{namespace:t.namespace})}function D(t){return v(y.RESET,{namespace:t.namespace})}function b(t){return v(y.INVALIDATE,{namespace:t.namespace})}function T(t){return v(y.INVALIDATE_ALL,{namespace:t.namespace})}function P(t){return Object(l.createDecorator)(function(e,n){e.methods||(e.methods={}),e.methods[n]=function(e){return t.data(this.$store.getters,e)}})}function g(t){return Object(l.createDecorator)(function(e,n){e.methods||(e.methods={}),e.methods[n]=function(e){return t.pending(this.$store.getters,e)}})}function I(t){return Object(l.createDecorator)(function(e,n){e.methods||(e.methods={}),e.methods[n]=function(e){return t.hasError(this.$store.getters,e)}})}function x(t){return Object(l.createDecorator)(function(e,n){e.methods||(e.methods={}),e.methods[n]=function(e){return t.fetched(this.$store.getters,e)}})}function j(t){return Object(l.createDecorator)(function(e,n){e.methods||(e.methods={}),e.methods[n]=function(e){return t.response(this.$store.getters,e)}})}function w(t,e){return void 0===e&&(e=t,t=void 0),function(t,e,n){var r={},o=function(o){t.hasOwnProperty(o)&&(r[o]=function(){return t[o]=t[o].bind(this),e=n[o].payload?n[o].payload:e,t[o]()(e)})};for(var i in t)o(i);return r}(function(t){var e={};for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n].key);return Object(m.mapGetters)(e)}(e),t,e)}var C=function t(e,n){var r=this;this.invalidateCreators=[],this.invalidateXhr=[],this.plugin=function(){var t=r;return function(e){t.store=e,a+"/"+h.ANY_PENDING in e.getters||e.registerModule("globalXhrState",p),e.registerModule(t.namespace,{namespaced:!0,modules:t.modules})}},this.reset=function(t){for(var e in r.modules)r.modules.hasOwnProperty(e)&&t.dispatch(r.modules[e].reset())},this.invalidateAll=function(t){if(void 0===t&&(t=[]),r.store){for(var e in r.modules)r.modules.hasOwnProperty(e)&&r.modules[e].options.cache&&!r.modules[e].options.alwaysRefetch&&r.store.dispatch(r.modules[e].invalidateAll());t.push(r.namespace);for(var n=0,o=r.invalidateCreators;n<o.length;n++){var i=o[n];t.includes(i.namespace)||i.invalidateAll(t)}for(var a=0,s=r.invalidateXhr;a<s.length;a++){var c=s[a];r.store.dispatch(c.invalidateAll(),{root:!0})}}},this.invalidates=function(e){if(e instanceof t||e[0]instanceof t)return r.invalidatesCreator(e);r.invalidatesXhr(e)},this.invalidatesCreator=function(t){Array.isArray(t)||(t=[t]),r.invalidateCreators=t},this.invalidatesXhr=function(t){Array.isArray(t)||(t=[t]),r.invalidateXhr=t},this.xhrStoresToModules=function(t){var e={};return t.forEach(function(t,n){t.setVuexXhrCreator(r),t.setNamespace(r.namespace+"/xhr"+n),e["xhr"+n]=t}),e},this.namespace=e,this.modules=this.xhrStoresToModules(n)},F=n(2),L=n.n(F);function G(t){if(void 0===t)return L()(JSON.stringify("undefined"));if("object"==typeof t){var e=Object.keys(t);if(e.includes("errorHandler")){if(1===e.length)return L()(JSON.stringify("undefined"));delete(t=JSON.parse(JSON.stringify(t))).errorHandler}}return L()(JSON.stringify(t))}var H,k=function(t,e,n){var r,o=null;var i=((r={})[y.SEND]=function(t,e){var r=s(i.method,t,e);return r=r.then(function(t){return n(),t})},r[y.FETCH]=function(e,n){return!t&&function(t,e){return t.getters[O.PENDING](e)}(e,n)?o||u(e,n):!t&&function(t,e){return t.getters[O.FETCHED](e)}(e,n)?u(e,n):o=(o=s(i.method,e,n)).finally(function(){o=null})},r[y.RESET]=function(t){t.commit(_.RESET)},r);return t||(i[y.FORCE_FETCH]=function(t,e){return s(i.method,t,e)},i[y.INVALIDATE]=function(t,e){var n=G(e);t.commit(_.INVALIDATE,{key:n})},i[y.INVALIDATE_ALL]=function(t){t.getters[O.PAYLOAD_KEYS]().forEach(function(e){return t.commit(_.INVALIDATE,{key:e})})}),i.method=e,i;function s(t,e,n){var r,o=t(n),i=G(n),s=Math.random();if(void 0!==o.then)(r=o).then(function(t){c(e,i,s,t)}).catch(function(t){var r=function(t,e){if(void 0!==t&&void 0!==t.errorHandler)return t.errorHandler(e);return!1}(n,t);!function(t,e,n,r,o){t.commit(_.FAILED,{key:e,response:r}),t.dispatch(a+"/"+f.FAILED,{key:n,response:r,catched:o},{root:!0})}(e,i,s,t,r)});else{var u=o;r=h({data:u}),c(e,i,s,{data:u})}return e.commit(_.REQUEST,{key:i}),e.dispatch(a+"/"+f.REQUEST,s,{root:!0}),r}function c(t,e,n,r){t.commit(_.RECEIVED,{key:e,response:r}),t.dispatch(a+"/"+f.RECEIVED,n,{root:!0})}function u(t,e){return h(t.getters[O.RESPONSE](e))}function h(t){return new Promise(function(e){e(t)})}},V=function(t){var e,n=((e={})[O.PENDING]=function(t){return function(e){return t.PENDING[G(e)]}},e);return t&&(n[O.HAS_ERROR]=function(t){return function(e){return t.ERROR[G(e)]}},n[O.FETCHED]=function(t){return function(e){return t.FETCHED[G(e)]}},n[O.DATA]=function(t){return function(e){var n=G(e);return t.RESPONSE[n]&&!t.ERROR[n]?t.RESPONSE[n].data:t.DEFAULT}},n[O.RESPONSE]=function(t){return function(e){return t.RESPONSE[G(e)]}},n[O.PAYLOAD_KEYS]=function(t){return function(){return Object.keys(t.RESPONSE)}}),n},X=function(t){var e={PENDING:{}};if(t.cache&&(e.ERROR={},e.FETCHED={},e.DEFAULT=t.default,e.RESPONSE={}),t.store&&t.store.state)for(var n in t.store.state)t.store.state instanceof Object&&!t.store.state.hasOwnProperty(n)||(e[n]=t.store.state[n]);return e},Y=function(t){var e;return(e={})[_.REQUEST]=function(t,e){var n;t.PENDING=Object.assign({},t.PENDING,((n={})[e.key]=!0,n))},e[_.RECEIVED]=function(e,n){var r,o,i,a;e.PENDING=Object.assign({},e.PENDING,((r={})[n.key]=!1,r)),t.cache&&(e.ERROR=Object.assign({},e.ERROR,((o={})[n.key]=!1,o)),e.FETCHED=Object.assign({},e.FETCHED,((i={})[n.key]=!0,i)),e.RESPONSE=Object.assign({},e.RESPONSE,((a={})[n.key]=n.response,a)))},e[_.FAILED]=function(e,n){var r,o,i,a;e.PENDING=Object.assign({},e.PENDING,((r={})[n.key]=!1,r)),t.cache&&(e.ERROR=Object.assign({},e.ERROR,((o={})[n.key]=!0,o)),e.FETCHED=Object.assign({},e.FETCHED,((i={})[n.key]=!1,i)),e.RESPONSE=Object.assign({},e.RESPONSE,((a={})[n.key]=n.response,a)))},e[_.INVALIDATE]=function(t,e){var n;t.FETCHED=Object.assign({},t.FETCHED,((n={})[e.key]=!1,n))},e[_.RESET]=function(e){Object.assign(e,X(t))},e},$="/",M=function(){function t(t){var e=this;this.namespace="",this.setNamespace=function(t){e.namespace=t},this.mapPending=function(t){return{key:e.namespace+$+O.PENDING,payload:t}},this.mapHasError=function(t){if(!e.options.cache)throw new Error("mapHasError is not available on this object");return{key:e.namespace+$+O.HAS_ERROR,payload:t}},this.mapFetched=function(t){if(!e.options.cache)throw new Error("mapFetched is not available on this object");return{key:e.namespace+$+O.FETCHED,payload:t}},this.mapData=function(t){if(!e.options.cache)throw new Error("mapData is not available on this object");return{key:e.namespace+$+O.DATA,payload:t}},this.mapResponse=function(t){if(!e.options.cache)throw new Error("mapResponse is not available on this object");return{key:e.namespace+$+O.RESPONSE,payload:t}},this.pending=function(t,n){return e.findGetter(t,O.PENDING)(n)},this.hasError=function(t,n){if(!e.options.cache)throw new Error("hasError is not available on this object");return e.findGetter(t,O.HAS_ERROR)(n)},this.fetched=function(t,n){if(!e.options.cache)throw new Error("fetched is not available on this object");return e.findGetter(t,O.FETCHED)(n)},this.data=function(t,n){if(!e.options.cache)throw new Error("data is not available on this object");return e.findGetter(t,O.DATA)(n)},this.response=function(t,n){if(!e.options.cache)throw new Error("response is not available on this object");return e.findGetter(t,O.RESPONSE)(n)},this.dataNamespace=function(){return e.namespace+$+O.DATA},this.fetch=function(){return e.namespace+$+y.FETCH},this.send=function(){return e.namespace+$+y.SEND},this.invalidate=function(){return e.namespace+$+y.INVALIDATE},this.invalidateAll=function(){return e.namespace+$+y.INVALIDATE_ALL},this.forceFetch=function(){if(!e.options.cache)throw new Error("forceFetch is not available on this object");return e.namespace+$+y.FORCE_FETCH},this.reset=function(){return e.namespace+$+y.RESET},this.setState=function(t,n){void 0===n&&(n=t,t=void 0),n.data&&!n.response&&(n={error:!1,pending:!1,response:{data:n.data}}),e.state.ERROR[G(t)]=n.error,e.state.FETCHED[G(t)]=n.response,e.state.PENDING[G(t)]=n.pending,e.state.RESPONSE[G(t)]=n.response},this.setMethod=function(t){e.actions.method=t},this.setVuexXhrCreator=function(t){e.vuexXhrCreator=t},this.inValidateGroup=function(){e.vuexXhrCreator&&e.vuexXhrCreator.invalidateAll()},this.mergeStore=function(t){t.mutations&&Object.assign(e.mutations,t.mutations),t.actions&&Object.assign(e.actions,t.actions),t.getters&&Object.assign(e.getters,t.getters)},this.findGetter=function(t,n){if(void 0!==t[e.namespace+$+n])return t[e.namespace+$+n];if(void 0!==t[n])return t[n];throw new Error("VuexXhr Error. Getter not found ("+n+")"+e.namespace)},this.namespaced=!0,this.options=t,this.options.alwaysRefetch=void 0===this.options.alwaysRefetch||this.options.alwaysRefetch,this.state=X(this.options),this.mutations=Y(this.options),this.actions=k(this.options.alwaysRefetch,this.options.method,this.inValidateGroup),this.getters=V(!this.options.cache||this.options.cache),this.options.store&&this.mergeStore(this.options.store)}return t.prototype.mockCall=function(t,e){var n=new Promise(function(t){t({data:e})});this.setMethod(function(){return n})},t}(),U=(H=function(t,e){return(H=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)},function(t,e){function n(){this.constructor=t}H(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}),q=function(t){function e(e){return(e=e||{}).cache=void 0===e.cache||e.cache,t.call(this,e)||this}return U(e,t),e}(M),Q=function(){var t=function(e,n){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(e,n)};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),J=function(t){function e(e){return(e=e||{}).cache=!1,e.alwaysRefetch=!0,t.call(this,e)||this}return Q(e,t),e}(M),K=function(){var t=function(e,n){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(e,n)};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),z=function(t){function e(e){return(e=e||{}).cache=!1,e.alwaysRefetch=!0,t.call(this,e)||this}return K(e,t),e}(M);n.d(e,"vxsActionFetch",function(){return N}),n.d(e,"vxsActionSend",function(){return S}),n.d(e,"vxsActionForcefetch",function(){return A}),n.d(e,"vxsActionReset",function(){return D}),n.d(e,"vxsActionInvalidate",function(){return b}),n.d(e,"vxsActionInvalidateAll",function(){return T}),n.d(e,"vxsDataGetter",function(){return P}),n.d(e,"vxsPendingGetter",function(){return g}),n.d(e,"vxsHasErrorGetter",function(){return I}),n.d(e,"vxsFetchedGetter",function(){return x}),n.d(e,"vxsResponseGetter",function(){return j}),n.d(e,"mapXhrGetters",function(){return w}),n.d(e,"VuexXhrCreator",function(){return C}),n.d(e,"VuexXhrGet",function(){return q}),n.d(e,"VuexXhrPut",function(){return J}),n.d(e,"VuexXhrPost",function(){return z});e.default={install:function(t){t.prototype.$anyXhrPending=function(){return this.$store.getters[a+"/"+h.ANY_PENDING]},t.prototype.$anyXhrError=function(){return this.$store.getters[a+"/"+h.ANY_ERROR]},t.prototype.$lastXhrErrorResponse=function(){return this.$store.getters[a+"/"+h.LAST_ERROR_RESPONSE]},t.prototype.$resetXhrErrors=function(){return this.$store.commit(a+"/"+f.RESET_ERROR)}}}}])});