yapi-apifox-to-typescript 4.2.1 → 4.2.3

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.
@@ -68,6 +68,20 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
68
68
  return resolved.type || 'string';
69
69
  };
70
70
 
71
+ _proto.getRequestParamType = function getRequestParamType(schema) {
72
+ var schemaType = this.getSchemaType(schema);
73
+ if (schemaType === 'boolean') return _types.RequestParamType.boolean;
74
+ if (schemaType === 'number') return _types.RequestParamType.number;
75
+ return _types.RequestParamType.string;
76
+ };
77
+
78
+ _proto.getRequestQueryType = function getRequestQueryType(schema) {
79
+ var schemaType = this.getSchemaType(schema);
80
+ if (schemaType === 'boolean') return _types.RequestQueryType.boolean;
81
+ if (schemaType === 'number') return _types.RequestQueryType.number;
82
+ return _types.RequestQueryType.string;
83
+ };
84
+
71
85
  _proto.convertParametersToReqParams = function convertParametersToReqParams(parameters, type) {
72
86
  var _this = this;
73
87
 
@@ -84,7 +98,7 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
84
98
  desc: p.description || '',
85
99
  example: p.example || '',
86
100
  required: p.required ? _types.Required.true : _types.Required.false,
87
- type: _this.getSchemaType(p.schema) === 'string' ? _types.RequestParamType.string : _types.RequestParamType.number
101
+ type: _this.getRequestParamType(p.schema)
88
102
  };
89
103
  });
90
104
  }
@@ -97,7 +111,7 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
97
111
  desc: p.description || '',
98
112
  example: p.example || '',
99
113
  required: p.required ? _types.Required.true : _types.Required.false,
100
- type: _this.getSchemaType(p.schema) === 'string' ? _types.RequestQueryType.string : _types.RequestQueryType.number
114
+ type: _this.getRequestQueryType(p.schema)
101
115
  };
102
116
  });
103
117
  };
@@ -173,14 +187,26 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
173
187
  };
174
188
  };
175
189
 
176
- _proto.normalizeSchema = function normalizeSchema(schema) {
190
+ _proto.normalizeSchema = function normalizeSchema(schema, visited) {
191
+ if (visited === void 0) {
192
+ visited = new Set();
193
+ }
194
+
177
195
  var result = (0, _extends2.default)({}, schema);
178
196
 
179
197
  if (schema.$ref) {
198
+ if (visited.has(schema.$ref)) {
199
+ // 循环引用,直接返回空对象避免无限递归
200
+ return {
201
+ type: 'object'
202
+ };
203
+ }
204
+
205
+ visited.add(schema.$ref);
180
206
  var resolved = this.resolveRef(schema.$ref);
181
207
 
182
208
  if (resolved) {
183
- return this.normalizeSchema(resolved);
209
+ return this.normalizeSchema(resolved, new Set(visited));
184
210
  }
185
211
  }
186
212
 
@@ -193,13 +219,13 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
193
219
  _value = _Object$entries2$_i[1];
194
220
 
195
221
  if (_value) {
196
- result.properties[key] = this.normalizeSchema(_value);
222
+ result.properties[key] = this.normalizeSchema(_value, new Set(visited));
197
223
  }
198
224
  }
199
225
  }
200
226
 
201
227
  if (schema.items) {
202
- result.items = this.normalizeSchema(schema.items);
228
+ result.items = this.normalizeSchema(schema.items, new Set(visited));
203
229
  }
204
230
 
205
231
  delete result.$ref;
@@ -432,31 +458,23 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
432
458
  var _this$openApiDoc5;
433
459
 
434
460
  if (!((_this$openApiDoc5 = this.openApiDoc) != null && _this$openApiDoc5.tags)) {
435
- console.log("\uD83D\uDCC1 getCats: \u8FD4\u56DE\u7A7A\u6570\u7EC4 (openApiDoc.tags \u4E0D\u5B58\u5728)");
436
461
  return [];
437
462
  }
438
463
 
439
464
  var tagIds = this.openApiDoc.tags.map(function (tag) {
440
465
  return _this2.getTagId(tag.name);
441
466
  });
442
- console.log("\uD83D\uDCC1 getCats (id=0 \u83B7\u53D6\u5168\u90E8): \u8FD4\u56DE " + tagIds.length + " \u4E2A\u5206\u7C7B");
443
- console.log(" Tags: " + this.openApiDoc.tags.map(function (t) {
444
- return t.name;
445
- }).join(', '));
446
467
  return tagIds;
447
468
  }
