axios-annotations 1.0.0 → 1.1.0
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/README.md
CHANGED
|
@@ -47,6 +47,13 @@ export default class TestService extends Service {
|
|
|
47
47
|
p2: data2
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
|
+
|
|
51
|
+
path(id) {
|
|
52
|
+
return this.requestWith("GET", "/path3/{id}")
|
|
53
|
+
.send({
|
|
54
|
+
id: data1
|
|
55
|
+
});
|
|
56
|
+
}
|
|
50
57
|
|
|
51
58
|
basic() {
|
|
52
59
|
return this.request("POST", "/path3?p1=a&p2=b", {field1: 'c', field: 'd'},
|
|
@@ -122,6 +129,8 @@ export default class TestService extends Service {
|
|
|
122
129
|
}
|
|
123
130
|
}
|
|
124
131
|
```
|
|
132
|
+
如果不爽部分IDE的`non-promise inspection info`下划线,也可以给方法加上`async`。
|
|
133
|
+
|
|
125
134
|
|
|
126
135
|
### QueryString Encoding
|
|
127
136
|
`key-values pair`转查询串算法,运行环境不支持`URLSearchParams`时使用默认算法,也可以自定义。
|
|
@@ -138,8 +147,6 @@ if (typeof URLSearchParams === "undefined") {
|
|
|
138
147
|
}
|
|
139
148
|
```
|
|
140
149
|
|
|
141
|
-
如果不爽部分IDE的`non-promise inspection info`下划线,也可以给方法加上`async`。
|
|
142
|
-
|
|
143
150
|
## Configuration
|
|
144
151
|
|
|
145
152
|
### Custom Config
|
|
@@ -446,6 +453,38 @@ import {authorizer} from "/path/config.js";
|
|
|
446
453
|
#### RequestBody(name)
|
|
447
454
|
+ name : string `方法返回值属性,默认为 body`
|
|
448
455
|
|
|
456
|
+
|
|
457
|
+
### Authorizer (Optional Plugin)
|
|
458
|
+
+ sessionKey : string `键值名称`
|
|
459
|
+
```javascript
|
|
460
|
+
authorizer.sessionKey = "$_SESSION"; // default value
|
|
461
|
+
```
|
|
462
|
+
+ getSession : Promise<Session> `获取授权信息`
|
|
463
|
+
```javascript
|
|
464
|
+
function onLogin(){
|
|
465
|
+
authorizer.getSession().then(session => {
|
|
466
|
+
// fetch other info / redirect to other page
|
|
467
|
+
store.dispatch('SAVE_USER_INFO' , info);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
```
|
|
471
|
+
+ checkResponse(response:AxiosResponse) : boolean `检查授权是否过期`
|
|
472
|
+
```javascript
|
|
473
|
+
class OAuth2Authorizer extends Authorizer {
|
|
474
|
+
checkResponse(response){
|
|
475
|
+
return response.stauts === 401; // default implement
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
+ withAuthentication(request: AxiosRequestConfig, session: Session) : void `请求附加认证信息,默认附加请求头`
|
|
481
|
+
```javascript
|
|
482
|
+
class OAuth2Authorizer extends Authorizer {
|
|
483
|
+
withAuthentication(request, session){
|
|
484
|
+
request.headers.Authorization = 'Bearer 0x123456';
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
```
|
|
449
488
|
## 运行环境
|
|
450
489
|
部分运行环境,例如微信小程序,`axios`需要降级。<br>
|
|
451
490
|
微信小程序:
|
package/lib/core/service.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=exports.ConfigMapping=void 0;var _common=require("./common"),_parser=_interopRequireDefault(require("./parser")),_config=require("./config");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _createForOfIteratorHelper(e,t){var r,n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=_unsupportedIterableToArray(e))||t&&e&&"number"==typeof e.length)return n&&(e=n),r=0,{s:t=function(){},n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:t};throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,o=!0,a=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){a=!0,i=e},f:function(){try{o||null==n.return||n.return()}finally{if(a)throw i}}}}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Map"===(r="Object"===r&&e.constructor?e.constructor.name:r)||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var ConfigMapping={requestHeaders:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:[],r={};if(e)for(var n in e){var i=e[n];r[n]="function"==typeof i?i.apply(void 0,t):i}return r},axiosConfig:function(e,t){var r={};return(e||[]).forEach(function(e){"function"==typeof e?Object.assign(r,e.apply(void 0,t)):Object.assign(r,e)}),r},querystring:function(e,t){if(e){var r,n=_parser.default.decode(t);for(r in t){var i=t[r],o=e[r];!o||o.body?_parser.default.delete(n,r):o.required?(0,_common.isNullOrEmpty)(i)&&!_parser.default.has(n,r)&&_parser.default.append(n,r,""):(0,_common.isNullOrEmpty)(i)&&_parser.default.has(n,r)&&_parser.default.delete(n,r)}return _parser.default.encode(n)}return""},body:function(e,t){if(e)for(var r in e)if(!0===e[r].body)return(0,_common.isNullOrEmpty)(t)?void 0:t[r];return null}},Service=(exports.ConfigMapping=ConfigMapping,function(){function n(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null;if(_classCallCheck(this,n),_defineProperty(this,"_path",""),_defineProperty(this,"_config",_config.config),_defineProperty(this,"_headers",{}),_defineProperty(this,"_params",{}),_defineProperty(this,"_configs",{}),_defineProperty(this,"_once",[]),!this._path&&e&&(this._path=e),"function"==typeof Object.getPrototypeOf?(Object.getPrototypeOf(this)._path&&(this._path=Object.getPrototypeOf(this)._path),Object.getPrototypeOf(this)._config&&(this._config=Object.getPrototypeOf(this)._config)):(this.__proto__._path&&(this._path=this.__proto__._path),this.__proto__._config&&(this._config=this.__proto__._config)),this.config&&this.config.plugins&&this.config.plugins.length){var t,r=_createForOfIteratorHelper(this.config.plugins);try{for(r.s();!(t=r.n()).done;)(0,t.value)(this.config)}catch(e){r.e(e)}finally{r.f()}}}return _createClass(n,[{key:"config",get:function(){return this._config},set:function(e){this._config=e}},{key:"path",get:function(){return this._path},set:function(e){this._path=e}},{key:"params",value:function(e,t){var r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{required:!1};if(!this._params||!this._params[e]||!Object.hasOwnProperty.call(this._params[e],t)){var r=Object.assign({required:!1,body:!1},r),n=this._params||{},i=this._params&&this._params[e]||{},o=i[t]||{};if(!0===r.body)for(var a in i)i[a].body=!1;this._params=Object.assign(n,_defineProperty({},e,Object.assign(i,_defineProperty({},t,Object.assign(o,r)))))}}},{key:"headers",value:function(e,t,r){var n=this._headers||{};Object.assign(n,_defineProperty({},e,Object.assign(n[e]||{},_defineProperty({},t,r))))}},{key:"configs",value:function(e,t){var r=this._configs||{};Object.assign(r,_defineProperty({},e,(r[e]||[]).concat(t)))}},{key:"pathVariable",value:function(e,t){var r=e,e=r.match(/{\w+}/g);return Array.isArray(e)&&e.forEach(function(e){e=e.substring(1,e.length-1);r=r.replaceAll("{".concat(e,"}"),t[e])}),r}},{key:"createRequestConfig",value:function(e,t,r){var n=3<arguments.length&&void 0!==arguments[3]?arguments[3]:[],i=4<arguments.length&&void 0!==arguments[4]?arguments[4]:[],o=ConfigMapping.querystring(this._params[e],r),r=ConfigMapping.body(this._params[e],r),n=ConfigMapping.requestHeaders(this._headers[e],n),e=ConfigMapping.axiosConfig(this._configs[e],i),i="".concat(t).concat(o?"?"+o:"");return Object.assign(e,{headers:Object.assign(n,e.headers||null)}),{path:i,body:r,config:e}}},{key:"request",value:function(e,t){var r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{},n=3<arguments.length&&void 0!==arguments[3]?arguments[3]:{},t=0<=t.indexOf("http")?t:this.config.baseURL+(0,_common.normalizePath)("/".concat(this.path||"","/").concat(t));return this.config.axios.request(Object.assign({method:e,url:t,data:r},n))}},{key:"requestWith",value:function(o,a){var s=this,f={},c=[],u={},n={param:function(e){return Object.assign(f,_defineProperty({},e,Object.assign(f[e]||{},{required:1<arguments.length&&void 0!==arguments[1]&&arguments[1],body:!1}))),n},header:function(e,t){return Object.assign(u,_defineProperty({},e,t)),n},body:function(e){var t,r=f[e]||{};for(t in f)f[t].body=!1;return Object.assign(f,_defineProperty({},e,Object.assign(r,{required:!1,body:!0}))),n},config:function(e){return c.push(e),n},send:function(
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=exports.ConfigMapping=void 0;var _common=require("./common"),_parser=_interopRequireDefault(require("./parser")),_config=require("./config");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _createForOfIteratorHelper(e,t){var r,n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=_unsupportedIterableToArray(e))||t&&e&&"number"==typeof e.length)return n&&(e=n),r=0,{s:t=function(){},n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:t};throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,o=!0,a=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){a=!0,i=e},f:function(){try{o||null==n.return||n.return()}finally{if(a)throw i}}}}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Map"===(r="Object"===r&&e.constructor?e.constructor.name:r)||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var ConfigMapping={requestHeaders:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:[],r={};if(e)for(var n in e){var i=e[n];r[n]="function"==typeof i?i.apply(void 0,t):i}return r},axiosConfig:function(e,t){var r={};return(e||[]).forEach(function(e){"function"==typeof e?Object.assign(r,e.apply(void 0,t)):Object.assign(r,e)}),r},querystring:function(e,t){if(e){var r,n=_parser.default.decode(t);for(r in t){var i=t[r],o=e[r];!o||o.body?_parser.default.delete(n,r):o.required?(0,_common.isNullOrEmpty)(i)&&!_parser.default.has(n,r)&&_parser.default.append(n,r,""):(0,_common.isNullOrEmpty)(i)&&_parser.default.has(n,r)&&_parser.default.delete(n,r)}return _parser.default.encode(n)}return""},body:function(e,t){if(e)for(var r in e)if(!0===e[r].body)return(0,_common.isNullOrEmpty)(t)?void 0:t[r];return null}},Service=(exports.ConfigMapping=ConfigMapping,function(){function n(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null;if(_classCallCheck(this,n),_defineProperty(this,"_path",""),_defineProperty(this,"_config",_config.config),_defineProperty(this,"_headers",{}),_defineProperty(this,"_params",{}),_defineProperty(this,"_configs",{}),_defineProperty(this,"_once",[]),!this._path&&e&&(this._path=e),"function"==typeof Object.getPrototypeOf?(Object.getPrototypeOf(this)._path&&(this._path=Object.getPrototypeOf(this)._path),Object.getPrototypeOf(this)._config&&(this._config=Object.getPrototypeOf(this)._config)):(this.__proto__._path&&(this._path=this.__proto__._path),this.__proto__._config&&(this._config=this.__proto__._config)),this.config&&this.config.plugins&&this.config.plugins.length){var t,r=_createForOfIteratorHelper(this.config.plugins);try{for(r.s();!(t=r.n()).done;)(0,t.value)(this.config)}catch(e){r.e(e)}finally{r.f()}}}return _createClass(n,[{key:"config",get:function(){return this._config},set:function(e){this._config=e}},{key:"path",get:function(){return this._path},set:function(e){this._path=e}},{key:"params",value:function(e,t){var r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{required:!1};if(!this._params||!this._params[e]||!Object.hasOwnProperty.call(this._params[e],t)){var r=Object.assign({required:!1,body:!1},r),n=this._params||{},i=this._params&&this._params[e]||{},o=i[t]||{};if(!0===r.body)for(var a in i)i[a].body=!1;this._params=Object.assign(n,_defineProperty({},e,Object.assign(i,_defineProperty({},t,Object.assign(o,r)))))}}},{key:"headers",value:function(e,t,r){var n=this._headers||{};Object.assign(n,_defineProperty({},e,Object.assign(n[e]||{},_defineProperty({},t,r))))}},{key:"configs",value:function(e,t){var r=this._configs||{};Object.assign(r,_defineProperty({},e,(r[e]||[]).concat(t)))}},{key:"pathVariable",value:function(e,t){var r=e,e=r.match(/{\w+}/g);return Array.isArray(e)&&e.forEach(function(e){e=e.substring(1,e.length-1);r=r.replaceAll("{".concat(e,"}"),t[e])}),r}},{key:"createRequestConfig",value:function(e,t,r){var n=3<arguments.length&&void 0!==arguments[3]?arguments[3]:[],i=4<arguments.length&&void 0!==arguments[4]?arguments[4]:[],o=ConfigMapping.querystring(this._params[e],r),r=ConfigMapping.body(this._params[e],r),n=ConfigMapping.requestHeaders(this._headers[e],n),e=ConfigMapping.axiosConfig(this._configs[e],i),i="".concat(t).concat(o?"?"+o:"");return Object.assign(e,{headers:Object.assign(n,e.headers||null)}),{path:i,body:r,config:e}}},{key:"request",value:function(e,t){var r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{},n=3<arguments.length&&void 0!==arguments[3]?arguments[3]:{},t=0<=t.indexOf("http")?t:this.config.baseURL+(0,_common.normalizePath)("/".concat(this.path||"","/").concat(t));return this.config.axios.request(Object.assign({method:e,url:t,data:r},n))}},{key:"requestWith",value:function(o,a){var s=this,f={},c=[],u={},n={param:function(e){return Object.assign(f,_defineProperty({},e,Object.assign(f[e]||{},{required:1<arguments.length&&void 0!==arguments[1]&&arguments[1],body:!1}))),n},header:function(e,t){return Object.assign(u,_defineProperty({},e,t)),n},body:function(e){var t,r=f[e]||{};for(t in f)f[t].body=!1;return Object.assign(f,_defineProperty({},e,Object.assign(r,{required:!1,body:!0}))),n},config:function(e){return c.push(e),n},send:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=ConfigMapping.querystring(f,e),r=ConfigMapping.body(f,e),n=ConfigMapping.requestHeaders(u,[e]),i=ConfigMapping.axiosConfig(c,[e]),e="".concat(s.pathVariable(a,e)).concat(t?"?"+t:"");return Object.assign(i,{headers:Object.assign(n,i.headers||null)}),s.request(o,e,r,i)}};return n}}]),n}());exports.default=Service;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function RequestMapping(s){var c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null;return function(e,h,t){var p;t?(c=c||"GET",p=t.value,t.value=function(){for(var i=this,e=arguments.length,o=new Array(e),t=0;t<e;t++)o[t]=arguments[t];var n,a,r,u=p.apply(this,o);return u&&Object.hasOwnProperty.call(u,"then")&&"function"==typeof u.then?new Promise(function(a,r){u.then(function(e){var e=i.createRequestConfig(h,i.pathVariable(s,e),e,o,o),t=e.path,n=e.body,e=e.config;i.request(c,t,n,e).then(a).catch(r)})}):(n=(r=this.createRequestConfig(h,this.pathVariable(s,u),u,o,o)).path,a=r.body,r=r.config,this.request(c,n,a,r))}):e.prototype._path=s}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=RequestMapping;
|
|
1
|
+
"use strict";function RequestMapping(s){var c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null;return function(e,h,t){var p;t?(c=c||"GET",p=t.value,t.value=function(){for(var i=this,e=arguments.length,o=new Array(e),t=0;t<e;t++)o[t]=arguments[t];var n,a,r,u=p.apply(this,o)||{};return u&&Object.hasOwnProperty.call(u,"then")&&"function"==typeof u.then?new Promise(function(a,r){u.then(function(e){var e=i.createRequestConfig(h,i.pathVariable(s,e),e,o,o),t=e.path,n=e.body,e=e.config;i.request(c,t,n,e).then(a).catch(r)})}):(n=(r=this.createRequestConfig(h,this.pathVariable(s,u),u,o,o)).path,a=r.body,r=r.config,this.request(c,n,a,r))}):e.prototype._path=s}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=RequestMapping;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _storage=_interopRequireDefault(require("./storage")),_history=_interopRequireDefault(require("./history"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function asyncGeneratorStep(e,t,r,
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _storage=_interopRequireDefault(require("./storage")),_history=_interopRequireDefault(require("./history"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function asyncGeneratorStep(e,t,n,r,s,o,i){try{var a=e[o](i),u=a.value}catch(e){return void n(e)}a.done?t(u):Promise.resolve(u).then(r,s)}function _asyncToGenerator(a){return function(){var e=this,i=arguments;return new Promise(function(t,n){var r=a.apply(e,i);function s(e){asyncGeneratorStep(r,t,n,s,o,"next",e)}function o(e){asyncGeneratorStep(r,t,n,s,o,"throw",e)}s(void 0)})}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function _createClass(e,t,n){return t&&_defineProperties(e.prototype,t),n&&_defineProperties(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function _defineProperty(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var Authorizer=function(){function e(){_classCallCheck(this,e),_defineProperty(this,"_sessionKey","$_SESSION"),_defineProperty(this,"_sessionStorage",new _storage.default),_defineProperty(this,"_sessionHistory",new _history.default)}var t,n,r,s,o;return _createClass(e,[{key:"sessionKey",get:function(){return this._sessionKey},set:function(e){this._sessionKey=e}},{key:"sessionStorage",get:function(){return this._sessionStorage},set:function(e){this._sessionStorage=e}},{key:"sessionHistory",get:function(){return this._sessionHistory},set:function(e){this._sessionHistory=e}},{key:"getSession",value:(o=_asyncToGenerator(regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.sessionStorage.get(this.sessionKey);case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}},e,this)})),function(){return o.apply(this,arguments)})},{key:"storageSession",value:(s=_asyncToGenerator(regeneratorRuntime.mark(function e(t){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.sessionStorage.set(this.sessionKey,t);case 2:case"end":return e.stop()}},e,this)})),function(e){return s.apply(this,arguments)})},{key:"refreshSession",value:(r=_asyncToGenerator(regeneratorRuntime.mark(function e(t){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",null);case 1:case"end":return e.stop()}},e)})),function(e){return r.apply(this,arguments)})},{key:"withAuthentication",value:function(e,t){var n,r;t&&(n=t.access_token,r=t.accessToken,t=t.token,(n||r||t)&&(e.headers.Authorization="Bearer "+(n||r||t)))}},{key:"checkSession",value:function(e,t){e=e.headers.Authorization||e.headers.authorization,e=e&&e.split(" ")[1]||"";return(t.access_token||t.accessToken||t.token)!==e&&(this.sessionHistory.size&&this.sessionHistory.check(e),!0)}},{key:"checkResponse",value:function(e){return 401!==(e||{status:0}).status}},{key:"onAuthorizedDenied",value:(n=_asyncToGenerator(regeneratorRuntime.mark(function e(t){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:throw t;case 1:case"end":return e.stop()}},e)})),function(e){return n.apply(this,arguments)})},{key:"onSessionInvalidated",value:function(){}},{key:"invalidateSession",value:(t=_asyncToGenerator(regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.sessionStorage.remove(this.sessionKey);case 2:this.onSessionInvalidated();case 3:case"end":return e.stop()}},e,this)})),function(){return t.apply(this,arguments)})}]),e}();exports.default=Authorizer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _common=require("../../core/common");function _classCallCheck(e,
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _common=require("../../core/common");function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var i=t[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var SessionHistory=function(){function e(){_classCallCheck(this,e),_defineProperty(this,"_history",new Array(10)),_defineProperty(this,"_position",0),_defineProperty(this,"_size",0)}return _createClass(e,[{key:"size",get:function(){return this._size}},{key:"add",value:function(t){var e,r,i,n,s;Object.keys(t).every(function(e){return(0,_common.isNullOrEmpty)(t[e])})||(e=t.access_token,r=t.accessToken,i=t.token,n=t.refresh_token,s=t.refreshToken,this._history[this._position]=Object.assign({},t,{access_token:e||r||i,refresh_token:n||s,invalid:!1}),this._position++,this._position%=this._history.length,this._size=this._history.reduce(function(e,t){return t?e+1:e},0))}},{key:"check",value:function(i){return this._history.some(function(e){var t,r;return!!e&&(t=e.access_token,r=e.accessToken,e=e.token,(t||r||e)===i)})}},{key:"deprecate",value:function(e){var r=0,i=e.refresh_token,n=e.refreshToken,e=this._history.find(function(e,t){return r=t,!!e&&(e.refresh_token||e.refreshToken)===(i||n)});e&&(e.invalid=!0,e=this._history[0],this._history[0]=this._history[r],this._history[r]=e)}},{key:"clean",value:function(){for(var e=0;e<this._history.length;++e){var t=this._history[e];t&&t.invalid&&(this._history[e]=null)}this._size=this._history.reduce(function(e,t){return t?e+1:e},0)}},{key:"isDeprecated",value:function(i){return this._history.some(function(e){if(e){var t=i.refresh_token,r=i.refreshToken;if(t||r)return(e.refresh_token||e.refreshToken)===(t||r)&&e.invalid}return!1})}}]),e}();exports.default=SessionHistory;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=AuthorizationPlugin;var _queue=_interopRequireDefault(require("./queue"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function asyncGeneratorStep(e,r,t,n,s,a,o){try{var u=e[a](o),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=AuthorizationPlugin;var _queue=_interopRequireDefault(require("./queue"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function asyncGeneratorStep(e,r,t,n,s,a,o){try{var u=e[a](o),i=u.value}catch(e){return void t(e)}u.done?r(i):Promise.resolve(i).then(n,s)}function _asyncToGenerator(u){return function(){var e=this,o=arguments;return new Promise(function(r,t){var n=u.apply(e,o);function s(e){asyncGeneratorStep(n,r,t,s,a,"next",e)}function a(e){asyncGeneratorStep(n,r,t,s,a,"throw",e)}s(void 0)})}}function AuthorizationPlugin(u){return function(e){var r,t,s=!1,a=null,o=new _queue.default(u);e.axios.interceptors.response.use(function(e){return a&&(u.sessionHistory.add(a),a=null),e},(r=_asyncToGenerator(regeneratorRuntime.mark(function e(r){var t,n;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t=r.response,u.checkResponse(t)){e.next=66;break}if(s){e.next=61;break}return s=!0,e.next=6,u.getSession();case 6:if(n=e.sent,u.sessionHistory.isDeprecated(n))return e.prev=8,e.next=11,u.onAuthorizedDenied(r);e.next=21;break;case 11:u.sessionHistory.clean(),e.next=17;break;case 14:throw e.prev=14,e.t0=e.catch(8),r;case 17:return e.prev=17,s=!1,o.clear(),e.finish(17);case 21:if(a=n,u.checkSession(r.config,n)){e.next=48;break}return e.prev=23,e.t1=u,e.next=27,u.refreshSession(n);case 27:return e.t2=e.sent,e.next=30,e.t1.storageSession.call(e.t1,e.t2);case 30:e.next=48;break;case 32:return e.prev=32,e.t3=e.catch(23),u.sessionHistory.deprecate(n),e.prev=35,e.next=38,u.onAuthorizedDenied(e.t3);case 38:u.sessionHistory.clean(),e.next=44;break;case 41:throw e.prev=41,e.t4=e.catch(35),r;case 44:return e.prev=44,s=!1,o.clear(),e.finish(44);case 48:for(s=!1;o.size;)o.pop();return e.prev=50,e.next=53,o.resend(r);case 53:return e.abrupt("return",e.sent);case 56:throw e.prev=56,e.t5=e.catch(50),e.t5;case 59:e.next=64;break;case 61:return e.next=63,o.push(r);case 63:return e.abrupt("return",e.sent);case 64:e.next=67;break;case 66:throw r;case 67:case"end":return e.stop()}},e,null,[[8,14,17,21],[23,32],[35,41,44,48],[50,56]])})),function(e){return r.apply(this,arguments)})),e.axios.interceptors.request.use((t=_asyncToGenerator(regeneratorRuntime.mark(function e(r){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,u.getSession();case 2:return t=e.sent,u.withAuthentication(r,t),e.abrupt("return",r);case 5:case"end":return e.stop()}},e)})),function(e){return t.apply(this,arguments)}))}}
|