axios-annotations 1.2.1 → 1.3.1

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
@@ -228,6 +228,114 @@ class AuthService extends Service {
228
228
  }
229
229
  ```
230
230
 
231
+ ## Abort
232
+ 注意:小程序端可能没有实现请求取消接口。
233
+ ### 静态注入
234
+
235
+ ```javascript
236
+ const controller = new AbortController();
237
+ // ...
238
+ class AuthService extends Service {
239
+ // ...
240
+ @RequestConfig({
241
+ signal: controller.signal
242
+ })
243
+ bar() {
244
+ // ....
245
+ return {};
246
+ }
247
+ }
248
+
249
+ // 取消请求
250
+ controller.abort()
251
+ ```
252
+
253
+ 或者:
254
+
255
+ ```javascript
256
+ const controller = new AbortController();
257
+
258
+ // ...
259
+ class AuthService extends Service {
260
+ // ...
261
+ @AbortSource(controller) bar() {
262
+ // ....
263
+ return {};
264
+ }
265
+ }
266
+
267
+ new AuthService().bar().then(() => {
268
+
269
+ }).catch(e => {
270
+ console.log(axios.isCancel(e));
271
+ });
272
+
273
+ // 取消请求
274
+ controller.abort()
275
+ ```
276
+
277
+ 兼容旧版 `CancelToken` `(deprecated)`:
278
+ ```javascript
279
+ const CancelToken = axios.CancelToken;
280
+
281
+ const controller = new AbortControllerAdapter(CancelToken);
282
+
283
+ // ...
284
+ class AuthService extends Service {
285
+ // ...
286
+ @AbortSource(controller) bar() {
287
+ // ....
288
+ return {};
289
+ }
290
+ }
291
+
292
+
293
+ new AuthService().bar().then(() => {
294
+
295
+ }).catch(e => {
296
+ console.log(axios.isCancel(e));
297
+ });
298
+
299
+ // 取消请求
300
+ controller.abort("cancel test")
301
+ ```
302
+
303
+ ### 动态注入中断源
304
+ 不确定中断时机。
305
+ ```javascript
306
+ // 自定义中断逻辑
307
+ class AbortSourceManager {
308
+ // ...
309
+
310
+ create () {
311
+ return AbortController();
312
+ }
313
+
314
+ abortAll () {
315
+ // ...
316
+ }
317
+ }
318
+
319
+ const manager = new AbortSourceManager();
320
+
321
+ // ...
322
+ class AuthService extends Service {
323
+ // ...
324
+ @RequestConfig((...args)=>{
325
+ const controller = manager.create();
326
+ return {
327
+ // ...
328
+ signal: controller.signal
329
+ };
330
+ })
331
+ bar() {
332
+ // ....
333
+ return {};
334
+ }
335
+ }
336
+ ```
337
+
338
+
231
339
  ## Plugin
232
340
 
233
341
  ### Custom Plugin
@@ -539,6 +647,10 @@ import {authorizer} from "/path/config.js";
539
647
  #### IgnoreResidualParams(ignore?)
540
648
  + ignore : boolean `拼接 QueryString 时是否忽略没有声明的参数`
541
649
 
650
+ #### RequestConfig(config)
651
+ + axiosConfig: Config - class decorator,注解类,传入框架配置对象,注意不是 AxiosConfig。
652
+ + axiosConfig: AxiosConfig | (...args:any[]) => AxiosConfig - method decorator,注解方法,注解方法时可根据请求参数构造配置对象。
653
+
542
654
  ```javascript
543
655
  // GET /foo?p1=p1&p2=p2&p3=p3