448
469
 
449
- console.log("\uD83D\uDCC1 getCats: \u8FD4\u56DE " + categoryIds.length + " \u4E2A\u5206\u7C7B: " + categoryIds.join(', '));
450
470
  return categoryIds;
451
471
  };
452
472
 
453
473
  _proto.getInterfaceList = /*#__PURE__*/function () {
454
474
  var _getInterfaceList = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(syntheticalConfig) {
455
- var _this$openApiDoc6,
456
- _this$openApiDoc7,
457
- _this3 = this;
475
+ var _this$openApiDoc6;
458
476
 
459
- var catId, interfaces, seenKeys, tagName, tag, methods, _i3, _Object$entries3, _Object$entries3$_i, path, pathItem, _iterator, _step, method, operation, xApifoxFolder, tagId, key, categoryTag, interfaceInfo;
477
+ var catId, interfaces, seenKeys, methods, _i3, _Object$entries3, _Object$entries3$_i, path, pathItem, _iterator, _step, method, operation, xApifoxFolder, tagId, key, categoryTag, interfaceInfo;
460
478
 
461
479
  return _regenerator.default.wrap(function _callee2$(_context2) {
462
480
  while (1) {
@@ -474,32 +492,29 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
474
492
  interfaces = []; // 用于去重:基于 path + method
475
493
 
476
494
  seenKeys = new Set(); // 调试:获取 tag 名称
495
+ // let tagName = 'unknown'
496
+ // if (this.openApiDoc?.tags) {
497
+ // const tag = this.openApiDoc.tags.find(
498
+ // t => this.getTagId(t.name) === catId,
499
+ // )
500
+ // tagName = tag?.name || `unknown(${catId})`
501
+ // }
477
502
 
478
- tagName = 'unknown';
479
-
480
- if ((_this$openApiDoc7 = this.openApiDoc) != null && _this$openApiDoc7.tags) {
481
- tag = this.openApiDoc.tags.find(function (t) {
482
- return _this3.getTagId(t.name) === catId;
483
- });
484
- tagName = (tag == null ? void 0 : tag.name) || "unknown(" + catId + ")";
485
- }
486
-
487
- console.log("\n\uD83D\uDCC2 \u5206\u7C7B [" + tagName + "] (id: " + catId + ") \u5F00\u59CB\u83B7\u53D6\u63A5\u53E3...");
488
503
  methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'];
489
504
  _i3 = 0, _Object$entries3 = Object.entries(this.openApiDoc.paths);
490
505
 
491
- case 10:
506
+ case 7:
492
507
  if (!(_i3 < _Object$entries3.length)) {
493
- _context2.next = 34;
508
+ _context2.next = 30;
494
509
  break;
495
510
  }
496
511
 
497
512
  _Object$entries3$_i = _Object$entries3[_i3], path = _Object$entries3$_i[0], pathItem = _Object$entries3$_i[1];
498
513
  _iterator = (0, _createForOfIteratorHelperLoose2.default)(methods);
499
514
 
500
- case 13:
515
+ case 10:
501
516
  if ((_step = _iterator()).done) {
502
- _context2.next = 31;
517
+ _context2.next = 27;
503
518
  break;
504
519
  }
505
520
 
@@ -507,19 +522,19 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
507
522
  operation = pathItem[method];
508
523
 
509
524
  if (operation) {
510
- _context2.next = 18;
525
+ _context2.next = 15;
511
526
  break;
512
527
  }
513
528
 
514
- return _context2.abrupt("continue", 29);
529
+ return _context2.abrupt("continue", 25);
515
530
 
516
- case 18:
531
+ case 15:
517
532
  // 获取接口的目录信息:优先使用 x-apifox-folder
518
533
  xApifoxFolder = this.getOperationFolder(operation);
519
534
  tagId = this.getTagId(xApifoxFolder);
520
535
 
521
536
  if (!(tagId === catId)) {
522
- _context2.next = 29;
537
+ _context2.next = 25;
523
538
  break;
524
539
  }
525
540
 
@@ -527,33 +542,32 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
527
542
  key = method.toUpperCase() + ":" + path;
528
543
 
529
544
  if (!seenKeys.has(key)) {
530
- _context2.next = 25;
545
+ _context2.next = 21;
531
546
  break;
532
547
  }
533
548
 
534
- console.log(" \u26A0\uFE0F \u8DF3\u8FC7\u91CD\u590D\u63A5\u53E3: " + key);
535
- return _context2.abrupt("continue", 29);
549
+ return _context2.abrupt("continue", 25);
536
550
 
537
- case 25:
551
+ case 21:
538
552
  seenKeys.add(key); // 根据实际的目录名称来确定分类
539
553
 
540
554
  categoryTag = xApifoxFolder;
541
555
  interfaceInfo = this.convertOperationToInterface(path, method, operation, categoryTag, catId);
542
556
  interfaces.push(interfaceInfo);
543
557
 
544
- case 29:
545
- _context2.next = 13;
558
+ case 25:
559
+ _context2.next = 10;
546
560
  break;
547
561
 
548
- case 31:
562
+ case 27:
549
563
  _i3++;
550
- _context2.next = 10;
564
+ _context2.next = 7;
551
565
  break;
552
566
 
553
- case 34:
567
+ case 30:
554
568
  return _context2.abrupt("return", interfaces);
555
569
 
556
- case 35:
570
+ case 31:
557
571
  case "end":
558
572
  return _context2.stop();
559
573
  }
package/lib/cjs/types.js CHANGED
@@ -52,6 +52,7 @@ exports.RequestParamType = RequestParamType;
52
52
  (function (RequestParamType) {
53
53
  RequestParamType["string"] = "string";
54
54
  RequestParamType["number"] = "number";
55
+ RequestParamType["boolean"] = "boolean";
55
56
  })(RequestParamType || (exports.RequestParamType = RequestParamType = {}));
56
57
 
57
58
  var RequestQueryType;
@@ -62,6 +63,7 @@ exports.RequestQueryType = RequestQueryType;
62
63
  (function (RequestQueryType) {
63
64
  RequestQueryType["string"] = "string";
64
65
  RequestQueryType["number"] = "number";
66
+ RequestQueryType["boolean"] = "boolean";
65
67
  })(RequestQueryType || (exports.RequestQueryType = RequestQueryType = {}));
66
68
 
67
69
  var RequestFormItemType;
@@ -54,6 +54,20 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
54
54
  return resolved.type || 'string';
55
55
  };
56
56
 
57
+ _proto.getRequestParamType = function getRequestParamType(schema) {
58
+ var schemaType = this.getSchemaType(schema);
59
+ if (schemaType === 'boolean') return RequestParamType.boolean;
60
+ if (schemaType === 'number') return RequestParamType.number;
61
+ return RequestParamType.string;
62
+ };
63
+
64
+ _proto.getRequestQueryType = function getRequestQueryType(schema) {
65
+ var schemaType = this.getSchemaType(schema);
66
+ if (schemaType === 'boolean') return RequestQueryType.boolean;
67
+ if (schemaType === 'number') return RequestQueryType.number;
68
+ return RequestQueryType.string;
69
+ };
70
+
57
71
  _proto.convertParametersToReqParams = function convertParametersToReqParams(parameters, type) {
58
72
  var _this = this;
59
73
 
@@ -70,7 +84,7 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
70
84
  desc: p.description || '',
71
85
  example: p.example || '',
72
86
  required: p.required ? Required.true : Required.false,
73
- type: _this.getSchemaType(p.schema) === 'string' ? RequestParamType.string : RequestParamType.number
87
+ type: _this.getRequestParamType(p.schema)
74
88
  };
75
89
  });
76
90
  }
