axios-annotations 2.3.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 (140) hide show
  1. package/README.md +774 -741
  2. package/es/core/builder.d.ts +33 -0
  3. package/es/core/builder.js +515 -0
  4. package/es/core/common.d.ts +36 -0
  5. package/es/core/common.js +148 -0
  6. package/es/core/config.d.ts +60 -0
  7. package/es/core/config.js +357 -0
  8. package/es/core/core-expect.js +3 -0
  9. package/es/core/expect.d.ts +2 -0
  10. package/es/core/expect.js +2 -0
  11. package/es/core/provider.d.ts +6 -0
  12. package/es/core/provider.js +172 -0
  13. package/es/core/service.d.ts +5 -0
  14. package/es/core/service.js +56 -0
  15. package/es/decorator/path-variables.d.ts +7 -0
  16. package/es/decorator/path-variables.js +32 -0
  17. package/es/decorator/request-body.d.ts +7 -0
  18. package/es/decorator/request-body.js +31 -0
  19. package/es/decorator/request-config.d.ts +7 -0
  20. package/es/decorator/request-config.js +25 -0
  21. package/es/decorator/request-header.d.ts +4 -0
  22. package/es/decorator/request-header.js +14 -0
  23. package/es/decorator/request-mapping.d.ts +6 -0
  24. package/es/decorator/request-mapping.js +56 -0
  25. package/es/decorator/request-param.d.ts +4 -0
  26. package/es/decorator/request-param.js +15 -0
  27. package/es/decorator/request-with.d.ts +3 -0
  28. package/es/decorator/request-with.js +8 -0
  29. package/es/index.d.ts +13 -0
  30. package/es/index.js +14 -0
  31. package/es/plugins/auth/authorizer.d.ts +24 -0
  32. package/es/plugins/auth/authorizer.js +268 -0
  33. package/es/plugins/auth/history.d.ts +15 -0
  34. package/es/plugins/auth/history.js +94 -0
  35. package/es/plugins/auth/index.d.ts +16 -0
  36. package/es/plugins/auth/index.js +358 -0
  37. package/es/plugins/auth/queue.d.ts +29 -0
  38. package/es/plugins/auth/queue.js +281 -0
  39. package/es/plugins/auth/storage.d.ts +6 -0
  40. package/es/plugins/auth/storage.js +203 -0
  41. package/lib/core/builder.d.ts +33 -0
  42. package/lib/core/builder.js +520 -0
  43. package/lib/core/common.d.ts +34 -4
  44. package/lib/core/common.js +139 -147
  45. package/lib/core/config.d.ts +15 -15
  46. package/lib/core/config.js +44 -14
  47. package/lib/core/core-expect.js +8 -0
  48. package/lib/core/expect.d.ts +2 -4
  49. package/lib/core/expect.js +6 -15
  50. package/lib/core/provider.d.ts +5 -5
  51. package/lib/core/provider.js +2 -2
  52. package/lib/core/service.d.ts +3 -151
  53. package/lib/core/service.js +42 -544
  54. package/lib/decorator/path-variables.d.ts +7 -0
  55. package/lib/decorator/path-variables.js +37 -0
  56. package/lib/decorator/request-body.d.ts +7 -1
  57. package/lib/decorator/request-body.js +28 -20
  58. package/lib/decorator/request-config.d.ts +6 -4
  59. package/lib/decorator/request-config.js +23 -16
  60. package/lib/decorator/request-header.d.ts +4 -2
  61. package/lib/decorator/request-header.js +11 -13
  62. package/lib/decorator/request-mapping.d.ts +6 -3
  63. package/lib/decorator/request-mapping.js +46 -43
  64. package/lib/decorator/request-param.d.ts +4 -1
  65. package/lib/decorator/request-param.js +12 -19
  66. package/lib/decorator/request-with.d.ts +3 -1
  67. package/lib/decorator/request-with.js +6 -13
  68. package/lib/index.d.ts +4 -11
  69. package/lib/index.js +19 -68
  70. package/lib/plugins/auth/authorizer.d.ts +6 -5
  71. package/lib/plugins/auth/authorizer.js +3 -2
  72. package/lib/plugins/auth/history.d.ts +5 -4
  73. package/lib/plugins/auth/history.js +2 -0
  74. package/lib/plugins/auth/index.d.ts +7 -0
  75. package/lib/plugins/auth/index.js +11 -3
  76. package/lib/plugins/auth/queue.d.ts +3 -2
  77. package/lib/plugins/auth/queue.js +31 -24
  78. package/lib/plugins/auth/storage.js +2 -2
  79. package/package.json +42 -7
  80. package/wechat-mp/core/builder.d.ts +33 -0
  81. package/wechat-mp/core/builder.js +520 -0
  82. package/wechat-mp/core/common.d.ts +36 -0
  83. package/wechat-mp/core/common.js +158 -0
  84. package/wechat-mp/core/config.d.ts +60 -0
  85. package/wechat-mp/core/config.js +362 -0
  86. package/wechat-mp/core/core-expect.js +8 -0
  87. package/wechat-mp/core/expect.d.ts +2 -0
  88. package/wechat-mp/core/expect.js +6 -0
  89. package/wechat-mp/core/provider.d.ts +6 -0
  90. package/wechat-mp/core/provider.js +178 -0
  91. package/wechat-mp/core/service.d.ts +5 -0
  92. package/wechat-mp/core/service.js +60 -0
  93. package/wechat-mp/decorator/path-variables.d.ts +7 -0
  94. package/wechat-mp/decorator/path-variables.js +37 -0
  95. package/wechat-mp/decorator/request-body.d.ts +7 -0
  96. package/wechat-mp/decorator/request-body.js +36 -0
  97. package/wechat-mp/decorator/request-config.d.ts +7 -0
  98. package/wechat-mp/decorator/request-config.js +30 -0
  99. package/wechat-mp/decorator/request-header.d.ts +4 -0
  100. package/wechat-mp/decorator/request-header.js +19 -0
  101. package/wechat-mp/decorator/request-mapping.d.ts +6 -0
  102. package/wechat-mp/decorator/request-mapping.js +61 -0
  103. package/wechat-mp/decorator/request-param.d.ts +4 -0
  104. package/wechat-mp/decorator/request-param.js +20 -0
  105. package/wechat-mp/decorator/request-with.d.ts +3 -0
  106. package/wechat-mp/decorator/request-with.js +13 -0
  107. package/wechat-mp/index.d.ts +13 -0
  108. package/wechat-mp/index.js +97 -0
  109. package/wechat-mp/plugins/auth/authorizer.d.ts +24 -0
  110. package/wechat-mp/plugins/auth/authorizer.js +272 -0
  111. package/wechat-mp/plugins/auth/history.d.ts +15 -0
  112. package/wechat-mp/plugins/auth/history.js +98 -0
  113. package/wechat-mp/plugins/auth/index.d.ts +16 -0
  114. package/wechat-mp/plugins/auth/index.js +376 -0
  115. package/wechat-mp/plugins/auth/queue.d.ts +29 -0
  116. package/wechat-mp/plugins/auth/queue.js +285 -0
  117. package/wechat-mp/plugins/auth/storage.d.ts +6 -0
  118. package/wechat-mp/plugins/auth/storage.js +207 -0
  119. package/index.d.ts +0 -1
  120. package/index.js +0 -127
  121. package/lib/core/cancel.d.ts +0 -30
  122. package/lib/core/cancel.js +0 -56
  123. package/lib/core/parser.d.ts +0 -19
  124. package/lib/core/parser.js +0 -79
  125. package/lib/decorator/abort-source.d.ts +0 -3
  126. package/lib/decorator/abort-source.js +0 -25
  127. package/lib/decorator/delete-mapping.d.ts +0 -1
  128. package/lib/decorator/delete-mapping.js +0 -13
  129. package/lib/decorator/get-mapping.d.ts +0 -1
  130. package/lib/decorator/get-mapping.js +0 -13
  131. package/lib/decorator/ignore-residual-params.d.ts +0 -1
  132. package/lib/decorator/ignore-residual-params.js +0 -24
  133. package/lib/decorator/patch-mapping.d.ts +0 -1
  134. package/lib/decorator/patch-mapping.js +0 -13
  135. package/lib/decorator/post-mapping.d.ts +0 -1
  136. package/lib/decorator/post-mapping.js +0 -13
  137. package/lib/decorator/put-mapping.d.ts +0 -1
  138. package/lib/decorator/put-mapping.js +0 -13
  139. package/plugins/auth/index.d.ts +0 -4
  140. 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,21 +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 maxTimes, 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:
