axios-annotations 2.2.0 → 3.0.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.
Files changed (141) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +977 -944
  3. package/es/core/builder.d.ts +33 -0
  4. package/es/core/builder.js +515 -0
  5. package/es/core/common.d.ts +36 -0
  6. package/es/core/common.js +148 -0
  7. package/es/core/config.d.ts +60 -0
  8. package/es/core/config.js +357 -0
  9. package/es/core/core-expect.js +3 -0
  10. package/es/core/expect.d.ts +2 -0
  11. package/es/core/expect.js +2 -0
  12. package/es/core/provider.d.ts +6 -0
  13. package/es/core/provider.js +172 -0
  14. package/es/core/service.d.ts +5 -0
  15. package/es/core/service.js +56 -0
  16. package/es/decorator/path-variables.d.ts +7 -0
  17. package/es/decorator/path-variables.js +32 -0
  18. package/es/decorator/request-body.d.ts +7 -0
  19. package/es/decorator/request-body.js +31 -0
  20. package/es/decorator/request-config.d.ts +7 -0
  21. package/es/decorator/request-config.js +25 -0
  22. package/es/decorator/request-header.d.ts +4 -0
  23. package/es/decorator/request-header.js +14 -0
  24. package/es/decorator/request-mapping.d.ts +6 -0
  25. package/es/decorator/request-mapping.js +56 -0
  26. package/es/decorator/request-param.d.ts +4 -0
  27. package/es/decorator/request-param.js +15 -0
  28. package/es/decorator/request-with.d.ts +3 -0
  29. package/es/decorator/request-with.js +8 -0
  30. package/es/index.d.ts +13 -0
  31. package/es/index.js +14 -0
  32. package/es/plugins/auth/authorizer.d.ts +24 -0
  33. package/es/plugins/auth/authorizer.js +268 -0
  34. package/es/plugins/auth/history.d.ts +15 -0
  35. package/es/plugins/auth/history.js +94 -0
  36. package/es/plugins/auth/index.d.ts +16 -0
  37. package/es/plugins/auth/index.js +358 -0
  38. package/es/plugins/auth/queue.d.ts +29 -0
  39. package/es/plugins/auth/queue.js +281 -0
  40. package/es/plugins/auth/storage.d.ts +6 -0
  41. package/es/plugins/auth/storage.js +203 -0
  42. package/lib/core/builder.d.ts +33 -0
  43. package/lib/core/builder.js +520 -0
  44. package/lib/core/common.d.ts +34 -4
  45. package/lib/core/common.js +139 -147
  46. package/lib/core/config.d.ts +15 -15
  47. package/lib/core/config.js +44 -14
  48. package/lib/core/core-expect.js +8 -0
  49. package/lib/core/expect.d.ts +2 -4
  50. package/lib/core/expect.js +2 -11
  51. package/lib/core/provider.d.ts +5 -5
  52. package/lib/core/provider.js +2 -2
  53. package/lib/core/service.d.ts +3 -151
  54. package/lib/core/service.js +42 -544
  55. package/lib/decorator/path-variables.d.ts +7 -0
  56. package/lib/decorator/path-variables.js +37 -0
  57. package/lib/decorator/request-body.d.ts +7 -1
  58. package/lib/decorator/request-body.js +28 -20
  59. package/lib/decorator/request-config.d.ts +6 -4
  60. package/lib/decorator/request-config.js +23 -16
  61. package/lib/decorator/request-header.d.ts +4 -2
  62. package/lib/decorator/request-header.js +11 -13
  63. package/lib/decorator/request-mapping.d.ts +6 -3
  64. package/lib/decorator/request-mapping.js +46 -43
  65. package/lib/decorator/request-param.d.ts +4 -1
  66. package/lib/decorator/request-param.js +12 -19
  67. package/lib/decorator/request-with.d.ts +3 -1
  68. package/lib/decorator/request-with.js +6 -13
  69. package/lib/index.d.ts +4 -11
  70. package/lib/index.js +19 -68
  71. package/lib/plugins/auth/authorizer.d.ts +6 -5
  72. package/lib/plugins/auth/authorizer.js +3 -2
  73. package/lib/plugins/auth/history.d.ts +5 -4
  74. package/lib/plugins/auth/history.js +2 -0
  75. package/lib/plugins/auth/index.d.ts +7 -0
  76. package/lib/plugins/auth/index.js +11 -3
  77. package/lib/plugins/auth/queue.d.ts +3 -2
  78. package/lib/plugins/auth/queue.js +40 -25
  79. package/lib/plugins/auth/storage.js +2 -2
  80. package/package.json +51 -16
  81. package/wechat-mp/core/builder.d.ts +33 -0
  82. package/wechat-mp/core/builder.js +520 -0
  83. package/wechat-mp/core/common.d.ts +36 -0
  84. package/wechat-mp/core/common.js +158 -0
  85. package/wechat-mp/core/config.d.ts +60 -0
  86. package/wechat-mp/core/config.js +362 -0
  87. package/wechat-mp/core/core-expect.js +8 -0
  88. package/wechat-mp/core/expect.d.ts +2 -0
  89. package/wechat-mp/core/expect.js +6 -0
  90. package/wechat-mp/core/provider.d.ts +6 -0
  91. package/wechat-mp/core/provider.js +178 -0
  92. package/wechat-mp/core/service.d.ts +5 -0
  93. package/wechat-mp/core/service.js +60 -0
  94. package/wechat-mp/decorator/path-variables.d.ts +7 -0
  95. package/wechat-mp/decorator/path-variables.js +37 -0
  96. package/wechat-mp/decorator/request-body.d.ts +7 -0
  97. package/wechat-mp/decorator/request-body.js +36 -0
  98. package/wechat-mp/decorator/request-config.d.ts +7 -0
  99. package/wechat-mp/decorator/request-config.js +30 -0
  100. package/wechat-mp/decorator/request-header.d.ts +4 -0
  101. package/wechat-mp/decorator/request-header.js +19 -0
  102. package/wechat-mp/decorator/request-mapping.d.ts +6 -0
  103. package/wechat-mp/decorator/request-mapping.js +61 -0
  104. package/wechat-mp/decorator/request-param.d.ts +4 -0
  105. package/wechat-mp/decorator/request-param.js +20 -0
  106. package/wechat-mp/decorator/request-with.d.ts +3 -0
  107. package/wechat-mp/decorator/request-with.js +13 -0
  108. package/wechat-mp/index.d.ts +13 -0
  109. package/wechat-mp/index.js +97 -0
  110. package/wechat-mp/plugins/auth/authorizer.d.ts +24 -0
  111. package/wechat-mp/plugins/auth/authorizer.js +272 -0
  112. package/wechat-mp/plugins/auth/history.d.ts +15 -0
  113. package/wechat-mp/plugins/auth/history.js +98 -0
  114. package/wechat-mp/plugins/auth/index.d.ts +16 -0
  115. package/wechat-mp/plugins/auth/index.js +376 -0
  116. package/wechat-mp/plugins/auth/queue.d.ts +29 -0
  117. package/wechat-mp/plugins/auth/queue.js +285 -0
  118. package/wechat-mp/plugins/auth/storage.d.ts +6 -0
  119. package/wechat-mp/plugins/auth/storage.js +207 -0
  120. package/index.d.ts +0 -1
  121. package/index.js +0 -127
  122. package/lib/core/cancel.d.ts +0 -30
  123. package/lib/core/cancel.js +0 -56
  124. package/lib/core/parser.d.ts +0 -19
  125. package/lib/core/parser.js +0 -79
  126. package/lib/decorator/abort-source.d.ts +0 -3
  127. package/lib/decorator/abort-source.js +0 -25
  128. package/lib/decorator/delete-mapping.d.ts +0 -1
  129. package/lib/decorator/delete-mapping.js +0 -13
  130. package/lib/decorator/get-mapping.d.ts +0 -1
  131. package/lib/decorator/get-mapping.js +0 -13
  132. package/lib/decorator/ignore-residual-params.d.ts +0 -1
  133. package/lib/decorator/ignore-residual-params.js +0 -24
  134. package/lib/decorator/patch-mapping.d.ts +0 -1
  135. package/lib/decorator/patch-mapping.js +0 -13
  136. package/lib/decorator/post-mapping.d.ts +0 -1
  137. package/lib/decorator/post-mapping.js +0 -13
  138. package/lib/decorator/put-mapping.d.ts +0 -1
  139. package/lib/decorator/put-mapping.js +0 -13
  140. package/plugins/auth/index.d.ts +0 -4
  141. package/plugins/auth/index.js +0 -26
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
2
+ var __awaiter = (void 0) && (void 0).__awaiter || function(thisArg, _arguments, P, generator) {
3
3
  function adopt(value) {
4
4
  return value instanceof P ? value : new P(function(resolve) {
5
5
  resolve(value);
@@ -26,7 +26,7 @@ var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, gener
26
26
  step((generator = generator.apply(thisArg, _arguments || [])).next());
27
27
  });
28
28
  };
29
- var __generator = this && this.__generator || function(thisArg, body) {
29
+ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
30
30
  var _ = {
31
31
  label: 0,
32
32
  sent: function sent() {
@@ -142,10 +142,10 @@ function AuthorizationPlugin(authorizer, options) {
142
142
  var unauthorized = false;
143
143
  var expiredSession = null;
144
144
  var queue = new queue_1["default"](authorizer, config, (options === null || options === void 0 ? void 0 : options.minTryRetryInterval) || 3000, (options === null || options === void 0 ? void 0 : options.maxTryRetryInterval) || 5000, (options === null || options === void 0 ? void 0 : options.retryTimes) || 3);
145
- new queue_1["default"](authorizer, config, (options === null || options === void 0 ? void 0 : options.minTryRetryInterval) || 3000, (options === null || options === void 0 ? void 0 : options.maxTryRetryInterval) || 5000, (options === null || options === void 0 ? void 0 : options.retryTimes) || 3);
146
145
  axios.interceptors.response.use(function(i) {
147
146
  if (expiredSession) {
148
147
  // Previously requests sent have been rejected, record and release it (will be resending)
148
+ // 会话过期加入过期队列,但未确认 refreshToken 是否失效
149
149
  authorizer.sessionHistory.add(expiredSession);
150
150
  expiredSession = null;
151
151
  }
@@ -202,6 +202,7 @@ function AuthorizationPlugin(authorizer, options) {
202
202
  throw e;
203
203
  case 5:
204
204
  unauthorized = false;
205
+ // 全部 reject 掉, 即抛出原始异常
205
206
  queue.clear();
206
207
  return [
207
208
  7 /*endfinally*/
@@ -229,6 +230,7 @@ function AuthorizationPlugin(authorizer, options) {
229
230
  // waiting for next accessToken refresh, on the server side, updates are synchronized.
230
231
  // Note:
231
232
  // even if the refreshing request send few times, the server side will only return the same token.
233
+ // 翻译:有效期内多次的刷新请求服务端应该保持 accessToken 不变,客户端不需要线程同步
232
234
  return [
233
235
  4 /*yield*/ ,
234
236
  _b.apply(_a, [
@@ -239,6 +241,7 @@ function AuthorizationPlugin(authorizer, options) {
239
241
  // waiting for next accessToken refresh, on the server side, updates are synchronized.
240
242
  // Note:
241
243
  // even if the refreshing request send few times, the server side will only return the same token.
244
+ // 翻译:有效期内多次的刷新请求服务端应该保持 accessToken 不变,客户端不需要线程同步
242
245
  _c.sent();
243
246
  return [
244
247
  3 /*break*/ ,
@@ -247,6 +250,8 @@ function AuthorizationPlugin(authorizer, options) {
247
250
  case 10:
248
251
  e2_1 = _c.sent();
249
252
  // invalid refresh_token
253
+ // 此处触发一般就是 refreshSession 刷新返回 401 / 500 等,非标准返回值需要自行抛出异常
254
+ // 作废会话,一般就是 refreshToken 失效
250
255
  authorizer.sessionHistory.deprecate(session);
251
256
  _c.label = 11;
252
257
  case 11:
@@ -273,6 +278,7 @@ function AuthorizationPlugin(authorizer, options) {
273
278
  case 14:
274
279
  unauthorized = false;
275
280
  // cancel all requests resending.
281
+ // clear 就是 全部 reject 掉
276
282
  queue.clear();
277
283
  return [
278
284
  7 /*endfinally*/
@@ -285,6 +291,7 @@ function AuthorizationPlugin(authorizer, options) {
285
291
  case 16:
286
292
  unauthorized = false;
287
293
  // the accessToken refreshed, send the request itself and startup resending queue cleaning.
294
+ // 清空 else 分支积压的请求(重发)
288
295
  (function() {
289
296
  while(queue.size){
290
297
  queue.pop();
@@ -303,6 +310,7 @@ function AuthorizationPlugin(authorizer, options) {
303
310
  queue.resend(e)
304
311
  ];
305
312
  case 18:
313
+ // 重发当前请求,当前请求没有压入队列需要单独处理
306
314
  return [
307
315
  2 /*return*/ ,
308
316
  _c.sent()
@@ -1,6 +1,6 @@
1
- import type { AxiosError } from "axios";
1
+ import type { AxiosInstance, AxiosError } from 'axios';
2
2
  import Authorizer from "./authorizer";
3
- import { Config } from "../../index";
3
+ import Config from "../../core/config";
4
4
  export default class PendingQueue {
5
5
  _queue: {
6
6
  error: AxiosError;
@@ -12,6 +12,7 @@ export default class PendingQueue {
12
12
  _minTryRetryInterval: number;
13
13
  _maxTryRetryInterval: number;
14
14
  _retryTimes: number;
15
+ _resendAxiosInstance: AxiosInstance | null;
15
16
  /**
16
17
  * @param {Authorizer} authorizer
17
18
  * @param {Config} config
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
2
+ var __awaiter = (void 0) && (void 0).__awaiter || function(thisArg, _arguments, P, generator) {
3
3
  function adopt(value) {
4
4
  return value instanceof P ? value : new P(function(resolve) {
5
5
  resolve(value);
@@ -26,7 +26,7 @@ var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, gener
26
26
  step((generator = generator.apply(thisArg, _arguments || [])).next());
27
27
  });
28
28
  };
29
- var __generator = this && this.__generator || function(thisArg, body) {
29
+ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
30
30
  var _ = {
31
31
  label: 0,
32
32
  sent: function sent() {
@@ -150,6 +150,7 @@ var PendingQueue = /** @class */ function() {
150
150
  retryTimes = 3;
151
151
  }
152
152
  this._queue = [];
153
+ this._resendAxiosInstance = null;
153
154
  this._authorizer = authorizer;
154
155
  this._config = config;
155
156
  this._minTryRetryInterval = minTryRetryInterval;
@@ -158,19 +159,20 @@ var PendingQueue = /** @class */ function() {
158
159
  }
159
160
  PendingQueue.prototype.resend = function(error_1) {
160
161
  return __awaiter(this, arguments, void 0, function(error, retries) {
161
- var i, session, axios, e_1;
162
+ var maxTimes, i, session, _a, e_1;
162
163
  if (retries === void 0) {
163
164
  retries = 3;
164
165
  }
165
- return __generator(this, function(_a) {
166
- switch(_a.label){
166
+ return __generator(this, function(_b) {
167
+ switch(_b.label){
167
168
  case 0:
169
+ maxTimes = Math.max(this._retryTimes, retries, 1);
168
170
  i = 0;
169
- _a.label = 1;
171
+ _b.label = 1;
170
172
  case 1:
171
- if (!(i < this._retryTimes)) return [
173
+ if (!(i < maxTimes)) return [
172
174
  3 /*break*/ ,
173
- 9
175
+ 11
174
176
  ];
175
177
  if (!(i > 0)) return [
176
178
  3 /*break*/ ,
@@ -181,54 +183,67 @@ var PendingQueue = /** @class */ function() {
181
183
  sleep(random(this._minTryRetryInterval, this._maxTryRetryInterval))
182
184
  ];
183
185
  case 2:
184
- _a.sent();
185
- _a.label = 3;
186
+ _b.sent();
187
+ _b.label = 3;
186
188
  case 3:
187
189
  return [
188
190
  4 /*yield*/ ,
189
191
  this._authorizer.getSession()
190
192
  ];
191
193
  case 4:
192
- session = _a.sent();
194
+ session = _b.sent();
193
195
  if (this._authorizer.sessionHistory.isDeprecated(session)) {
194
196
  // without re-login again, residual invalid session
195
197
  throw error;
196
198
  }
197
- _a.label = 5;
199
+ _b.label = 5;
198
200
  case 5:
199
- _a.trys.push([
201
+ _b.trys.push([
200
202
  5,
201
- 7,
203
+ 9,
202
204
  ,
203
- 8
205
+ 10
204
206
  ]);
207
+ if (!!this._resendAxiosInstance) return [
208
+ 3 /*break*/ ,
209
+ 7
210
+ ];
211
+ _a = this;
205
212
  return [
206
213
  4 /*yield*/ ,
207
- this._config.requestAxiosInstance()
214
+ this._config.axiosProvider.get()
208
215
  ];
209
216
  case 6:
210
- axios = _a.sent();
217
+ _a._resendAxiosInstance = _b.sent().create();
218
+ _b.label = 7;
219
+ case 7:
211
220
  this._authorizer.withAuthentication(error.config, session);
221
+ return [
222
+ 4 /*yield*/ ,
223
+ this._resendAxiosInstance.request(error.config)
224
+ ];
225
+ case 8:
226
+ // fix: 401 循环 忘了加 await
212
227
  return [
213
228
  2 /*return*/ ,
214
- axios.request(error.config)
229
+ _b.sent()
215
230
  ];
216
- case 7:
217
- e_1 = _a.sent();
218
- if (i >= retries - 1) {
231
+ case 9:
232
+ e_1 = _b.sent();
233
+ if (i >= maxTimes) {
219
234
  throw e_1;
220
235
  }
221
236
  return [
222
237
  3 /*break*/ ,
223
- 8
238
+ 10
224
239
  ];
225
- case 8:
226
- ++i;
240
+ case 10:
241
+ i++;
227
242
  return [
228
243
  3 /*break*/ ,
229
244
  1
230
245
  ];
231
- case 9:
246
+ case 11:
232
247
  throw error;
233
248
  }
234
249
  });
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
2
+ var __awaiter = (void 0) && (void 0).__awaiter || function(thisArg, _arguments, P, generator) {
3
3
  function adopt(value) {
4
4
  return value instanceof P ? value : new P(function(resolve) {
5
5
  resolve(value);
@@ -26,7 +26,7 @@ var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, gener
26
26
  step((generator = generator.apply(thisArg, _arguments || [])).next());
27
27
  });
28
28
  };
29
- var __generator = this && this.__generator || function(thisArg, body) {
29
+ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
30
30
  var _ = {
31
31
  label: 0,
32
32
  sent: function sent() {
package/package.json CHANGED
@@ -1,16 +1,51 @@
1
- {
2
- "name": "axios-annotations",
3
- "version": "2.2.0",
4
- "description": "HTTP client library uses Axios without Typescript.",
5
- "main": "index.js",
6
- "miniprogram": "lib",
7
- "miniprogramRoot": "lib",
8
- "keywords": [
9
- "axios",
10
- "axios-annotations",
11
- "axios-decorators"
12
- ],
13
- "author": "sitorhy",
14
- "license": "MIT",
15
- "homepage": "https://github.com/sitorhy/axios-annotations#readme"
16
- }
1
+ {
2
+ "name": "axios-annotations",
3
+ "version": "3.0.0",
4
+ "description": "HTTP client library uses Axios without Typescript.",
5
+ "author": "sitorhy",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/sitorhy/axios-annotations#readme",
8
+ "keywords": [
9
+ "axios",
10
+ "axios-annotations",
11
+ "axios-decorators"
12
+ ],
13
+
14
+ "type": "module",
15
+
16
+ "main": "./lib/index.js",
17
+ "module": "./es/index.js",
18
+ "types": "./lib/index.d.ts",
19
+
20
+ "miniprogram": "wechat-mp",
21
+ "miniprogramRoot": "wechat-mp",
22
+
23
+ "files": [
24
+ "lib",
25
+ "es",
26
+ "wechat-mp"
27
+ ],
28
+
29
+ "exports": {
30
+ ".": {
31
+ "import": {
32
+ "types": "./es/index.d.ts",
33
+ "default": "./es/index.js"
34
+ },
35
+ "require": {
36
+ "types": "./lib/index.d.ts",
37
+ "default": "./lib/index.js"
38
+ }
39
+ },
40
+ "./plugins/auth": {
41
+ "import": {
42
+ "types": "./es/plugins/auth/index.d.ts",
43
+ "default": "./es/plugins/auth/index.js"
44
+ },
45
+ "require": {
46
+ "types": "./lib/plugins/auth/index.d.ts",
47
+ "default": "./lib/plugins/auth/index.js"
48
+ }
49
+ }
50
+ }
51
+ }
@@ -0,0 +1,33 @@
1
+ import type { AxiosResponse, Method } from 'axios';
2
+ import type Config from "./config";
3
+ import { AxiosRequestConfig } from "axios";
4
+ export type ParamsMapper<T = any> = (params: Record<string, any>) => T;
5
+ export declare enum ParamsMappingType {
6
+ HEADER = "HEADER",
7
+ PARAM = "PARAM",
8
+ BODY = "BODY",
9
+ PATH_VARIABLE = "PATH_VARIABLE"
10
+ }
11
+ export interface ParamsMapping<T = any> {
12
+ key: string;
13
+ value: T | ParamsMapper<T>;
14
+ required?: boolean;
15
+ }
16
+ export type AxiosConfigMapper = (params: Record<string, any>) => AxiosRequestConfig;
17
+ export default class RequestBuilder {
18
+ private _configs;
19
+ private readonly _mapping;
20
+ private readonly _pathVariablesMapping;
21
+ private _addMapping;
22
+ private _addPathVariablesMapping;
23
+ private _clearMapping;
24
+ param(key: string, required?: boolean): this;
25
+ param(mapping: ParamsMapping): this;
26
+ header(key: string, required?: boolean): this;
27
+ header(mapping: ParamsMapping): this;
28
+ body(keyOrMapping: string | ParamsMapping): this;
29
+ pathVariable(keyOrMapping: string | ParamsMapping): this;
30
+ config(cfg: AxiosRequestConfig | AxiosConfigMapper): this;
31
+ build(config: Config, path: string, method: Method, source?: Record<string, any>): AxiosRequestConfig;
32
+ buildWith(config: Config, path: string, method: Method, source?: Record<string, any>): Promise<AxiosResponse<any>>;
33
+ }