@@ -83,7 +97,7 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
83
97
  desc: p.description || '',
84
98
  example: p.example || '',
85
99
  required: p.required ? Required.true : Required.false,
86
- type: _this.getSchemaType(p.schema) === 'string' ? RequestQueryType.string : RequestQueryType.number
100
+ type: _this.getRequestQueryType(p.schema)
87
101
  };
88
102
  });
89
103
  };
@@ -159,14 +173,26 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
159
173
  };
160
174
  };
161
175
 
162
- _proto.normalizeSchema = function normalizeSchema(schema) {
176
+ _proto.normalizeSchema = function normalizeSchema(schema, visited) {
177
+ if (visited === void 0) {
178
+ visited = new Set();
179
+ }
180
+
163
181
  var result = _extends({}, schema);
164
182
 
165
183
  if (schema.$ref) {
184
+ if (visited.has(schema.$ref)) {
185
+ // 循环引用,直接返回空对象避免无限递归
186
+ return {
187
+ type: 'object'
188
+ };
189
+ }
190
+
191
+ visited.add(schema.$ref);
166
192
  var resolved = this.resolveRef(schema.$ref);
167
193
 
168
194
  if (resolved) {
169
- return this.normalizeSchema(resolved);
195
+ return this.normalizeSchema(resolved, new Set(visited));
170
196
  }
171
197
  }
172
198
 
@@ -179,13 +205,13 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
179
205
  _value = _Object$entries2$_i[1];
180
206
 
181
207
  if (_value) {
182
- result.properties[key] = this.normalizeSchema(_value);
208
+ result.properties[key] = this.normalizeSchema(_value, new Set(visited));
183
209
  }
184
210
  }
185
211
  }