544
656
  class TestService extends Service {
@@ -0,0 +1,29 @@
1
+ import {AxiosInstance} from "axios";
2
+
3
+ export default class Config {
4
+ constructor(protocol?: string, host?: string, port?: number, prefix?: string, plugins?: ((config: Config) => void)[]);
5
+
6
+ static forName(name: string): Config;
7
+
8
+ host: string;
9
+
10
+ port: number | null | string;
11
+
12
+ protocol: string;
13
+
14
+ prefix: string;
15
+
16
+ origin: string;
17
+
18
+ baseURL: string;
19
+
20
+ axios: AxiosInstance;
21
+
22
+ plugins: ((config: Config) => void)[];
23
+
24
+ register(name: string): Config;
25
+
26
+ unregister(): Config;
27
+ }
28
+
29
+ export const config: Config;
@@ -0,0 +1,15 @@
1
+ declare const URLSearchParamsParser: {
2
+ encode: (encoder: object) => string;
3
+
4
+ decode: (data: object) => object;
5
+
6
+ has: (encoder: object, key: string) => boolean;
7
+
8
+ delete: (encoder: object, key: string) => void;
9
+
10
+ get: (encoder: object, key: string) => any;
11
+
12
+ append: (encoder: object, key: string, value: any) => void;
13
+ }
14
+
15
+ export default URLSearchParamsParser;
@@ -0,0 +1,33 @@
1
+ import Config from "./config";
2
+ import {AxiosPromise, AxiosRequestConfig} from "axios";
3
+ import {AbortControllerAdapter} from "../decorator/abort-source";
4
+
5
+ export interface RequestController {
6
+ ignoreResidualParams: (ignore?: boolean) => RequestController;
7
+
8
+ param: (key: string, required?: boolean) => RequestController;
9
+
10
+ header: (header: string, value: string | ((...args: any[]) => string)) => RequestController;
11
+
12
+ body: (key: string) => RequestController;
13
+
14
+ config: (config: Partial<AxiosRequestConfig>) => RequestController;
15
+
16
+ send: (data?: Record<string, any>) => AxiosPromise<any>;
17
+
18
+ with: (registration: string) => RequestController;
19
+
20
+ abort: (abortController: (AbortController | AbortControllerAdapter) | ((...args: any[]) => (AbortControllerAdapter | AbortController))) => RequestController;
21
+ }
22
+
23
+ export default class Service {
24
+ constructor(path?: string);
25
+
26
+ config: Config;
27
+
28
+ path: string;
29
+
30
+ request(method: string, path: string, data?: any, config?: Partial<AxiosRequestConfig>): AxiosPromise<any>;
31
+
32
+ requestWith(method: string, path?: string): RequestController;
33
+ }
@@ -0,0 +1,9 @@
1
+ import {AxiosPromise, CancelToken} from "axios";
2
+
3
+ export class AbortControllerAdapter {
4
+ constructor(CancelToken: CancelToken);
5
+
6
+ abort(reason?: string): void;
7
+ }
8
+
9
+ export default function AbortSource(abortController: (AbortController | AbortControllerAdapter) | ((...args: any[]) => (AbortController | AbortControllerAdapter))): (() => AxiosPromise<any>);
@@ -0,0 +1 @@
1
+ module.exports = require("../lib/decorator/abort-source");
@@ -0,0 +1,3 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function DeleteMapping(path?: string): (() => AxiosPromise<any>);
@@ -0,0 +1,3 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function GetMapping(path?: string): (() => AxiosPromise<any>);
@@ -0,0 +1,3 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function IgnoreResidualParams(ignore?: boolean): (() => AxiosPromise<any>);
@@ -0,0 +1,3 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function PatchMapping(path?: string): (() => AxiosPromise<any>);
@@ -0,0 +1,3 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function PostMapping(path?: string): (() => AxiosPromise<any>);
@@ -0,0 +1,3 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function PutMapping(path?: string): (() => AxiosPromise<any>);
@@ -0,0 +1,3 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function RequestBody(name?: string): (() => AxiosPromise<any>);
@@ -0,0 +1,7 @@
1
+ import {AxiosPromise, AxiosRequestConfig} from "axios";
2
+ import Config from "../core/config";
3
+ import Service from "../core/service";
4
+
5
+ export default function RequestConfig(config: Config): (() => Service);
6
+
7
+ export default function RequestConfig(config: ((...args: any[]) => Partial<AxiosRequestConfig>) | Partial<AxiosRequestConfig>): (() => AxiosPromise<any>);
@@ -0,0 +1,5 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function RequestHeader(header: string, value: string): (() => AxiosPromise<any>);
4
+
5
+ export default function RequestHeader(header: string, value: ((...args: any[]) => string)): (() => AxiosPromise<any>);
@@ -0,0 +1,6 @@
1
+ import {AxiosPromise} from "axios";
2
+ import Service from "../core/service";
3
+
4
+ export default function RequestMapping(path: string, method: string): (() => AxiosPromise<any>);
5
+
6
+ export default function RequestMapping(path: string): (() => Service);
@@ -0,0 +1,3 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function RequestParam(name: string, required?: boolean): (() => AxiosPromise<any>);
@@ -0,0 +1,3 @@
1
+ import {AxiosPromise} from "axios";
2
+
3
+ export default function RequestWith(registration: string): (() => AxiosPromise<any>);
@@ -1,5 +1,6 @@
1
1
  import Config from "./config";
2
2
  import {AxiosPromise, AxiosRequestConfig} from "axios";
3
+ import {AbortControllerAdapter} from "../decorator/abort-source";
3
4
 
4
5
  export interface RequestController {
5
6
  ignoreResidualParams: (ignore?: boolean) => RequestController;
@@ -15,6 +16,8 @@ export interface RequestController {
15
16
  send: (data?: Record<string, any>) => AxiosPromise<any>;
16
17
 
17
18
  with: (registration: string) => RequestController;
19
+
20
+ abort: (abortController: (AbortController | AbortControllerAdapter) | ((...args: any[]) => (AbortControllerAdapter | AbortController))) => RequestController;
18
21
  }
19
22
 
20
23
  export default class Service {
@@ -27,4 +30,4 @@ export default class Service {
27
30
  request(method: string, path: string, data?: any, config?: Partial<AxiosRequestConfig>): AxiosPromise<any>;
28
31
 
29
32
  requestWith(method: string, path?: string): RequestController;
30
- }
33
+ }
@@ -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=_interopRequireWildcard(require("./config"));function _getRequireWildcardCache(e){var t,r;return"function"!=typeof WeakMap?null:(t=new WeakMap,r=new WeakMap,(_getRequireWildcardCache=function(e){return e?r:t})(e))}function _interopRequireWildcard(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!==_typeof(e)&&"function"!=typeof e)return{default:e};t=_getRequireWildcardCache(t);if(t&&t.has(e))return t.get(e);var r,n,i={},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&((n=o?Object.getOwnPropertyDescriptor(e,r):null)&&(n.get||n.set)?Object.defineProperty(i,r,n):i[r]=e[r]);return i.default=e,t&&t.set(e,i),i}function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _createForOfIteratorHelper(e,t){var r,n,i,o,a="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(a)return n=!(r=!0),{s:function(){a=a.call(e)},n:function(){var e=a.next();return r=e.done,e},e:function(e){n=!0,i=e},f:function(){try{r||null==a.return||a.return()}finally{if(n)throw i}}};if(Array.isArray(e)||(a=_unsupportedIterableToArray(e))||t&&e&&"number"==typeof e.length)return a&&(e=a),o=0,{s:t=function(){},n:function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}},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.")}function _unsupportedIterableToArray(e,t){var r;if(e)return"string"==typeof e?_arrayLikeToArray(e,t):"Map"===(r="Object"===(r=Object.prototype.toString.call(e).slice(8,-1))&&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,_toPropertyKey(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=_toPropertyKey(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _toPropertyKey(e){e=_toPrimitive(e,"string");return"symbol"===_typeof(e)?e:String(e)}function _toPrimitive(e,t){if("object"!==_typeof(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0===r)return("string"===t?String:Number)(e);r=r.call(e,t||"default");if("object"!==_typeof(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}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,r){if(!r)return"";var n;if(t){var i,o,a=_parser.default.decode(r);for(i in r){var s=r[i],f=t[i];f?f.body?_parser.default.delete(a,i):f.required?(0,_common.isNullOrEmpty)(s)&&!_parser.default.has(a,i)&&_parser.default.append(a,i,""):(0,_common.isNullOrEmpty)(s)&&_parser.default.has(a,i)&&_parser.default.delete(a,i):e&&!0===e.ignoreResidualParams&&_parser.default.delete(a,i)}for(o in t){var u=t[o];u&&!u.body&&u.required&&!_parser.default.has(a,o)&&_parser.default.append(a,o,"")}return _parser.default.encode(a)}return e&&!0===e.ignoreResidualParams?"":(n=_parser.default.decode(r),_parser.default.encode(n))},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,"_for",{}),_defineProperty(this,"_features",{}),!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:"for",value:function(e,t){if(1===arguments.length)return this._for[e];Object.assign(this._for,_defineProperty({},e,t))}},{key:"features",value:function(e,t){if(1===arguments.length)return this._features[e];Object.assign(this._features,_defineProperty({},e,t))}},{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._features[e],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?(0<=t.lastIndexOf("?")?"&":"?")+o:"");return Object.assign(e,{headers:Object.assign(n,e.headers||null)}),{path:i,body:r,config:e,query:o}}},{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){var a=this,s=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"",f={},u=[],c={},p={},l=this.config,n={with:function(e){e=_config.default.forName(e);return e&&(l=e),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},ignoreResidualParams:function(){return Object.assign(p,{ignoreResidualParams:!0===(!(0<arguments.length&&void 0!==arguments[0])||arguments[0])}),n},header:function(e,t){return Object.assign(c,_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 u.push(e),n},send:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=ConfigMapping.querystring(p,f,e),r=ConfigMapping.body(f,e),n=ConfigMapping.requestHeaders(c,[e]),i=ConfigMapping.axiosConfig(u,[e]);return Object.assign(i,{headers:Object.assign(n,i.headers||null)}),l?(0,_common.forward)(l.axios,l.origin,l.prefix,a.path,s,o,t,r,i):(n="".concat(a.pathVariable(s||"",e)).concat(t?(0<=s.lastIndexOf("?")?"&":"?")+t:""),a.request(o,n,r,i))}};return n}}]),n}());exports.default=Service;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=exports.ConfigMapping=void 0;var _common=require("./common"),_parser=_interopRequireDefault(require("./parser")),_config=_interopRequireWildcard(require("./config"));function _getRequireWildcardCache(e){var t,r;return"function"!=typeof WeakMap?null:(t=new WeakMap,r=new WeakMap,(_getRequireWildcardCache=function(e){return e?r:t})(e))}function _interopRequireWildcard(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!==_typeof(e)&&"function"!=typeof e)return{default:e};t=_getRequireWildcardCache(t);if(t&&t.has(e))return t.get(e);var r,n,i={},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&((n=o?Object.getOwnPropertyDescriptor(e,r):null)&&(n.get||n.set)?Object.defineProperty(i,r,n):i[r]=e[r]);return i.default=e,t&&t.set(e,i),i}function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _createForOfIteratorHelper(e,t){var r,n,i,o,a="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(a)return n=!(r=!0),{s:function(){a=a.call(e)},n:function(){var e=a.next();return r=e.done,e},e:function(e){n=!0,i=e},f:function(){try{r||null==a.return||a.return()}finally{if(n)throw i}}};if(Array.isArray(e)||(a=_unsupportedIterableToArray(e))||t&&e&&"number"==typeof e.length)return a&&(e=a),o=0,{s:t=function(){},n:function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}},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.")}function _unsupportedIterableToArray(e,t){var r;if(e)return"string"==typeof e?_arrayLikeToArray(e,t):"Map"===(r="Object"===(r=Object.prototype.toString.call(e).slice(8,-1))&&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,_toPropertyKey(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=_toPropertyKey(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _toPropertyKey(e){e=_toPrimitive(e,"string");return"symbol"===_typeof(e)?e:String(e)}function _toPrimitive(e,t){if("object"!==_typeof(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0===r)return("string"===t?String:Number)(e);r=r.call(e,t||"default");if("object"!==_typeof(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}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,r){if(!r)return"";var n;if(t){var i,o,a=_parser.default.decode(r);for(i in r){var s=r[i],f=t[i];f?f.body?_parser.default.delete(a,i):f.required?(0,_common.isNullOrEmpty)(s)&&!_parser.default.has(a,i)&&_parser.default.append(a,i,""):(0,_common.isNullOrEmpty)(s)&&_parser.default.has(a,i)&&_parser.default.delete(a,i):e&&!0===e.ignoreResidualParams&&_parser.default.delete(a,i)}for(o in t){var u=t[o];u&&!u.body&&u.required&&!_parser.default.has(a,o)&&_parser.default.append(a,o,"")}return _parser.default.encode(a)}return e&&!0===e.ignoreResidualParams?"":(n=_parser.default.decode(r),_parser.default.encode(n))},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,"_for",{}),_defineProperty(this,"_features",{}),!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:"for",value:function(e,t){if(1===arguments.length)return this._for[e];Object.assign(this._for,_defineProperty({},e,t))}},{key:"abort",value:function(e,t){t=Object.assign(this.features(e)||{},{abortController:t});this.features(e,t)}},{key:"features",value:function(e,t){if(1===arguments.length)return this._features[e];Object.assign(this._features,_defineProperty({},e,t))}},{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._features[e],this._params[e],r),r=ConfigMapping.body(this._params[e],r),n=ConfigMapping.requestHeaders(this._headers[e],n),a=ConfigMapping.axiosConfig(this._configs[e],i),t="".concat(t).concat(o?(0<=t.lastIndexOf("?")?"&":"?")+o:"");return this._features&&this._features[e]&&this._features[e].abortController&&(i="function"==typeof(e=this._features[e].abortController)?e.apply(void 0,i):e)&&(i.source?a.cancelToken=i.source.token:a.signal=i.signal),Object.assign(a,{headers:Object.assign(n,a.headers||null)}),{path:t,body:r,config:a,query:o}}},{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(a){var s=this,f=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"",u={},c=[],p={},l={},d=this.config,n={with:function(e){e=_config.default.forName(e);return e&&(d=e),n},param:function(e){return Object.assign(u,_defineProperty({},e,Object.assign(u[e]||{},{required:1<arguments.length&&void 0!==arguments[1]&&arguments[1],body:!1}))),n},abort:function(e){return Object.assign(l,{abortController:e}),n},ignoreResidualParams:function(){return Object.assign(l,{ignoreResidualParams:!0===(!(0<arguments.length&&void 0!==arguments[0])||arguments[0])}),n},header:function(e,t){return Object.assign(p,_defineProperty({},e,t)),n},body:function(e){var t,r=u[e]||{};for(t in u)u[t].body=!1;return Object.assign(u,_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(l,u,e),r=ConfigMapping.body(u,e),n=ConfigMapping.requestHeaders(p,[e]),i=ConfigMapping.axiosConfig(c,[e]),o=l.abortController;return Object.assign(i,{headers:Object.assign(n,i.headers||null)}),o&&(n="function"==typeof o?o.apply(void 0,[e]):o)&&(n.source?i.cancelToken=n.source.token:i.signal=n.signal),d?(0,_common.forward)(d.axios,d.origin,d.prefix,s.path,f,a,t,r,i):(o="".concat(s.pathVariable(f||"",e)).concat(t?(0<=f.lastIndexOf("?")?"&":"?")+t:""),s.request(a,o,r,i))}};return n}}]),n}());exports.default=Service;
@@ -0,0 +1,9 @@
1
+ import {AxiosPromise, CancelToken} from "axios";
2
+
3
+ export class AbortControllerAdapter {
4
+ constructor(CancelToken: CancelToken);
5
+
6
+ abort(reason?: string): void;
7
+ }
8
+
9
+ export default function AbortSource(abortController: (AbortController | AbortControllerAdapter) | ((...args: any[]) => (AbortController | AbortControllerAdapter))): (() => AxiosPromise<any>);
@@ -0,0 +1 @@
1
+ "use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}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 o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,_toPropertyKey(o.key),o)}}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=_toPropertyKey(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _toPropertyKey(e){e=_toPrimitive(e,"string");return"symbol"===_typeof(e)?e:String(e)}function _toPrimitive(e,t){if("object"!==_typeof(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0===r)return("string"===t?String:Number)(e);r=r.call(e,t||"default");if("object"!==_typeof(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}Object.defineProperty(exports,"__esModule",{value:!0}),exports.AbortControllerAdapter=void 0,exports.default=_default;var AbortControllerAdapter=function(){function t(e){_classCallCheck(this,t),_defineProperty(this,"_source",null),this._source=e.source()}return _createClass(t,[{key:"signal",get:function(){return{reason:this._source.token&&this._source.token.reason&&this._source.token.reason.message||"",aborted:!(!this._source.token||!this._source.token.reason)&&"ERR_CANCELED"===this._source.token.reason.code}}},{key:"source",get:function(){return this._source}},{key:"abort",value:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";e?this._source.cancel(e):this._source.cancel()}}]),t}();function _default(u){return function(e,n,t){var i;t&&(i=t.value,t.value=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];var o=u;return"function"==typeof u&&(o=u.apply(void 0,t)),this.abort(n,o),i.apply(this,arguments)})}}exports.AbortControllerAdapter=AbortControllerAdapter;
@@ -4,4 +4,4 @@ import Service from "../core/service";
4
4
 
