@weapnl/js-junction 0.1.1 → 0.1.2
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 +3 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/api.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## v0.1.2
|
|
6
|
+
- Fixed a bug where not all subsequent requests would be cancelled.
|
|
7
|
+
|
|
5
8
|
## v0.1.1
|
|
6
9
|
- Added functionality to add the same callback on a request multiple times.
|
|
7
10
|
- ⚠️ 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/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