axios-annotations 1.3.3 → 2.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.
- package/README.md +248 -105
- package/index.d.ts +1 -0
- package/index.js +121 -0
- package/lib/core/cancel.d.ts +30 -0
- package/lib/core/cancel.js +56 -0
- package/lib/core/common.d.ts +6 -0
- package/lib/core/common.js +166 -1
- package/lib/core/config.d.ts +60 -29
- package/lib/core/config.js +332 -1
- package/lib/core/parser.d.ts +19 -15
- package/lib/core/parser.js +79 -1
- package/lib/core/provider.d.ts +6 -0
- package/lib/core/provider.js +172 -0
- package/lib/core/service.d.ts +153 -33
- package/lib/core/service.js +562 -1
- package/lib/decorator/abort-source.d.ts +3 -9
- package/lib/decorator/abort-source.js +25 -1
- package/lib/decorator/delete-mapping.d.ts +1 -3
- package/lib/decorator/delete-mapping.js +13 -1
- package/lib/decorator/get-mapping.d.ts +1 -3
- package/lib/decorator/get-mapping.js +13 -1
- package/lib/decorator/ignore-residual-params.d.ts +1 -3
- package/lib/decorator/ignore-residual-params.js +24 -1
- package/lib/decorator/patch-mapping.d.ts +1 -3
- package/lib/decorator/patch-mapping.js +13 -1
- package/lib/decorator/post-mapping.d.ts +1 -3
- package/lib/decorator/post-mapping.js +13 -1
- package/lib/decorator/put-mapping.d.ts +1 -3
- package/lib/decorator/put-mapping.js +13 -1
- package/lib/decorator/request-body.d.ts +1 -3
- package/lib/decorator/request-body.js +28 -1
- package/lib/decorator/request-config.d.ts +5 -7
- package/lib/decorator/request-config.js +23 -1
- package/lib/decorator/request-header.d.ts +2 -5
- package/lib/decorator/request-header.js +21 -1
- package/lib/decorator/request-mapping.d.ts +3 -6
- package/lib/decorator/request-mapping.js +58 -1
- package/lib/decorator/request-param.d.ts +1 -3
- package/lib/decorator/request-param.js +27 -1
- package/lib/decorator/request-with.d.ts +1 -3
- package/lib/decorator/request-with.js +20 -1
- package/lib/index.d.ts +19 -0
- package/lib/index.js +139 -0
- package/lib/plugins/auth/authorizer.d.ts +23 -49
- package/lib/plugins/auth/authorizer.js +271 -1
- package/lib/plugins/auth/history.d.ts +14 -15
- package/lib/plugins/auth/history.js +96 -1
- package/lib/plugins/auth/index.d.ts +9 -4
- package/lib/plugins/auth/index.js +368 -1
- package/lib/plugins/auth/queue.d.ts +28 -16
- package/lib/plugins/auth/queue.js +270 -1
- package/lib/plugins/auth/storage.d.ts +6 -7
- package/lib/plugins/auth/storage.js +207 -1
- package/package.json +16 -16
- package/plugins/auth/index.d.ts +4 -4
- package/plugins/auth/index.js +26 -1
- package/core/config.d.ts +0 -29
- package/core/config.js +0 -1
- package/core/parser.d.ts +0 -15
- package/core/parser.js +0 -1
- package/core/service.d.ts +0 -33
- package/core/service.js +0 -1
- package/decorator/abort-source.d.ts +0 -9
- package/decorator/abort-source.js +0 -1
- package/decorator/delete-mapping.d.ts +0 -1
- package/decorator/delete-mapping.js +0 -1
- package/decorator/get-mapping.d.ts +0 -1
- package/decorator/get-mapping.js +0 -1
- package/decorator/ignore-residual-params.d.ts +0 -1
- package/decorator/ignore-residual-params.js +0 -1
- package/decorator/patch-mapping.d.ts +0 -1
- package/decorator/patch-mapping.js +0 -1
- package/decorator/post-mapping.d.ts +0 -1
- package/decorator/post-mapping.js +0 -1
- package/decorator/put-mapping.d.ts +0 -3
- package/decorator/put-mapping.js +0 -1
- package/decorator/request-body.d.ts +0 -3
- package/decorator/request-body.js +0 -1
- package/decorator/request-config.d.ts +0 -29
- package/decorator/request-config.js +0 -1
- package/decorator/request-header.d.ts +0 -1
- package/decorator/request-header.js +0 -1
- package/decorator/request-mapping.d.ts +0 -1
- package/decorator/request-mapping.js +0 -1
- package/decorator/request-param.d.ts +0 -1
- package/decorator/request-param.js +0 -1
- package/decorator/request-with.d.ts +0 -1
- package/decorator/request-with.js +0 -1
- package/lib/plugins/cache/index.js +0 -1
- package/plugins/auth/authorizer.d.ts +0 -49
- package/plugins/auth/authorizer.js +0 -1
- package/plugins/auth/history.d.ts +0 -15
- package/plugins/auth/history.js +0 -1
- package/plugins/auth/queue.d.ts +0 -16
- package/plugins/auth/queue.js +0 -1
- package/plugins/auth/storage.d.ts +0 -7
- package/plugins/auth/storage.js +0 -1
- package/plugins/cache/index.js +0 -1
|
@@ -1 +1,368 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = this && this.__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 = this && this.__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
|
+
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
|
+
axios.interceptors.response.use(function(i) {
|
|
147
|
+
if (expiredSession) {
|
|
148
|
+
// Previously requests sent have been rejected, record and release it (will be resending)
|
|
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
|
+
queue.clear();
|
|
206
|
+
return [
|
|
207
|
+
7 /*endfinally*/
|
|
208
|
+
];
|
|
209
|
+
case 6:
|
|
210
|
+
expiredSession = session;
|
|
211
|
+
if (!!authorizer.checkSession(e.config, session)) return [
|
|
212
|
+
3 /*break*/ ,
|
|
213
|
+
16
|
|
214
|
+
];
|
|
215
|
+
_c.label = 7;
|
|
216
|
+
case 7:
|
|
217
|
+
_c.trys.push([
|
|
218
|
+
7,
|
|
219
|
+
10,
|
|
220
|
+
,
|
|
221
|
+
16
|
|
222
|
+
]);
|
|
223
|
+
_b = (_a = authorizer).storageSession;
|
|
224
|
+
return [
|
|
225
|
+
4 /*yield*/ ,
|
|
226
|
+
authorizer.refreshSession(session)
|
|
227
|
+
];
|
|
228
|
+
case 8:
|
|
229
|
+
// waiting for next accessToken refresh, on the server side, updates are synchronized.
|
|
230
|
+
// Note:
|
|
231
|
+
// even if the refreshing request send few times, the server side will only return the same token.
|
|
232
|
+
return [
|
|
233
|
+
4 /*yield*/ ,
|
|
234
|
+
_b.apply(_a, [
|
|
235
|
+
_c.sent()
|
|
236
|
+
])
|
|
237
|
+
];
|
|
238
|
+
case 9:
|
|
239
|
+
// waiting for next accessToken refresh, on the server side, updates are synchronized.
|
|
240
|
+
// Note:
|
|
241
|
+
// even if the refreshing request send few times, the server side will only return the same token.
|
|
242
|
+
_c.sent();
|
|
243
|
+
return [
|
|
244
|
+
3 /*break*/ ,
|
|
245
|
+
16
|
|
246
|
+
];
|
|
247
|
+
case 10:
|
|
248
|
+
e2_1 = _c.sent();
|
|
249
|
+
// invalid refresh_token
|
|
250
|
+
authorizer.sessionHistory.deprecate(session);
|
|
251
|
+
_c.label = 11;
|
|
252
|
+
case 11:
|
|
253
|
+
_c.trys.push([
|
|
254
|
+
11,
|
|
255
|
+
13,
|
|
256
|
+
14,
|
|
257
|
+
15
|
|
258
|
+
]);
|
|
259
|
+
return [
|
|
260
|
+
4 /*yield*/ ,
|
|
261
|
+
authorizer.onAuthorizedDenied(e2_1)
|
|
262
|
+
];
|
|
263
|
+
case 12:
|
|
264
|
+
_c.sent();
|
|
265
|
+
authorizer.sessionHistory.clean();
|
|
266
|
+
return [
|
|
267
|
+
3 /*break*/ ,
|
|
268
|
+
15
|
|
269
|
+
];
|
|
270
|
+
case 13:
|
|
271
|
+
e3_1 = _c.sent();
|
|
272
|
+
throw e;
|
|
273
|
+
case 14:
|
|
274
|
+
unauthorized = false;
|
|
275
|
+
// cancel all requests resending.
|
|
276
|
+
queue.clear();
|
|
277
|
+
return [
|
|
278
|
+
7 /*endfinally*/
|
|
279
|
+
];
|
|
280
|
+
case 15:
|
|
281
|
+
return [
|
|
282
|
+
3 /*break*/ ,
|
|
283
|
+
16
|
|
284
|
+
];
|
|
285
|
+
case 16:
|
|
286
|
+
unauthorized = false;
|
|
287
|
+
// the accessToken refreshed, send the request itself and startup resending queue cleaning.
|
|
288
|
+
(function() {
|
|
289
|
+
while(queue.size){
|
|
290
|
+
queue.pop();
|
|
291
|
+
}
|
|
292
|
+
})();
|
|
293
|
+
_c.label = 17;
|
|
294
|
+
case 17:
|
|
295
|
+
_c.trys.push([
|
|
296
|
+
17,
|
|
297
|
+
19,
|
|
298
|
+
,
|
|
299
|
+
20
|
|
300
|
+
]);
|
|
301
|
+
return [
|
|
302
|
+
4 /*yield*/ ,
|
|
303
|
+
queue.resend(e)
|
|
304
|
+
];
|
|
305
|
+
case 18:
|
|
306
|
+
return [
|
|
307
|
+
2 /*return*/ ,
|
|
308
|
+
_c.sent()
|
|
309
|
+
];
|
|
310
|
+
case 19:
|
|
311
|
+
e4_1 = _c.sent();
|
|
312
|
+
// 500 502 503 ...
|
|
313
|
+
throw e4_1;
|
|
314
|
+
case 20:
|
|
315
|
+
return [
|
|
316
|
+
3 /*break*/ ,
|
|
317
|
+
23
|
|
318
|
+
];
|
|
319
|
+
case 21:
|
|
320
|
+
return [
|
|
321
|
+
4 /*yield*/ ,
|
|
322
|
+
queue.push(e)
|
|
323
|
+
];
|
|
324
|
+
case 22:
|
|
325
|
+
// No, 401 request occurred, put in to resending queue itself and waiting.
|
|
326
|
+
return [
|
|
327
|
+
2 /*return*/ ,
|
|
328
|
+
_c.sent()
|
|
329
|
+
];
|
|
330
|
+
case 23:
|
|
331
|
+
return [
|
|
332
|
+
3 /*break*/ ,
|
|
333
|
+
25
|
|
334
|
+
];
|
|
335
|
+
case 24:
|
|
336
|
+
// HTTP Code = 500 503 502 ... just throw it
|
|
337
|
+
throw e;
|
|
338
|
+
case 25:
|
|
339
|
+
return [
|
|
340
|
+
2 /*return*/
|
|
341
|
+
];
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
axios.interceptors.request.use(function(request) {
|
|
347
|
+
return __awaiter(this, void 0, void 0, function() {
|
|
348
|
+
var session;
|
|
349
|
+
return __generator(this, function(_a) {
|
|
350
|
+
switch(_a.label){
|
|
351
|
+
case 0:
|
|
352
|
+
return [
|
|
353
|
+
4 /*yield*/ ,
|
|
354
|
+
authorizer.getSession()
|
|
355
|
+
];
|
|
356
|
+
case 1:
|
|
357
|
+
session = _a.sent();
|
|
358
|
+
authorizer.withAuthentication(request, session);
|
|
359
|
+
return [
|
|
360
|
+
2 /*return*/ ,
|
|
361
|
+
request
|
|
362
|
+
];
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
};
|
|
368
|
+
}
|
|
@@ -1,16 +1,28 @@
|
|
|
1
|
-
import {AxiosError
|
|
2
|
-
import Authorizer from "./authorizer";
|
|
3
|
-
|
|
4
|
-
export default class PendingQueue {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
1
|
+
import type { AxiosError } from "axios";
|
|
2
|
+
import Authorizer from "./authorizer";
|
|
3
|
+
import { Config } from "../../index";
|
|
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
|
+
/**
|
|
16
|
+
* @param {Authorizer} authorizer
|
|
17
|
+
* @param {Config} config
|
|
18
|
+
* @param {number} minTryRetryInterval
|
|
19
|
+
* @param {number} maxTryRetryInterval
|
|
20
|
+
* @param retryTimes
|
|
21
|
+
*/
|
|
22
|
+
constructor(authorizer: Authorizer, config: Config, minTryRetryInterval?: number, maxTryRetryInterval?: number, retryTimes?: number);
|
|
23
|
+
resend(error: AxiosError, retries?: number): Promise<any>;
|
|
24
|
+
push(error: AxiosError): Promise<any>;
|
|
25
|
+
pop(): void;
|
|
26
|
+
clear(): void;
|
|
27
|
+
get size(): number;
|
|
28
|
+
}
|