Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/v2' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
pmdarrow committed Aug 11, 2022
2 parents c587e9b + 86263c8 commit c8915b0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 64 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

(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`

### 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)
Expand Down
2 changes: 1 addition & 1 deletion dist/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
86 changes: 29 additions & 57 deletions dist/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ 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
Expand All @@ -63,8 +61,7 @@ var StoreCreator = /** @class */ (function () {
var resourceState = cloneDeep(this.resource.state);
var state = Object.assign({
pending: {},
error: {},
source: {},
error: {}
}, resourceState);
var actions = this.resource.actions;
Object.keys(actions).forEach(function (action) {
Expand All @@ -79,7 +76,6 @@ var StoreCreator = /** @class */ (function () {
}
state["pending"][property] = false;
state["error"][property] = null;
state["source"][property] = null;
});
return state;
};
Expand All @@ -89,8 +85,7 @@ var StoreCreator = /** @class */ (function () {
var resourceState = cloneDeep(_this.resource.state);
var state = Object.assign({
pending: {},
error: {},
source: {},
error: {}
}, resourceState);
var actions = _this.resource.actions;
Object.keys(actions).forEach(function (action) {
Expand All @@ -105,7 +100,6 @@ var StoreCreator = /** @class */ (function () {
}
state["pending"][property] = false;
state["error"][property] = null;
state["source"][property] = null;
});
return state;
};
Expand All @@ -118,55 +112,40 @@ 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, autoCancel = _a.autoCancel, beforeRequest = _a.beforeRequest, onSuccess = _a.onSuccess, onCancel = _a.onCancel, onError = _a.onError, axios = _a.axios;
mutations["" + commitString] = function (state, requestConfig) {
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) {
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, requestConfig);
beforeRequest(state, actionParams);
}
};
mutations[commitString + "_" + _this.successSuffix] = function (state, _a) {
var payload = _a.payload, requestConfig = _a.requestConfig;
mutations["".concat(commitString, "_").concat(_this.successSuffix)] = function (state, _a) {
var payload = _a.payload, actionParams = _a.actionParams;
if (property !== null) {
state.pending[property] = false;
state.error[property] = null;
state.source[property] = null;
}
if (onSuccess) {
onSuccess(state, payload, axios, requestConfig);
onSuccess(state, payload, axios, actionParams);
}
else if (property !== null) {
state[property] = payload.data;
}
};
mutations[commitString + "_" + _this.errorSuffix] = function (state, _a) {
var payload = _a.payload, requestConfig = _a.requestConfig, isCancellationErr = _a.isCancellationErr;
mutations["".concat(commitString, "_").concat(_this.errorSuffix)] = function (state, _a) {
var payload = _a.payload, actionParams = _a.actionParams;
if (property !== null) {
state.pending[property] = false;
state.error[property] = payload;
state.source[property] = null;
}
if (!isCancellationErr && onError) {
onError(state, payload, axios, requestConfig);
}
else if (isCancellationErr && onCancel) {
onCancel(state, payload, axios, requestConfig);
if (onError) {
onError(state, payload, axios, actionParams);
}
else if (property !== null) {
// sets property to it's default value in case of an error
state[property] = defaultState[property];
}
};
Expand All @@ -178,36 +157,31 @@ 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, autoCancel = _a.autoCancel;
storeActions[dispatchString] = function (_a, requestConfig) {
var _a = actions[action], dispatchString = _a.dispatchString, commitString = _a.commitString, requestFn = _a.requestFn;
storeActions[dispatchString] = function (_a, actionParams) {
var commit = _a.commit;
if (requestConfig === void 0) { requestConfig = cloneDeep(StoreCreator.DEFAULT_REQUEST_CONFIG); }
if (actionParams === void 0) { actionParams = { params: {}, data: {} }; }
return __awaiter(_this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_b) {
if (!requestConfig.params)
requestConfig.params = {};
if (!requestConfig.data)
requestConfig.data = {};
commit(commitString, requestConfig);
return [2 /*return*/, requestFn(requestConfig)
if (!actionParams.params)
actionParams.params = {};
if (!actionParams.data)
actionParams.data = {};
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,
requestConfig: requestConfig
actionParams: actionParams
});
return Promise.resolve(response);
}, function (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(commitString + "_" + _this.errorSuffix, { payload: error, requestConfig: requestConfig, isCancellationErr: isCancellationErr });
return Promise.reject(error);
}
else {
return Promise.resolve();
}
commit("".concat(commitString, "_").concat(_this.errorSuffix), {
payload: error,
actionParams: actionParams
});
return Promise.reject(error);
})];
});
});
Expand All @@ -224,8 +198,6 @@ 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) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}());
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vuex-rest-api",
"version": "2.13.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",
Expand All @@ -12,13 +12,13 @@
"lodash.clonedeep": "^4.5"
},
"peerDependencies": {
"axios": "^0.21.1"
"axios": "^0.27"
},
"scripts": {
"build": "tsc -p ."
},
"devDependencies": {
"typescript": "^3.6"
"typescript": "^4.6"
},
"files": [
"dist",
Expand Down
1 change: 0 additions & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs"
},
"dependencies": {
"axios": "^0.21.1",
"vue": "^2.2.6",
"vue-router": "^2.3.1",
"vuex": "^2.3.1"
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"removeComments": false,
"declaration": true,
"lib": [
"es2015"
"es2018",
"DOM"
]
},
"include": [
Expand Down

0 comments on commit c8915b0

Please sign in to comment.