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
@@ -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
+ }
@@ -0,0 +1,515 @@
1
+ var __assign = this && this.__assign || function() {
2
+ __assign = Object.assign || function(t) {
3
+ for(var s, i = 1, n = arguments.length; i < n; i++){
4
+ s = arguments[i];
5
+ for(var p in s)if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
6
+ }
7
+ return t;
8
+ };
9
+ return __assign.apply(this, arguments);
10
+ };
11
+ var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
12
+ function adopt(value) {
13
+ return value instanceof P ? value : new P(function(resolve) {
14
+ resolve(value);
15
+ });
16
+ }
17
+ return new (P || (P = Promise))(function(resolve, reject) {
18
+ function fulfilled(value) {
19
+ try {
20
+ step(generator.next(value));
21
+ } catch (e) {
22
+ reject(e);
23
+ }
24
+ }
25
+ function rejected(value) {
26
+ try {
27
+ step(generator["throw"](value));
28
+ } catch (e) {
29
+ reject(e);
30
+ }
31
+ }
32
+ function step(result) {
33
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
34
+ }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ };
38
+ var __generator = this && this.__generator || function(thisArg, body) {
39
+ var _ = {
40
+ label: 0,
41
+ sent: function sent() {
42
+ if (t[0] & 1) throw t[1];
43
+ return t[1];
44
+ },
45
+ trys: [],
46
+ ops: []
47
+ }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
48
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
49
+ return this;
50
+ }), g;
51
+ function verb(n) {
52
+ return function(v) {
53
+ return step([
54
+ n,
55
+ v
56
+ ]);
57
+ };
58
+ }
59
+ function step(op) {
60
+ if (f) throw new TypeError("Generator is already executing.");
61
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
62
+ 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;
63
+ if (y = 0, t) op = [
64
+ op[0] & 2,
65
+ t.value
66
+ ];
67
+ switch(op[0]){
68
+ case 0:
69
+ case 1:
70
+ t = op;
71
+ break;
72
+ case 4:
73
+ _.label++;
74
+ return {
75
+ value: op[1],
76
+ done: false
77
+ };
78
+ case 5:
79
+ _.label++;
80
+ y = op[1];
81
+ op = [
82
+ 0
83
+ ];
84
+ continue;
85
+ case 7:
86
+ op = _.ops.pop();
87
+ _.trys.pop();
88
+ continue;
89
+ default:
90
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
91
+ _ = 0;
92
+ continue;
93
+ }
94
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
95
+ _.label = op[1];
96
+ break;
97
+ }
98
+ if (op[0] === 6 && _.label < t[1]) {
99
+ _.label = t[1];
100
+ t = op;
101
+ break;
102
+ }
103
+ if (t && _.label < t[2]) {
104
+ _.label = t[2];
105
+ _.ops.push(op);
106
+ break;
107
+ }
108
+ if (t[2]) _.ops.pop();
109
+ _.trys.pop();
110
+ continue;
111
+ }
112
+ op = body.call(thisArg, _);
113
+ } catch (e) {
114
+ op = [
115
+ 6,
116
+ e
117
+ ];
118
+ y = 0;
119
+ } finally{
120
+ f = t = 0;
121
+ }
122
+ if (op[0] & 5) throw op[1];
123
+ return {
124
+ value: op[0] ? op[1] : void 0,
125
+ done: true
126
+ };
127
+ }
128
+ };
129
+ var __values = this && this.__values || function(o) {
130
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
131
+ if (m) return m.call(o);
132
+ if (o && typeof o.length === "number") return {
133
+ next: function next() {
134
+ if (o && i >= o.length) o = void 0;
135
+ return {
136
+ value: o && o[i++],
137
+ done: !o
138
+ };
139
+ }
140
+ };
141
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
142
+ };
143
+ var __read = this && this.__read || function(o, n) {
144
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
145
+ if (!m) return o;
146
+ var i = m.call(o), r, ar = [], e;
147
+ try {
148
+ while((n === void 0 || n-- > 0) && !(r = i.next()).done)ar.push(r.value);
149
+ } catch (error) {
150
+ e = {
151
+ error: error
152
+ };
153
+ } finally{
154
+ try {
155
+ if (r && !r.done && (m = i["return"])) m.call(i);
156
+ } finally{
157
+ if (e) throw e.error;
158
+ }
159
+ }
160
+ return ar;
161
+ };
162
+ var __spreadArray = this && this.__spreadArray || function(to, from, pack) {
163
+ if (pack || arguments.length === 2) for(var i = 0, l = from.length, ar; i < l; i++){
164
+ if (ar || !(i in from)) {
165
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
166
+ ar[i] = from[i];
167
+ }
168
+ }
169
+ return to.concat(ar || Array.prototype.slice.call(from));
170
+ };
171
+ import { mergeAxiosConfigs, isNullOrEmpty, normalizePath, isPlainObject, formatUrl } from "./common.js";
172
+ export var ParamsMappingType;
173
+ (function(ParamsMappingType) {
174
+ // 请求头
175
+ ParamsMappingType["HEADER"] = "HEADER";
176
+ // 查询串
177
+ ParamsMappingType["PARAM"] = "PARAM";
178
+ // 请求体
179
+ ParamsMappingType["BODY"] = "BODY";
180
+ // 路径变量
181
+ ParamsMappingType["PATH_VARIABLE"] = "PATH_VARIABLE";
182
+ })(ParamsMappingType || (ParamsMappingType = {}));
183
+ var RequestBuilder = /** @class */ function() {
184
+ function RequestBuilder() {
185
+ this._configs = null;
186
+ this._mapping = new Map();
187
+ this._pathVariablesMapping = [];
188
+ }
189
+ RequestBuilder.prototype._addMapping = function(type, mapping) {
190
+ var scopeMapping = this._mapping.get(type);
191
+ if (!scopeMapping) {
192
+ scopeMapping = new Map();
193
+ this._mapping.set(type, scopeMapping);
194
+ }
195
+ scopeMapping.set(mapping.key, mapping);
196
+ };
197
+ RequestBuilder.prototype._addPathVariablesMapping = function(mapping) {
198
+ this._pathVariablesMapping.push(mapping);
199
+ };
200
+ RequestBuilder.prototype._clearMapping = function(type) {
201
+ var scopeMapping = this._mapping.get(type);
202
+ if (scopeMapping) {
203
+ scopeMapping.clear();
204
+ }
205
+ };
206
+ RequestBuilder.prototype.param = function(keyOrMapping, required) {
207
+ if (typeof keyOrMapping === 'string') {
208
+ this._addMapping(ParamsMappingType.PARAM, {
209
+ value: undefined,
210
+ key: keyOrMapping,
211
+ // 查询串为空默认不填充
212
+ required: required === true
213
+ });
214
+ } else {
215
+ if (isNullOrEmpty(keyOrMapping.key)) {
216
+ throw new Error('param mapping key is required');
217
+ }
218
+ this._addMapping(ParamsMappingType.PARAM, __assign(__assign({}, keyOrMapping), {
219
+ required: keyOrMapping.required === true
220
+ }));
221
+ }
222
+ return this;
223
+ };
224
+ RequestBuilder.prototype.header = function(keyOrMapping, required) {
225
+ if (typeof keyOrMapping === 'string') {
226
+ this._addMapping(ParamsMappingType.HEADER, {
227
+ value: undefined,
228
+ key: keyOrMapping,
229
+ // 请求头默认强制填充,找不到填充空字符串
230
+ required: required !== false
231
+ });
232
+ } else {
233
+ if (isNullOrEmpty(keyOrMapping.key)) {
234
+ throw new Error('header mapping key is required');
235
+ }
236
+ this._addMapping(ParamsMappingType.HEADER, __assign(__assign({}, keyOrMapping), {
237
+ required: keyOrMapping.required === true
238
+ }));
239
+ }
240
+ return this;
241
+ };
242
+ // 请求体只有一个规则,因为不同请求体类型无法合并,请求体自动不存在则设置 null
243
+ RequestBuilder.prototype.body = function(keyOrMapping) {
244
+ this._clearMapping(ParamsMappingType.BODY);
245
+ if (typeof keyOrMapping === 'string') {
246
+ this._addMapping(ParamsMappingType.BODY, {
247
+ value: undefined,
248
+ key: keyOrMapping,
249
+ // 请求体 required 不生效,无法给出不同类型的默认值
250
+ required: false
251
+ });
252
+ } else {
253
+ if (isNullOrEmpty(keyOrMapping.key)) {
254
+ throw new Error('body mapping key is required');
255
+ }
256
+ this._addMapping(ParamsMappingType.BODY, __assign(__assign({}, keyOrMapping), {
257
+ required: false
258
+ }));
259
+ }
260
+ return this;
261
+ };
262
+ // 路径参数集合
263
+ // 路径参数数据源要求可合并并且不需要指定key,单独数据集合
264
+ RequestBuilder.prototype.pathVariable = function(keyOrMapping) {
265
+ if (typeof keyOrMapping === 'string') {
266
+ this._addPathVariablesMapping({
267
+ value: undefined,
268
+ key: keyOrMapping,
269
+ required: false // 必填参数无意义,占位符是固定的字符
270
+ });
271
+ } else {
272
+ this._addPathVariablesMapping(__assign(__assign({}, keyOrMapping), {
273
+ required: false
274
+ }));
275
+ }
276
+ return this;
277
+ };
278
+ RequestBuilder.prototype.config = function(cfg) {
279
+ if (!this._configs) {
280
+ this._configs = [];
281
+ }
282
+ if (this._configs.indexOf(cfg) === -1) {
283
+ this._configs.push(cfg);
284
+ }
285
+ return this;
286
+ };
287
+ RequestBuilder.prototype.build = function(config, path, method, source) {
288
+ var e_1, _a, _b, _c, e_2, _d, _e, _f, e_3, _g, e_4, _h;
289
+ if (source === void 0) {
290
+ source = {};
291
+ }
292
+ var params = {};
293
+ var headers = {};
294
+ var url = path.indexOf("http") >= 0 ? path : config.baseURL + normalizePath("/".concat(path));
295
+ var body = null;
296
+ // 处理查询串
297
+ var paramMapping = this._mapping.get(ParamsMappingType.PARAM);
298
+ if (paramMapping) {
299
+ try {
300
+ for(var paramMapping_1 = __values(paramMapping), paramMapping_1_1 = paramMapping_1.next(); !paramMapping_1_1.done; paramMapping_1_1 = paramMapping_1.next()){
301
+ var _j = __read(paramMapping_1_1.value, 2), key = _j[0], mapping = _j[1];
302
+ if (typeof mapping.value === 'function') {
303
+ var mapper = mapping.value;
304
+ var result = mapper.call(undefined, source);
305
+ if (isNullOrEmpty(result)) {
306
+ if (mapping.required === false) {
307
+ continue;
308
+ }
309
+ }
310
+ Object.assign(params, (_b = {}, _b[key] = result, _b));
311
+ } else {
312
+ // 如果指定了静态值,则使用静态值
313
+ var value = mapping.value;
314
+ if (isNullOrEmpty(value)) {
315
+ // 没有指定静态值,查询数据源
316
+ value = source[key];
317
+ }
318
+ if (isNullOrEmpty(value)) {
319
+ if (mapping.required === false) {
320
+ continue;
321
+ } else {
322
+ value = '';
323
+ }
324
+ }
325
+ Object.assign(params, (_c = {}, _c[key] = value, _c));
326
+ }
327
+ }
328
+ } catch (e_1_1) {
329
+ e_1 = {
330
+ error: e_1_1
331
+ };
332
+ } finally{
333
+ try {
334
+ if (paramMapping_1_1 && !paramMapping_1_1.done && (_a = paramMapping_1["return"])) _a.call(paramMapping_1);
335
+ } finally{
336
+ if (e_1) throw e_1.error;
337
+ }
338
+ }
339
+ }
340
+ // 处理请求头
341
+ var headerMapping = this._mapping.get(ParamsMappingType.HEADER);
342
+ if (headerMapping) {
343
+ try {
344
+ for(var headerMapping_1 = __values(headerMapping), headerMapping_1_1 = headerMapping_1.next(); !headerMapping_1_1.done; headerMapping_1_1 = headerMapping_1.next()){
345
+ var _k = __read(headerMapping_1_1.value, 2), key = _k[0], mapping = _k[1];
346
+ if (typeof mapping.value === 'function') {
347
+ var mapper = mapping.value;
348
+ var result = mapper.call(undefined, source);
349
+ if (isNullOrEmpty(result)) {
350
+ if (mapping.required === true) {
351
+ result = '';
352
+ } else {
353
+ continue;
354
+ }
355
+ }
356
+ Object.assign(headers, (_e = {}, _e[key] = result, _e));
357
+ } else {
358
+ var value = mapping.value;
359
+ if (isNullOrEmpty(value)) {
360
+ value = source[key];
361
+ }
362
+ if (isNullOrEmpty(value)) {
363
+ if (mapping.required === false) {
364
+ continue;
365
+ } else {
366
+ value = '';
367
+ }
368
+ }
369
+ Object.assign(headers, (_f = {}, _f[key] = value, _f));
370
+ }
371
+ }
372
+ } catch (e_2_1) {
373
+ e_2 = {
374
+ error: e_2_1
375
+ };
376
+ } finally{
377
+ try {
378
+ if (headerMapping_1_1 && !headerMapping_1_1.done && (_d = headerMapping_1["return"])) _d.call(headerMapping_1);
379
+ } finally{
380
+ if (e_2) throw e_2.error;
381
+ }
382
+ }
383
+ }
384
+ // 处理请求体
385
+ var bodyMapping = this._mapping.get(ParamsMappingType.BODY);
386
+ if (bodyMapping) {
387
+ try {
388
+ for(var bodyMapping_1 = __values(bodyMapping), bodyMapping_1_1 = bodyMapping_1.next(); !bodyMapping_1_1.done; bodyMapping_1_1 = bodyMapping_1.next()){
389
+ var _l = __read(bodyMapping_1_1.value, 2), key = _l[0], mapping = _l[1];
390
+ if (typeof mapping.value === 'function') {
391
+ var mapper = mapping.value;
392
+ var result = mapper.call(undefined, source);
393
+ if (result === undefined) {
394
+ result = null;
395
+ }
396
+ body = result;
397
+ } else {
398
+ var value = mapping.value;
399
+ if (isNullOrEmpty(value)) {
400
+ value = source[key];
401
+ }
402
+ if (value === undefined) {
403
+ value = null;
404
+ }
405
+ body = value;
406
+ }
407
+ }
408
+ } catch (e_3_1) {
409
+ e_3 = {
410
+ error: e_3_1
411
+ };
412
+ } finally{
413
+ try {
414
+ if (bodyMapping_1_1 && !bodyMapping_1_1.done && (_g = bodyMapping_1["return"])) _g.call(bodyMapping_1);
415
+ } finally{
416
+ if (e_3) throw e_3.error;
417
+ }
418
+ }
419
+ }
420
+ var mergeConfigs = {
421
+ headers: headers,
422
+ url: url,
423
+ method: method,
424
+ data: body,
425
+ params: params
426
+ };
427
+ if (this._configs) {
428
+ mergeConfigs = mergeAxiosConfigs.apply(void 0, __spreadArray([], __read(this._configs.map(function(cfg) {
429
+ return mergeAxiosConfigs(mergeConfigs, typeof cfg === 'function' ? cfg(source) : cfg);
430
+ })), false));
431
+ }
432
+ var pathVariables = null;
433
+ if (this._pathVariablesMapping.length) {
434
+ pathVariables = {};
435
+ try {
436
+ for(var _m = __values(this._pathVariablesMapping), _o = _m.next(); !_o.done; _o = _m.next()){
437
+ var mapping = _o.value;
438
+ if (typeof mapping.value === 'function') {
439
+ Object.assign(pathVariables, mapping.value(source));
440
+ } else {
441
+ var value = mapping.value;
442
+ if (isNullOrEmpty(value)) {
443
+ if (!mapping.key) {
444
+ // 如果不指定key,则使用 source 数据源本体
445
+ // 所以,如果需要开启路径参数功能至少配置一个 @PathVariables()
446
+ value = source;
447
+ } else {
448
+ value = source[mapping.key];
449
+ }
450
+ }
451
+ if (value === undefined) {
452
+ // 说明找不到
453
+ value = null;
454
+ } else {
455
+ if (!isPlainObject(value)) {
456
+ console.error('check path variables collection is plain object: ', value);
457
+ value = null;
458
+ }
459
+ }
460
+ Object.assign(pathVariables, value);
461
+ }
462
+ }
463
+ } catch (e_4_1) {
464
+ e_4 = {
465
+ error: e_4_1
466
+ };
467
+ } finally{
468
+ try {
469
+ if (_o && !_o.done && (_h = _m["return"])) _h.call(_m);
470
+ } finally{
471
+ if (e_4) throw e_4.error;
472
+ }
473
+ }
474
+ }
475
+ if (pathVariables) {
476
+ // 路径变量不为空,认为有意图使用占位符
477
+ var url_1 = mergeConfigs.url;
478
+ if (url_1) {
479
+ // url 的声明为 String?
480
+ mergeConfigs.url = formatUrl(url_1, pathVariables);
481
+ }
482
+ var baseUrl = mergeConfigs.baseURL;
483
+ if (baseUrl) {
484
+ // 一般不会对 baseUrl 使用占位符
485
+ mergeConfigs.baseURL = formatUrl(baseUrl, pathVariables);
486
+ }
487
+ }
488
+ return mergeConfigs;
489
+ };
490
+ RequestBuilder.prototype.buildWith = function(config_1, path_1, method_1) {
491
+ return __awaiter(this, arguments, void 0, function(config, path, method, source) {
492
+ var axiosInstance;
493
+ if (source === void 0) {
494
+ source = {};
495
+ }
496
+ return __generator(this, function(_a) {
497
+ switch(_a.label){
498
+ case 0:
499
+ return [
500
+ 4 /*yield*/ ,
501
+ config.requestAxiosInstance()
502
+ ];
503
+ case 1:
504
+ axiosInstance = _a.sent();
505
+ return [
506
+ 2 /*return*/ ,
507
+ axiosInstance.request(this.build(config, path, method, source))
508
+ ];
509
+ }
510
+ });
511
+ });
512
+ };
513
+ return RequestBuilder;
514
+ }();
515
+ export default RequestBuilder;
@@ -0,0 +1,36 @@
1
+ import type { AxiosRequestConfig } from 'axios';
2
+ import RequestBuilder from "./builder";
3
+ import Config from "./config";
4
+ export declare function normalizePath(path: string): string;
5
+ export declare function isNullOrEmpty(param: any): boolean;
6
+ type MetaPropertyDescriptor = PropertyDescriptor & {
7
+ builder: RequestBuilder;
8
+ config?: Config;
9
+ };
10
+ export declare function castToMetaDescriptor(descriptor: PropertyDescriptor): MetaPropertyDescriptor;
11
+ /**
12
+ * Merges multiple Axios request configurations with specific logic for Axios.
13
+ * - `headers` and `params` are shallowly merged.
14
+ * - All other properties are replaced by the last provided value.
15
+ *
16
+ * @param sources A list of AxiosRequestConfig objects to merge.
17
+ * @returns A new, merged AxiosRequestConfig object.
18
+ */
19
+ export declare function mergeAxiosConfigs(...sources: AxiosRequestConfig[]): AxiosRequestConfig;
20
+ /**
21
+ * 替换链接占位符,注意使用基本类型
22
+ * Replaces placeholders in a URL with values from a data object.
23
+ * e.g., formatUrl("http://a.com/{c}?d={e}", {c: 'x', e: 100}) -> "http://a.com/x?d=100"
24
+ *
25
+ * @param url The URL string with placeholders like {key}.
26
+ * @param data The plain object data source.
27
+ * @returns The formatted URL string.
28
+ */
29
+ export declare function formatUrl(url: string, data: Record<string, any>): string;
30
+ /**
31
+ * Checks if a value is a plain object, i.e., an object created by `{}` or `new Object()`.
32
+ * @param value The value to check.
33
+ * @returns True if the value is a plain object, false otherwise.
34
+ */
35
+ export declare function isPlainObject(value: any): value is Record<string, any>;
36
+ export {};