@weapnl/js-junction 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +3 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/api.js +11 -108
- package/src/builder/model.js +24 -5
- package/src/mixins/responseEventsMixin.js +196 -0
- package/src/request.js +32 -210
- package/src/response/responseEvents.js +182 -0
- package/src/response/responseEventsHandler.js +149 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## v0.1.3
|
|
6
|
+
- Added functionality to add the same global callback on an api multiple times.
|
|
7
|
+
- Callbacks (response events) are now cleared from the Request after a request has been made and the response events have been called once.
|
|
8
|
+
|
|
9
|
+
## v0.1.2
|
|
10
|
+
- Fixed a bug where not all subsequent requests would be cancelled.
|
|
11
|
+
|
|
5
12
|
## v0.1.1
|
|
6
13
|
- Added functionality to add the same callback on a request multiple times.
|
|
7
14
|
- ⚠️ With the old functionality setting a callback on a request would overwrite the globally defined callback. This is no longer the case, as both the global and request callbacks will be executed.
|
package/README.md
CHANGED
|
@@ -290,8 +290,9 @@ let request = await api.request('users/files')
|
|
|
290
290
|
});
|
|
291
291
|
```
|
|
292
292
|
|
|
293
|
-
**
|
|
294
|
-
|
|
293
|
+
**Response events**
|
|
294
|
+
- You can set global response events which will be called for every request by adding the event callback on the Api class.
|
|
295
|
+
- You can set response events directly on requests so they will only be called for that specific request. After executing the request, they will be automatically reset.
|
|
295
296
|
```javascript
|
|
296
297
|
let request = await api.request('users')
|
|
297
298
|
.onSuccess((data) => {
|
package/dist/index.js
CHANGED
|
@@ -26,7 +26,7 @@ return /******/ (() => { // webpackBootstrap
|
|
|
26
26
|
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
27
27
|
|
|
28
28
|
"use strict";
|
|
29
|
-
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (/* binding */ Api)\n/* harmony export */ });\n/* harmony import */ var _request__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./request */ \"./src/request.js\");\n/* harmony import */ var _batch__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./batch */ \"./src/batch.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! axios */ \"./node_modules/axios/lib/axios.js\");\n/* provided dependency */ var _ = __webpack_require__(/*! lodash */ \"./node_modules/lodash/lodash.js\");\nfunction _typeof(o) { \"@babel/helpers - typeof\"; return _typeof = \"function\" == typeof Symbol && \"symbol\" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && \"function\" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? \"symbol\" : typeof o; }, _typeof(o); }\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\nfunction _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }\nfunction _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, \"prototype\", { writable: false }); return Constructor; }\nfunction _toPropertyKey(arg) { var key = _toPrimitive(arg, \"string\"); return _typeof(key) === \"symbol\" ? key : String(key); }\nfunction _toPrimitive(input, hint) { if (_typeof(input) !== \"object\" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || \"default\"); if (_typeof(res) !== \"object\") return res; throw new TypeError(\"@@toPrimitive must return a primitive value.\"); } return (hint === \"string\" ? String : Number)(input); }\n\n\n\nvar Api = /*#__PURE__*/function () {\n function Api() {\n _classCallCheck(this, Api);\n this.setHeader('X-Requested-With', 'XMLHttpRequest');\n this._requests = [];\n this.host('/').suffix('');\n this._onSuccess = null;\n this._onError = null;\n this._onValidationError = null;\n this._onUnauthorized = null;\n this._onForbidden = null;\n this._onFinished = null;\n this._onCancelled = null;\n }\n\n /**\n * @param {string} host\n *\n * @returns {this} The current instance.\n */\n _createClass(Api, [{\n key: \"host\",\n value: function host(_host) {\n this._host = _host;\n return this;\n }\n\n /**\n * @param {string} suffix\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"suffix\",\n value: function suffix(_suffix) {\n if (!_.startsWith(_suffix, '/') && _suffix) {\n _suffix = \"/\".concat(_suffix);\n }\n this._suffix = _suffix;\n return this;\n }\n\n /**\n * @returns {string} Url containing the host and suffix.\n */\n }, {\n key: \"baseUrl\",\n get: function get() {\n var url = '';\n if (this._host) url += this._host;\n if (this._suffix) url += this._suffix;\n return url;\n }\n\n /**\n * @param {Request} request\n */\n }, {\n key: \"cancelRunning\",\n value: function cancelRunning(request) {\n var _this$_requests$reque;\n if (!request.key) {\n return;\n }\n (_this$_requests$reque = this._requests[request.key]) === null || _this$_requests$reque === void 0 || _this$_requests$reque.cancel();\n this._requests[request.key] = request;\n }\n\n /**\n * @param {Request} request\n */\n }, {\n key: \"removeRequest\",\n value: function removeRequest(request) {\n if (!request.key) {\n return;\n }\n if (request.response.statusCode !== 0) {\n delete this._requests[request.key];\n }\n }\n\n /**\n * @param {string} uri\n *\n * @returns {Request} The created request.\n */\n }, {\n key: \"request\",\n value: function request(uri) {\n if (!uri) throw new Error(\"Request url is empty.\");\n if (!_.startsWith(uri, '/')) {\n uri = \"/\".concat(uri);\n }\n var request = new _request__WEBPACK_IMPORTED_MODULE_0__[\"default\"]();\n request.setUrl(uri).setApi(this);\n if (this._onSuccess) request.onSuccess(this._onSuccess);\n if (this._onError) request.onError(this._onError);\n if (this._onValidationError) request.onValidationError(this._onValidationError);\n if (this._onUnauthorized) request.onUnauthorized(this._onUnauthorized);\n if (this._onForbidden) request.onForbidden(this._onForbidden);\n if (this._onFinished) request.onFinished(this._onFinished);\n if (this._onCancelled) request.onCancelled(this._onCancelled);\n return request;\n }\n\n /**\n * @returns {this} The current instance.\n */\n }, {\n key: \"cancelRequests\",\n value: function cancelRequests() {\n this._requests.forEach(function (request) {\n request.cancel();\n });\n this._requests = [];\n return this;\n }\n\n /**\n * @param {array} requests\n * @returns Batch\n */\n }, {\n key: \"batch\",\n value: function batch(requests) {\n return new _batch__WEBPACK_IMPORTED_MODULE_1__[\"default\"](requests);\n }\n\n /**\n * @param {string} token\n */\n }, {\n key: \"setBearer\",\n value: function setBearer(token) {\n this.setHeader('Authorization', 'Bearer ' + token);\n }\n }, {\n key: \"resetBearer\",\n value: function resetBearer() {\n this.removeHeader('Authorization');\n }\n\n /**\n * @param {string} token\n */\n }, {\n key: \"setCsrf\",\n value: function setCsrf(token) {\n this.setHeader('X-CSRF-TOKEN', token);\n }\n }, {\n key: \"resetCsrf\",\n value: function resetCsrf() {\n this.removeHeader('X-CSRF-TOKEN');\n }\n\n /**\n * @param {string} key\n * @param {string} value\n */\n }, {\n key: \"setHeader\",\n value: function setHeader(key, value) {\n axios__WEBPACK_IMPORTED_MODULE_2__[\"default\"].defaults.headers.common[key] = value;\n }\n\n /**\n * @param {string} key\n */\n }, {\n key: \"removeHeader\",\n value: function removeHeader(key) {\n delete axios__WEBPACK_IMPORTED_MODULE_2__[\"default\"].defaults.headers.common[key];\n }\n\n /**\n * Set the default 'onSuccess' event handler.\n *\n * @param {function(Response.data)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onSuccess\",\n value: function onSuccess() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onSuccess = callback;\n return this;\n }\n\n /**\n * Set the default 'onError' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onError\",\n value: function onError() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onError = callback;\n return this;\n }\n\n /**\n * Set the default 'onValidationError' event handler.\n *\n * @param {function(Response.validation)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onValidationError\",\n value: function onValidationError() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onValidationError = callback;\n return this;\n }\n\n /**\n * Set the default 'onUnauthorized' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onUnauthorized\",\n value: function onUnauthorized() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onUnauthorized = callback;\n return this;\n }\n\n /**\n * Set the default 'onForbidden' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onForbidden\",\n value: function onForbidden() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onForbidden = callback;\n return this;\n }\n\n /**\n * Set the default 'onFinished' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onFinished\",\n value: function onFinished() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onFinished = callback;\n return this;\n }\n\n /**\n * Set the default 'onCancelled' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onCancelled\",\n value: function onCancelled() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onCancelled = callback;\n return this;\n }\n\n /**\n * @param {function(Response)} onSuccess\n * @param {function(Error)} onError\n *\n * @returns {this}\n */\n }, {\n key: \"responseInterceptors\",\n value: function responseInterceptors() {\n var onSuccess = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n var onError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};\n axios__WEBPACK_IMPORTED_MODULE_2__[\"default\"].interceptors.response.use(function (response) {\n onSuccess(response);\n return response;\n }, function (error) {\n onError(error);\n return Promise.reject(error);\n });\n return this;\n }\n }]);\n return Api;\n}();\n\n\n//# sourceURL=webpack://Api/./src/api.js?");
|
|
29
|
+
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (/* binding */ Api)\n/* harmony export */ });\n/* harmony import */ var _request__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./request */ \"./src/request.js\");\n/* harmony import */ var _batch__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./batch */ \"./src/batch.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! axios */ \"./node_modules/axios/lib/axios.js\");\n/* provided dependency */ var _ = __webpack_require__(/*! lodash */ \"./node_modules/lodash/lodash.js\");\nfunction _typeof(o) { \"@babel/helpers - typeof\"; return _typeof = \"function\" == typeof Symbol && \"symbol\" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && \"function\" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? \"symbol\" : typeof o; }, _typeof(o); }\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\nfunction _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }\nfunction _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, \"prototype\", { writable: false }); return Constructor; }\nfunction _toPropertyKey(arg) { var key = _toPrimitive(arg, \"string\"); return _typeof(key) === \"symbol\" ? key : String(key); }\nfunction _toPrimitive(input, hint) { if (_typeof(input) !== \"object\" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || \"default\"); if (_typeof(res) !== \"object\") return res; throw new TypeError(\"@@toPrimitive must return a primitive value.\"); } return (hint === \"string\" ? String : Number)(input); }\n\n\n\nvar Api = /*#__PURE__*/function () {\n function Api() {\n _classCallCheck(this, Api);\n this.setHeader('X-Requested-With', 'XMLHttpRequest');\n this._requests = [];\n this.host('/').suffix('');\n this._onSuccess = null;\n this._onError = null;\n this._onValidationError = null;\n this._onUnauthorized = null;\n this._onForbidden = null;\n this._onFinished = null;\n this._onCancelled = null;\n }\n\n /**\n * @param {string} host\n *\n * @returns {this} The current instance.\n */\n _createClass(Api, [{\n key: \"host\",\n value: function host(_host) {\n this._host = _host;\n return this;\n }\n\n /**\n * @param {string} suffix\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"suffix\",\n value: function suffix(_suffix) {\n if (!_.startsWith(_suffix, '/') && _suffix) {\n _suffix = \"/\".concat(_suffix);\n }\n this._suffix = _suffix;\n return this;\n }\n\n /**\n * @returns {string} Url containing the host and suffix.\n */\n }, {\n key: \"baseUrl\",\n get: function get() {\n var url = '';\n if (this._host) url += this._host;\n if (this._suffix) url += this._suffix;\n return url;\n }\n\n /**\n * @param {Request} request\n */\n }, {\n key: \"cancelRunning\",\n value: function cancelRunning(request) {\n var _this$_requests$reque;\n if (!request.key) {\n return;\n }\n (_this$_requests$reque = this._requests[request.key]) === null || _this$_requests$reque === void 0 || _this$_requests$reque.cancel();\n this._requests[request.key] = request;\n }\n\n /**\n * @param {Request} request\n */\n }, {\n key: \"removeRequest\",\n value: function removeRequest(request) {\n if (!request.key) {\n return;\n }\n if (request.response.isFinished) {\n delete this._requests[request.key];\n }\n }\n\n /**\n * @param {string} uri\n *\n * @returns {Request} The created request.\n */\n }, {\n key: \"request\",\n value: function request(uri) {\n if (!uri) throw new Error(\"Request url is empty.\");\n if (!_.startsWith(uri, '/')) {\n uri = \"/\".concat(uri);\n }\n var request = new _request__WEBPACK_IMPORTED_MODULE_0__[\"default\"]();\n request.setUrl(uri).setApi(this);\n if (this._onSuccess) request.onSuccess(this._onSuccess);\n if (this._onError) request.onError(this._onError);\n if (this._onValidationError) request.onValidationError(this._onValidationError);\n if (this._onUnauthorized) request.onUnauthorized(this._onUnauthorized);\n if (this._onForbidden) request.onForbidden(this._onForbidden);\n if (this._onFinished) request.onFinished(this._onFinished);\n if (this._onCancelled) request.onCancelled(this._onCancelled);\n return request;\n }\n\n /**\n * @returns {this} The current instance.\n */\n }, {\n key: \"cancelRequests\",\n value: function cancelRequests() {\n this._requests.forEach(function (request) {\n request.cancel();\n });\n this._requests = [];\n return this;\n }\n\n /**\n * @param {array} requests\n * @returns Batch\n */\n }, {\n key: \"batch\",\n value: function batch(requests) {\n return new _batch__WEBPACK_IMPORTED_MODULE_1__[\"default\"](requests);\n }\n\n /**\n * @param {string} token\n */\n }, {\n key: \"setBearer\",\n value: function setBearer(token) {\n this.setHeader('Authorization', 'Bearer ' + token);\n }\n }, {\n key: \"resetBearer\",\n value: function resetBearer() {\n this.removeHeader('Authorization');\n }\n\n /**\n * @param {string} token\n */\n }, {\n key: \"setCsrf\",\n value: function setCsrf(token) {\n this.setHeader('X-CSRF-TOKEN', token);\n }\n }, {\n key: \"resetCsrf\",\n value: function resetCsrf() {\n this.removeHeader('X-CSRF-TOKEN');\n }\n\n /**\n * @param {string} key\n * @param {string} value\n */\n }, {\n key: \"setHeader\",\n value: function setHeader(key, value) {\n axios__WEBPACK_IMPORTED_MODULE_2__[\"default\"].defaults.headers.common[key] = value;\n }\n\n /**\n * @param {string} key\n */\n }, {\n key: \"removeHeader\",\n value: function removeHeader(key) {\n delete axios__WEBPACK_IMPORTED_MODULE_2__[\"default\"].defaults.headers.common[key];\n }\n\n /**\n * Set the default 'onSuccess' event handler.\n *\n * @param {function(Response.data)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onSuccess\",\n value: function onSuccess() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onSuccess = callback;\n return this;\n }\n\n /**\n * Set the default 'onError' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onError\",\n value: function onError() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onError = callback;\n return this;\n }\n\n /**\n * Set the default 'onValidationError' event handler.\n *\n * @param {function(Response.validation)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onValidationError\",\n value: function onValidationError() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onValidationError = callback;\n return this;\n }\n\n /**\n * Set the default 'onUnauthorized' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onUnauthorized\",\n value: function onUnauthorized() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onUnauthorized = callback;\n return this;\n }\n\n /**\n * Set the default 'onForbidden' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onForbidden\",\n value: function onForbidden() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onForbidden = callback;\n return this;\n }\n\n /**\n * Set the default 'onFinished' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onFinished\",\n value: function onFinished() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onFinished = callback;\n return this;\n }\n\n /**\n * Set the default 'onCancelled' event handler.\n *\n * @param {function(Response)} callback\n *\n * @returns {this} The current instance.\n */\n }, {\n key: \"onCancelled\",\n value: function onCancelled() {\n var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n this._onCancelled = callback;\n return this;\n }\n\n /**\n * @param {function(Response)} onSuccess\n * @param {function(Error)} onError\n *\n * @returns {this}\n */\n }, {\n key: \"responseInterceptors\",\n value: function responseInterceptors() {\n var onSuccess = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};\n var onError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};\n axios__WEBPACK_IMPORTED_MODULE_2__[\"default\"].interceptors.response.use(function (response) {\n onSuccess(response);\n return response;\n }, function (error) {\n onError(error);\n return Promise.reject(error);\n });\n return this;\n }\n }]);\n return Api;\n}();\n\n\n//# sourceURL=webpack://Api/./src/api.js?");
|
|
30
30
|
|
|
31
31
|
/***/ }),
|
|
32
32
|
|
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -2,6 +2,11 @@ import Request from './request';
|
|
|
2
2
|
import Batch from './batch';
|
|
3
3
|
import axios from 'axios';
|
|
4
4
|
|
|
5
|
+
import responseEventsMixin from './mixins/responseEventsMixin';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @mixes responseEventsMixin
|
|
9
|
+
*/
|
|
5
10
|
export default class Api {
|
|
6
11
|
constructor () {
|
|
7
12
|
this.setHeader('X-Requested-With', 'XMLHttpRequest');
|
|
@@ -10,13 +15,7 @@ export default class Api {
|
|
|
10
15
|
|
|
11
16
|
this.host('/').suffix('');
|
|
12
17
|
|
|
13
|
-
this.
|
|
14
|
-
this._onError = null;
|
|
15
|
-
this._onValidationError = null;
|
|
16
|
-
this._onUnauthorized = null;
|
|
17
|
-
this._onForbidden = null;
|
|
18
|
-
this._onFinished = null;
|
|
19
|
-
this._onCancelled = null;
|
|
18
|
+
this._initResponseEvents();
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
/**
|
|
@@ -77,7 +76,7 @@ export default class Api {
|
|
|
77
76
|
return;
|
|
78
77
|
}
|
|
79
78
|
|
|
80
|
-
if (request.response.
|
|
79
|
+
if (request.response.isFinished) {
|
|
81
80
|
delete this._requests[request.key];
|
|
82
81
|
}
|
|
83
82
|
}
|
|
@@ -96,17 +95,10 @@ export default class Api {
|
|
|
96
95
|
|
|
97
96
|
const request = new Request();
|
|
98
97
|
|
|
99
|
-
request
|
|
98
|
+
request
|
|
99
|
+
.setUrl(uri)
|
|
100
100
|
.setApi(this);
|
|
101
101
|
|
|
102
|
-
if (this._onSuccess) request.onSuccess(this._onSuccess);
|
|
103
|
-
if (this._onError) request.onError(this._onError);
|
|
104
|
-
if (this._onValidationError) request.onValidationError(this._onValidationError);
|
|
105
|
-
if (this._onUnauthorized) request.onUnauthorized(this._onUnauthorized);
|
|
106
|
-
if (this._onForbidden) request.onForbidden(this._onForbidden);
|
|
107
|
-
if (this._onFinished) request.onFinished(this._onFinished);
|
|
108
|
-
if (this._onCancelled) request.onCancelled(this._onCancelled);
|
|
109
|
-
|
|
110
102
|
return request;
|
|
111
103
|
}
|
|
112
104
|
|
|
@@ -168,97 +160,6 @@ export default class Api {
|
|
|
168
160
|
delete axios.defaults.headers.common[key];
|
|
169
161
|
}
|
|
170
162
|
|
|
171
|
-
/**
|
|
172
|
-
* Set the default 'onSuccess' event handler.
|
|
173
|
-
*
|
|
174
|
-
* @param {function(Response.data)} callback
|
|
175
|
-
*
|
|
176
|
-
* @returns {this} The current instance.
|
|
177
|
-
*/
|
|
178
|
-
onSuccess (callback = () => {}) {
|
|
179
|
-
this._onSuccess = callback;
|
|
180
|
-
|
|
181
|
-
return this;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Set the default 'onError' event handler.
|
|
186
|
-
*
|
|
187
|
-
* @param {function(Response)} callback
|
|
188
|
-
*
|
|
189
|
-
* @returns {this} The current instance.
|
|
190
|
-
*/
|
|
191
|
-
onError (callback = () => {}) {
|
|
192
|
-
this._onError = callback;
|
|
193
|
-
|
|
194
|
-
return this;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Set the default 'onValidationError' event handler.
|
|
199
|
-
*
|
|
200
|
-
* @param {function(Response.validation)} callback
|
|
201
|
-
*
|
|
202
|
-
* @returns {this} The current instance.
|
|
203
|
-
*/
|
|
204
|
-
onValidationError (callback = () => {}) {
|
|
205
|
-
this._onValidationError = callback;
|
|
206
|
-
|
|
207
|
-
return this;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Set the default 'onUnauthorized' event handler.
|
|
212
|
-
*
|
|
213
|
-
* @param {function(Response)} callback
|
|
214
|
-
*
|
|
215
|
-
* @returns {this} The current instance.
|
|
216
|
-
*/
|
|
217
|
-
onUnauthorized (callback = () => {}) {
|
|
218
|
-
this._onUnauthorized = callback;
|
|
219
|
-
|
|
220
|
-
return this;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Set the default 'onForbidden' event handler.
|
|
225
|
-
*
|
|
226
|
-
* @param {function(Response)} callback
|
|
227
|
-
*
|
|
228
|
-
* @returns {this} The current instance.
|
|
229
|
-
*/
|
|
230
|
-
onForbidden (callback = () => {}) {
|
|
231
|
-
this._onForbidden = callback;
|
|
232
|
-
|
|
233
|
-
return this;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Set the default 'onFinished' event handler.
|
|
238
|
-
*
|
|
239
|
-
* @param {function(Response)} callback
|
|
240
|
-
*
|
|
241
|
-
* @returns {this} The current instance.
|
|
242
|
-
*/
|
|
243
|
-
onFinished (callback = () => {}) {
|
|
244
|
-
this._onFinished = callback;
|
|
245
|
-
|
|
246
|
-
return this;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Set the default 'onCancelled' event handler.
|
|
251
|
-
*
|
|
252
|
-
* @param {function(Response)} callback
|
|
253
|
-
*
|
|
254
|
-
* @returns {this} The current instance.
|
|
255
|
-
*/
|
|
256
|
-
onCancelled (callback = () => {}) {
|
|
257
|
-
this._onCancelled = callback;
|
|
258
|
-
|
|
259
|
-
return this;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
163
|
/**
|
|
263
164
|
* @param {function(Response)} onSuccess
|
|
264
165
|
* @param {function(Error)} onError
|
|
@@ -279,3 +180,5 @@ export default class Api {
|
|
|
279
180
|
return this;
|
|
280
181
|
}
|
|
281
182
|
}
|
|
183
|
+
|
|
184
|
+
Object.assign(Api.prototype, responseEventsMixin);
|
package/src/builder/model.js
CHANGED
|
@@ -145,7 +145,11 @@ export default class Model extends Request {
|
|
|
145
145
|
});
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
|
|
148
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
149
|
+
responseEventsHandler.setOnSuccessData(items);
|
|
150
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
151
|
+
|
|
152
|
+
this.clearAllCallbacks();
|
|
149
153
|
|
|
150
154
|
return items;
|
|
151
155
|
}
|
|
@@ -177,7 +181,11 @@ export default class Model extends Request {
|
|
|
177
181
|
item = this.constructor.fromJson(this._response.data);
|
|
178
182
|
}
|
|
179
183
|
|
|
180
|
-
|
|
184
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
185
|
+
responseEventsHandler.setOnSuccessData(item);
|
|
186
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
187
|
+
|
|
188
|
+
this.clearAllCallbacks();
|
|
181
189
|
|
|
182
190
|
return item;
|
|
183
191
|
}
|
|
@@ -205,7 +213,11 @@ export default class Model extends Request {
|
|
|
205
213
|
item = this.constructor.fromJson(this._response.data);
|
|
206
214
|
}
|
|
207
215
|
|
|
208
|
-
|
|
216
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
217
|
+
responseEventsHandler.setOnSuccessData(item);
|
|
218
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
219
|
+
|
|
220
|
+
this.clearAllCallbacks();
|
|
209
221
|
|
|
210
222
|
return item;
|
|
211
223
|
}
|
|
@@ -233,7 +245,11 @@ export default class Model extends Request {
|
|
|
233
245
|
item = this.constructor.fromJson(this._response.data);
|
|
234
246
|
}
|
|
235
247
|
|
|
236
|
-
|
|
248
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
249
|
+
responseEventsHandler.setOnSuccessData(item);
|
|
250
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
251
|
+
|
|
252
|
+
this.clearAllCallbacks();
|
|
237
253
|
|
|
238
254
|
return item;
|
|
239
255
|
}
|
|
@@ -252,7 +268,10 @@ export default class Model extends Request {
|
|
|
252
268
|
|
|
253
269
|
this._connection.removeRequest(this);
|
|
254
270
|
|
|
255
|
-
|
|
271
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
272
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
273
|
+
|
|
274
|
+
this.clearAllCallbacks();
|
|
256
275
|
|
|
257
276
|
return !! this._response.data;
|
|
258
277
|
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import ResponseEvents from '../response/responseEvents';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @mixin responseEventsMixin
|
|
5
|
+
*/
|
|
6
|
+
const responseEventsMixin = {
|
|
7
|
+
/**
|
|
8
|
+
* Constructor of the mixin.
|
|
9
|
+
*
|
|
10
|
+
* @private
|
|
11
|
+
*/
|
|
12
|
+
_initResponseEvents () {
|
|
13
|
+
this._responseEvents = new ResponseEvents();
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Add `onSuccess` callback to be called after the request.
|
|
18
|
+
*
|
|
19
|
+
* @param {function()} callback
|
|
20
|
+
*
|
|
21
|
+
* @returns {this} The current instance.
|
|
22
|
+
*/
|
|
23
|
+
onSuccess (callback = () => {}) {
|
|
24
|
+
this._responseEvents.addOnSuccessCallback(callback);
|
|
25
|
+
|
|
26
|
+
return this;
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Add `onError` callback to be called after the request.
|
|
31
|
+
*
|
|
32
|
+
* @param {function()} callback
|
|
33
|
+
*
|
|
34
|
+
* @returns {this} The current instance.
|
|
35
|
+
*/
|
|
36
|
+
onError (callback = () => {}) {
|
|
37
|
+
this._responseEvents.addOnErrorCallback(callback);
|
|
38
|
+
|
|
39
|
+
return this;
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Add `onValidationError` callback to be called after the request.
|
|
44
|
+
*
|
|
45
|
+
* @param {function()} callback
|
|
46
|
+
*
|
|
47
|
+
* @returns {this} The current instance.
|
|
48
|
+
*/
|
|
49
|
+
onValidationError (callback = () => {}) {
|
|
50
|
+
this._responseEvents.addOnValidationErrorCallback(callback);
|
|
51
|
+
|
|
52
|
+
return this;
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Add `onUnauthorized` callback to be called after the request.
|
|
57
|
+
*
|
|
58
|
+
* @param {function()} callback
|
|
59
|
+
*
|
|
60
|
+
* @returns {this} The current instance.
|
|
61
|
+
*/
|
|
62
|
+
onUnauthorized (callback = () => {}) {
|
|
63
|
+
this._responseEvents.addOnUnauthorizedCallback(callback);
|
|
64
|
+
|
|
65
|
+
return this;
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Add `onForbidden` callback to be called after the request.
|
|
70
|
+
*
|
|
71
|
+
* @param {function()} callback
|
|
72
|
+
*
|
|
73
|
+
* @returns {this} The current instance.
|
|
74
|
+
*/
|
|
75
|
+
onForbidden (callback = () => {}) {
|
|
76
|
+
this._responseEvents.addOnForbiddenCallback(callback);
|
|
77
|
+
|
|
78
|
+
return this;
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Add `onFinished` callback to be called after the request.
|
|
83
|
+
*
|
|
84
|
+
* @param {function()} callback
|
|
85
|
+
*
|
|
86
|
+
* @returns {this} The current instance.
|
|
87
|
+
*/
|
|
88
|
+
onFinished (callback = () => {}) {
|
|
89
|
+
this._responseEvents.addOnFinishedCallback(callback);
|
|
90
|
+
|
|
91
|
+
return this;
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Add `onCancelled` callback to be called after the request.
|
|
96
|
+
*
|
|
97
|
+
* @param {function()} callback
|
|
98
|
+
*
|
|
99
|
+
* @returns {this} The current instance.
|
|
100
|
+
*/
|
|
101
|
+
onCancelled (callback = () => {}) {
|
|
102
|
+
this._responseEvents.addOnCancelledCallback(callback);
|
|
103
|
+
|
|
104
|
+
return this;
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Clears all callbacks.
|
|
109
|
+
*
|
|
110
|
+
* @returns {this} The current instance.
|
|
111
|
+
*/
|
|
112
|
+
clearAllCallbacks () {
|
|
113
|
+
this._responseEvents.clearAllCallbacks();
|
|
114
|
+
|
|
115
|
+
return this;
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Clears all `onSuccess` callbacks.
|
|
120
|
+
*
|
|
121
|
+
* @returns {this} The current instance.
|
|
122
|
+
*/
|
|
123
|
+
clearOnSuccessCallbacks () {
|
|
124
|
+
this._responseEvents.clearOnSuccessCallbacks();
|
|
125
|
+
|
|
126
|
+
return this;
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Clears all `onError` callbacks.
|
|
131
|
+
*
|
|
132
|
+
* @returns {this} The current instance.
|
|
133
|
+
*/
|
|
134
|
+
clearOnErrorCallbacks () {
|
|
135
|
+
this._responseEvents.clearOnErrorCallbacks();
|
|
136
|
+
|
|
137
|
+
return this;
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Clears all `onValidationError` callbacks.
|
|
142
|
+
*
|
|
143
|
+
* @returns {this} The current instance.
|
|
144
|
+
*/
|
|
145
|
+
clearOnValidationErrorCallbacks () {
|
|
146
|
+
this._responseEvents.clearOnValidationErrorCallbacks();
|
|
147
|
+
|
|
148
|
+
return this;
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Clears all `onUnauthorized` callbacks.
|
|
153
|
+
*
|
|
154
|
+
* @returns {this} The current instance.
|
|
155
|
+
*/
|
|
156
|
+
clearOnUnauthorizedCallbacks () {
|
|
157
|
+
this._responseEvents.clearOnUnauthorizedCallbacks();
|
|
158
|
+
|
|
159
|
+
return this;
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Clears all `onForbidden` callbacks.
|
|
164
|
+
*
|
|
165
|
+
* @returns {this} The current instance.
|
|
166
|
+
*/
|
|
167
|
+
clearOnForbiddenCallbacks () {
|
|
168
|
+
this._responseEvents.clearOnForbiddenCallbacks();
|
|
169
|
+
|
|
170
|
+
return this;
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Clears all `onFinished` callbacks.
|
|
175
|
+
*
|
|
176
|
+
* @returns {this} The current instance.
|
|
177
|
+
*/
|
|
178
|
+
clearOnFinishedCallbacks () {
|
|
179
|
+
this._responseEvents.clearOnFinishedCallbacks();
|
|
180
|
+
|
|
181
|
+
return this;
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Clears all `onCancelled` callbacks.
|
|
186
|
+
*
|
|
187
|
+
* @returns {this} The current instance.
|
|
188
|
+
*/
|
|
189
|
+
clearOnCancelledCallbacks () {
|
|
190
|
+
this._responseEvents.clearOnCancelledCallbacks();
|
|
191
|
+
|
|
192
|
+
return this;
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
export default responseEventsMixin;
|
package/src/request.js
CHANGED
|
@@ -8,12 +8,15 @@ import actionMixin from './mixins/actionMixin';
|
|
|
8
8
|
import filterMixin from './mixins/filterMixin';
|
|
9
9
|
import modifierMixin from './mixins/modifierMixin';
|
|
10
10
|
import paginationMixin from './mixins/paginationMixin';
|
|
11
|
+
import responseEventsMixin from './mixins/responseEventsMixin';
|
|
12
|
+
import ResponseEventsHandler from './response/responseEventsHandler';
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* @mixes actionMixin
|
|
14
16
|
* @mixes filterMixin
|
|
15
17
|
* @mixes modifierMixin
|
|
16
18
|
* @mixes paginationMixin
|
|
19
|
+
* @mixes responseEventsMixin
|
|
17
20
|
*/
|
|
18
21
|
export default class Request {
|
|
19
22
|
constructor () {
|
|
@@ -24,14 +27,7 @@ export default class Request {
|
|
|
24
27
|
this._modifiers = new Modifiers();
|
|
25
28
|
this._pagination = new Pagination();
|
|
26
29
|
this._customParameters = [];
|
|
27
|
-
|
|
28
|
-
this._onSuccessCallbacks = [];
|
|
29
|
-
this._onErrorCallbacks = [];
|
|
30
|
-
this._onValidationErrorCallbacks = [];
|
|
31
|
-
this._onUnauthorizedCallbacks = [];
|
|
32
|
-
this._onForbiddenCallbacks = [];
|
|
33
|
-
this._onFinishedCallbacks = [];
|
|
34
|
-
this._onCancelledCallbacks = [];
|
|
30
|
+
this._initResponseEvents();
|
|
35
31
|
|
|
36
32
|
this._connection = new Connection();
|
|
37
33
|
|
|
@@ -95,7 +91,10 @@ export default class Request {
|
|
|
95
91
|
|
|
96
92
|
this._connection.removeRequest(this);
|
|
97
93
|
|
|
98
|
-
|
|
94
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
95
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
96
|
+
|
|
97
|
+
this.clearAllCallbacks();
|
|
99
98
|
|
|
100
99
|
return this;
|
|
101
100
|
}
|
|
@@ -117,7 +116,10 @@ export default class Request {
|
|
|
117
116
|
|
|
118
117
|
this._connection.removeRequest(this);
|
|
119
118
|
|
|
120
|
-
|
|
119
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
120
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
121
|
+
|
|
122
|
+
this.clearAllCallbacks();
|
|
121
123
|
|
|
122
124
|
return this;
|
|
123
125
|
}
|
|
@@ -139,7 +141,10 @@ export default class Request {
|
|
|
139
141
|
|
|
140
142
|
this._connection.removeRequest(this);
|
|
141
143
|
|
|
142
|
-
|
|
144
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
145
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
146
|
+
|
|
147
|
+
this.clearAllCallbacks();
|
|
143
148
|
|
|
144
149
|
return this;
|
|
145
150
|
}
|
|
@@ -158,7 +163,10 @@ export default class Request {
|
|
|
158
163
|
|
|
159
164
|
this._connection.removeRequest(this);
|
|
160
165
|
|
|
161
|
-
|
|
166
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
167
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
168
|
+
|
|
169
|
+
this.clearAllCallbacks();
|
|
162
170
|
|
|
163
171
|
return this;
|
|
164
172
|
}
|
|
@@ -200,7 +208,10 @@ export default class Request {
|
|
|
200
208
|
},
|
|
201
209
|
});
|
|
202
210
|
|
|
203
|
-
|
|
211
|
+
const responseEventsHandler = this._createResponseEventsHandler();
|
|
212
|
+
await responseEventsHandler.triggerResponseEvents(this._response);
|
|
213
|
+
|
|
214
|
+
this.clearAllCallbacks();
|
|
204
215
|
|
|
205
216
|
return this;
|
|
206
217
|
}
|
|
@@ -219,205 +230,15 @@ export default class Request {
|
|
|
219
230
|
}
|
|
220
231
|
|
|
221
232
|
/**
|
|
222
|
-
* @
|
|
223
|
-
*
|
|
224
|
-
* @returns {this} The current instance.
|
|
225
|
-
*/
|
|
226
|
-
onSuccess (callback = () => {}) {
|
|
227
|
-
this._onSuccessCallbacks.push(callback);
|
|
228
|
-
|
|
229
|
-
return this;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* @param {function(Response)} callback
|
|
234
|
-
*
|
|
235
|
-
* @returns {this} The current instance.
|
|
236
|
-
*/
|
|
237
|
-
onError (callback = () => {}) {
|
|
238
|
-
this._onErrorCallbacks.push(callback);
|
|
239
|
-
|
|
240
|
-
return this;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* @param {function(Response.validation)} callback
|
|
245
|
-
*
|
|
246
|
-
* @returns {this} The current instance.
|
|
247
|
-
*/
|
|
248
|
-
onValidationError (callback = () => {}) {
|
|
249
|
-
this._onValidationErrorCallbacks.push(callback);
|
|
250
|
-
|
|
251
|
-
return this;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* @param {function(Response)} callback
|
|
256
|
-
*
|
|
257
|
-
* @returns {this} The current instance.
|
|
258
|
-
*/
|
|
259
|
-
onUnauthorized (callback = () => {}) {
|
|
260
|
-
this._onUnauthorizedCallbacks.push(callback);
|
|
261
|
-
|
|
262
|
-
return this;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* @param {function(Response)} callback
|
|
267
|
-
*
|
|
268
|
-
* @returns {this} The current instance.
|
|
269
|
-
*/
|
|
270
|
-
onForbidden (callback = () => {}) {
|
|
271
|
-
this._onForbiddenCallbacks.push(callback);
|
|
272
|
-
|
|
273
|
-
return this;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* @param {function(Response)} callback
|
|
278
|
-
*
|
|
279
|
-
* @returns {this} The current instance.
|
|
280
|
-
*/
|
|
281
|
-
onFinished (callback = () => {}) {
|
|
282
|
-
this._onFinishedCallbacks.push(callback);
|
|
283
|
-
|
|
284
|
-
return this;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* @param {function(Response)} callback
|
|
289
|
-
*
|
|
290
|
-
* @returns {this} The current instance.
|
|
291
|
-
*/
|
|
292
|
-
onCancelled (callback = () => {}) {
|
|
293
|
-
this._onCancelledCallbacks.push(callback);
|
|
294
|
-
|
|
295
|
-
return this;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Clears all `onSuccess` callbacks.
|
|
300
|
-
*
|
|
301
|
-
* @returns {this} The current instance.
|
|
233
|
+
* @returns {ResponseEventsHandler}
|
|
234
|
+
* @private
|
|
302
235
|
*/
|
|
303
|
-
|
|
304
|
-
|
|
236
|
+
_createResponseEventsHandler () {
|
|
237
|
+
const responseEventsHandler = new ResponseEventsHandler();
|
|
238
|
+
responseEventsHandler.addResponseEvents(this._connection._api._responseEvents);
|
|
239
|
+
responseEventsHandler.addResponseEvents(this._responseEvents);
|
|
305
240
|
|
|
306
|
-
return
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Clears all `onError` callbacks.
|
|
311
|
-
*
|
|
312
|
-
* @returns {this} The current instance.
|
|
313
|
-
*/
|
|
314
|
-
clearOnErrorCallbacks () {
|
|
315
|
-
this._onErrorCallbacks = [];
|
|
316
|
-
|
|
317
|
-
return this;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Clears all `onValidationError` callbacks.
|
|
322
|
-
*
|
|
323
|
-
* @returns {this} The current instance.
|
|
324
|
-
*/
|
|
325
|
-
clearOnValidationErrorCallbacks () {
|
|
326
|
-
this._onValidationErrorCallbacks = [];
|
|
327
|
-
|
|
328
|
-
return this;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* Clears all `onUnauthorized` callbacks.
|
|
333
|
-
*
|
|
334
|
-
* @returns {this} The current instance.
|
|
335
|
-
*/
|
|
336
|
-
clearOnUnauthorizedCallbacks () {
|
|
337
|
-
this._onUnauthorizedCallbacks = [];
|
|
338
|
-
|
|
339
|
-
return this;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Clears all `onForbidden` callbacks.
|
|
344
|
-
*
|
|
345
|
-
* @returns {this} The current instance.
|
|
346
|
-
*/
|
|
347
|
-
clearOnForbiddenCallbacks () {
|
|
348
|
-
this._onForbiddenCallbacks = [];
|
|
349
|
-
|
|
350
|
-
return this;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Clears all `onFinished` callbacks.
|
|
355
|
-
*
|
|
356
|
-
* @returns {this} The current instance.
|
|
357
|
-
*/
|
|
358
|
-
clearOnFinishedCallbacks () {
|
|
359
|
-
this._onFinishedCallbacks = [];
|
|
360
|
-
|
|
361
|
-
return this;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* Clears all `onCancelled` callbacks.
|
|
366
|
-
*
|
|
367
|
-
* @returns {this} The current instance.
|
|
368
|
-
*/
|
|
369
|
-
clearOnCancelledCallbacks () {
|
|
370
|
-
this._onCancelledCallbacks = [];
|
|
371
|
-
|
|
372
|
-
return this;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* @param {Response} response
|
|
377
|
-
* @param {*} successResponse
|
|
378
|
-
*/
|
|
379
|
-
async triggerResponseEvents (response, successResponse = null) {
|
|
380
|
-
function executeCallbacks(callbacks, ...data) {
|
|
381
|
-
return Promise.all(callbacks.map(async callback => {
|
|
382
|
-
await callback(...data);
|
|
383
|
-
}))
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
387
|
-
await executeCallbacks(this._onSuccessCallbacks, ...[successResponse, response.data].filter((value) => !! value));
|
|
388
|
-
} else {
|
|
389
|
-
switch (response.statusCode) {
|
|
390
|
-
case 401:
|
|
391
|
-
await executeCallbacks(this._onUnauthorizedCallbacks, response);
|
|
392
|
-
break;
|
|
393
|
-
case 403:
|
|
394
|
-
await executeCallbacks(this._onForbiddenCallbacks, response);
|
|
395
|
-
break;
|
|
396
|
-
case 422:
|
|
397
|
-
const validation = {
|
|
398
|
-
message: response.validation.message,
|
|
399
|
-
errors: {},
|
|
400
|
-
};
|
|
401
|
-
|
|
402
|
-
_.each(response.validation.errors, (value, key) => {
|
|
403
|
-
return _.set(validation.errors, key, value);
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
await executeCallbacks(this._onValidationErrorCallbacks, validation);
|
|
407
|
-
break;
|
|
408
|
-
default:
|
|
409
|
-
await executeCallbacks(this._onErrorCallbacks, response);
|
|
410
|
-
break;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
if (response.isFinished) {
|
|
415
|
-
await executeCallbacks(this._onFinishedCallbacks, response);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
if (response.isCancelled) {
|
|
419
|
-
await executeCallbacks(this._onCancelledCallbacks, response);
|
|
420
|
-
}
|
|
241
|
+
return responseEventsHandler;
|
|
421
242
|
}
|
|
422
243
|
|
|
423
244
|
/**
|
|
@@ -488,3 +309,4 @@ Object.assign(Request.prototype, actionMixin);
|
|
|
488
309
|
Object.assign(Request.prototype, filterMixin);
|
|
489
310
|
Object.assign(Request.prototype, modifierMixin);
|
|
490
311
|
Object.assign(Request.prototype, paginationMixin);
|
|
312
|
+
Object.assign(Request.prototype, responseEventsMixin);
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
export default class ResponseEvents {
|
|
2
|
+
constructor() {
|
|
3
|
+
this._onSuccessCallbacks = [];
|
|
4
|
+
this._onErrorCallbacks = [];
|
|
5
|
+
this._onValidationErrorCallbacks = [];
|
|
6
|
+
this._onUnauthorizedCallbacks = [];
|
|
7
|
+
this._onForbiddenCallbacks = [];
|
|
8
|
+
this._onFinishedCallbacks = [];
|
|
9
|
+
this._onCancelledCallbacks = [];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {function()} callback
|
|
14
|
+
*
|
|
15
|
+
* @returns {this} The current instance.
|
|
16
|
+
*/
|
|
17
|
+
addOnSuccessCallback (callback = () => {}) {
|
|
18
|
+
this._onSuccessCallbacks.push(callback);
|
|
19
|
+
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {function()} callback
|
|
25
|
+
*
|
|
26
|
+
* @returns {this} The current instance.
|
|
27
|
+
*/
|
|
28
|
+
addOnErrorCallback (callback = () => {}) {
|
|
29
|
+
this._onErrorCallbacks.push(callback);
|
|
30
|
+
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {function()} callback
|
|
36
|
+
*
|
|
37
|
+
* @returns {this} The current instance.
|
|
38
|
+
*/
|
|
39
|
+
addOnValidationErrorCallback (callback = () => {}) {
|
|
40
|
+
this._onValidationErrorCallbacks.push(callback);
|
|
41
|
+
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param {function()} callback
|
|
47
|
+
*
|
|
48
|
+
* @returns {this} The current instance.
|
|
49
|
+
*/
|
|
50
|
+
addOnUnauthorizedCallback (callback = () => {}) {
|
|
51
|
+
this._onUnauthorizedCallbacks.push(callback);
|
|
52
|
+
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {function()} callback
|
|
58
|
+
*
|
|
59
|
+
* @returns {this} The current instance.
|
|
60
|
+
*/
|
|
61
|
+
addOnForbiddenCallback (callback = () => {}) {
|
|
62
|
+
this._onForbiddenCallbacks.push(callback);
|
|
63
|
+
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @param {function()} callback
|
|
69
|
+
*
|
|
70
|
+
* @returns {this} The current instance.
|
|
71
|
+
*/
|
|
72
|
+
addOnFinishedCallback (callback = () => {}) {
|
|
73
|
+
this._onFinishedCallbacks.push(callback);
|
|
74
|
+
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param {function()} callback
|
|
80
|
+
*
|
|
81
|
+
* @returns {this} The current instance.
|
|
82
|
+
*/
|
|
83
|
+
addOnCancelledCallback (callback = () => {}) {
|
|
84
|
+
this._onCancelledCallbacks.push(callback);
|
|
85
|
+
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Clears all `onSuccess` callbacks.
|
|
91
|
+
*
|
|
92
|
+
* @returns {this} The current instance.
|
|
93
|
+
*/
|
|
94
|
+
clearOnSuccessCallbacks () {
|
|
95
|
+
this._onSuccessCallbacks = [];
|
|
96
|
+
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Clears all `onError` callbacks.
|
|
102
|
+
*
|
|
103
|
+
* @returns {this} The current instance.
|
|
104
|
+
*/
|
|
105
|
+
clearOnErrorCallbacks () {
|
|
106
|
+
this._onErrorCallbacks = [];
|
|
107
|
+
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Clears all `onValidationError` callbacks.
|
|
113
|
+
*
|
|
114
|
+
* @returns {this} The current instance.
|
|
115
|
+
*/
|
|
116
|
+
clearOnValidationErrorCallbacks () {
|
|
117
|
+
this._onValidationErrorCallbacks = [];
|
|
118
|
+
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Clears all `onUnauthorized` callbacks.
|
|
124
|
+
*
|
|
125
|
+
* @returns {this} The current instance.
|
|
126
|
+
*/
|
|
127
|
+
clearOnUnauthorizedCallbacks () {
|
|
128
|
+
this._onUnauthorizedCallbacks = [];
|
|
129
|
+
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Clears all `onForbidden` callbacks.
|
|
135
|
+
*
|
|
136
|
+
* @returns {this} The current instance.
|
|
137
|
+
*/
|
|
138
|
+
clearOnForbiddenCallbacks () {
|
|
139
|
+
this._onForbiddenCallbacks = [];
|
|
140
|
+
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Clears all `onFinished` callbacks.
|
|
146
|
+
*
|
|
147
|
+
* @returns {this} The current instance.
|
|
148
|
+
*/
|
|
149
|
+
clearOnFinishedCallbacks () {
|
|
150
|
+
this._onFinishedCallbacks = [];
|
|
151
|
+
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Clears all `onCancelled` callbacks.
|
|
157
|
+
*
|
|
158
|
+
* @returns {this} The current instance.
|
|
159
|
+
*/
|
|
160
|
+
clearOnCancelledCallbacks () {
|
|
161
|
+
this._onCancelledCallbacks = [];
|
|
162
|
+
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Clears all callbacks.
|
|
168
|
+
*
|
|
169
|
+
* @returns {this} The current instance.
|
|
170
|
+
*/
|
|
171
|
+
clearAllCallbacks () {
|
|
172
|
+
this.clearOnSuccessCallbacks()
|
|
173
|
+
.clearOnErrorCallbacks()
|
|
174
|
+
.clearOnValidationErrorCallbacks()
|
|
175
|
+
.clearOnUnauthorizedCallbacks()
|
|
176
|
+
.clearOnForbiddenCallbacks()
|
|
177
|
+
.clearOnFinishedCallbacks()
|
|
178
|
+
.clearOnCancelledCallbacks();
|
|
179
|
+
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
export default class ResponseEventsHandler {
|
|
2
|
+
constructor () {
|
|
3
|
+
this._responseEventsList = [];
|
|
4
|
+
|
|
5
|
+
this._onSuccessData = null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {ResponseEvents} responseEvents
|
|
10
|
+
* @returns {ResponseEventsHandler}
|
|
11
|
+
*/
|
|
12
|
+
addResponseEvents (responseEvents) {
|
|
13
|
+
this._responseEventsList.push(responseEvents);
|
|
14
|
+
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {*} data
|
|
20
|
+
* @returns {ResponseEventsHandler}
|
|
21
|
+
*/
|
|
22
|
+
setOnSuccessData (data) {
|
|
23
|
+
this._onSuccessData = data;
|
|
24
|
+
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {Response} response
|
|
30
|
+
*/
|
|
31
|
+
async triggerResponseEvents (response) {
|
|
32
|
+
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
33
|
+
await this._executeOnSuccessCallbacks(response);
|
|
34
|
+
} else {
|
|
35
|
+
switch (response.statusCode) {
|
|
36
|
+
case 401:
|
|
37
|
+
await this._executeOnUnauthorizedCallbacks(response);
|
|
38
|
+
break;
|
|
39
|
+
case 403:
|
|
40
|
+
await this._executeOnForbiddenCallbacks(response);
|
|
41
|
+
break;
|
|
42
|
+
case 422:
|
|
43
|
+
await this._executeOnValidationErrorCallbacks(response);
|
|
44
|
+
break;
|
|
45
|
+
default:
|
|
46
|
+
await this._executeOnErrorCallbacks(response);
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (response.isFinished) {
|
|
52
|
+
await this._executeOnFinishedCallbacks(response);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (response.isCancelled) {
|
|
56
|
+
await this._executeOnCancelledCallbacks(response);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {function[]} callbacks
|
|
62
|
+
* @param data
|
|
63
|
+
* @returns {Promise}
|
|
64
|
+
* @private
|
|
65
|
+
*/
|
|
66
|
+
_executeCallbacks(callbacks, ...data) {
|
|
67
|
+
return Promise.all(callbacks.map(async callback => {
|
|
68
|
+
await callback(...data);
|
|
69
|
+
}))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {Response} response
|
|
74
|
+
*/
|
|
75
|
+
async _executeOnSuccessCallbacks (response) {
|
|
76
|
+
await this._executeCallbacks(
|
|
77
|
+
this._responseEventsList.flatMap((responseEvent) => responseEvent._onSuccessCallbacks),
|
|
78
|
+
...[this._onSuccessData, response.data].filter((value) => !! value)
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {Response} response
|
|
84
|
+
*/
|
|
85
|
+
async _executeOnErrorCallbacks (response) {
|
|
86
|
+
await this._executeCallbacks(
|
|
87
|
+
this._responseEventsList.flatMap((responseEvent) => responseEvent._onErrorCallbacks),
|
|
88
|
+
response
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @param {Response} response
|
|
94
|
+
*/
|
|
95
|
+
async _executeOnValidationErrorCallbacks (response) {
|
|
96
|
+
const validation = {
|
|
97
|
+
message: response.validation.message,
|
|
98
|
+
errors: {},
|
|
99
|
+
};
|
|
100
|
+
_.each(response.validation.errors, (value, key) => {
|
|
101
|
+
return _.set(validation.errors, key, value);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
await this._executeCallbacks(
|
|
105
|
+
this._responseEventsList.flatMap((responseEvent) => responseEvent._onValidationErrorCallbacks),
|
|
106
|
+
validation
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @param {Response} response
|
|
112
|
+
*/
|
|
113
|
+
async _executeOnUnauthorizedCallbacks (response) {
|
|
114
|
+
await this._executeCallbacks(
|
|
115
|
+
this._responseEventsList.flatMap((responseEvent) => responseEvent._onUnauthorizedCallbacks),
|
|
116
|
+
response
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @param {Response} response
|
|
122
|
+
*/
|
|
123
|
+
async _executeOnForbiddenCallbacks (response) {
|
|
124
|
+
await this._executeCallbacks(
|
|
125
|
+
this._responseEventsList.flatMap((responseEvent) => responseEvent._onForbiddenCallbacks),
|
|
126
|
+
response
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {Response} response
|
|
132
|
+
*/
|
|
133
|
+
async _executeOnFinishedCallbacks (response) {
|
|
134
|
+
await this._executeCallbacks(
|
|
135
|
+
this._responseEventsList.flatMap((responseEvent) => responseEvent._onFinishedCallbacks),
|
|
136
|
+
response
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {Response} response
|
|
142
|
+
*/
|
|
143
|
+
async _executeOnCancelledCallbacks (response) {
|
|
144
|
+
await this._executeCallbacks(
|
|
145
|
+
this._responseEventsList.flatMap((responseEvent) => responseEvent._onCancelledCallbacks),
|
|
146
|
+
response
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|