168
169
  maxTimes = Math.max(this._retryTimes, retries, 1);
169
- console.log(maxTimes);
170
170
  i = 0;
171
- _a.label = 1;
171
+ _b.label = 1;
172
172
  case 1:
173
173
  if (!(i < maxTimes)) return [
174
174
  3 /*break*/ ,
175
- 10
175
+ 11
176
176
  ];
177
177
  if (!(i > 0)) return [
178
178
  3 /*break*/ ,
@@ -183,60 +183,67 @@ var PendingQueue = /** @class */ function() {
183
183
  sleep(random(this._minTryRetryInterval, this._maxTryRetryInterval))
184
184
  ];
185
185
  case 2:
186
- _a.sent();
187
- _a.label = 3;
186
+ _b.sent();
187
+ _b.label = 3;
188
188
  case 3:
189
189
  return [
190
190
  4 /*yield*/ ,
191
191
  this._authorizer.getSession()
192
192
  ];
193
193
  case 4:
194
- session = _a.sent();
194
+ session = _b.sent();
195
195
  if (this._authorizer.sessionHistory.isDeprecated(session)) {
196
196
  // without re-login again, residual invalid session
197
197
  throw error;
198
198
  }
199
- _a.label = 5;
199
+ _b.label = 5;
200
200
  case 5:
201
- _a.trys.push([
201
+ _b.trys.push([
202
202
  5,
203
- 8,
203
+ 9,
204
204
  ,
205
- 9
205
+ 10
206
206
  ]);
207
+ if (!!this._resendAxiosInstance) return [
208
+ 3 /*break*/ ,
209
+ 7
210
+ ];
211
+ _a = this;
207
212
  return [
208
213
  4 /*yield*/ ,
209
214
  this._config.axiosProvider.get()
210
215
  ];
211
216
  case 6:
212
- axios = _a.sent().create();
217
+ _a._resendAxiosInstance = _b.sent().create();
218
+ _b.label = 7;
219
+ case 7:
213
220
  this._authorizer.withAuthentication(error.config, session);
214
221
  return [
215
222
  4 /*yield*/ ,
216
- axios.request(error.config)
223
+ this._resendAxiosInstance.request(error.config)
217
224
  ];
218
- case 7:
225
+ case 8:
219
226
  // fix: 401 循环 忘了加 await
220
227
  return [
221
228
  2 /*return*/ ,
222
- _a.sent()
229
+ _b.sent()
223
230
  ];
224
- case 8:
225
- e_1 = _a.sent();
231
+ case 9:
232
+ e_1 = _b.sent();
226
233
  if (i >= maxTimes) {
227
234
  throw e_1;
228
235
  }
229
236
  return [
230
237
  3 /*break*/ ,
231
- 9
238
+ 10
232
239
  ];
233
- case 9:
240
+ case 10:
234
241
  i++;
235
242
  return [
236
243
  3 /*break*/ ,
237
244
  1
238
245
  ];
239
- case 10:
246
+ case 11:
240
247
  throw error;
241
248
  }
242
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
1
  {
2
2
  "name": "axios-annotations",
3
- "version": "2.3.0",
3
+ "version": "3.0.0",
4
4
  "description": "HTTP client library uses Axios without Typescript.",
5
- "main": "index.js",
6
- "miniprogram": "lib",
7
- "miniprogramRoot": "lib",
5
+ "author": "sitorhy",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/sitorhy/axios-annotations#readme",
8
8
  "keywords": [
9
9
  "axios",
10
10
  "axios-annotations",
11
11
  "axios-decorators"
12
12
  ],
13
- "author": "sitorhy",
14
- "license": "MIT",
15
- "homepage": "https://github.com/sitorhy/axios-annotations#readme"
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
+ }
16
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
+ }