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,268 @@
1
+ var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
2
+ function adopt(value) {
3
+ return value instanceof P ? value : new P(function(resolve) {
4
+ resolve(value);
5
+ });
6
+ }
7
+ return new (P || (P = Promise))(function(resolve, reject) {
8
+ function fulfilled(value) {
9
+ try {
10
+ step(generator.next(value));
11
+ } catch (e) {
12
+ reject(e);
13
+ }
14
+ }
15
+ function rejected(value) {
16
+ try {
17
+ step(generator["throw"](value));
18
+ } catch (e) {
19
+ reject(e);
20
+ }
21
+ }
22
+ function step(result) {
23
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
24
+ }
25
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
26
+ });
27
+ };
28
+ var __generator = this && this.__generator || function(thisArg, body) {
29
+ var _ = {
30
+ label: 0,
31
+ sent: function sent() {
32
+ if (t[0] & 1) throw t[1];
33
+ return t[1];
34
+ },
35
+ trys: [],
36
+ ops: []
37
+ }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
38
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
39
+ return this;
40
+ }), g;
41
+ function verb(n) {
42
+ return function(v) {
43
+ return step([
44
+ n,
45
+ v
46
+ ]);
47
+ };
48
+ }
49
+ function step(op) {
50
+ if (f) throw new TypeError("Generator is already executing.");
51
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
52
+ 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;
53
+ if (y = 0, t) op = [
54
+ op[0] & 2,
55
+ t.value
56
+ ];
57
+ switch(op[0]){
58
+ case 0:
59
+ case 1:
60
+ t = op;
61
+ break;
62
+ case 4:
63
+ _.label++;
64
+ return {
65
+ value: op[1],
66
+ done: false
67
+ };
68
+ case 5:
69
+ _.label++;
70
+ y = op[1];
71
+ op = [
72
+ 0
73
+ ];
74
+ continue;
75
+ case 7:
76
+ op = _.ops.pop();
77
+ _.trys.pop();
78
+ continue;
79
+ default:
80
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
81
+ _ = 0;
82
+ continue;
83
+ }
84
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
85
+ _.label = op[1];
86
+ break;
87
+ }
88
+ if (op[0] === 6 && _.label < t[1]) {
89
+ _.label = t[1];
90
+ t = op;
91
+ break;
92
+ }
93
+ if (t && _.label < t[2]) {
94
+ _.label = t[2];
95
+ _.ops.push(op);
96
+ break;
97
+ }
98
+ if (t[2]) _.ops.pop();
99
+ _.trys.pop();
100
+ continue;
101
+ }
102
+ op = body.call(thisArg, _);
103
+ } catch (e) {
104
+ op = [
105
+ 6,
106
+ e
107
+ ];
108
+ y = 0;
109
+ } finally{
110
+ f = t = 0;
111
+ }
112
+ if (op[0] & 5) throw op[1];
113
+ return {
114
+ value: op[0] ? op[1] : void 0,
115
+ done: true
116
+ };
117
+ }
118
+ };
119
+ import SessionStorage from "./storage.js";
120
+ import SessionHistory from "./history.js";
121
+ var Authorizer = /** @class */ function() {
122
+ function Authorizer() {
123
+ this._sessionKey = "$_SESSION";
124
+ this._sessionStorage = new SessionStorage();
125
+ this._sessionHistory = new SessionHistory();
126
+ }
127
+ Object.defineProperty(Authorizer.prototype, "sessionKey", {
128
+ get: function get() {
129
+ return this._sessionKey;
130
+ },
131
+ set: function set(value) {
132
+ this._sessionKey = value;
133
+ },
134
+ enumerable: false,
135
+ configurable: true
136
+ });
137
+ Object.defineProperty(Authorizer.prototype, "sessionStorage", {
138
+ get: function get() {
139
+ return this._sessionStorage;
140
+ },
141
+ set: function set(value) {
142
+ this._sessionStorage = value;
143
+ },
144
+ enumerable: false,
145
+ configurable: true
146
+ });
147
+ Object.defineProperty(Authorizer.prototype, "sessionHistory", {
148
+ get: function get() {
149
+ return this._sessionHistory;
150
+ },
151
+ set: function set(value) {
152
+ this._sessionHistory = value;
153
+ },
154
+ enumerable: false,
155
+ configurable: true
156
+ });
157
+ Authorizer.prototype.getSession = function() {
158
+ return __awaiter(this, void 0, void 0, function() {
159
+ return __generator(this, function(_a) {
160
+ switch(_a.label){
161
+ case 0:
162
+ return [
163
+ 4 /*yield*/ ,
164
+ this.sessionStorage.get(this.sessionKey)
165
+ ];
166
+ case 1:
167
+ return [
168
+ 2 /*return*/ ,
169
+ _a.sent()
170
+ ];
171
+ }
172
+ });
173
+ });
174
+ };
175
+ Authorizer.prototype.storageSession = function(session) {
176
+ return __awaiter(this, void 0, void 0, function() {
177
+ return __generator(this, function(_a) {
178
+ switch(_a.label){
179
+ case 0:
180
+ return [
181
+ 4 /*yield*/ ,
182
+ this.sessionStorage.set(this.sessionKey, session)
183
+ ];
184
+ case 1:
185
+ _a.sent();
186
+ return [
187
+ 2 /*return*/
188
+ ];
189
+ }
190
+ });
191
+ });
192
+ };
193
+ Authorizer.prototype.refreshSession = function(_session) {
194
+ return __awaiter(this, void 0, void 0, function() {
195
+ return __generator(this, function(_a) {
196
+ return [
197
+ 2 /*return*/ ,
198
+ null
199
+ ];
200
+ });
201
+ });
202
+ };
203
+ // append auth headers
204
+ Authorizer.prototype.withAuthentication = function(request, session) {
205
+ if (session) {
206
+ var access_token = session.access_token, accessToken = session.accessToken, token = session.token;
207
+ if (access_token || accessToken || token) {
208
+ if (request.headers) {
209
+ request.headers['Authorization'] = "Bearer " + (access_token || accessToken || token);
210
+ }
211
+ }
212
+ }
213
+ };
214
+ // 匹配队列中的请求头token跟会话token是否一致
215
+ Authorizer.prototype.checkSession = function(request, session) {
216
+ var header = (request.headers || {})["Authorization"] || (request.headers || {})["authorization"];
217
+ var jwt = typeof header === "string" ? header.split(" ")[1] || "" : "";
218
+ var token = session.access_token || session.accessToken || session.token;
219
+ if (token === jwt) {
220
+ // request header token equal to invalid session token
221
+ return false;
222
+ }
223
+ // request time is too long , token may be refreshed few times
224
+ if (this.sessionHistory.size) {
225
+ if (this.sessionHistory.check(jwt)) {
226
+ return false;
227
+ }
228
+ }
229
+ return true;
230
+ };
231
+ Authorizer.prototype.checkResponse = function(response) {
232
+ var status = (response || {
233
+ status: 0
234
+ }).status;
235
+ return status !== 401;
236
+ };
237
+ Authorizer.prototype.onAuthorizedDenied = function(error) {
238
+ return __awaiter(this, void 0, void 0, function() {
239
+ return __generator(this, function(_a) {
240
+ throw error;
241
+ });
242
+ });
243
+ };
244
+ Authorizer.prototype.onSessionInvalidated = function() {
245
+ return;
246
+ };
247
+ Authorizer.prototype.invalidateSession = function() {
248
+ return __awaiter(this, void 0, void 0, function() {
249
+ return __generator(this, function(_a) {
250
+ switch(_a.label){
251
+ case 0:
252
+ return [
253
+ 4 /*yield*/ ,
254
+ this.sessionStorage.remove(this.sessionKey)
255
+ ];
256
+ case 1:
257
+ _a.sent();
258
+ this.onSessionInvalidated();
259
+ return [
260
+ 2 /*return*/
261
+ ];
262
+ }
263
+ });
264
+ });
265
+ };
266
+ return Authorizer;
267
+ }();
268
+ export default Authorizer;
@@ -0,0 +1,15 @@
1
+ import { BasicSession } from "./index";
2
+ /**
3
+ * 缓存过期会话,判断请求是否过期
4
+ */
5
+ export default class SessionHistory {
6
+ _history: (BasicSession | null)[];
7
+ _position: number;
8
+ _size: number;
9
+ get size(): number;
10
+ add(session: BasicSession): void;
11
+ check(jwt: string): boolean;
12
+ deprecate(session: BasicSession): void;
13
+ clean(): void;
14
+ isDeprecated(session: BasicSession): boolean;
15
+ }
@@ -0,0 +1,94 @@
1
+ import { isNullOrEmpty } from "../../core/common.js";
2
+ /**
3
+ * 缓存过期会话,判断请求是否过期
4
+ */ var SessionHistory = /** @class */ function() {
5
+ function SessionHistory() {
6
+ // store up to 10 sessions, include expired or not
7
+ this._history = new Array(10);
8
+ // next session storage location
9
+ this._position = 0;
10
+ this._size = 0;
11
+ }
12
+ Object.defineProperty(SessionHistory.prototype, "size", {
13
+ get: function get() {
14
+ return this._size;
15
+ },
16
+ enumerable: false,
17
+ configurable: true
18
+ });
19
+ // store primary keys for session
20
+ // 缓存 session 历史,这里只是添加并未作废,防止同时存在多个有效 session,这里允许存放有效 session
21
+ // 使用 invalid 作为标记
22
+ SessionHistory.prototype.add = function(session) {
23
+ if (Object.keys(session).every(function(i) {
24
+ return isNullOrEmpty(session[i]);
25
+ })) {
26
+ return;
27
+ }
28
+ var access_token = session.access_token, accessToken = session.accessToken, token = session.token, refresh_token = session.refresh_token, refreshToken = session.refreshToken;
29
+ this._history[this._position] = Object.assign({}, session, {
30
+ access_token: access_token || accessToken || token,
31
+ refresh_token: refresh_token || refreshToken,
32
+ invalid: false
33
+ });
34
+ this._position++;
35
+ this._position %= this._history.length;
36
+ this._size = this._history.reduce(function(s, i) {
37
+ return i ? s + 1 : s;
38
+ }, 0);
39
+ };
40
+ // session exist or not
41
+ SessionHistory.prototype.check = function(jwt) {
42
+ return this._history.some(function(session) {
43
+ if (session) {
44
+ var access_token = session.access_token, accessToken = session.accessToken, token = session.token;
45
+ return (access_token || accessToken || token) === jwt;
46
+ }
47
+ return false;
48
+ });
49
+ };
50
+ // mark the session expired
51
+ SessionHistory.prototype.deprecate = function(session) {
52
+ var position = 0;
53
+ var refresh_token = session.refresh_token, refreshToken = session.refreshToken;
54
+ var matching = this._history.find(function(s, i) {
55
+ position = i;
56
+ if (s) {
57
+ return (s.refresh_token || s.refreshToken) === (refresh_token || refreshToken);
58
+ }
59
+ return false;
60
+ });
61
+ if (matching) {
62
+ matching.invalid = true;
63
+ var first = this._history[0];
64
+ this._history[0] = this._history[position];
65
+ this._history[position] = first;
66
+ }
67
+ };
68
+ // remove expired sessions
69
+ SessionHistory.prototype.clean = function() {
70
+ for(var i = 0; i < this._history.length; ++i){
71
+ var s = this._history[i];
72
+ if (s && s.invalid) {
73
+ this._history[i] = null;
74
+ }
75
+ }
76
+ this._size = this._history.reduce(function(s, i) {
77
+ return i ? s + 1 : s;
78
+ }, 0);
79
+ };
80
+ // is the session expired
81
+ SessionHistory.prototype.isDeprecated = function(session) {
82
+ return this._history.some(function(s) {
83
+ if (s) {
84
+ var refresh_token = session.refresh_token, refreshToken = session.refreshToken;
85
+ if (refresh_token || refreshToken) {
86
+ return (s.refresh_token || s.refreshToken) === (refresh_token || refreshToken) && s.invalid;
87
+ }
88
+ }
89
+ return false;
90
+ });
91
+ };
92
+ return SessionHistory;
93
+ }();
94
+ export default SessionHistory;
@@ -0,0 +1,16 @@
1
+ import Authorizer from "./authorizer";
2
+ import type { ConfigPlugin } from "../../core/config";
3
+ export { default as Authorizer } from "./authorizer";
4
+ export { default as SessionStorage } from "./storage";
5
+ export type BasicSession = {
6
+ access_token?: string;
7
+ accessToken?: string;
8
+ token?: string;
9
+ refreshToken?: string;
10
+ refresh_token?: string;
11
+ } & Record<string, any>;
12
+ export default function AuthorizationPlugin(authorizer: Authorizer, options?: {
13
+ minTryRetryInterval: number;
14
+ maxTryRetryInterval: number;
15
+ retryTimes: number;
16
+ }): ConfigPlugin;