@vuex-orm/plugin-axios
Version:
Vuex ORM plugin for adding smooth integration with axios.
16 lines (13 loc) • 6.67 kB
JavaScript
var VuexORMAxios=function(){"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 t=function(){return(t=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function e(t,e,n,o){return new(n||(n=Promise))((function(i,r){function s(t){try{u(o.next(t))}catch(t){r(t)}}function a(t){try{u(o.throw(t))}catch(t){r(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}u((o=o.apply(t,e||[])).next())}))}function n(t,e){var n,o,i,r,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return r={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function a(r){return function(a){return function(r){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,o&&(i=2&r[0]?o.return:r[0]?o.throw||((i=o.return)&&i.call(o),0):o.next)&&!(i=i.call(o,r[1])).done)return i;switch(o=0,i&&(r=[2&r[0],i.value]),r[0]){case 0:case 1:i=r;break;case 4:return s.label++,{value:r[1],done:!1};case 5:s.label++,o=r[1],r=[0];continue;case 7:r=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==r[0]&&2!==r[0])){s=0;continue}if(3===r[0]&&(!i||r[1]>i[0]&&r[1]<i[3])){s.label=r[1];break}if(6===r[0]&&s.label<i[1]){s.label=i[1],i=r;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(r);break}i[2]&&s.ops.pop(),s.trys.pop();continue}r=e.call(t,s)}catch(t){r=[6,t],o=0}finally{n=i=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,a])}}}var o=function(){function o(t,e,n){this.entities=null,this.isSaved=!1,this.model=t,this.config=e,this.response=n}return o.prototype.save=function(){return e(this,void 0,void 0,(function(){var e,o,i,r;return n(this,(function(n){switch(n.label){case 0:return e=this.getDataFromResponse(),this.validateData(e)?(o=this.config.persistBy||"insertOrUpdate",this.validatePersistAction(o)||(console.warn('[Vuex ORM Axios] The "persistBy" option configured is not a recognized value. Response data will be persisted by the default `insertOrUpdate` method.'),o="insertOrUpdate"),i=this.getPersistOptions(),r=this,[4,this.model[o](t({data:e},i))]):(console.warn("[Vuex ORM Axios] The response data could not be saved to the store because it is not an object or an array. You might want to use `dataTransformer` option to handle non-array/object response before saving it to the store."),[2]);case 1:return r.entities=n.sent(),this.isSaved=!0,[2]}}))}))},o.prototype.delete=function(){return e(this,void 0,void 0,(function(){return n(this,(function(t){switch(t.label){case 0:if(void 0===this.config.delete)throw new Error("[Vuex ORM Axios] Could not delete records because the `delete` option is not set.");return[4,this.model.delete(this.config.delete)];case 1:return t.sent(),[2]}}))}))},o.prototype.getDataFromResponse=function(){return this.config.dataTransformer?this.config.dataTransformer(this.response):this.config.dataKey?this.response.data[this.config.dataKey]:this.response.data},o.prototype.getPersistOptions=function(){var t=this.config.persistOptions;if(t&&"object"==typeof t)return Object.keys(t).filter(this.validatePersistAction).reduce((function(e,n){return e[n]=t[n],e}),{})},o.prototype.validateData=function(t){return null!==t&&"object"==typeof t},o.prototype.validatePersistAction=function(t){return["create","insert","update","insertOrUpdate"].includes(t)},o}(),i=function(){function i(t){this.config={save:!0},this.model=t,this.registerActions()}return Object.defineProperty(i.prototype,"axios",{get:function(){if(!this.model.axios)throw new Error("[Vuex ORM Axios] The axios instance is not registered. Please register the axios instance to the model.");return this.model.axios},enumerable:!1,configurable:!0}),i.prototype.registerActions=function(){var t=this.model.apiConfig.actions;if(t)for(var e in t){var n=t[e];"function"==typeof n?this.registerFunctionAction(e,n):this.registerObjectAction(e,n)}},i.prototype.registerObjectAction=function(e,n){var o=this;this[e]=function(e){return o.request(t(t({},n),e))}},i.prototype.registerFunctionAction=function(t,e){this[t]=e.bind(this)},i.prototype.get=function(e,n){return void 0===n&&(n={}),this.request(t({method:"get",url:e},n))},i.prototype.post=function(e,n,o){return void 0===n&&(n={}),void 0===o&&(o={}),this.request(t({method:"post",url:e,data:n},o))},i.prototype.put=function(e,n,o){return void 0===n&&(n={}),void 0===o&&(o={}),this.request(t({method:"put",url:e,data:n},o))},i.prototype.patch=function(e,n,o){return void 0===n&&(n={}),void 0===o&&(o={}),this.request(t({method:"patch",url:e,data:n},o))},i.prototype.delete=function(e,n){return void 0===n&&(n={}),this.request(t({method:"delete",url:e},n))},i.prototype.request=function(t){return e(this,void 0,void 0,(function(){var e,o;return n(this,(function(n){switch(n.label){case 0:return e=this.createConfig(t),[4,this.axios.request(e)];case 1:return o=n.sent(),[2,this.createResponse(o,e)]}}))}))},i.prototype.createConfig=function(e){return t(t(t(t({},this.config),this.model.globalApiConfig),this.model.apiConfig),e)},i.prototype.createResponse=function(t,i){return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return e=new o(this.model,i,t),void 0===i.delete?[3,2]:[4,e.delete()];case 1:return n.sent(),[2,e];case 2:return i.save?[4,e.save()]:[3,4];case 3:n.sent(),n.label=4;case 4:return[2,e]}}))}))},i}();var r=function(){function t(t,e){this.model=t.Model,this.config=e}return t.prototype.plugin=function(){var t,e;t=this.model,e=this.config,t.axios=e.axios||null,t.globalApiConfig=e,t.apiConfig={},t.setAxios=function(t){this.axios=t},t.api=function(){return new i(this)}},t}();return{install:function(t,e){new r(t,e).plugin()}}}();