axios-annotations 2.3.0 → 3.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.
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 +57 -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 +43 -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 +61 -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
@@ -0,0 +1,376 @@
1
+ "use strict";
2
+ var __awaiter = (void 0) && (void 0).__awaiter || function(thisArg, _arguments, P, generator) {
3
+ function adopt(value) {
4
+ return value instanceof P ? value : new P(function(resolve) {
5
+ resolve(value);
6
+ });
7
+ }
8
+ return new (P || (P = Promise))(function(resolve, reject) {
9
+ function fulfilled(value) {
10
+ try {
11
+ step(generator.next(value));
12
+ } catch (e) {
13
+ reject(e);
14
+ }
15
+ }
16
+ function rejected(value) {
17
+ try {
18
+ step(generator["throw"](value));
19
+ } catch (e) {
20
+ reject(e);
21
+ }
22
+ }
23
+ function step(result) {
24
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
25
+ }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ };
29
+ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
30
+ var _ = {
31
+ label: 0,
32
+ sent: function sent() {
33
+ if (t[0] & 1) throw t[1];
34
+ return t[1];
35
+ },
36
+ trys: [],
37
+ ops: []
38
+ }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
39
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
40
+ return this;
41
+ }), g;
42
+ function verb(n) {
43
+ return function(v) {
44
+ return step([
45
+ n,
46
+ v
47
+ ]);
48
+ };
49
+ }
50
+ function step(op) {
51
+ if (f) throw new TypeError("Generator is already executing.");
52
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
53
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
54
+ if (y = 0, t) op = [
55
+ op[0] & 2,
56
+ t.value
57
+ ];
58
+ switch(op[0]){
59
+ case 0:
60
+ case 1:
61
+ t = op;
62
+ break;
63
+ case 4:
64
+ _.label++;
65
+ return {
66
+ value: op[1],
67
+ done: false
68
+ };
69
+ case 5:
70
+ _.label++;
71
+ y = op[1];
72
+ op = [
73
+ 0
74
+ ];
75
+ continue;
76
+ case 7:
77
+ op = _.ops.pop();
78
+ _.trys.pop();
79
+ continue;
80
+ default:
81
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
82
+ _ = 0;
83
+ continue;
84
+ }
85
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
86
+ _.label = op[1];
87
+ break;
88
+ }
89
+ if (op[0] === 6 && _.label < t[1]) {
90
+ _.label = t[1];
91
+ t = op;
92
+ break;
93
+ }
94
+ if (t && _.label < t[2]) {
95
+ _.label = t[2];
96
+ _.ops.push(op);
97
+ break;
98
+ }
99
+ if (t[2]) _.ops.pop();
100
+ _.trys.pop();
101
+ continue;
102
+ }
103
+ op = body.call(thisArg, _);
104
+ } catch (e) {
105
+ op = [
106
+ 6,
107
+ e
108
+ ];
109
+ y = 0;
110
+ } finally{
111
+ f = t = 0;
112
+ }
113
+ if (op[0] & 5) throw op[1];
114
+ return {
115
+ value: op[0] ? op[1] : void 0,
116
+ done: true
117
+ };
118
+ }
119
+ };
120
+ Object.defineProperty(exports, "__esModule", {
121
+ value: true
122
+ });
123
+ exports.SessionStorage = exports.Authorizer = void 0;
124
+ exports["default"] = AuthorizationPlugin;
125
+ var queue_1 = require("./queue");
126
+ var authorizer_1 = require("./authorizer");
127
+ Object.defineProperty(exports, "Authorizer", {
128
+ enumerable: true,
129
+ get: function get() {
130
+ return authorizer_1["default"];
131
+ }
132
+ });
133
+ var storage_1 = require("./storage");
134
+ Object.defineProperty(exports, "SessionStorage", {
135
+ enumerable: true,
136
+ get: function get() {
137
+ return storage_1["default"];
138
+ }
139
+ });
140
+ function AuthorizationPlugin(authorizer, options) {
141
+ return function(axios, config) {
142
+ var unauthorized = false;
143
+ var expiredSession = null;
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
+ axios.interceptors.response.use(function(i) {
146
+ if (expiredSession) {
147
+ // Previously requests sent have been rejected, record and release it (will be resending)
148
+ // 会话过期加入过期队列,但未确认 refreshToken 是否失效
149
+ authorizer.sessionHistory.add(expiredSession);
150
+ expiredSession = null;
151
+ }
152
+ return i;
153
+ }, function(e) {
154
+ return __awaiter(this, void 0, void 0, function() {
155
+ var response, session, e5_1, _a, _b, e2_1, e3_1, e4_1;
156
+ return __generator(this, function(_c) {
157
+ switch(_c.label){
158
+ case 0:
159
+ response = e.response;
160
+ if (!!authorizer.checkResponse(response)) return [
161
+ 3 /*break*/ ,
162
+ 24
163
+ ];
164
+ if (!!unauthorized) return [
165
+ 3 /*break*/ ,
166
+ 21
167
+ ];
168
+ unauthorized = true;
169
+ return [
170
+ 4 /*yield*/ ,
171
+ authorizer.getSession()
172
+ ];
173
+ case 1:
174
+ session = _c.sent();
175
+ if (!authorizer.sessionHistory.isDeprecated(session)) return [
176
+ 3 /*break*/ ,
177
+ 6
178
+ ];
179
+ _c.label = 2;
180
+ case 2:
181
+ _c.trys.push([
182
+ 2,
183
+ 4,
184
+ 5,
185
+ 6
186
+ ]);
187
+ // You may override this method for home page redirecting.
188
+ return [
189
+ 4 /*yield*/ ,
190
+ authorizer.onAuthorizedDenied(e)
191
+ ];
192
+ case 3:
193
+ // You may override this method for home page redirecting.
194
+ _c.sent();
195
+ authorizer.sessionHistory.clean();
196
+ return [
197
+ 3 /*break*/ ,
198
+ 6
199
+ ];
200
+ case 4:
201
+ e5_1 = _c.sent();
202
+ throw e;
203
+ case 5:
204
+ unauthorized = false;
205
+ // 全部 reject 掉, 即抛出原始异常
206
+ queue.clear();
207
+ return [
208
+ 7 /*endfinally*/
209
+ ];
210
+ case 6:
211
+ expiredSession = session;
212
+ if (!!authorizer.checkSession(e.config, session)) return [
213
+ 3 /*break*/ ,
214
+ 16
215
+ ];
216
+ _c.label = 7;
217
+ case 7:
218
+ _c.trys.push([
219
+ 7,
220
+ 10,
221
+ ,
222
+ 16
223
+ ]);
224
+ _b = (_a = authorizer).storageSession;
225
+ return [
226
+ 4 /*yield*/ ,
227
+ authorizer.refreshSession(session)
228
+ ];
229
+ case 8:
230
+ // waiting for next accessToken refresh, on the server side, updates are synchronized.
231
+ // Note:
232
+ // even if the refreshing request send few times, the server side will only return the same token.
233
+ // 翻译:有效期内多次的刷新请求服务端应该保持 accessToken 不变,客户端不需要线程同步
234
+ return [
235
+ 4 /*yield*/ ,
236
+ _b.apply(_a, [
237
+ _c.sent()
238
+ ])
239
+ ];
240
+ case 9:
241
+ // waiting for next accessToken refresh, on the server side, updates are synchronized.
242
+ // Note:
243
+ // even if the refreshing request send few times, the server side will only return the same token.
244
+ // 翻译:有效期内多次的刷新请求服务端应该保持 accessToken 不变,客户端不需要线程同步
245
+ _c.sent();
246
+ return [
247
+ 3 /*break*/ ,
248
+ 16
249
+ ];
250
+ case 10:
251
+ e2_1 = _c.sent();
252
+ // invalid refresh_token
253
+ // 此处触发一般就是 refreshSession 刷新返回 401 / 500 等,非标准返回值需要自行抛出异常
254
+ // 作废会话,一般就是 refreshToken 失效
255
+ authorizer.sessionHistory.deprecate(session);
256
+ _c.label = 11;
257
+ case 11:
258
+ _c.trys.push([
259
+ 11,
260
+ 13,
261
+ 14,
262
+ 15
263
+ ]);
264
+ return [
265
+ 4 /*yield*/ ,
266
+ authorizer.onAuthorizedDenied(e2_1)
267
+ ];
268
+ case 12:
269
+ _c.sent();
270
+ authorizer.sessionHistory.clean();
271
+ return [
272
+ 3 /*break*/ ,
273
+ 15
274
+ ];
275
+ case 13:
276
+ e3_1 = _c.sent();
277
+ throw e;
278
+ case 14:
279
+ unauthorized = false;
280
+ // cancel all requests resending.
281
+ // clear 就是 全部 reject 掉
282
+ queue.clear();
283
+ return [
284
+ 7 /*endfinally*/
285
+ ];
286
+ case 15:
287
+ return [
288
+ 3 /*break*/ ,
289
+ 16
290
+ ];
291
+ case 16:
292
+ unauthorized = false;
293
+ // the accessToken refreshed, send the request itself and startup resending queue cleaning.
294
+ // 清空 else 分支积压的请求(重发)
295
+ (function() {
296
+ while(queue.size){
297
+ queue.pop();
298
+ }
299
+ })();
300
+ _c.label = 17;
301
+ case 17:
302
+ _c.trys.push([
303
+ 17,
304
+ 19,
305
+ ,
306
+ 20
307
+ ]);
308
+ return [
309
+ 4 /*yield*/ ,
310
+ queue.resend(e)
311
+ ];
312
+ case 18:
313
+ // 重发当前请求,当前请求没有压入队列需要单独处理
314
+ return [
315
+ 2 /*return*/ ,
316
+ _c.sent()
317
+ ];
318
+ case 19:
319
+ e4_1 = _c.sent();
320
+ // 500 502 503 ...
321
+ throw e4_1;
322
+ case 20:
323
+ return [
324
+ 3 /*break*/ ,
325
+ 23
326
+ ];
327
+ case 21:
328
+ return [
329
+ 4 /*yield*/ ,
330
+ queue.push(e)
331
+ ];
332
+ case 22:
333
+ // No, 401 request occurred, put in to resending queue itself and waiting.
334
+ return [
335
+ 2 /*return*/ ,
336
+ _c.sent()
337
+ ];
338
+ case 23:
339
+ return [
340
+ 3 /*break*/ ,
341
+ 25
342
+ ];
343
+ case 24:
344
+ // HTTP Code = 500 503 502 ... just throw it
345
+ throw e;
346
+ case 25:
347
+ return [
348
+ 2 /*return*/
349
+ ];
350
+ }
351
+ });
352
+ });
353
+ });
354
+ axios.interceptors.request.use(function(request) {
355
+ return __awaiter(this, void 0, void 0, function() {
356
+ var session;
357
+ return __generator(this, function(_a) {
358
+ switch(_a.label){
359
+ case 0:
360
+ return [
361
+ 4 /*yield*/ ,
362
+ authorizer.getSession()
363
+ ];
364
+ case 1:
365
+ session = _a.sent();
366
+ authorizer.withAuthentication(request, session);
367
+ return [
368
+ 2 /*return*/ ,
369
+ request
370
+ ];
371
+ }
372
+ });
373
+ });
374
+ });
375
+ };
376
+ }
@@ -0,0 +1,29 @@
1
+ import type { AxiosInstance, AxiosError } from 'axios';
2
+ import Authorizer from "./authorizer";
3
+ import Config from "../../core/config";
4
+ export default class PendingQueue {
5
+ _queue: {
6
+ error: AxiosError;
7
+ resolve: (arg: any) => void;
8
+ reject: (arg: any) => void;
9
+ }[];
10
+ _authorizer: Authorizer;
11
+ _config: Config;
12
+ _minTryRetryInterval: number;
13
+ _maxTryRetryInterval: number;
14
+ _retryTimes: number;
15
+ _resendAxiosInstance: AxiosInstance | null;
16
+ /**
17
+ * @param {Authorizer} authorizer
18
+ * @param {Config} config
19
+ * @param {number} minTryRetryInterval
20
+ * @param {number} maxTryRetryInterval
21
+ * @param retryTimes
22
+ */
23
+ constructor(authorizer: Authorizer, config: Config, minTryRetryInterval?: number, maxTryRetryInterval?: number, retryTimes?: number);
24
+ resend(error: AxiosError, retries?: number): Promise<any>;
25
+ push(error: AxiosError): Promise<any>;
26
+ pop(): void;
27
+ clear(): void;
28
+ get size(): number;
29
+ }
@@ -0,0 +1,285 @@
1
+ "use strict";
2
+ var __awaiter = (void 0) && (void 0).__awaiter || function(thisArg, _arguments, P, generator) {
3
+ function adopt(value) {
4
+ return value instanceof P ? value : new P(function(resolve) {
5
+ resolve(value);
6
+ });
7
+ }
8
+ return new (P || (P = Promise))(function(resolve, reject) {
9
+ function fulfilled(value) {
10
+ try {
11
+ step(generator.next(value));
12
+ } catch (e) {
13
+ reject(e);
14
+ }
15
+ }
16
+ function rejected(value) {
17
+ try {
18
+ step(generator["throw"](value));
19
+ } catch (e) {
20
+ reject(e);
21
+ }
22
+ }
23
+ function step(result) {
24
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
25
+ }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ };
29
+ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
30
+ var _ = {
31
+ label: 0,
32
+ sent: function sent() {
33
+ if (t[0] & 1) throw t[1];
34
+ return t[1];
35
+ },
36
+ trys: [],
37
+ ops: []
38
+ }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
39
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
40
+ return this;
41
+ }), g;
42
+ function verb(n) {
43
+ return function(v) {
44
+ return step([
45
+ n,
46
+ v
47
+ ]);
48
+ };
49
+ }
50
+ function step(op) {
51
+ if (f) throw new TypeError("Generator is already executing.");
52
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
53
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
54
+ if (y = 0, t) op = [
55
+ op[0] & 2,
56
+ t.value
57
+ ];
58
+ switch(op[0]){
59
+ case 0:
60
+ case 1:
61
+ t = op;
62
+ break;
63
+ case 4:
64
+ _.label++;
65
+ return {
66
+ value: op[1],
67
+ done: false
68
+ };
69
+ case 5:
70
+ _.label++;
71
+ y = op[1];
72
+ op = [
73
+ 0
74
+ ];
75
+ continue;
76
+ case 7:
77
+ op = _.ops.pop();
78
+ _.trys.pop();
79
+ continue;
80
+ default:
81
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
82
+ _ = 0;
83
+ continue;
84
+ }
85
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
86
+ _.label = op[1];
87
+ break;
88
+ }
89
+ if (op[0] === 6 && _.label < t[1]) {
90
+ _.label = t[1];
91
+ t = op;
92
+ break;
93
+ }
94
+ if (t && _.label < t[2]) {
95
+ _.label = t[2];
96
+ _.ops.push(op);
97
+ break;
98
+ }
99
+ if (t[2]) _.ops.pop();
100
+ _.trys.pop();
101
+ continue;
102
+ }
103
+ op = body.call(thisArg, _);
104
+ } catch (e) {
105
+ op = [
106
+ 6,
107
+ e
108
+ ];
109
+ y = 0;
110
+ } finally{
111
+ f = t = 0;
112
+ }
113
+ if (op[0] & 5) throw op[1];
114
+ return {
115
+ value: op[0] ? op[1] : void 0,
116
+ done: true
117
+ };
118
+ }
119
+ };
120
+ Object.defineProperty(exports, "__esModule", {
121
+ value: true
122
+ });
123
+ function random(min, max) {
124
+ return (Math.random() * (max - min + 1) | 0) + min;
125
+ }
126
+ function sleep(time) {
127
+ return new Promise(function(resolve) {
128
+ var t = setTimeout(function() {
129
+ clearTimeout(t);
130
+ t = undefined;
131
+ resolve();
132
+ }, time);
133
+ });
134
+ }
135
+ var PendingQueue = /** @class */ function() {
136
+ /**
137
+ * @param {Authorizer} authorizer
138
+ * @param {Config} config
139
+ * @param {number} minTryRetryInterval
140
+ * @param {number} maxTryRetryInterval
141
+ * @param retryTimes
142
+ */ function PendingQueue(authorizer, config, minTryRetryInterval, maxTryRetryInterval, retryTimes) {
143
+ if (minTryRetryInterval === void 0) {
144
+ minTryRetryInterval = 3000;
145
+ }
146
+ if (maxTryRetryInterval === void 0) {
147
+ maxTryRetryInterval = 5000;
148
+ }
149
+ if (retryTimes === void 0) {
150
+ retryTimes = 3;
151
+ }
152
+ this._queue = [];
153
+ this._resendAxiosInstance = null;
154
+ this._authorizer = authorizer;
155
+ this._config = config;
156
+ this._minTryRetryInterval = minTryRetryInterval;
157
+ this._maxTryRetryInterval = maxTryRetryInterval;
158
+ this._retryTimes = retryTimes;
159
+ }
160
+ PendingQueue.prototype.resend = function(error_1) {
161
+ return __awaiter(this, arguments, void 0, function(error, retries) {
162
+ var maxTimes, i, session, _a, e_1;
163
+ if (retries === void 0) {
164
+ retries = 3;
165
+ }
166
+ return __generator(this, function(_b) {
167
+ switch(_b.label){
168
+ case 0:
169
+ maxTimes = Math.max(this._retryTimes, retries, 1);
170
+ i = 0;
171
+ _b.label = 1;
172
+ case 1:
173
+ if (!(i < maxTimes)) return [
174
+ 3 /*break*/ ,
175
+ 11
176
+ ];
177
+ if (!(i > 0)) return [
178
+ 3 /*break*/ ,
179
+ 3
180
+ ];
181
+ return [
182
+ 4 /*yield*/ ,
183
+ sleep(random(this._minTryRetryInterval, this._maxTryRetryInterval))
184
+ ];
185
+ case 2:
186
+ _b.sent();
187
+ _b.label = 3;
188
+ case 3:
189
+ return [
190
+ 4 /*yield*/ ,
191
+ this._authorizer.getSession()
192
+ ];
193
+ case 4:
194
+ session = _b.sent();
195
+ if (this._authorizer.sessionHistory.isDeprecated(session)) {
196
+ // without re-login again, residual invalid session
197
+ throw error;
198
+ }
199
+ _b.label = 5;
200
+ case 5:
201
+ _b.trys.push([
202
+ 5,
203
+ 9,
204
+ ,
205
+ 10
206
+ ]);
207
+ if (!!this._resendAxiosInstance) return [
208
+ 3 /*break*/ ,
209
+ 7
210
+ ];
211
+ _a = this;
212
+ return [
213
+ 4 /*yield*/ ,
214
+ this._config.axiosProvider.get()
215
+ ];
216
+ case 6:
217
+ _a._resendAxiosInstance = _b.sent().create();
218
+ _b.label = 7;
219
+ case 7:
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
227
+ return [
228
+ 2 /*return*/ ,
229
+ _b.sent()
230
+ ];
231
+ case 9:
232
+ e_1 = _b.sent();
233
+ if (i >= maxTimes) {
234
+ throw e_1;
235
+ }
236
+ return [
237
+ 3 /*break*/ ,
238
+ 10
239
+ ];
240
+ case 10:
241
+ i++;
242
+ return [
243
+ 3 /*break*/ ,
244
+ 1
245
+ ];
246
+ case 11:
247
+ throw error;
248
+ }
249
+ });
250
+ });
251
+ };
252
+ PendingQueue.prototype.push = function(error) {
253
+ var _this = this;
254
+ return new Promise(function(resolve, reject) {
255
+ _this._queue.push({
256
+ error: error,
257
+ resolve: resolve,
258
+ reject: reject
259
+ });
260
+ });
261
+ };
262
+ PendingQueue.prototype.pop = function() {
263
+ var requestSaved = this._queue.shift();
264
+ if (!requestSaved) {
265
+ return;
266
+ }
267
+ var error = requestSaved.error, resolve = requestSaved.resolve, reject = requestSaved.reject;
268
+ this.resend(error).then(resolve)["catch"](reject);
269
+ };
270
+ PendingQueue.prototype.clear = function() {
271
+ this._queue.splice(0).forEach(function(_a) {
272
+ var error = _a.error, reject = _a.reject;
273
+ reject(error);
274
+ });
275
+ };
276
+ Object.defineProperty(PendingQueue.prototype, "size", {
277
+ get: function get() {
278
+ return this._queue.length;
279
+ },
280
+ enumerable: false,
281
+ configurable: true
282
+ });
283
+ return PendingQueue;
284
+ }();
285
+ exports["default"] = PendingQueue;
@@ -0,0 +1,6 @@
1
+ export default class SessionStorage {
2
+ _inMemoryStorage: Record<string, any>;
3
+ set(key: string, value: any): Promise<void>;
4
+ get(key: string): Promise<any>;
5
+ remove(key: string): Promise<void>;
6
+ }