UNPKG

huddlebv-plugin-axios

Version:

Fork of Vuex ORM plugin for adding smooth integration with axios. Fork is needed because the highest version we can use is 0.9.1 but that version was dependent on a axios version with multiple vulnerabilities.

1 lines 5.27 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).VuexORMAxios=e()}(this,function(){"use strict";var o=function(){return(o=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 t(r,s,a,u){return new(a=a||Promise)(function(t,e){function n(t){try{i(u.next(t))}catch(t){e(t)}}function o(t){try{i(u.throw(t))}catch(t){e(t)}}function i(e){e.done?t(e.value):new a(function(t){t(e.value)}).then(n,o)}i((u=u.apply(r,s||[])).next())})}function i(n,o){var i,r,s,t,a={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return t={next:e(0),throw:e(1),return:e(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function(e){if(i)throw new TypeError("Generator is already executing.");for(;a;)try{if(i=1,r&&(s=2&e[0]?r.return:e[0]?r.throw||((s=r.return)&&s.call(r),0):r.next)&&!(s=s.call(r,e[1])).done)return s;switch(r=0,s&&(e=[2&e[0],s.value]),e[0]){case 0:case 1:s=e;break;case 4:return a.label++,{value:e[1],done:!1};case 5:a.label++,r=e[1],e=[0];continue;case 7:e=a.ops.pop(),a.trys.pop();continue;default:if(!(s=0<(s=a.trys).length&&s[s.length-1])&&(6===e[0]||2===e[0])){a=0;continue}if(3===e[0]&&(!s||e[1]>s[0]&&e[1]<s[3])){a.label=e[1];break}if(6===e[0]&&a.label<s[1]){a.label=s[1],s=e;break}if(s&&a.label<s[2]){a.label=s[2],a.ops.push(e);break}s[2]&&a.ops.pop(),a.trys.pop();continue}e=o.call(n,a)}catch(t){e=[6,t],r=0}finally{i=s=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}([e,t])}}}var r=(e.prototype.save=function(){return t(this,void 0,void 0,function(){var e,n;return i(this,function(t){switch(t.label){case 0:return e=this.getDataFromResponse(),this.validateData(e)?[4,(n=this).model.insertOrUpdate({data:e})]:(console.warn("[Vuex ORM Axios] The response data could not be saved to the store because it's 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 n.entities=t.sent(),this.isSaved=!0,[2]}})})},e.prototype.delete=function(){return t(this,void 0,void 0,function(){return i(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]}})})},e.prototype.getDataFromResponse=function(){return this.config.dataTransformer?this.config.dataTransformer(this.response):this.config.dataKey?this.response.data[this.config.dataKey]:this.response.data},e.prototype.validateData=function(t){return null!==t&&"object"==typeof t},e);function e(t,e,n){this.entities=null,this.isSaved=!1,this.model=t,this.config=e,this.response=n}var n=(Object.defineProperty(s.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:!0,configurable:!0}),s.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)}},s.prototype.registerObjectAction=function(t,e){var n=this;this[t]=function(t){return n.request(o(o({},e),t))}},s.prototype.registerFunctionAction=function(t,e){this[t]=e.bind(this)},s.prototype.get=function(t,e){return void 0===e&&(e={}),this.request(o({method:"get",url:t},e))},s.prototype.post=function(t,e,n){return void 0===e&&(e={}),void 0===n&&(n={}),this.request(o({method:"post",url:t,data:e},n))},s.prototype.put=function(t,e,n){return void 0===e&&(e={}),void 0===n&&(n={}),this.request(o({method:"put",url:t,data:e},n))},s.prototype.patch=function(t,e,n){return void 0===e&&(e={}),void 0===n&&(n={}),this.request(o({method:"patch",url:t,data:e},n))},s.prototype.delete=function(t,e){return void 0===e&&(e={}),this.request(o({method:"delete",url:t},e))},s.prototype.request=function(o){return t(this,void 0,void 0,function(){var e,n;return i(this,function(t){switch(t.label){case 0:return e=this.createConfig(o),[4,this.axios.request(e)];case 1:return n=t.sent(),[2,this.createResponse(n,e)]}})})},s.prototype.createConfig=function(t){return o(o(o(o({},this.config),this.model.globalApiConfig),this.model.apiConfig),t)},s.prototype.createResponse=function(n,o){return t(this,void 0,void 0,function(){var e;return i(this,function(t){switch(t.label){case 0:return e=new r(this.model,o,n),void 0===o.delete?[3,2]:[4,e.delete()];case 1:return t.sent(),[2,e];case 2:return o.save?[4,e.save()]:[3,4];case 3:t.sent(),t.label=4;case 4:return[2,e]}})})},s);function s(t){this.config={save:!0},this.model=t,this.registerActions()}var a=(u.prototype.plugin=function(){!function(t,e){t.axios=e.axios||null,t.globalApiConfig=e,t.apiConfig={},t.setAxios=function(t){this.axios=t},t.api=function(){return new n(this)}}(this.model,this.config)},u);function u(t,e){this.model=t.Model,this.config=e}return{install:function(t,e){new a(t,e).plugin()}}});