186
212
 
187
213
  if (schema.items) {
188
- result.items = this.normalizeSchema(schema.items);
214
+ result.items = this.normalizeSchema(schema.items, new Set(visited));
189
215
  }
190
216
 
191
217
  delete result.$ref;
@@ -418,31 +444,23 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
418
444
  var _this$openApiDoc5;
419
445
 
420
446
  if (!((_this$openApiDoc5 = this.openApiDoc) != null && _this$openApiDoc5.tags)) {
421
- console.log("\uD83D\uDCC1 getCats: \u8FD4\u56DE\u7A7A\u6570\u7EC4 (openApiDoc.tags \u4E0D\u5B58\u5728)");
422
447
  return [];
423
448
  }
424
449
 
425
450
  var tagIds = this.openApiDoc.tags.map(function (tag) {
426
451
  return _this2.getTagId(tag.name);
427
452
  });
428
- console.log("\uD83D\uDCC1 getCats (id=0 \u83B7\u53D6\u5168\u90E8): \u8FD4\u56DE " + tagIds.length + " \u4E2A\u5206\u7C7B");
429
- console.log(" Tags: " + this.openApiDoc.tags.map(function (t) {
430
- return t.name;
431
- }).join(', '));
432
453
  return tagIds;
433
454
  }
434
455
 
435
- console.log("\uD83D\uDCC1 getCats: \u8FD4\u56DE " + categoryIds.length + " \u4E2A\u5206\u7C7B: " + categoryIds.join(', '));
436
456
  return categoryIds;
437
457
  };
438
458
 
