yapi-apifox-to-typescript 4.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.
@@ -0,0 +1,1202 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
6
+
7
+ exports.__esModule = true;
8
+ exports.Generator = void 0;
9
+
10
+ var _taggedTemplateLiteralLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteralLoose"));
11
+
12
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
13
+
14
+ var _createForOfIteratorHelperLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelperLoose"));
15
+
16
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
17
+
18
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
19
+
20
+ var changeCase = _interopRequireWildcard(require("change-case"));
21
+
22
+ var _dayjs = _interopRequireDefault(require("dayjs"));
23
+
24
+ var _fsExtra = _interopRequireDefault(require("fs-extra"));
25
+
26
+ var _path = _interopRequireDefault(require("path"));
27
+
28
+ var _ApifoxToYApiData = require("./ApifoxToYApiData");
29
+
30
+ var _vtils = require("vtils");
31
+
32
+ var _types = require("./types");
33
+
34
+ var _child_process = require("child_process");
35
+
36
+ var _utils = require("./utils");
37
+
38
+ var _SwaggerToYApiServer = require("./SwaggerToYApiServer");
39
+
40
+ var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8, _templateObject9, _templateObject10, _templateObject11, _templateObject12;
41
+
42
+ /**
43
+ * @see https://webpack.js.org/guides/tree-shaking/#mark-a-function-call-as-side-effect-free
44
+ * @see https://terser.org/docs/api-reference.html#annotations
45
+ */
46
+ var COMPRESSOR_TREE_SHAKING_ANNOTATION = '/*#__PURE__*/';
47
+
48
+ var Generator = /*#__PURE__*/function () {
49
+ /** 配置 */
50
+ function Generator(config, options) {
51
+ var _this = this;
52
+
53
+ if (options === void 0) {
54
+ options = {
55
+ cwd: process.cwd()
56
+ };
57
+ }
58
+
59
+ this.options = options;
60
+ this.config = [];
61
+ this.disposes = [];
62
+ this.fetchProject = (0, _vtils.memoize)( /*#__PURE__*/function () {
63
+ var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref) {
64
+ var serverUrl, token, projectInfo, basePath;
65
+ return _regenerator.default.wrap(function _callee$(_context) {
66
+ while (1) {
67
+ switch (_context.prev = _context.next) {
68
+ case 0:
69
+ serverUrl = _ref.serverUrl, token = _ref.token;
70
+ _context.next = 3;
71
+ return _this.fetchApi(serverUrl + "/api/project/get", {
72
+ token: token
73
+ });
74
+
75
+ case 3:
76
+ projectInfo = _context.sent;
77
+ basePath = ("/" + (projectInfo.basepath || '/')).replace(/\/+$/, '').replace(/^\/+/, '/');
78
+ projectInfo.basepath = basePath; // 实现项目在 YApi 上的地址
79
+
80
+ projectInfo._url = serverUrl + "/project/" + projectInfo._id + "/interface/api";
81
+ return _context.abrupt("return", projectInfo);
82
+
83
+ case 8:
84
+ case "end":
85
+ return _context.stop();
86
+ }
87
+ }
88
+ }, _callee);
89
+ }));
90
+
91
+ return function (_x) {
92
+ return _ref2.apply(this, arguments);
93
+ };
94
+ }(), function (_ref3) {
95
+ var serverUrl = _ref3.serverUrl,
96
+ token = _ref3.token;
97
+ return serverUrl + "|" + token;
98
+ });
99
+ this.fetchExport = (0, _vtils.memoize)( /*#__PURE__*/function () {
100
+ var _ref5 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(_ref4) {
101
+ var serverUrl, token, projectInfo, categoryList;
102
+ return _regenerator.default.wrap(function _callee2$(_context2) {
103
+ while (1) {
104
+ switch (_context2.prev = _context2.next) {
105
+ case 0:
106
+ serverUrl = _ref4.serverUrl, token = _ref4.token;
107
+ _context2.next = 3;
108
+ return _this.fetchProject({
109
+ serverUrl: serverUrl,
110
+ token: token
111
+ });
112
+
113
+ case 3:
114
+ projectInfo = _context2.sent;
115
+ _context2.next = 6;
116
+ return _this.fetchApi(serverUrl + "/api/plugin/export", {
117
+ type: 'json',
118
+ status: 'all',
119
+ isWiki: 'false',
120
+ token: token
121
+ });
122
+
123
+ case 6:
124
+ categoryList = _context2.sent;
125
+ return _context2.abrupt("return", categoryList.map(function (cat) {
126
+ var _cat$list, _cat$list$, _cat$list2, _cat$list2$;
127
+
128
+ var projectId = ((_cat$list = cat.list) == null ? void 0 : (_cat$list$ = _cat$list[0]) == null ? void 0 : _cat$list$.project_id) || 0;
129
+ var catId = ((_cat$list2 = cat.list) == null ? void 0 : (_cat$list2$ = _cat$list2[0]) == null ? void 0 : _cat$list2$.catid) || 0; // 实现分类在 YApi 上的地址
130
+
131
+ // 实现分类在 YApi 上的地址
132
+ cat._url = serverUrl + "/project/" + projectId + "/interface/api/cat_" + catId;
133
+ cat.list = (cat.list || []).map(function (item) {
134
+ var interfaceId = item._id; // 实现接口在 YApi 上的地址
135
+
136
+ // 实现接口在 YApi 上的地址
137
+ item._url = serverUrl + "/project/" + projectId + "/interface/api/" + interfaceId;
138
+ item.path = "" + projectInfo.basepath + item.path;
139
+ return item;
140
+ });
141
+ return cat;
142
+ }));
143
+
144
+ case 8:
145
+ case "end":
146
+ return _context2.stop();
147
+ }
148
+ }
149
+ }, _callee2);
150
+ }));
151
+
152
+ return function (_x2) {
153
+ return _ref5.apply(this, arguments);
154
+ };
155
+ }(), function (_ref6) {
156
+ var serverUrl = _ref6.serverUrl,
157
+ token = _ref6.token;
158
+ return serverUrl + "|" + token;
159
+ });
160
+ // config 可能是对象或数组,统一为数组
161
+ this.config = (0, _vtils.castArray)(config);
162
+ }
163
+
164
+ var _proto = Generator.prototype;
165
+
166
+ _proto.prepare = /*#__PURE__*/function () {
167
+ var _prepare = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4() {
168
+ var _this2 = this;
169
+
170
+ return _regenerator.default.wrap(function _callee4$(_context4) {
171
+ while (1) {
172
+ switch (_context4.prev = _context4.next) {
173
+ case 0:
174
+ _context4.next = 2;
175
+ return Promise.all( // config 可能是对象或数组,统一为数组
176
+ this.config.map( /*#__PURE__*/function () {
177
+ var _ref7 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(item) {
178
+ var swaggerToYApiServer;
179
+ return _regenerator.default.wrap(function _callee3$(_context3) {
180
+ while (1) {
181
+ switch (_context3.prev = _context3.next) {
182
+ case 0:
183
+ if (!(item.serverType === 'swagger')) {
184
+ _context3.next = 6;
185
+ break;
186
+ }
187
+
188
+ swaggerToYApiServer = new _SwaggerToYApiServer.SwaggerToYApiServer({
189
+ swaggerJsonUrl: item.serverUrl
190
+ });
191
+ _context3.next = 4;
192
+ return swaggerToYApiServer.start();
193
+
194
+ case 4:
195
+ item.serverUrl = _context3.sent;
196
+
197
+ _this2.disposes.push(function () {
198
+ return swaggerToYApiServer.stop();
199
+ });
200
+
201
+ case 6:
202
+ if (item.serverUrl) {
203
+ // 去除地址后面的 /
204
+ // fix: https://github.com/fjc0k/yapi-to-typescript/issues/22
205
+ item.serverUrl = item.serverUrl.replace(/\/+$/, '');
206
+ }
207
+
208
+ return _context3.abrupt("return", item);
209
+
210
+ case 8:
211
+ case "end":
212
+ return _context3.stop();
213
+ }
214
+ }
215
+ }, _callee3);
216
+ }));
217
+
218
+ return function (_x3) {
219
+ return _ref7.apply(this, arguments);
220
+ };
221
+ }()));
222
+
223
+ case 2:
224
+ this.config = _context4.sent;
225
+
226
+ case 3:
227
+ case "end":
228
+ return _context4.stop();
229
+ }
230
+ }
231
+ }, _callee4, this);
232
+ }));
233
+
234
+ function prepare() {
235
+ return _prepare.apply(this, arguments);
236
+ }
237
+
238
+ return prepare;
239
+ }();
240
+
241
+ _proto.generate = /*#__PURE__*/function () {
242
+ var _generate = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee10() {
243
+ var _this3 = this;
244
+
245
+ var outputFileList;
246
+ return _regenerator.default.wrap(function _callee10$(_context10) {
247
+ while (1) {
248
+ switch (_context10.prev = _context10.next) {
249
+ case 0:
250
+ outputFileList = Object.create(null);
251
+ _context10.next = 3;
252
+ return Promise.all(this.config.map( /*#__PURE__*/function () {
253
+ var _ref8 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee9(serverConfig, serverIndex) {
254
+ var projects;
255
+ return _regenerator.default.wrap(function _callee9$(_context9) {
256
+ while (1) {
257
+ switch (_context9.prev = _context9.next) {
258
+ case 0:
259
+ projects = serverConfig.projects.reduce(function (projects, project) {
260
+ projects.push.apply(projects, (0, _vtils.castArray)(project.token).map(function (token) {
261
+ return (0, _extends2.default)({}, project, {
262
+ token: token
263
+ });
264
+ }));
265
+ return projects;
266
+ }, []);
267
+ return _context9.abrupt("return", Promise.all(projects.map( /*#__PURE__*/function () {
268
+ var _ref9 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee8(projectConfig, projectIndex) {
269
+ var projectInfo, apifoxInstance;
270
+ return _regenerator.default.wrap(function _callee8$(_context8) {
271
+ while (1) {
272
+ switch (_context8.prev = _context8.next) {
273
+ case 0:
274
+ apifoxInstance = null;
275
+
276
+ if (!(serverConfig.serverType === 'apifox')) {
277
+ _context8.next = 8;
278
+ break;
279
+ }
280
+
281
+ apifoxInstance = new _ApifoxToYApiData.ApifoxToYApiData({
282
+ token: projectConfig.token,
283
+ categories: projectConfig.categories
284
+ });
285
+ _context8.next = 5;
286
+ return apifoxInstance.getProjectInfo();
287
+
288
+ case 5:
289
+ projectInfo = _context8.sent;
290
+ _context8.next = 11;
291
+ break;
292
+
293
+ case 8:
294
+ _context8.next = 10;
295
+ return _this3.fetchProjectInfo((0, _extends2.default)({}, serverConfig, projectConfig));
296
+
297
+ case 10:
298
+ projectInfo = _context8.sent;
299
+
300
+ case 11:
301
+ _context8.next = 13;
302
+ return Promise.all(projectConfig.categories.map( /*#__PURE__*/function () {
303
+ var _ref10 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7(categoryConfig, categoryIndex) {
304
+ var categoryIds, _categoryIds, excludedCategoryIds, codes, _iterator, _step, _outputFileList$group, groupedCodes;
305
+
306
+ return _regenerator.default.wrap(function _callee7$(_context7) {
307
+ while (1) {
308
+ switch (_context7.prev = _context7.next) {
309
+ case 0:
310
+ // 分类处理
311
+ // 数组化
312
+ categoryIds = (0, _vtils.castArray)(categoryConfig.id); // 全部分类
313
+
314
+ // 全部分类
315
+ if (categoryIds.includes(0)) {
316
+ (_categoryIds = categoryIds).push.apply(_categoryIds, projectInfo.cats.map(function (cat) {
317
+ return cat._id;
318
+ }));
319
+ } // 唯一化
320
+
321
+
322
+ // 唯一化
323
+ categoryIds = (0, _vtils.uniq)(categoryIds); // 去掉被排除的分类
324
+
325
+ // 去掉被排除的分类
326
+ excludedCategoryIds = categoryIds.filter(function (id) {
327
+ return id < 0;
328
+ }).map(Math.abs);
329
+ categoryIds = categoryIds.filter(function (id) {
330
+ return !excludedCategoryIds.includes(Math.abs(id));
331
+ }); // 删除不存在的分类
332
+
333
+ // 删除不存在的分类
334
+ categoryIds = categoryIds.filter(function (id) {
335
+ return !!projectInfo.cats.find(function (cat) {
336
+ return cat._id === id;
337
+ });
338
+ }); // 顺序化
339
+
340
+ // 顺序化
341
+ categoryIds = categoryIds.sort();
342
+ _context7.next = 9;
343
+ return Promise.all(categoryIds.map( /*#__PURE__*/function () {
344
+ var _ref11 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(id, categoryIndex2) {
345
+ var syntheticalConfig, interfaceList, _apifoxInstance, interfaceCodes, groupedInterfaceCodes;
346
+
347
+ return _regenerator.default.wrap(function _callee6$(_context6) {
348
+ while (1) {
349
+ switch (_context6.prev = _context6.next) {
350
+ case 0:
351
+ categoryConfig = (0, _extends2.default)({}, categoryConfig, {
352
+ id: id
353
+ });
354
+ syntheticalConfig = (0, _extends2.default)({}, serverConfig, projectConfig, categoryConfig, {
355
+ mockUrl: projectInfo.getMockUrl()
356
+ });
357
+ syntheticalConfig.target = syntheticalConfig.target || 'typescript';
358
+ syntheticalConfig.devUrl = projectInfo.getDevUrl(syntheticalConfig.devEnvName);
359
+ syntheticalConfig.prodUrl = projectInfo.getProdUrl(syntheticalConfig.prodEnvName); // 接口列表
360
+
361
+ // 接口列表
362
+ interfaceList = [];
363
+
364
+ if (!(syntheticalConfig.serverType === 'apifox')) {
365
+ _context6.next = 15;
366
+ break;
367
+ }
368
+
369
+ _context6.next = 9;
370
+ return (_apifoxInstance = apifoxInstance) == null ? void 0 : _apifoxInstance.getInterfaceList(syntheticalConfig);
371
+
372
+ case 9:
373
+ _context6.t0 = _context6.sent;
374
+
375
+ if (_context6.t0) {
376
+ _context6.next = 12;
377
+ break;
378
+ }
379
+
380
+ _context6.t0 = [];
381
+
382
+ case 12:
383
+ interfaceList = _context6.t0;
384
+ _context6.next = 18;
385
+ break;
386
+
387
+ case 15:
388
+ _context6.next = 17;
389
+ return _this3.fetchInterfaceList(syntheticalConfig);
390
+
391
+ case 17:
392
+ interfaceList = _context6.sent;
393
+
394
+ case 18:
395
+ interfaceList = interfaceList.map(function (interfaceInfo) {
396
+ // 实现 _project 字段
397
+ interfaceInfo._project = (0, _vtils.omit)(projectInfo, ['cats', 'getMockUrl', 'getDevUrl', 'getProdUrl']); // 预处理
398
+
399
+ // 预处理
400
+ var _interfaceInfo = (0, _vtils.isFunction)(syntheticalConfig.preproccessInterface) ? syntheticalConfig.preproccessInterface((0, _vtils.cloneDeepFast)(interfaceInfo), changeCase, syntheticalConfig) : interfaceInfo;
401
+
402
+ return _interfaceInfo;
403
+ }).filter(Boolean);
404
+ interfaceList.sort(function (a, b) {
405
+ return a._id - b._id;
406
+ });
407
+ _context6.next = 22;
408
+ return Promise.all(interfaceList.map( /*#__PURE__*/function () {
409
+ var _ref12 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(interfaceInfo) {
410
+ var outputFilePath, categoryUID, code, weights;
411
+ return _regenerator.default.wrap(function _callee5$(_context5) {
412
+ while (1) {
413
+ switch (_context5.prev = _context5.next) {
414
+ case 0:
415
+ outputFilePath = _path.default.resolve(_this3.options.cwd, typeof syntheticalConfig.outputFilePath === 'function' ? syntheticalConfig.outputFilePath(interfaceInfo, changeCase) : syntheticalConfig.outputFilePath);
416
+ categoryUID = "_" + serverIndex + "_" + projectIndex + "_" + categoryIndex + "_" + categoryIndex2;
417
+ _context5.next = 4;
418
+ return _this3.generateInterfaceCode(syntheticalConfig, interfaceInfo, categoryUID);
419
+
420
+ case 4:
421
+ code = _context5.sent;
422
+ weights = [serverIndex, projectIndex, categoryIndex, categoryIndex2];
423
+ return _context5.abrupt("return", {
424
+ categoryUID: categoryUID,
425
+ outputFilePath: outputFilePath,
426
+ weights: weights,
427
+ code: code
428
+ });
429
+
430
+ case 7:
431
+ case "end":
432
+ return _context5.stop();
433
+ }
434
+ }
435
+ }, _callee5);
436
+ }));
437
+
438
+ return function (_x12) {
439
+ return _ref12.apply(this, arguments);
440
+ };
441
+ }()));
442
+
443
+ case 22:
444
+ interfaceCodes = _context6.sent;
445
+ groupedInterfaceCodes = (0, _vtils.groupBy)(interfaceCodes, function (item) {
446
+ return item.outputFilePath;
447
+ });
448
+ return _context6.abrupt("return", Object.keys(groupedInterfaceCodes).map(function (outputFilePath) {
449
+ var categoryCode = [].concat((0, _vtils.uniq)((0, _utils.sortByWeights)(groupedInterfaceCodes[outputFilePath]).map(function (item) {
450
+ return item.categoryUID;
451
+ })).map(function (categoryUID) {
452
+ if (syntheticalConfig.typesOnly) return '';
453
+ var text = '';
454
+
455
+ if (syntheticalConfig.withoutUrl !== true) {
456
+ text += "const devUrl" + categoryUID + " = " + JSON.stringify(syntheticalConfig.devUrl) + " as any\n const mockUrl" + categoryUID + " = " + JSON.stringify(syntheticalConfig.mockUrl) + " as any\n const prodUrl" + categoryUID + " = " + JSON.stringify(syntheticalConfig.prodUrl) + " as any";
457
+ }
458
+
459
+ if (syntheticalConfig.ignoreUselessConfigVar !== true) {
460
+ text += "const dataKey" + categoryUID + " = " + JSON.stringify(syntheticalConfig.dataKey) + " as any";
461
+ }
462
+
463
+ return (0, _vtils.dedent)(text);
464
+ }), (0, _utils.sortByWeights)(groupedInterfaceCodes[outputFilePath]).map(function (item) {
465
+ return item.code;
466
+ })).filter(Boolean).join('\n\n');
467
+
468
+ if (!outputFileList[outputFilePath]) {
469
+ outputFileList[outputFilePath] = {
470
+ syntheticalConfig: syntheticalConfig,
471
+ content: [],
472
+ requestFunctionFilePath: syntheticalConfig.requestFunctionFilePath ? _path.default.resolve(_this3.options.cwd, syntheticalConfig.requestFunctionFilePath) : _path.default.join(_path.default.dirname(outputFilePath), 'request.ts'),
473
+ requestHookMakerFilePath: syntheticalConfig.reactHooks && syntheticalConfig.reactHooks.enabled ? syntheticalConfig.reactHooks.requestHookMakerFilePath ? _path.default.resolve(_this3.options.cwd, syntheticalConfig.reactHooks.requestHookMakerFilePath) : _path.default.join(_path.default.dirname(outputFilePath), 'makeRequestHook.ts') : ''
474
+ };
475
+ }
476
+
477
+ return {
478
+ outputFilePath: outputFilePath,
479
+ code: categoryCode,
480
+ weights: (0, _vtils.last)((0, _utils.sortByWeights)(groupedInterfaceCodes[outputFilePath])).weights
481
+ };
482
+ }));
483
+
484
+ case 25:
485
+ case "end":
486
+ return _context6.stop();
487
+ }
488
+ }
489
+ }, _callee6);
490
+ }));
491
+
492
+ return function (_x10, _x11) {
493
+ return _ref11.apply(this, arguments);
494
+ };
495
+ }()));
496
+
497
+ case 9:
498
+ codes = _context7.sent.flat();
499
+
500
+ for (_iterator = (0, _createForOfIteratorHelperLoose2.default)((0, _vtils.values)((0, _vtils.groupBy)(codes, function (item) {
501
+ return item.outputFilePath;
502
+ }))); !(_step = _iterator()).done;) {
503
+ groupedCodes = _step.value;
504
+ (0, _utils.sortByWeights)(groupedCodes);
505
+
506
+ (_outputFileList$group = outputFileList[groupedCodes[0].outputFilePath].content).push.apply(_outputFileList$group, groupedCodes.map(function (item) {
507
+ return item.code;
508
+ }));
509
+ }
510
+
511
+ case 11:
512
+ case "end":
513
+ return _context7.stop();
514
+ }
515
+ }
516
+ }, _callee7);
517
+ }));
518
+
519
+ return function (_x8, _x9) {
520
+ return _ref10.apply(this, arguments);
521
+ };
522
+ }()));
523
+
524
+ case 13:
525
+ case "end":
526
+ return _context8.stop();
527
+ }
528
+ }
529
+ }, _callee8);
530
+ }));
531
+
532
+ return function (_x6, _x7) {
533
+ return _ref9.apply(this, arguments);
534
+ };
535
+ }())));
536
+
537
+ case 2:
538
+ case "end":
539
+ return _context9.stop();
540
+ }
541
+ }
542
+ }, _callee9);
543
+ }));
544
+
545
+ return function (_x4, _x5) {
546
+ return _ref8.apply(this, arguments);
547
+ };
548
+ }()));
549
+
550
+ case 3:
551
+ return _context10.abrupt("return", outputFileList);
552
+
553
+ case 4:
554
+ case "end":
555
+ return _context10.stop();
556
+ }
557
+ }
558
+ }, _callee10, this);
559
+ }));
560
+
561
+ function generate() {
562
+ return _generate.apply(this, arguments);
563
+ }
564
+
565
+ return generate;
566
+ }();
567
+
568
+ _proto.write = /*#__PURE__*/function () {
569
+ var _write = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee12(outputFileList) {
570
+ var _this4 = this;
571
+
572
+ return _regenerator.default.wrap(function _callee12$(_context12) {
573
+ while (1) {
574
+ switch (_context12.prev = _context12.next) {
575
+ case 0:
576
+ return _context12.abrupt("return", Promise.all(Object.keys(outputFileList).map( /*#__PURE__*/function () {
577
+ var _ref13 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee11(outputFilePath) {
578
+ var _outputFileList$outpu, content, requestFunctionFilePath, requestHookMakerFilePath, syntheticalConfig, rawRequestFunctionFilePath, rawRequestHookMakerFilePath, rawOutputContent, prettier, prettyOutputContent, outputContent;
579
+
580
+ return _regenerator.default.wrap(function _callee11$(_context11) {
581
+ while (1) {
582
+ switch (_context11.prev = _context11.next) {
583
+ case 0:
584
+ _outputFileList$outpu = outputFileList[outputFilePath], content = _outputFileList$outpu.content, requestFunctionFilePath = _outputFileList$outpu.requestFunctionFilePath, requestHookMakerFilePath = _outputFileList$outpu.requestHookMakerFilePath, syntheticalConfig = _outputFileList$outpu.syntheticalConfig;
585
+ rawRequestFunctionFilePath = requestFunctionFilePath;
586
+ rawRequestHookMakerFilePath = requestHookMakerFilePath; // 支持 .jsx? 后缀
587
+
588
+ // 支持 .jsx? 后缀
589
+ outputFilePath = outputFilePath.replace(/\.js(x)?$/, '.ts$1');
590
+ requestFunctionFilePath = requestFunctionFilePath.replace(/\.js(x)?$/, '.ts$1');
591
+ requestHookMakerFilePath = requestHookMakerFilePath.replace(/\.js(x)?$/, '.ts$1');
592
+
593
+ if (syntheticalConfig.typesOnly) {
594
+ _context11.next = 20;
595
+ break;
596
+ }
597
+
598
+ _context11.next = 9;
599
+ return _fsExtra.default.pathExists(rawRequestFunctionFilePath);
600
+
601
+ case 9:
602
+ if (_context11.sent) {
603
+ _context11.next = 12;
604
+ break;
605
+ }
606
+
607
+ _context11.next = 12;
608
+ return _fsExtra.default.outputFile(requestFunctionFilePath, (0, _vtils.dedent)(_templateObject || (_templateObject = (0, _taggedTemplateLiteralLoose2.default)(["\n import type { RequestFunctionParams } from 'yapi-apifox-to-typescript'\n\n export interface RequestOptions {\n /**\n * \u4F7F\u7528\u7684\u670D\u52A1\u5668\u3002\n *\n * - `prod`: \u751F\u4EA7\u670D\u52A1\u5668\n * - `dev`: \u6D4B\u8BD5\u670D\u52A1\u5668\n * - `mock`: \u6A21\u62DF\u670D\u52A1\u5668\n *\n * @default prod\n */\n server?: 'prod' | 'dev' | 'mock',\n }\n\n export default function request<TResponseData>(\n payload: RequestFunctionParams,\n options: RequestOptions = {\n server: 'prod',\n },\n ): Promise<TResponseData> {\n return new Promise<TResponseData>((resolve, reject) => {\n // \u57FA\u672C\u5730\u5740\n ", "\n\n // \u8BF7\u6C42\u5730\u5740\n const url = `${baseUrl}${payload.path}`\n\n // \u5177\u4F53\u8BF7\u6C42\u903B\u8F91\n })\n }\n "], ["\n import type { RequestFunctionParams } from 'yapi-apifox-to-typescript'\n\n export interface RequestOptions {\n /**\n * \u4F7F\u7528\u7684\u670D\u52A1\u5668\u3002\n *\n * - \\`prod\\`: \u751F\u4EA7\u670D\u52A1\u5668\n * - \\`dev\\`: \u6D4B\u8BD5\u670D\u52A1\u5668\n * - \\`mock\\`: \u6A21\u62DF\u670D\u52A1\u5668\n *\n * @default prod\n */\n server?: 'prod' | 'dev' | 'mock',\n }\n\n export default function request<TResponseData>(\n payload: RequestFunctionParams,\n options: RequestOptions = {\n server: 'prod',\n },\n ): Promise<TResponseData> {\n return new Promise<TResponseData>((resolve, reject) => {\n // \u57FA\u672C\u5730\u5740\n ", "\n\n // \u8BF7\u6C42\u5730\u5740\n const url = \\`\\${baseUrl}\\${payload.path}\\`\n\n // \u5177\u4F53\u8BF7\u6C42\u903B\u8F91\n })\n }\n "])), syntheticalConfig.withoutUrl ? "const baseUrl = ''" : " const baseUrl = options.server === 'mock'\n ? payload.mockUrl\n : options.server === 'dev'\n ? payload.devUrl\n : payload.prodUrl"));
609
+
610
+ case 12:
611
+ _context11.t0 = syntheticalConfig.reactHooks && syntheticalConfig.reactHooks.enabled;
612
+
613
+ if (!_context11.t0) {
614
+ _context11.next = 17;
615
+ break;
616
+ }
617
+
618
+ _context11.next = 16;
619
+ return _fsExtra.default.pathExists(rawRequestHookMakerFilePath);
620
+
621
+ case 16:
622
+ _context11.t0 = !_context11.sent;
623
+
624
+ case 17:
625
+ if (!_context11.t0) {
626
+ _context11.next = 20;
627
+ break;
628
+ }
629
+
630
+ _context11.next = 20;
631
+ return _fsExtra.default.outputFile(requestHookMakerFilePath, (0, _vtils.dedent)(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteralLoose2.default)(["\n import { useState, useEffect } from 'react'\n import type { RequestConfig } from 'yapi-apifox-to-typescript'\n import type { Request } from ", "\n import baseRequest from ", "\n\n export default function makeRequestHook<TRequestData, TRequestConfig extends RequestConfig, TRequestResult extends ReturnType<typeof baseRequest>>(request: Request<TRequestData, TRequestConfig, TRequestResult>) {\n type Data = TRequestResult extends Promise<infer R> ? R : TRequestResult\n return function useRequest(requestData: TRequestData) {\n // \u4E00\u4E2A\u7B80\u5355\u7684 Hook \u5B9E\u73B0\uFF0C\u5B9E\u9645\u9879\u76EE\u53EF\u7ED3\u5408\u5176\u4ED6\u5E93\u4F7F\u7528\uFF0C\u6BD4\u5982\uFF1A\n // @umijs/hooks \u7684 useRequest (https://github.com/umijs/hooks)\n // swr (https://github.com/zeit/swr)\n\n const [loading, setLoading] = useState(true)\n const [data, setData] = useState<Data>()\n\n useEffect(() => {\n request(requestData).then(data => {\n setLoading(false)\n setData(data as any)\n })\n }, [JSON.stringify(requestData)])\n\n return {\n loading,\n data,\n }\n }\n }\n "])), JSON.stringify((0, _utils.getNormalizedRelativePath)(requestHookMakerFilePath, outputFilePath)), JSON.stringify((0, _utils.getNormalizedRelativePath)(requestHookMakerFilePath, requestFunctionFilePath))));
632
+
633
+ case 20:
634
+ // 始终写入主文件
635
+ rawOutputContent = (0, _vtils.dedent)(_templateObject3 || (_templateObject3 = (0, _taggedTemplateLiteralLoose2.default)(["\n /* tslint:disable */\n /* eslint-disable */\n\n /* \u8BE5\u6587\u4EF6\u7531 yapi-apifox-to-typescript \u81EA\u52A8\u751F\u6210\uFF0C\u8BF7\u52FF\u76F4\u63A5\u4FEE\u6539\uFF01\uFF01\uFF01 */\n\n ", "\n "])), syntheticalConfig.typesOnly ? (0, _vtils.dedent)(_templateObject4 || (_templateObject4 = (0, _taggedTemplateLiteralLoose2.default)(["\n // @ts-ignore\n type FileData = File\n\n ", "\n "])), content.join('\n\n').trim()) : (0, _vtils.dedent)(_templateObject5 || (_templateObject5 = (0, _taggedTemplateLiteralLoose2.default)(["\n // @ts-ignore\n // prettier-ignore\n import { QueryStringArrayFormat, Method, ", "FileData, prepare } from 'yapi-apifox-to-typescript'\n // @ts-ignore\n // prettier-ignore\n import type { RequestConfig, RequestFunctionRestArgs } from 'yapi-apifox-to-typescript'\n // @ts-ignore\n import request from ", "\n ", "\n\n type UserRequestRestArgs = RequestFunctionRestArgs<typeof request>\n\n // Request: \u76EE\u524D React Hooks \u529F\u80FD\u6709\u7528\u5230\n export type Request<TRequestData, TRequestConfig extends RequestConfig, TRequestResult> = (\n TRequestConfig['requestDataOptional'] extends true\n ? (requestData?: TRequestData, ...args: RequestFunctionRestArgs<typeof request>) => TRequestResult\n : (requestData: TRequestData, ...args: RequestFunctionRestArgs<typeof request>) => TRequestResult\n ) & {\n requestConfig: TRequestConfig\n }\n\n ", "\n "])), syntheticalConfig.ignoreUselessConfigVar ? "" : "RequestBodyType, ResponseBodyType, ", JSON.stringify((0, _utils.getNormalizedRelativePath)(outputFilePath, requestFunctionFilePath)), !syntheticalConfig.reactHooks || !syntheticalConfig.reactHooks.enabled ? '' : (0, _vtils.dedent)(_templateObject6 || (_templateObject6 = (0, _taggedTemplateLiteralLoose2.default)(["\n // @ts-ignore\n import makeRequestHook from ", "\n "])), JSON.stringify((0, _utils.getNormalizedRelativePath)(outputFilePath, requestHookMakerFilePath))), content.join('\n\n').trim())); // ref: https://prettier.io/docs/en/options.html
636
+
637
+ _context11.next = 23;
638
+ return (0, _utils.getPrettier)(_this4.options.cwd);
639
+
640
+ case 23:
641
+ prettier = _context11.sent;
642
+ _context11.t1 = prettier;
643
+ _context11.t2 = rawOutputContent;
644
+ _context11.t3 = _extends2.default;
645
+ _context11.t4 = {};
646
+ _context11.next = 30;
647
+ return (0, _utils.getCachedPrettierOptions)();
648
+
649
+ case 30:
650
+ _context11.t5 = _context11.sent;
651
+ _context11.t6 = {
652
+ filepath: outputFilePath
653
+ };
654
+ _context11.t7 = (0, _context11.t3)(_context11.t4, _context11.t5, _context11.t6);
655
+ _context11.next = 35;
656
+ return _context11.t1.format.call(_context11.t1, _context11.t2, _context11.t7);
657
+
658
+ case 35:
659
+ prettyOutputContent = _context11.sent;
660
+ outputContent = (0, _vtils.dedent)(_templateObject7 || (_templateObject7 = (0, _taggedTemplateLiteralLoose2.default)(["\n /* prettier-ignore-start */\n ", "\n /* prettier-ignore-end */\n "])), prettyOutputContent) + "\n";
661
+ _context11.next = 39;
662
+ return _fsExtra.default.outputFile(outputFilePath, outputContent);
663
+
664
+ case 39:
665
+ if (!(syntheticalConfig.target === 'javascript')) {
666
+ _context11.next = 44;
667
+ break;
668
+ }
669
+
670
+ _context11.next = 42;
671
+ return _this4.tsc(outputFilePath);
672
+
673
+ case 42:
674
+ _context11.next = 44;
675
+ return Promise.all([_fsExtra.default.remove(requestFunctionFilePath).catch(_vtils.noop), _fsExtra.default.remove(requestHookMakerFilePath).catch(_vtils.noop), _fsExtra.default.remove(outputFilePath).catch(_vtils.noop)]);
676
+
677
+ case 44:
678
+ case "end":
679
+ return _context11.stop();
680
+ }
681
+ }
682
+ }, _callee11);
683
+ }));
684
+
685
+ return function (_x14) {
686
+ return _ref13.apply(this, arguments);
687
+ };
688
+ }())));
689
+
690
+ case 1:
691
+ case "end":
692
+ return _context12.stop();
693
+ }
694
+ }
695
+ }, _callee12);
696
+ }));
697
+
698
+ function write(_x13) {
699
+ return _write.apply(this, arguments);
700
+ }
701
+
702
+ return write;
703
+ }();
704
+
705
+ _proto.tsc = /*#__PURE__*/function () {
706
+ var _tsc = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee13(file) {
707
+ var _this5 = this;
708
+
709
+ return _regenerator.default.wrap(function _callee13$(_context13) {
710
+ while (1) {
711
+ switch (_context13.prev = _context13.next) {
712
+ case 0:
713
+ return _context13.abrupt("return", new Promise(function (resolve) {
714
+ // add this to fix bug that not-generator-file-on-window
715
+ var command = "" + (require('os').platform() === 'win32' ? 'node ' : '') + JSON.stringify(require.resolve("typescript/bin/tsc"));
716
+ (0, _child_process.exec)(command + " --target ES2019 --module ESNext --jsx preserve --declaration --esModuleInterop " + JSON.stringify(file), {
717
+ cwd: _this5.options.cwd,
718
+ env: process.env
719
+ }, function () {
720
+ return resolve();
721
+ });
722
+ }));
723
+
724
+ case 1:
725
+ case "end":
726
+ return _context13.stop();
727
+ }
728
+ }
729
+ }, _callee13);
730
+ }));
731
+
732
+ function tsc(_x15) {
733
+ return _tsc.apply(this, arguments);
734
+ }
735
+
736
+ return tsc;
737
+ }();
738
+
739
+ _proto.fetchApi = /*#__PURE__*/function () {
740
+ var _fetchApi = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee14(url, query) {
741
+ var res;
742
+ return _regenerator.default.wrap(function _callee14$(_context14) {
743
+ while (1) {
744
+ switch (_context14.prev = _context14.next) {
745
+ case 0:
746
+ _context14.next = 2;
747
+ return (0, _utils.httpGet)(url, query);
748
+
749
+ case 2:
750
+ res = _context14.sent;
751
+
752
+ /* istanbul ignore next */
753
+ if (res && res.errcode) {
754
+ (0, _utils.throwError)(res.errmsg + " [\u8BF7\u6C42\u5730\u5740: " + url + "] [\u8BF7\u6C42\u53C2\u6570: " + new URLSearchParams(query).toString() + "]");
755
+ }
756
+
757
+ return _context14.abrupt("return", res.data || res);
758
+
759
+ case 5:
760
+ case "end":
761
+ return _context14.stop();
762
+ }
763
+ }
764
+ }, _callee14);
765
+ }));
766
+
767
+ function fetchApi(_x16, _x17) {
768
+ return _fetchApi.apply(this, arguments);
769
+ }
770
+
771
+ return fetchApi;
772
+ }();
773
+
774
+ /** 获取分类的接口列表 */
775
+ _proto.fetchInterfaceList =
776
+ /*#__PURE__*/
777
+ function () {
778
+ var _fetchInterfaceList = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee15(_ref14) {
779
+ var serverUrl, token, id, category;
780
+ return _regenerator.default.wrap(function _callee15$(_context15) {
781
+ while (1) {
782
+ switch (_context15.prev = _context15.next) {
783
+ case 0:
784
+ serverUrl = _ref14.serverUrl, token = _ref14.token, id = _ref14.id;
785
+ _context15.next = 3;
786
+ return this.fetchExport({
787
+ serverUrl: serverUrl,
788
+ token: token
789
+ });
790
+
791
+ case 3:
792
+ _context15.t0 = _context15.sent;
793
+
794
+ if (_context15.t0) {
795
+ _context15.next = 6;
796
+ break;
797
+ }
798
+
799
+ _context15.t0 = [];
800
+
801
+ case 6:
802
+ category = _context15.t0.find(function (cat) {
803
+ return !(0, _vtils.isEmpty)(cat) && !(0, _vtils.isEmpty)(cat.list) && cat.list[0].catid === id;
804
+ });
805
+
806
+ if (category) {
807
+ category.list.forEach(function (interfaceInfo) {
808
+ // 实现 _category 字段
809
+ interfaceInfo._category = (0, _vtils.omit)(category, ['list']);
810
+ });
811
+ }
812
+
813
+ return _context15.abrupt("return", category ? category.list : []);
814
+
815
+ case 9:
816
+ case "end":
817
+ return _context15.stop();
818
+ }
819
+ }
820
+ }, _callee15, this);
821
+ }));
822
+
823
+ function fetchInterfaceList(_x18) {
824
+ return _fetchInterfaceList.apply(this, arguments);
825
+ }
826
+
827
+ return fetchInterfaceList;
828
+ }()
829
+ /** 获取项目信息 */
830
+ ;
831
+
832
+ _proto.fetchProjectInfo =
833
+ /*#__PURE__*/
834
+ function () {
835
+ var _fetchProjectInfo = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee16(syntheticalConfig) {
836
+ var projectInfo, projectCats;
837
+ return _regenerator.default.wrap(function _callee16$(_context16) {
838
+ while (1) {
839
+ switch (_context16.prev = _context16.next) {
840
+ case 0:
841
+ _context16.next = 2;
842
+ return this.fetchProject(syntheticalConfig);
843
+
844
+ case 2:
845
+ projectInfo = _context16.sent;
846
+ _context16.next = 5;
847
+ return this.fetchApi(syntheticalConfig.serverUrl + "/api/interface/getCatMenu", {
848
+ token: syntheticalConfig.token,
849
+ project_id: projectInfo._id
850
+ });
851
+
852
+ case 5:
853
+ projectCats = _context16.sent;
854
+ return _context16.abrupt("return", (0, _extends2.default)({}, projectInfo, {
855
+ cats: projectCats,
856
+ getMockUrl: function getMockUrl() {
857
+ return syntheticalConfig.serverUrl + "/mock/" + projectInfo._id;
858
+ },
859
+ getDevUrl: function getDevUrl(devEnvName) {
860
+ var env = projectInfo.env.find(function (e) {
861
+ return e.name === devEnvName;
862
+ });
863
+ return env && env.domain
864
+ /* istanbul ignore next */
865
+ || '';
866
+ },
867
+ getProdUrl: function getProdUrl(prodEnvName) {
868
+ var env = projectInfo.env.find(function (e) {
869
+ return e.name === prodEnvName;
870
+ });
871
+ return env && env.domain
872
+ /* istanbul ignore next */
873
+ || '';
874
+ }
875
+ }));
876
+
877
+ case 7:
878
+ case "end":
879
+ return _context16.stop();
880
+ }
881
+ }
882
+ }, _callee16, this);
883
+ }));
884
+
885
+ function fetchProjectInfo(_x19) {
886
+ return _fetchProjectInfo.apply(this, arguments);
887
+ }
888
+
889
+ return fetchProjectInfo;
890
+ }()
891
+ /** 生成接口代码 */
892
+ ;
893
+
894
+ _proto.generateInterfaceCode =
895
+ /*#__PURE__*/
896
+ function () {
897
+ var _generateInterfaceCode = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee17(syntheticalConfig, interfaceInfo, categoryUID) {
898
+ var _syntheticalConfig$js, _syntheticalConfig$js2, _syntheticalConfig$js3, _syntheticalConfig$js4;
899
+
900
+ var extendedInterfaceInfo, requestFunctionName, requestConfigName, requestConfigTypeName, requestDataTypeName, responseDataTypeName, requestDataJsonSchema, requestDataType, responseDataJsonSchema, responseDataType, isRequestDataOptional, requestHookName, paramNames, paramNamesLiteral, queryNames, queryNamesLiteral, genComment, requestFunctionExtraInfo, requestHeaders;
901
+ return _regenerator.default.wrap(function _callee17$(_context17) {
902
+ while (1) {
903
+ switch (_context17.prev = _context17.next) {
904
+ case 0:
905
+ extendedInterfaceInfo = (0, _extends2.default)({}, interfaceInfo, {
906
+ parsedPath: _path.default.parse(interfaceInfo.path)
907
+ });
908
+
909
+ if (!(0, _vtils.isFunction)(syntheticalConfig.getRequestFunctionName)) {
910
+ _context17.next = 7;
911
+ break;
912
+ }
913
+
914
+ _context17.next = 4;
915
+ return syntheticalConfig.getRequestFunctionName(extendedInterfaceInfo, changeCase);
916
+
917
+ case 4:
918
+ _context17.t0 = _context17.sent;
919
+ _context17.next = 8;
920
+ break;
921
+
922
+ case 7:
923
+ _context17.t0 = changeCase.camelCase(extendedInterfaceInfo.parsedPath.name);
924
+
925
+ case 8:
926
+ requestFunctionName = _context17.t0;
927
+ requestConfigName = changeCase.camelCase(requestFunctionName + "RequestConfig");
928
+ requestConfigTypeName = 'RequestConfig';
929
+
930
+ if (!(0, _vtils.isFunction)(syntheticalConfig.getRequestDataTypeName)) {
931
+ _context17.next = 17;
932
+ break;
933
+ }
934
+
935
+ _context17.next = 14;
936
+ return syntheticalConfig.getRequestDataTypeName(extendedInterfaceInfo, changeCase);
937
+
938
+ case 14:
939
+ _context17.t1 = _context17.sent;
940
+ _context17.next = 18;
941
+ break;
942
+
943
+ case 17:
944
+ _context17.t1 = changeCase.pascalCase(requestFunctionName + "Request");
945
+
946
+ case 18:
947
+ requestDataTypeName = _context17.t1;
948
+
949
+ if (!(0, _vtils.isFunction)(syntheticalConfig.getResponseDataTypeName)) {
950
+ _context17.next = 25;
951
+ break;
952
+ }
953
+
954
+ _context17.next = 22;
955
+ return syntheticalConfig.getResponseDataTypeName(extendedInterfaceInfo, changeCase);
956
+
957
+ case 22:
958
+ _context17.t2 = _context17.sent;
959
+ _context17.next = 26;
960
+ break;
961
+
962
+ case 25:
963
+ _context17.t2 = changeCase.pascalCase(requestFunctionName + "Response");
964
+
965
+ case 26:
966
+ responseDataTypeName = _context17.t2;
967
+ requestDataJsonSchema = (0, _utils.getRequestDataJsonSchema)(extendedInterfaceInfo, syntheticalConfig.customTypeMapping || {});
968
+ _context17.next = 30;
969
+ return (0, _utils.jsonSchemaToType)(requestDataJsonSchema, requestDataTypeName);
970
+
971
+ case 30:
972
+ requestDataType = _context17.sent;
973
+ responseDataJsonSchema = (0, _utils.getResponseDataJsonSchema)(extendedInterfaceInfo, syntheticalConfig.customTypeMapping || {}, syntheticalConfig.dataKey);
974
+ _context17.next = 34;
975
+ return (0, _utils.jsonSchemaToType)(responseDataJsonSchema, responseDataTypeName);
976
+
977
+ case 34:
978
+ responseDataType = _context17.sent;
979
+ // @ts-ignore
980
+ isRequestDataOptional = /(\{\}|any)$/.test(requestDataType);
981
+
982
+ if (!(syntheticalConfig.reactHooks && syntheticalConfig.reactHooks.enabled)) {
983
+ _context17.next = 47;
984
+ break;
985
+ }
986
+
987
+ if (!(0, _vtils.isFunction)(syntheticalConfig.reactHooks.getRequestHookName)) {
988
+ _context17.next = 43;
989
+ break;
990
+ }
991
+
992
+ _context17.next = 40;
993
+ return syntheticalConfig.reactHooks.getRequestHookName(extendedInterfaceInfo, changeCase);
994
+
995
+ case 40:
996
+ _context17.t4 = _context17.sent;
997
+ _context17.next = 44;
998
+ break;
999
+
1000
+ case 43:
1001
+ _context17.t4 = "use" + changeCase.pascalCase(requestFunctionName);
1002
+
1003
+ case 44:
1004
+ _context17.t3 = _context17.t4;
1005
+ _context17.next = 48;
1006
+ break;
1007
+
1008
+ case 47:
1009
+ _context17.t3 = '';
1010
+
1011
+ case 48:
1012
+ requestHookName = _context17.t3;
1013
+ // 支持路径参数
1014
+ paramNames = (extendedInterfaceInfo.req_params
1015
+ /* istanbul ignore next */
1016
+ || []).map(function (item) {
1017
+ return item.name;
1018
+ });
1019
+ paramNamesLiteral = JSON.stringify(paramNames); // const paramNameType =
1020
+ // paramNames.length === 0 ? 'string' : `'${paramNames.join("' | '")}'`
1021
+ // 支持查询参数
1022
+
1023
+ queryNames = (extendedInterfaceInfo.req_query
1024
+ /* istanbul ignore next */
1025
+ || []).map(function (item) {
1026
+ return item.name;
1027
+ });
1028
+ queryNamesLiteral = JSON.stringify(queryNames); // const queryNameType =
1029
+ // queryNames.length === 0 ? 'string' : `'${queryNames.join("' | '")}'`
1030
+ // 接口注释
1031
+
1032
+ genComment = function genComment(genTitle) {
1033
+ var _ref15 = (0, _extends2.default)({}, syntheticalConfig.comment, syntheticalConfig.serverType === 'swagger' ? {
1034
+ tag: false,
1035
+ updateTime: false,
1036
+ link: false
1037
+ } : {}),
1038
+ _ref15$enabled = _ref15.enabled,
1039
+ isEnabled = _ref15$enabled === void 0 ? true : _ref15$enabled,
1040
+ _ref15$title = _ref15.title,
1041
+ hasTitle = _ref15$title === void 0 ? true : _ref15$title,
1042
+ _ref15$category = _ref15.category,
1043
+ hasCategory = _ref15$category === void 0 ? true : _ref15$category,
1044
+ _ref15$tag = _ref15.tag,
1045
+ hasTag = _ref15$tag === void 0 ? true : _ref15$tag,
1046
+ _ref15$requestHeader = _ref15.requestHeader,
1047
+ hasRequestHeader = _ref15$requestHeader === void 0 ? true : _ref15$requestHeader,
1048
+ _ref15$updateTime = _ref15.updateTime,
1049
+ hasUpdateTime = _ref15$updateTime === void 0 ? true : _ref15$updateTime,
1050
+ _ref15$link = _ref15.link,
1051
+ hasLink = _ref15$link === void 0 ? true : _ref15$link,
1052
+ extraTags = _ref15.extraTags;
1053
+
1054
+ if (!isEnabled) {
1055
+ return '';
1056
+ } // 转义标题中的 /
1057
+
1058
+
1059
+ var escapedTitle = String(extendedInterfaceInfo.title).replace(/\//g, '\\/');
1060
+ var description = hasLink ? "[" + escapedTitle + "\u2197](" + extendedInterfaceInfo._url + ")" : escapedTitle;
1061
+ var summary = [hasCategory && {
1062
+ label: '分类',
1063
+ value: hasLink ? "[" + extendedInterfaceInfo._category.name + "\u2197](" + extendedInterfaceInfo._category._url + ")" : extendedInterfaceInfo._category.name
1064
+ }, hasTag && {
1065
+ label: '标签',
1066
+ value: extendedInterfaceInfo.tag.map(function (tag) {
1067
+ return "`" + tag + "`";
1068
+ })
1069
+ }, hasRequestHeader && {
1070
+ label: '请求头',
1071
+ value: "`" + extendedInterfaceInfo.method.toUpperCase() + " " + extendedInterfaceInfo.path + "`"
1072
+ }, hasUpdateTime && {
1073
+ label: '更新时间',
1074
+ value: process.env.JEST_WORKER_ID // 测试时使用 unix 时间戳
1075
+ ? String(extendedInterfaceInfo.up_time) :
1076
+ /* istanbul ignore next */
1077
+ "`" + (0, _dayjs.default)(extendedInterfaceInfo.up_time * 1000).format('YYYY-MM-DD HH:mm:ss') + "`"
1078
+ }];
1079
+
1080
+ if (typeof extraTags === 'function') {
1081
+ var tags = extraTags(extendedInterfaceInfo);
1082
+
1083
+ for (var _iterator2 = (0, _createForOfIteratorHelperLoose2.default)(tags), _step2; !(_step2 = _iterator2()).done;) {
1084
+ var tag = _step2.value;
1085
+ ;
1086
+ (tag.position === 'start' ? summary.unshift : summary.push).call(summary, {
1087
+ label: tag.name,
1088
+ value: tag.value
1089
+ });
1090
+ }
1091
+ }
1092
+
1093
+ var titleComment = hasTitle ? (0, _vtils.dedent)(_templateObject8 || (_templateObject8 = (0, _taggedTemplateLiteralLoose2.default)(["\n * ", "\n *\n "])), genTitle(description)) : '';
1094
+ var extraComment = summary.filter(function (item) {
1095
+ return typeof item !== 'boolean' && !(0, _vtils.isEmpty)(item.value);
1096
+ }).map(function (item) {
1097
+ var _item = item;
1098
+ return "* @" + _item.label + " " + (0, _vtils.castArray)(_item.value).join(', ');
1099
+ }).join('\n');
1100
+ return (0, _vtils.dedent)(_templateObject9 || (_templateObject9 = (0, _taggedTemplateLiteralLoose2.default)(["\n /**\n ", "\n */\n "])), [titleComment, extraComment].filter(Boolean).join('\n'));
1101
+ }; // 请求参数额外信息
1102
+
1103
+
1104
+ if (!(typeof syntheticalConfig.setRequestFunctionExtraInfo === 'function')) {
1105
+ _context17.next = 60;
1106
+ break;
1107
+ }
1108
+
1109
+ _context17.next = 57;
1110
+ return syntheticalConfig.setRequestFunctionExtraInfo(extendedInterfaceInfo, changeCase);
1111
+
1112
+ case 57:
1113
+ _context17.t5 = _context17.sent;
1114
+ _context17.next = 61;
1115
+ break;
1116
+
1117
+ case 60:
1118
+ _context17.t5 = {};
1119
+
1120
+ case 61:
1121
+ requestFunctionExtraInfo = _context17.t5;
1122
+ requestHeaders = JSON.stringify((extendedInterfaceInfo.req_headers || []).filter(function (item) {
1123
+ return item.name.toLowerCase() !== 'content-type';
1124
+ }).reduce(function (res, item) {
1125
+ res[item.name] = item.value;
1126
+ return res;
1127
+ }, {}));
1128
+ return _context17.abrupt("return", (0, _vtils.dedent)(_templateObject10 || (_templateObject10 = (0, _taggedTemplateLiteralLoose2.default)(["\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n "])), genComment(function (title) {
1129
+ return "\u63A5\u53E3 " + title + " \u7684 **\u8BF7\u6C42\u7C7B\u578B**";
1130
+ }), requestDataType.trim(), genComment(function (title) {
1131
+ return "\u63A5\u53E3 " + title + " \u7684 **\u8FD4\u56DE\u7C7B\u578B**";
1132
+ }), responseDataType.trim(), syntheticalConfig.typesOnly ? '' : (0, _vtils.dedent)(_templateObject11 || (_templateObject11 = (0, _taggedTemplateLiteralLoose2.default)(["\n ", "\n ", "\n path: ", ",\n method: Method.", ",\n ", "", "", "", "", "\n }\n\n ", "\n export const ", " = ", " (\n requestData", ": ", ",\n ...args: UserRequestRestArgs\n ) => {\n return request<", ">(\n prepare(", ", requestData),\n ...args,\n )\n }\n\n ", ".requestConfig = ", "\n\n ", "\n "])), genComment(function (title) {
1133
+ return "\u63A5\u53E3 " + title + " \u7684 **\u8BF7\u6C42\u914D\u7F6E**";
1134
+ }), syntheticalConfig.withoutUrl !== true ? "const " + requestConfigName + ": " + requestConfigTypeName + " = " + COMPRESSOR_TREE_SHAKING_ANNOTATION + " {\n mockUrl: mockUrl" + categoryUID + ",\n devUrl: devUrl" + categoryUID + ",\n prodUrl: prodUrl" + categoryUID + "," : "const " + requestConfigName + ": " + requestConfigTypeName + " = " + COMPRESSOR_TREE_SHAKING_ANNOTATION + " {\n ", JSON.stringify(extendedInterfaceInfo.path), extendedInterfaceInfo.method, paramNames.length === 0 ? '' : "paramNames: " + paramNamesLiteral + ",", queryNames.length === 0 ? '' : "queryNames: " + queryNamesLiteral + ",", syntheticalConfig.queryStringArrayFormat ? "queryStringArrayFormat: QueryStringArrayFormat." + syntheticalConfig.queryStringArrayFormat + "," : '', requestHeaders === '{}' ? '' : "requestHeaders: " + requestHeaders + ",", syntheticalConfig.ignoreUselessConfigVar ? "" : "requestBodyType: RequestBodyType." + (extendedInterfaceInfo.method === _types.Method.GET ? _types.RequestBodyType.query : extendedInterfaceInfo.req_body_type
1135
+ /* istanbul ignore next */
1136
+ || _types.RequestBodyType.none) + ",\n responseBodyType: ResponseBodyType." + extendedInterfaceInfo.res_body_type + ",\n dataKey: dataKey" + categoryUID + ",\n requestDataOptional: " + JSON.stringify(isRequestDataOptional) + ",\n requestDataJsonSchema: " + JSON.stringify((_syntheticalConfig$js = syntheticalConfig.jsonSchema) != null && _syntheticalConfig$js.enabled && ((_syntheticalConfig$js2 = syntheticalConfig.jsonSchema) == null ? void 0 : _syntheticalConfig$js2.requestData) !== false ? requestDataJsonSchema : {}) + ",\n responseDataJsonSchema: " + JSON.stringify((_syntheticalConfig$js3 = syntheticalConfig.jsonSchema) != null && _syntheticalConfig$js3.enabled && ((_syntheticalConfig$js4 = syntheticalConfig.jsonSchema) == null ? void 0 : _syntheticalConfig$js4.responseData) !== false ? responseDataJsonSchema : {}) + ",\n requestFunctionName: " + JSON.stringify(requestFunctionName) + ",\n extraInfo: " + JSON.stringify(requestFunctionExtraInfo), genComment(function (title) {
1137
+ return "\u63A5\u53E3 " + title + " \u7684 **\u8BF7\u6C42\u51FD\u6570**";
1138
+ }), requestFunctionName, COMPRESSOR_TREE_SHAKING_ANNOTATION, isRequestDataOptional ? '?' : '', requestDataTypeName, responseDataTypeName, requestConfigName, requestFunctionName, requestConfigName, !syntheticalConfig.reactHooks || !syntheticalConfig.reactHooks.enabled ? '' : (0, _vtils.dedent)(_templateObject12 || (_templateObject12 = (0, _taggedTemplateLiteralLoose2.default)(["\n ", "\n export const ", " = ", " makeRequestHook<", ", ", ", ReturnType<typeof ", ">>(", ")\n "])), genComment(function (title) {
1139
+ return "\u63A5\u53E3 " + title + " \u7684 **React Hook**";
1140
+ }), requestHookName, COMPRESSOR_TREE_SHAKING_ANNOTATION, requestDataTypeName, requestConfigTypeName, requestFunctionName, requestFunctionName))));
1141
+
1142
+ case 64:
1143
+ case "end":
1144
+ return _context17.stop();
1145
+ }
1146
+ }
1147
+ }, _callee17);
1148
+ }));
1149
+
1150
+ function generateInterfaceCode(_x20, _x21, _x22) {
1151
+ return _generateInterfaceCode.apply(this, arguments);
1152
+ }
1153
+
1154
+ return generateInterfaceCode;
1155
+ }();
1156
+
1157
+ _proto.destroy = /*#__PURE__*/function () {
1158
+ var _destroy = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee19() {
1159
+ return _regenerator.default.wrap(function _callee19$(_context19) {
1160
+ while (1) {
1161
+ switch (_context19.prev = _context19.next) {
1162
+ case 0:
1163
+ return _context19.abrupt("return", Promise.all(this.disposes.map( /*#__PURE__*/function () {
1164
+ var _ref16 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee18(dispose) {
1165
+ return _regenerator.default.wrap(function _callee18$(_context18) {
1166
+ while (1) {
1167
+ switch (_context18.prev = _context18.next) {
1168
+ case 0:
1169
+ return _context18.abrupt("return", dispose());
1170
+
1171
+ case 1:
1172
+ case "end":
1173
+ return _context18.stop();
1174
+ }
1175
+ }
1176
+ }, _callee18);
1177
+ }));
1178
+
1179
+ return function (_x23) {
1180
+ return _ref16.apply(this, arguments);
1181
+ };
1182
+ }())));
1183
+
1184
+ case 1:
1185
+ case "end":
1186
+ return _context19.stop();
1187
+ }
1188
+ }
1189
+ }, _callee19, this);
1190
+ }));
1191
+
1192
+ function destroy() {
1193
+ return _destroy.apply(this, arguments);
1194
+ }
1195
+
1196
+ return destroy;
1197
+ }();
1198
+
1199
+ return Generator;
1200
+ }();
1201
+
1202
+ exports.Generator = Generator;