5
5
  export default function RequestConfig(config: Config): (() => Service);
6
6
 
7
- export default function RequestConfig(config: Partial<AxiosRequestConfig>): (() => AxiosPromise<any>);
7
+ export default function RequestConfig(config: ((...args: any[]) => Partial<AxiosRequestConfig>) | Partial<AxiosRequestConfig>): (() => AxiosPromise<any>);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axios-annotations",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+ssh://git@github.com/sitorhy/axios-annotations.git"
@@ -0,0 +1,49 @@
1
+ import {AxiosError, AxiosPromise, AxiosRequestConfig} from "axios";
2
+ import SessionStorage from "./storage";
3
+ import SessionHistory from "./history";
4
+
5
+ export interface Session {
6
+ access_token?: string;
7
+
8
+ refresh_token?: string;
9
+
10
+ token?: string;
11
+
12
+ accessToken?: string;
13
+
14
+ refreshToken?: string;
15
+
16
+ expires_in?: number;
17
+
18
+ expiresIn?: number;
19
+
20
+ scope?: string;
21
+
22
+ token_type?: string;
23
+ }
24
+
25
+ export default class Authorizer {
26
+ sessionKey: string;
27
+
28
+ sessionStorage: SessionStorage;
29
+
30
+ sessionHistory: SessionHistory;
31
+
32
+ getSession(): Promise<Partial<Session>>;
33
+
34
+ storageSession(session: Session): Promise<void>;
35
+
36
+ refreshSession(session: Session): Promise<Partial<Session>> | Promise<Record<string, any>> | Promise<any>;
37
+
38
+ withAuthentication(request: AxiosRequestConfig, session: Partial<Session>): void;
39
+
40
+ checkSession(request: AxiosRequestConfig, session: Partial<Session>): boolean;
41
+
42
+ checkResponse(response: Response): boolean;
43
+
44
+ onAuthorizedDenied(error: AxiosError): Promise<void>;
45
+
46
+ onSessionInvalidated(): void;
47
+
48
+ invalidateSession(): Promise<void>;
49
+ }
@@ -0,0 +1,15 @@
1
+ import {Session} from "./authorizer";
2
+
3
+ export default class SessionHistory {
4
+ add(session: Session): void;
5
+
6
+ check(jwt: string): boolean;
7
+
8
+ deprecate(session: Session): void;
9
+
10
+ clean(): void;
11
+
12
+ isDeprecated(session: Session): boolean;
13
+
14
+ size: number;
15
+ }
@@ -0,0 +1,4 @@
1
+ import Authorizer from "./authorizer";
2
+ import Config from "../../core/config";
3
+
4
+ export default function AuthorizationPlugin(authorizer: Authorizer): (config: Config) => void;
@@ -0,0 +1,16 @@
1
+ import {AxiosError, AxiosPromise} from "axios";
2
+ import Authorizer from "./authorizer";
3
+
4
+ export default class PendingQueue {
5
+ constructor(authorizer: Authorizer);
6
+
7
+ resend(error: AxiosError, retries?: number): AxiosPromise<any>;
8
+
9
+ push(error: AxiosError): AxiosPromise<any>;
10
+
11
+ pop(): void;
12
+
13
+ clear(): void;
14
+
15
+ size: number;
16
+ }
@@ -0,0 +1,7 @@
1
+ export default class SessionStorage {
2
+ set(key: string, value: any): Promise<void>;
3
+
4
+ get(key: string): Promise<any>;
5
+
6
+ remove(key: string): Promise<void>;
7
+ }