From d1c3bbc8699624a8fc413312017b0dcd8457745d Mon Sep 17 00:00:00 2001 From: Christian Malek Date: Tue, 13 Jul 2021 20:35:33 +0200 Subject: [PATCH 1/6] update axios peer dep to ^0.21 --- CHANGELOG.md | 4 ++++ package.json | 4 ++-- tests/package.json | 1 - 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09eccad..d9c024c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ (format is `dd.mm.yyyy` ) +### 2.14.0 (13.07.2021) + +* updated axios peer dependency to version `^0.21` + ### 2.13.0 (19.07.2020) * allow all HTTP request methods to set a payload (contributed by @dries007, see https://github.com/christianmalek/vuex-rest-api/pull/103) diff --git a/package.json b/package.json index 58f561d..d65478b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vuex-rest-api", - "version": "2.13.0", + "version": "2.14.0", "description": "A helper utility to simplify the usage of REST APIs with Vuex. Based on axios.", "author": "Christian Malek", "license": "MIT", @@ -12,7 +12,7 @@ "lodash.clonedeep": "^4.5" }, "peerDependencies": { - "axios": "^0.19" + "axios": "^0.21" }, "scripts": { "build": "tsc -p ." diff --git a/tests/package.json b/tests/package.json index 6516885..8c42301 100644 --- a/tests/package.json +++ b/tests/package.json @@ -14,7 +14,6 @@ "lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs" }, "dependencies": { - "axios": "^0.19.1", "vue": "^2.2.6", "vue-router": "^2.3.1", "vuex": "^2.3.1" From f6f6465f1b6f9e7bcdcafb9e8bbb1283fcdd3901 Mon Sep 17 00:00:00 2001 From: Christian Malek Date: Tue, 24 May 2022 19:20:21 +0200 Subject: [PATCH 2/6] update ts lib to es2018 and add DOM lib --- tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index d53a140..ac4ad38 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,8 @@ "removeComments": false, "declaration": true, "lib": [ - "es2015" + "es2018", + "DOM" ] }, "include": [ From 3b084e4b169460e4b9b223e85f15c487ba9df0ff Mon Sep 17 00:00:00 2001 From: Christian Malek Date: Tue, 24 May 2022 19:21:46 +0200 Subject: [PATCH 3/6] update typescript to ^4.6 and axios to ^0.27 --- dist/Resource.js | 2 +- dist/Store.js | 10 +++++----- dist/index.js | 2 +- package.json | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/dist/Resource.js b/dist/Resource.js index a307fff..fe0295a 100644 --- a/dist/Resource.js +++ b/dist/Resource.js @@ -20,7 +20,7 @@ var Resource = /** @class */ (function () { var headersFn = this.getHeadersFn(options); if (this.HTTPMethod.indexOf(options.method) === -1) { var methods = this.HTTPMethod.join(", "); - throw new Error("Illegal HTTP method set. Following methods are allowed: " + methods + ". You chose \"" + options.method + "\"."); + throw new Error("Illegal HTTP method set. Following methods are allowed: ".concat(methods, ". You chose \"").concat(options.method, "\".")); } var urlFn; if (typeof options.path === "function") { diff --git a/dist/Store.js b/dist/Store.js index 08f6631..2591e17 100644 --- a/dist/Store.js +++ b/dist/Store.js @@ -113,7 +113,7 @@ var StoreCreator = /** @class */ (function () { var actions = this.resource.actions; Object.keys(actions).forEach(function (action) { var _a = actions[action], property = _a.property, commitString = _a.commitString, beforeRequest = _a.beforeRequest, onSuccess = _a.onSuccess, onError = _a.onError, axios = _a.axios; - mutations["" + commitString] = function (state, actionParams) { + mutations["".concat(commitString)] = function (state, actionParams) { if (property !== null) { state.pending[property] = true; state.error[property] = null; @@ -122,7 +122,7 @@ var StoreCreator = /** @class */ (function () { beforeRequest(state, actionParams); } }; - mutations[commitString + "_" + _this.successSuffix] = function (state, _a) { + mutations["".concat(commitString, "_").concat(_this.successSuffix)] = function (state, _a) { var payload = _a.payload, actionParams = _a.actionParams; if (property !== null) { state.pending[property] = false; @@ -135,7 +135,7 @@ var StoreCreator = /** @class */ (function () { state[property] = payload.data; } }; - mutations[commitString + "_" + _this.errorSuffix] = function (state, _a) { + mutations["".concat(commitString, "_").concat(_this.errorSuffix)] = function (state, _a) { var payload = _a.payload, actionParams = _a.actionParams; if (property !== null) { state.pending[property] = false; @@ -171,13 +171,13 @@ var StoreCreator = /** @class */ (function () { commit(commitString, actionParams); return [2 /*return*/, requestFn(actionParams.params, actionParams.data) .then(function (response) { - commit(commitString + "_" + _this.successSuffix, { + commit("".concat(commitString, "_").concat(_this.successSuffix), { payload: response, actionParams: actionParams }); return Promise.resolve(response); }, function (error) { - commit(commitString + "_" + _this.errorSuffix, { + commit("".concat(commitString, "_").concat(_this.errorSuffix), { payload: error, actionParams: actionParams }); diff --git a/dist/index.js b/dist/index.js index cf252b9..684a554 100644 --- a/dist/index.js +++ b/dist/index.js @@ -35,7 +35,7 @@ var Vapi = /** @class */ (function () { }; Vapi.prototype.getStore = function (options) { if (options === void 0) { options = {}; } - return Store_1.createStore(this.resource, options); + return (0, Store_1.createStore)(this.resource, options); }; return Vapi; }()); diff --git a/package.json b/package.json index d65478b..b10b728 100644 --- a/package.json +++ b/package.json @@ -12,13 +12,13 @@ "lodash.clonedeep": "^4.5" }, "peerDependencies": { - "axios": "^0.21" + "axios": "^0.27" }, "scripts": { "build": "tsc -p ." }, "devDependencies": { - "typescript": "^3.6" + "typescript": "^4.6" }, "files": [ "dist", From 971916e7fd56566970611cb8063c6098ab23eba2 Mon Sep 17 00:00:00 2001 From: Christian Malek Date: Tue, 24 May 2022 19:22:08 +0200 Subject: [PATCH 4/6] increase version to 2.15.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b10b728..7077aca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vuex-rest-api", - "version": "2.14.0", + "version": "2.15.0", "description": "A helper utility to simplify the usage of REST APIs with Vuex. Based on axios.", "author": "Christian Malek", "license": "MIT", From 86263c8ffa4ba04d08f57b40c00f0643d2d28aef Mon Sep 17 00:00:00 2001 From: Christian Malek Date: Tue, 24 May 2022 19:43:38 +0200 Subject: [PATCH 5/6] update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9c024c..07d077c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ (format is `dd.mm.yyyy` ) +### 2.15.0 (24.05.2022) + +* updated axios peer dependency to version `^0.27` +* updated typescript dev dependency to `^4.6` +* updated typescript lib configuration from es2015 to es2018 and DOM + ### 2.14.0 (13.07.2021) * updated axios peer dependency to version `^0.21` From d0b7ee3a75faa315895bc2a1df0314a7688a9ee3 Mon Sep 17 00:00:00 2001 From: Peter Darrow Date: Wed, 10 Aug 2022 22:21:32 -0300 Subject: [PATCH 6/6] Rebuild dist after merging in latest upstream --- dist/Store.js | 80 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/dist/Store.js b/dist/Store.js index 2591e17..8bca586 100644 --- a/dist/Store.js +++ b/dist/Store.js @@ -37,12 +37,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) { }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createStore = void 0; +var axios_1 = require("axios"); var cloneDeep = require("lodash.clonedeep"); var StoreCreator = /** @class */ (function () { function StoreCreator(resource, options) { this.successSuffix = "SUCCEEDED"; this.errorSuffix = "FAILED"; this.resource = resource; + this.resource = resource; this.options = Object.assign({ createStateFn: false, namespaced: false @@ -61,7 +63,8 @@ var StoreCreator = /** @class */ (function () { var resourceState = cloneDeep(this.resource.state); var state = Object.assign({ pending: {}, - error: {} + error: {}, + source: {}, }, resourceState); var actions = this.resource.actions; Object.keys(actions).forEach(function (action) { @@ -76,6 +79,7 @@ var StoreCreator = /** @class */ (function () { } state["pending"][property] = false; state["error"][property] = null; + state["source"][property] = null; }); return state; }; @@ -85,7 +89,8 @@ var StoreCreator = /** @class */ (function () { var resourceState = cloneDeep(_this.resource.state); var state = Object.assign({ pending: {}, - error: {} + error: {}, + source: {}, }, resourceState); var actions = _this.resource.actions; Object.keys(actions).forEach(function (action) { @@ -100,6 +105,7 @@ var StoreCreator = /** @class */ (function () { } state["pending"][property] = false; state["error"][property] = null; + state["source"][property] = null; }); return state; }; @@ -112,40 +118,55 @@ var StoreCreator = /** @class */ (function () { var mutations = {}; var actions = this.resource.actions; Object.keys(actions).forEach(function (action) { - var _a = actions[action], property = _a.property, commitString = _a.commitString, beforeRequest = _a.beforeRequest, onSuccess = _a.onSuccess, onError = _a.onError, axios = _a.axios; - mutations["".concat(commitString)] = function (state, actionParams) { + var _a = actions[action], property = _a.property, commitString = _a.commitString, autoCancel = _a.autoCancel, beforeRequest = _a.beforeRequest, onSuccess = _a.onSuccess, onCancel = _a.onCancel, onError = _a.onError, axios = _a.axios; + mutations["".concat(commitString)] = function (state, requestConfig) { if (property !== null) { state.pending[property] = true; state.error[property] = null; + // If autoCancel is enabled and this property maps to a source state, cancel the current pending request. + if (autoCancel && state.source[property]) { + state.source[property].cancel(); + } + // If the request config doesn't contain a cancel token, set one in state for convenience. We'll let the user + // provided token take precedence here though in case it's needed for a special controlled flow. + if (!requestConfig.cancelToken) { + var source = StoreCreator.CANCEL_TOKEN_PROVIDER.source(); + state.source[property] = source; + requestConfig["cancelToken"] = source.token; + } } if (beforeRequest) { - beforeRequest(state, actionParams); + beforeRequest(state, requestConfig); } }; mutations["".concat(commitString, "_").concat(_this.successSuffix)] = function (state, _a) { - var payload = _a.payload, actionParams = _a.actionParams; + var payload = _a.payload, requestConfig = _a.requestConfig; if (property !== null) { state.pending[property] = false; state.error[property] = null; + state.source[property] = null; } if (onSuccess) { - onSuccess(state, payload, axios, actionParams); + onSuccess(state, payload, axios, requestConfig); } else if (property !== null) { state[property] = payload.data; } }; mutations["".concat(commitString, "_").concat(_this.errorSuffix)] = function (state, _a) { - var payload = _a.payload, actionParams = _a.actionParams; + var payload = _a.payload, requestConfig = _a.requestConfig, isCancellationErr = _a.isCancellationErr; if (property !== null) { state.pending[property] = false; state.error[property] = payload; + state.source[property] = null; + } + if (!isCancellationErr && onError) { + onError(state, payload, axios, requestConfig); } - if (onError) { - onError(state, payload, axios, actionParams); + else if (isCancellationErr && onCancel) { + onCancel(state, payload, axios, requestConfig); } else if (property !== null) { - // sets property to it's default value in case of an error state[property] = defaultState[property]; } }; @@ -157,31 +178,36 @@ var StoreCreator = /** @class */ (function () { var storeActions = {}; var actions = this.resource.actions; Object.keys(actions).forEach(function (action) { - var _a = actions[action], dispatchString = _a.dispatchString, commitString = _a.commitString, requestFn = _a.requestFn; - storeActions[dispatchString] = function (_a, actionParams) { + var _a = actions[action], dispatchString = _a.dispatchString, commitString = _a.commitString, requestFn = _a.requestFn, autoCancel = _a.autoCancel; + storeActions[dispatchString] = function (_a, requestConfig) { var commit = _a.commit; - if (actionParams === void 0) { actionParams = { params: {}, data: {} }; } + if (requestConfig === void 0) { requestConfig = cloneDeep(StoreCreator.DEFAULT_REQUEST_CONFIG); } return __awaiter(_this, void 0, void 0, function () { var _this = this; return __generator(this, function (_b) { - if (!actionParams.params) - actionParams.params = {}; - if (!actionParams.data) - actionParams.data = {}; - commit(commitString, actionParams); - return [2 /*return*/, requestFn(actionParams.params, actionParams.data) + if (!requestConfig.params) + requestConfig.params = {}; + if (!requestConfig.data) + requestConfig.data = {}; + commit(commitString, requestConfig); + return [2 /*return*/, requestFn(requestConfig) .then(function (response) { commit("".concat(commitString, "_").concat(_this.successSuffix), { payload: response, - actionParams: actionParams + requestConfig: requestConfig }); return Promise.resolve(response); }, function (error) { - commit("".concat(commitString, "_").concat(_this.errorSuffix), { - payload: error, - actionParams: actionParams - }); - return Promise.reject(error); + // We'll ignore the err if autoCancel is enabled and the cause is cancellation. + var isCancellationErr = axios_1.default.isCancel(error); + var shouldHandleErr = !autoCancel || !isCancellationErr; + if (shouldHandleErr) { + commit("".concat(commitString, "_").concat(_this.errorSuffix), { payload: error, requestConfig: requestConfig, isCancellationErr: isCancellationErr }); + return Promise.reject(error); + } + else { + return Promise.resolve(); + } })]; }); }); @@ -198,6 +224,8 @@ var StoreCreator = /** @class */ (function () { actions: this.createActions() }; }; + StoreCreator.CANCEL_TOKEN_PROVIDER = axios_1.default.CancelToken; + StoreCreator.DEFAULT_REQUEST_CONFIG = { params: {}, data: {} }; return StoreCreator; }()); function createStore(resource, options) {