439
459
  _proto.getInterfaceList = /*#__PURE__*/function () {
440
460
  var _getInterfaceList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(syntheticalConfig) {
441
- var _this$openApiDoc6,
442
- _this$openApiDoc7,
443
- _this3 = this;
461
+ var _this$openApiDoc6;
444
462
 
445
- var catId, interfaces, seenKeys, tagName, tag, methods, _i3, _Object$entries3, _Object$entries3$_i, path, pathItem, _iterator, _step, method, operation, xApifoxFolder, tagId, key, categoryTag, interfaceInfo;
463
+ var catId, interfaces, seenKeys, methods, _i3, _Object$entries3, _Object$entries3$_i, path, pathItem, _iterator, _step, method, operation, xApifoxFolder, tagId, key, categoryTag, interfaceInfo;
446
464
 
447
465
  return _regeneratorRuntime.wrap(function _callee2$(_context2) {
448
466
  while (1) {
@@ -460,32 +478,29 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
460
478
  interfaces = []; // 用于去重:基于 path + method
461
479
 
462
480
  seenKeys = new Set(); // 调试:获取 tag 名称
481
+ // let tagName = 'unknown'
482
+ // if (this.openApiDoc?.tags) {
483
+ // const tag = this.openApiDoc.tags.find(
484
+ // t => this.getTagId(t.name) === catId,
485
+ // )
486
+ // tagName = tag?.name || `unknown(${catId})`
487
+ // }
463
488
 
464
- tagName = 'unknown';
465
-
466
- if ((_this$openApiDoc7 = this.openApiDoc) != null && _this$openApiDoc7.tags) {
467
- tag = this.openApiDoc.tags.find(function (t) {
468
- return _this3.getTagId(t.name) === catId;
469
- });
470
- tagName = (tag == null ? void 0 : tag.name) || "unknown(" + catId + ")";
471
- }
472
-
473
- console.log("\n\uD83D\uDCC2 \u5206\u7C7B [" + tagName + "] (id: " + catId + ") \u5F00\u59CB\u83B7\u53D6\u63A5\u53E3...");
474
489
  methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'];
475
490
  _i3 = 0, _Object$entries3 = Object.entries(this.openApiDoc.paths);
476
491
 
477
- case 10:
492
+ case 7:
478
493
  if (!(_i3 < _Object$entries3.length)) {
479
- _context2.next = 34;
494
+ _context2.next = 30;
480
495
  break;
481
496
  }
482
497
 
483
498
  _Object$entries3$_i = _Object$entries3[_i3], path = _Object$entries3$_i[0], pathItem = _Object$entries3$_i[1];
484
499
  _iterator = _createForOfIteratorHelperLoose(methods);
485
500
 
486
- case 13:
501
+ case 10:
487
502
  if ((_step = _iterator()).done) {
488
- _context2.next = 31;
503
+ _context2.next = 27;
489
504
  break;
490
505
  }
491
506
 
@@ -493,19 +508,19 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
493
508
  operation = pathItem[method];
494
509
 
495
510
  if (operation) {
496
- _context2.next = 18;
511
+ _context2.next = 15;
497
512
  break;
498
513
  }
499
514
 
500
- return _context2.abrupt("continue", 29);
515
+ return _context2.abrupt("continue", 25);
501
516
 
502
- case 18:
517
+ case 15:
503
518
  // 获取接口的目录信息:优先使用 x-apifox-folder
504
519
  xApifoxFolder = this.getOperationFolder(operation);
505
520
  tagId = this.getTagId(xApifoxFolder);
506
521
 
507
522
  if (!(tagId === catId)) {
508
- _context2.next = 29;
523
+ _context2.next = 25;
509
524
  break;
510
525
  }
511
526
 
@@ -513,33 +528,32 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
513
528
  key = method.toUpperCase() + ":" + path;
514
529
 
515
530
  if (!seenKeys.has(key)) {
516
- _context2.next = 25;
531
+ _context2.next = 21;
517
532
  break;
518
533
  }
519
534
 
520
- console.log(" \u26A0\uFE0F \u8DF3\u8FC7\u91CD\u590D\u63A5\u53E3: " + key);
521
- return _context2.abrupt("continue", 29);
535
+ return _context2.abrupt("continue", 25);
522
536
 
523
- case 25:
537
+ case 21:
524
538
  seenKeys.add(key); // 根据实际的目录名称来确定分类
525
539
 
526
540
  categoryTag = xApifoxFolder;
527
541
  interfaceInfo = this.convertOperationToInterface(path, method, operation, categoryTag, catId);
528
542
  interfaces.push(interfaceInfo);
529
543
 
530
- case 29:
531
- _context2.next = 13;
544
+ case 25:
545
+ _context2.next = 10;
532
546
  break;
533
547
 
534
- case 31:
548
+ case 27:
535
549
  _i3++;
536
- _context2.next = 10;
550
+ _context2.next = 7;
537
551
  break;
538
552
 
539
- case 34:
553
+ case 30:
540
554
  return _context2.abrupt("return", interfaces);
541
555
 
542
- case 35:
556
+ case 31:
543
557
  case "end":
544
558
  return _context2.stop();
545
559
  }
@@ -666,7 +666,9 @@ export declare enum RequestParamType {
666
666
  /** 字符串 */
667
667
  string = "string",
668
668
  /** 数字 */
669
- number = "number"
669
+ number = "number",
670
+ /** 布尔值 */
671
+ boolean = "boolean"
670
672
  }
671
673
 
672
674
  /** 请求查询参数类型 */
@@ -674,7 +676,9 @@ export declare enum RequestQueryType {
674
676
  /** 字符串 */
675
677
  string = "string",
676
678
  /** 数字 */
677
- number = "number"
679
+ number = "number",
680
+ /** 布尔值 */
681
+ boolean = "boolean"
678
682
  }
679
683
 
680
684
  /** 是否必需 */
package/lib/esm/types.js CHANGED
@@ -39,6 +39,7 @@ export var RequestParamType;
39
39
  (function (RequestParamType) {
40
40
  RequestParamType["string"] = "string";
41
41
  RequestParamType["number"] = "number";
42
+ RequestParamType["boolean"] = "boolean";
42
43
  })(RequestParamType || (RequestParamType = {}));
43
44
 
44
45
  export var RequestQueryType;
@@ -47,6 +48,7 @@ export var RequestQueryType;
47
48
  (function (RequestQueryType) {
48
49
  RequestQueryType["string"] = "string";
49
50
  RequestQueryType["number"] = "number";
51
+ RequestQueryType["boolean"] = "boolean";
50
52
  })(RequestQueryType || (RequestQueryType = {}));
51
53
 
52
54
  export var RequestFormItemType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yapi-apifox-to-typescript",
3
- "version": "4.2.1",
3
+ "version": "4.2.3",
4
4
  "description": "根据 YApi 或 Apifox 的接口定义生成 TypeScript/JavaScript 的接口类型及其请求函数代码。",
5
5
  "keywords": [
6
6
  "yapi",