yapi-apifox-to-typescript 4.0.0 → 4.1.1

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.
@@ -15,11 +15,13 @@ var _axios = _interopRequireDefault(require("axios"));
15
15
 
16
16
  var _types = require("./types");
17
17
 
18
+ var _utils = require("./utils");
19
+
18
20
  var ApifoxToYApiData = /*#__PURE__*/function () {
19
21
  function ApifoxToYApiData(options) {
20
- this.options = options;
21
22
  this.schemas = void 0;
22
23
  this.folders = void 0;
24
+ this.options = void 0;
23
25
  this.project = {
24
26
  _id: 0,
25
27
  _url: '',
@@ -34,6 +36,7 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
34
36
  };
35
37
  this.schemas = {};
36
38
  this.folders = [];
39
+ this.options = options;
37
40
  }
38
41
  /**
39
42
  * http-api-tree 这个接口拿到所有的folder(category)
@@ -49,7 +52,7 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
49
52
  var _getProjectInfo = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
50
53
  var _this = this;
51
54
 
52
- var folders, schemas, cats;
55
+ var folders, schemas;
53
56
  return _regenerator.default.wrap(function _callee$(_context) {
54
57
  while (1) {
55
58
  switch (_context.prev = _context.next) {
@@ -65,25 +68,24 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
65
68
  case 5:
66
69
  schemas = _context.sent;
67
70
  // 初始化
68
- // this.schemas = schemas.data.data
69
71
  schemas.data.data.forEach(function (item) {
70
72
  _this.schemas[item.id] = item.jsonSchema;
71
73
  });
72
- this.folders = folders.data.data;
73
- cats = folders.data.data.map(function (item) {
74
- return {
75
- _id: item.folder.id,
76
- _url: '',
77
- name: item.name,
78
- desc: item.key,
79
- list: [],
80
- add_time: 0,
81
- up_time: 0
82
- };
83
- });
74
+ this.folders = folders.data.data; // const categoryIds = getFilteredCat(this.options, [])
75
+ // 由于这里面所有的cats是递归的,所以涉及到多层级的cats如果判定权限。这里先做一遍过滤,后续就不再做了
76
+ // const cats: Category[] = folders.data.data.map(item => ({
77
+ // _id: item.folder.id,
78
+ // _url: '',
79
+ // name: item.name,
80
+ // desc: item.key,
81
+ // list: [],
82
+ // add_time: 0,
83
+ // up_time: 0,
84
+ // }))
85
+
84
86
  this.project._id = schemas.data.data[0].projectId;
85
87
  return _context.abrupt("return", (0, _extends2.default)({}, this.project, {
86
- cats: cats,
88
+ cats: [],
87
89
  getMockUrl: function getMockUrl() {
88
90
  return '';
89
91
  },
@@ -95,7 +97,7 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
95
97
  }
96
98
  }));
97
99
 
98
- case 11:
100
+ case 10:
99
101
  case "end":
100
102
  return _context.stop();
101
103
  }
@@ -109,10 +111,73 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
109
111
 
110
112
  return getProjectInfo;
111
113
  }()
114
+ /**
115
+ * 根据筛选后的权限目录,对当前folder进行筛选
116
+ * 如果传入的cats为空,表明获取所有
117
+ */
118
+ ;
119
+
120
+ _proto.getValidCat = function getValidCat(cats, allFolders) {
121
+ var _this2 = this;
122
+
123
+ if (allFolders === void 0) {
124
+ allFolders = this.folders;
125
+ }
126
+
127
+ var folders = [];
128
+ allFolders.forEach(function (item) {
129
+ if (item.type !== 'apiDetailFolder') return;
130
+ if (cats.length && !cats.includes(item.folder.id)) return;
131
+ folders.push(item.folder.id);
132
+
133
+ if (item.type === 'apiDetailFolder' && item.children.length) {
134
+ folders.push.apply(folders, _this2.getValidCat(cats, item.children));
135
+ }
136
+ });
137
+ return folders;
138
+ }
139
+ /**
140
+ * 根据传入的目录配置,返回有效的目录列表
141
+ */
142
+ ;
143
+
144
+ _proto.getCats = function getCats(cats) {
145
+ var categoryIds = (0, _utils.getFilteredCat)(cats, []); // 对cat进行有效性过滤
146
+
147
+ return this.getValidCat(categoryIds);
148
+ }
149
+ /**
150
+ * 通过目录的ID查找目录
151
+ */
152
+ ;
153
+
154
+ _proto.findCats = function findCats(id, folders) {
155
+ var _this3 = this;
156
+
157
+ if (folders === void 0) {
158
+ folders = this.folders;
159
+ }
160
+
161
+ var folder = null;
162
+ folders.forEach(function (item) {
163
+ if (folder) return;
164
+
165
+ if (item.type === 'apiDetailFolder' && item.folder.id === id) {
166
+ folder = item;
167
+ return;
168
+ }
169
+
170
+ if (item.type === 'apiDetailFolder' && item.children) {
171
+ folder = _this3.findCats(id, item.children);
172
+ }
173
+ });
174
+ return folder;
175
+ }
112
176
  /**
113
177
  * 由于yapi中基本没有处理ref的方案
114
178
  * 都是直接由具体的参数值管理,所以这里需要对所有的ref进行转换
115
- * TODO:目前这里只考虑了一个层级,如果是多个层级,需要再递归处理
179
+ *
180
+ * 在apifox中,对于cat来说,cat的children有可能是folder,对于folder,我们要过滤掉
116
181
  */
117
182
  ;
118
183
 
@@ -120,7 +185,7 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
120
185
  /*#__PURE__*/
121
186
  function () {
122
187
  var _getInterfaceList = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(syntheticalConfig) {
123
- var _this2 = this;
188
+ var _this4 = this;
124
189
 
125
190
  var cat, transformRefToSchema;
126
191
  return _regenerator.default.wrap(function _callee3$(_context3) {
@@ -128,9 +193,7 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
128
193
  switch (_context3.prev = _context3.next) {
129
194
  case 0:
130
195
  // syntheticalConfig.id 就是catId
131
- cat = this.folders.find(function (item) {
132
- return item.folder.id === syntheticalConfig.id;
133
- });
196
+ cat = this.findCats(syntheticalConfig.id);
134
197
 
135
198
  if (cat) {
136
199
  _context3.next = 3;
@@ -142,21 +205,23 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
142
205
  case 3:
143
206
  transformRefToSchema = function transformRefToSchema(refString) {
144
207
  var refId = refString.split('/').pop();
145
- return _this2.schemas[refId];
208
+ return _this4.schemas[refId];
146
209
  };
147
210
 
148
- return _context3.abrupt("return", Promise.all(cat.children.map( /*#__PURE__*/function () {
149
- var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(item) {
150
- var _resultData$responses, _resultData$responses2, _resultData$responses3, _resultData$responses4, _resultData$responses5, _resultData$responses6, _resultData$responses7, _resultData$requestBo, _resultData$requestBo2, _resultData$parameter, _resultData$parameter2;
211
+ return _context3.abrupt("return", Promise.all(cat.children.filter(function (child) {
212
+ return child.type === 'apiDetail';
213
+ }).map( /*#__PURE__*/function () {
214
+ var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(child) {
215
+ var _resultData$responses, _resultData$responses2, _resultData$responses3, _resultData$responses4, _resultData$responses5, _resultData$responses6, _resultData$responses7, _resultData$requestBo, _resultData$requestBo2, _resultData$parameter, _resultData$parameter2, _resultData$requestBo3;
151
216
 
152
- var result, resultData, resbodyIndex, respref, dataProperties, _item, _dataProperties$_item, dataPropRef, reqref;
217
+ var result, resultData, resbodyIndex, respref, dataProperties, item, _dataProperties$item$, dataPropRef, reqref;
153
218
 
154
219
  return _regenerator.default.wrap(function _callee2$(_context2) {
155
220
  while (1) {
156
221
  switch (_context2.prev = _context2.next) {
157
222
  case 0:
158
223
  _context2.next = 2;
159
- return _axios.default.get("https://apifox.com/api/v1/shared-docs/" + _this2.options.token + "/http-apis/" + item.api.id);
224
+ return _axios.default.get("https://apifox.com/api/v1/shared-docs/" + _this4.options.token + "/http-apis/" + child.api.id);
160
225
 
161
226
  case 2:
162
227
  result = _context2.sent;
@@ -182,14 +247,14 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
182
247
  dataProperties = (_resultData$responses5 = resultData.responses[resbodyIndex].jsonSchema) == null ? void 0 : (_resultData$responses6 = _resultData$responses5.properties) == null ? void 0 : (_resultData$responses7 = _resultData$responses6.data) == null ? void 0 : _resultData$responses7.properties;
183
248
 
184
249
  if (dataProperties) {
185
- for (_item in dataProperties) {
250
+ for (item in dataProperties) {
186
251
  // @ts-ignore
187
- dataPropRef = (_dataProperties$_item = dataProperties[_item].items) == null ? void 0 : _dataProperties$_item['$ref']; // @ts-ignore
252
+ dataPropRef = (_dataProperties$item$ = dataProperties[item].items) == null ? void 0 : _dataProperties$item$['$ref']; // @ts-ignore
188
253
 
189
254
  // @ts-ignore
190
- if (dataProperties[_item].type === 'array' && dataPropRef) {
255
+ if (dataProperties[item].type === 'array' && dataPropRef) {
191
256
  // @ts-ignore
192
- resultData.responses[resbodyIndex].jsonSchema.properties.data.properties[_item].items = transformRefToSchema(dataPropRef);
257
+ resultData.responses[resbodyIndex].jsonSchema.properties.data.properties[item].items = transformRefToSchema(dataPropRef);
193
258
  }
194
259
  }
195
260
  } // 处理request中可能存在的ref
@@ -218,7 +283,7 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
218
283
  up_time: 0,
219
284
  list: []
220
285
  },
221
- _project: (0, _extends2.default)({}, _this2.project),
286
+ _project: (0, _extends2.default)({}, _this4.project),
222
287
  _url: '',
223
288
  title: resultData.name,
224
289
  status: 'undone',
@@ -253,9 +318,17 @@ var ApifoxToYApiData = /*#__PURE__*/function () {
253
318
  type: item.type === 'string' ? _types.RequestQueryType.string : _types.RequestQueryType.number
254
319
  };
255
320
  })) || [],
256
- req_body_type: resultData.requestBody.jsonSchema ? _types.RequestBodyType.json : _types.RequestBodyType.raw,
321
+ req_body_type: resultData.requestBody.type === 'multipart/form-data' ? _types.RequestBodyType.form : resultData.requestBody.jsonSchema ? _types.RequestBodyType.json : _types.RequestBodyType.raw,
257
322
  req_body_is_json_schema: !!resultData.requestBody.jsonSchema,
258
- req_body_form: [],
323
+ req_body_form: ((_resultData$requestBo3 = resultData.requestBody.parameters) == null ? void 0 : _resultData$requestBo3.map(function (item) {
324
+ return {
325
+ name: item.name,
326
+ desc: item.description,
327
+ example: item.example,
328
+ required: item.required ? _types.Required.true : _types.Required.false,
329
+ type: item.type === 'string' ? _types.RequestFormItemType.text : _types.RequestFormItemType.file
330
+ };
331
+ })) || [],
259
332
  req_body_other: resultData.requestBody.jsonSchema ? JSON.stringify(resultData.requestBody.jsonSchema) : '',
260
333
  res_body_type: _types.ResponseBodyType.json,
261
334
  res_body_is_json_schema: true,
@@ -301,49 +301,24 @@ var Generator = /*#__PURE__*/function () {
301
301
  _context8.next = 13;
302
302
  return Promise.all(projectConfig.categories.map( /*#__PURE__*/function () {
303
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;
304
+ var categoryIds, codes, _iterator, _step, _outputFileList$group, groupedCodes;
305
305
 
306
306
  return _regenerator.default.wrap(function _callee7$(_context7) {
307
307
  while (1) {
308
308
  switch (_context7.prev = _context7.next) {
309
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;
310
+ categoryIds = [];
311
+
312
+ if (serverConfig.serverType === 'apifox' && apifoxInstance) {
313
+ categoryIds = apifoxInstance.getCats(categoryConfig);
314
+ } else {
315
+ categoryIds = (0, _utils.getFilteredCat)(categoryConfig, projectInfo.cats);
316
+ }
317
+
318
+ _context7.next = 4;
343
319
  return Promise.all(categoryIds.map( /*#__PURE__*/function () {
344
320
  var _ref11 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(id, categoryIndex2) {
345
- var syntheticalConfig, interfaceList, _apifoxInstance, interfaceCodes, groupedInterfaceCodes;
346
-
321
+ var syntheticalConfig, interfaceList, interfaceCodes, groupedInterfaceCodes;
347
322
  return _regenerator.default.wrap(function _callee6$(_context6) {
348
323
  while (1) {
349
324
  switch (_context6.prev = _context6.next) {
@@ -361,37 +336,27 @@ var Generator = /*#__PURE__*/function () {
361
336
  // 接口列表
362
337
  interfaceList = [];
363
338
 
364
- if (!(syntheticalConfig.serverType === 'apifox')) {
365
- _context6.next = 15;
339
+ if (!(syntheticalConfig.serverType === 'apifox' && apifoxInstance)) {
340
+ _context6.next = 12;
366
341
  break;
367
342
  }
368
343
 
369
344
  _context6.next = 9;
370
- return (_apifoxInstance = apifoxInstance) == null ? void 0 : _apifoxInstance.getInterfaceList(syntheticalConfig);
345
+ return apifoxInstance.getInterfaceList(syntheticalConfig);
371
346
 
372
347
  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;
348
+ interfaceList = _context6.sent;
349
+ _context6.next = 15;
385
350
  break;
386
351
 
387
- case 15:
388
- _context6.next = 17;
352
+ case 12:
353
+ _context6.next = 14;
389
354
  return _this3.fetchInterfaceList(syntheticalConfig);
390
355
 
391
- case 17:
356
+ case 14:
392
357
  interfaceList = _context6.sent;
393
358
 
394
- case 18:
359
+ case 15:
395
360
  interfaceList = interfaceList.map(function (interfaceInfo) {
396
361
  // 实现 _project 字段
397
362
  interfaceInfo._project = (0, _vtils.omit)(projectInfo, ['cats', 'getMockUrl', 'getDevUrl', 'getProdUrl']); // 预处理
@@ -404,7 +369,7 @@ var Generator = /*#__PURE__*/function () {
404
369
  interfaceList.sort(function (a, b) {
405
370
  return a._id - b._id;
406
371
  });
407
- _context6.next = 22;
372
+ _context6.next = 19;
408
373
  return Promise.all(interfaceList.map( /*#__PURE__*/function () {
409
374
  var _ref12 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(interfaceInfo) {
410
375
  var outputFilePath, categoryUID, code, weights;
@@ -440,7 +405,7 @@ var Generator = /*#__PURE__*/function () {
440
405
  };
441
406
  }()));
442
407
 
443
- case 22:
408
+ case 19:
444
409
  interfaceCodes = _context6.sent;
445
410
  groupedInterfaceCodes = (0, _vtils.groupBy)(interfaceCodes, function (item) {
446
411
  return item.outputFilePath;
@@ -453,11 +418,11 @@ var Generator = /*#__PURE__*/function () {
453
418
  var text = '';
454
419
 
455
420
  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";
421
+ 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
422
  }
458
423
 
459
424
  if (syntheticalConfig.ignoreUselessConfigVar !== true) {
460
- text += "const dataKey" + categoryUID + " = " + JSON.stringify(syntheticalConfig.dataKey) + " as any";
425
+ text += "const dataKey" + categoryUID + " = " + JSON.stringify(syntheticalConfig.dataKey) + " as any;";
461
426
  }
462
427
 
463
428
  return (0, _vtils.dedent)(text);
@@ -481,7 +446,7 @@ var Generator = /*#__PURE__*/function () {
481
446
  };
482
447
  }));
483
448
 
484
- case 25:
449
+ case 22:
485
450
  case "end":
486
451
  return _context6.stop();
487
452
  }
@@ -494,7 +459,7 @@ var Generator = /*#__PURE__*/function () {
494
459
  };
495
460
  }()));
496
461
 
497
- case 9:
462
+ case 4:
498
463
  codes = _context7.sent.flat();
499
464
 
500
465
  for (_iterator = (0, _createForOfIteratorHelperLoose2.default)((0, _vtils.values)((0, _vtils.groupBy)(codes, function (item) {
@@ -508,7 +473,7 @@ var Generator = /*#__PURE__*/function () {
508
473
  }));
509
474
  }
510
475
 
511
- case 11:
476
+ case 6:
512
477
  case "end":
513
478
  return _context7.stop();
514
479
  }
@@ -1119,9 +1084,8 @@ var Generator = /*#__PURE__*/function () {
1119
1084
 
1120
1085
  case 61:
1121
1086
  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) {
1087
+ requestHeaders = JSON.stringify((extendedInterfaceInfo.req_headers || [] // .filter(item => item.name.toLowerCase() !== 'content-type')
1088
+ ).reduce(function (res, item) {
1125
1089
  res[item.name] = item.value;
1126
1090
  return res;
1127
1091
  }, {}));
@@ -1129,11 +1093,11 @@ var Generator = /*#__PURE__*/function () {
1129
1093
  return "\u63A5\u53E3 " + title + " \u7684 **\u8BF7\u6C42\u7C7B\u578B**";
1130
1094
  }), requestDataType.trim(), genComment(function (title) {
1131
1095
  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) {
1096
+ }), 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
1097
  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
1098
+ }), 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 + " {", 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
1099
  /* 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) {
1100
+ || _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
1101
  return "\u63A5\u53E3 " + title + " \u7684 **\u8BF7\u6C42\u51FD\u6570**";
1138
1102
  }), 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
1103
  return "\u63A5\u53E3 " + title + " \u7684 **React Hook**";
package/lib/cjs/utils.js CHANGED
@@ -3,7 +3,7 @@
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  exports.__esModule = true;
6
- exports.getCachedPrettierOptions = void 0;
6
+ exports.getFilteredCat = exports.getCachedPrettierOptions = void 0;
7
7
  exports.getNormalizedRelativePath = getNormalizedRelativePath;
8
8
  exports.getPrettier = getPrettier;
9
9
  exports.getPrettierOptions = getPrettierOptions;
@@ -49,12 +49,12 @@ var _toJsonSchema = _interopRequireDefault(require("to-json-schema"));
49
49
 
50
50
  var _vtils = require("vtils");
51
51
 
52
+ var _types = require("./types");
53
+
52
54
  var _jsonSchemaToTypescript = require("json-schema-to-typescript");
53
55
 
54
56
  var _helpers = require("./helpers");
55
57
 
56
- var _types = require("./types");
57
-
58
58
  var _url2 = require("url");
59
59
 
60
60
  /**
@@ -727,6 +727,10 @@ exports.getCachedPrettierOptions = getCachedPrettierOptions;
727
727
  function httpGet(_x4, _x5) {
728
728
  return _httpGet.apply(this, arguments);
729
729
  }
730
+ /**
731
+ * 获取过滤后的菜单
732
+ */
733
+
730
734
 
731
735
  function _httpGet() {
732
736
  _httpGet = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(url, query) {
@@ -763,4 +767,39 @@ function _httpGet() {
763
767
  }, _callee4);
764
768
  }));
765
769
  return _httpGet.apply(this, arguments);
766
- }
770
+ }
771
+
772
+ var getFilteredCat = function getFilteredCat(cat, cats) {
773
+ // 分类处理
774
+ // 数组化
775
+ var categoryIds = (0, _vtils.castArray)(cat.id); // 全部分类
776
+
777
+ if (categoryIds.includes(0)) {
778
+ var _categoryIds;
779
+
780
+ (_categoryIds = categoryIds).push.apply(_categoryIds, cats.map(function (c) {
781
+ return c._id;
782
+ }));
783
+ } // 唯一化
784
+
785
+
786
+ categoryIds = (0, _vtils.uniq)(categoryIds); // 去掉被排除的分类
787
+
788
+ var excludedCategoryIds = categoryIds.filter(function (id) {
789
+ return id < 0;
790
+ }).map(Math.abs);
791
+ categoryIds = categoryIds.filter(function (id) {
792
+ return !excludedCategoryIds.includes(Math.abs(id));
793
+ }); // 删除不存在的分类
794
+
795
+ categoryIds = categoryIds.filter(function (id) {
796
+ return !!cats.find(function (c) {
797
+ return c._id === id;
798
+ });
799
+ }); // 顺序化
800
+
801
+ categoryIds = categoryIds.sort();
802
+ return categoryIds;
803
+ };
804
+
805
+ exports.getFilteredCat = getFilteredCat;
@@ -2,12 +2,13 @@ import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
4
4
  import axios from 'axios';
5
- import { RequestBodyType, RequestParamType, RequestQueryType, Required, ResponseBodyType } from './types';
5
+ import { RequestBodyType, RequestFormItemType, RequestParamType, RequestQueryType, Required, ResponseBodyType } from './types';
6
+ import { getFilteredCat } from './utils';
6
7
  export var ApifoxToYApiData = /*#__PURE__*/function () {
7
8
  function ApifoxToYApiData(options) {
8
- this.options = options;
9
9
  this.schemas = void 0;
10
10
  this.folders = void 0;
11
+ this.options = void 0;
11
12
  this.project = {
12
13
  _id: 0,
13
14
  _url: '',
@@ -22,6 +23,7 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
22
23
  };
23
24
  this.schemas = {};
24
25
  this.folders = [];
26
+ this.options = options;
25
27
  }
26
28
  /**
27
29
  * http-api-tree 这个接口拿到所有的folder(category)
@@ -37,7 +39,7 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
37
39
  var _getProjectInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
38
40
  var _this = this;
39
41
 
40
- var folders, schemas, cats;
42
+ var folders, schemas;
41
43
  return _regeneratorRuntime.wrap(function _callee$(_context) {
42
44
  while (1) {
43
45
  switch (_context.prev = _context.next) {
@@ -53,25 +55,24 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
53
55
  case 5:
54
56
  schemas = _context.sent;
55
57
  // 初始化
56
- // this.schemas = schemas.data.data
57
58
  schemas.data.data.forEach(function (item) {
58
59
  _this.schemas[item.id] = item.jsonSchema;
59
60
  });
60
- this.folders = folders.data.data;
61
- cats = folders.data.data.map(function (item) {
62
- return {
63
- _id: item.folder.id,
64
- _url: '',
65
- name: item.name,
66
- desc: item.key,
67
- list: [],
68
- add_time: 0,
69
- up_time: 0
70
- };
71
- });
61
+ this.folders = folders.data.data; // const categoryIds = getFilteredCat(this.options, [])
62
+ // 由于这里面所有的cats是递归的,所以涉及到多层级的cats如果判定权限。这里先做一遍过滤,后续就不再做了
63
+ // const cats: Category[] = folders.data.data.map(item => ({
64
+ // _id: item.folder.id,
65
+ // _url: '',
66
+ // name: item.name,
67
+ // desc: item.key,
68
+ // list: [],
69
+ // add_time: 0,
70
+ // up_time: 0,
71
+ // }))
72
+
72
73
  this.project._id = schemas.data.data[0].projectId;
73
74
  return _context.abrupt("return", _extends({}, this.project, {
74
- cats: cats,
75
+ cats: [],
75
76
  getMockUrl: function getMockUrl() {
76
77
  return '';
77
78
  },
@@ -83,7 +84,7 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
83
84
  }
84
85
  }));
85
86
 
86
- case 11:
87
+ case 10:
87
88
  case "end":
88
89
  return _context.stop();
89
90
  }
@@ -97,10 +98,73 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
97
98
 
98
99
  return getProjectInfo;
99
100
  }()
101
+ /**
102
+ * 根据筛选后的权限目录,对当前folder进行筛选
103
+ * 如果传入的cats为空,表明获取所有
104
+ */
105
+ ;
106
+
107
+ _proto.getValidCat = function getValidCat(cats, allFolders) {
108
+ var _this2 = this;
109
+
110
+ if (allFolders === void 0) {
111
+ allFolders = this.folders;
112
+ }
113
+
114
+ var folders = [];
115
+ allFolders.forEach(function (item) {
116
+ if (item.type !== 'apiDetailFolder') return;
117
+ if (cats.length && !cats.includes(item.folder.id)) return;
118
+ folders.push(item.folder.id);
119
+
120
+ if (item.type === 'apiDetailFolder' && item.children.length) {
121
+ folders.push.apply(folders, _this2.getValidCat(cats, item.children));
122
+ }
123
+ });
124
+ return folders;
125
+ }
126
+ /**
127
+ * 根据传入的目录配置,返回有效的目录列表
128
+ */
129
+ ;
130
+
131
+ _proto.getCats = function getCats(cats) {
132
+ var categoryIds = getFilteredCat(cats, []); // 对cat进行有效性过滤
133
+
134
+ return this.getValidCat(categoryIds);
135
+ }
136
+ /**
137
+ * 通过目录的ID查找目录
138
+ */
139
+ ;
140
+
141
+ _proto.findCats = function findCats(id, folders) {
142
+ var _this3 = this;
143
+
144
+ if (folders === void 0) {
145
+ folders = this.folders;
146
+ }
147
+
148
+ var folder = null;
149
+ folders.forEach(function (item) {
150
+ if (folder) return;
151
+
152
+ if (item.type === 'apiDetailFolder' && item.folder.id === id) {
153
+ folder = item;
154
+ return;
155
+ }
156
+
157
+ if (item.type === 'apiDetailFolder' && item.children) {
158
+ folder = _this3.findCats(id, item.children);
159
+ }
160
+ });
161
+ return folder;
162
+ }
100
163
  /**
101
164
  * 由于yapi中基本没有处理ref的方案
102
165
  * 都是直接由具体的参数值管理,所以这里需要对所有的ref进行转换
103
- * TODO:目前这里只考虑了一个层级,如果是多个层级,需要再递归处理
166
+ *
167
+ * 在apifox中,对于cat来说,cat的children有可能是folder,对于folder,我们要过滤掉
104
168
  */
105
169
  ;
106
170
 
@@ -108,7 +172,7 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
108
172
  /*#__PURE__*/
109
173
  function () {
110
174
  var _getInterfaceList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(syntheticalConfig) {
111
- var _this2 = this;
175
+ var _this4 = this;
112
176
 
113
177
  var cat, transformRefToSchema;
114
178
  return _regeneratorRuntime.wrap(function _callee3$(_context3) {
@@ -116,9 +180,7 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
116
180
  switch (_context3.prev = _context3.next) {
117
181
  case 0:
118
182
  // syntheticalConfig.id 就是catId
119
- cat = this.folders.find(function (item) {
120
- return item.folder.id === syntheticalConfig.id;
121
- });
183
+ cat = this.findCats(syntheticalConfig.id);
122
184
 
123
185
  if (cat) {
124
186
  _context3.next = 3;
@@ -130,21 +192,23 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
130
192
  case 3:
131
193
  transformRefToSchema = function transformRefToSchema(refString) {
132
194
  var refId = refString.split('/').pop();
133
- return _this2.schemas[refId];
195
+ return _this4.schemas[refId];
134
196
  };
135
197
 
136
- return _context3.abrupt("return", Promise.all(cat.children.map( /*#__PURE__*/function () {
137
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(item) {
138
- var _resultData$responses, _resultData$responses2, _resultData$responses3, _resultData$responses4, _resultData$responses5, _resultData$responses6, _resultData$responses7, _resultData$requestBo, _resultData$requestBo2, _resultData$parameter, _resultData$parameter2;
198
+ return _context3.abrupt("return", Promise.all(cat.children.filter(function (child) {
199
+ return child.type === 'apiDetail';
200
+ }).map( /*#__PURE__*/function () {
201
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(child) {
202
+ var _resultData$responses, _resultData$responses2, _resultData$responses3, _resultData$responses4, _resultData$responses5, _resultData$responses6, _resultData$responses7, _resultData$requestBo, _resultData$requestBo2, _resultData$parameter, _resultData$parameter2, _resultData$requestBo3;
139
203
 
140
- var result, resultData, resbodyIndex, respref, dataProperties, _item, _dataProperties$_item, dataPropRef, reqref;
204
+ var result, resultData, resbodyIndex, respref, dataProperties, item, _dataProperties$item$, dataPropRef, reqref;
141
205
 
142
206
  return _regeneratorRuntime.wrap(function _callee2$(_context2) {
143
207
  while (1) {
144
208
  switch (_context2.prev = _context2.next) {
145
209
  case 0:
146
210
  _context2.next = 2;
147
- return axios.get("https://apifox.com/api/v1/shared-docs/" + _this2.options.token + "/http-apis/" + item.api.id);
211
+ return axios.get("https://apifox.com/api/v1/shared-docs/" + _this4.options.token + "/http-apis/" + child.api.id);
148
212
 
149
213
  case 2:
150
214
  result = _context2.sent;
@@ -170,14 +234,14 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
170
234
  dataProperties = (_resultData$responses5 = resultData.responses[resbodyIndex].jsonSchema) == null ? void 0 : (_resultData$responses6 = _resultData$responses5.properties) == null ? void 0 : (_resultData$responses7 = _resultData$responses6.data) == null ? void 0 : _resultData$responses7.properties;
171
235
 
172
236
  if (dataProperties) {
173
- for (_item in dataProperties) {
237
+ for (item in dataProperties) {
174
238
  // @ts-ignore
175
- dataPropRef = (_dataProperties$_item = dataProperties[_item].items) == null ? void 0 : _dataProperties$_item['$ref']; // @ts-ignore
239
+ dataPropRef = (_dataProperties$item$ = dataProperties[item].items) == null ? void 0 : _dataProperties$item$['$ref']; // @ts-ignore
176
240
 
177
241
  // @ts-ignore
178
- if (dataProperties[_item].type === 'array' && dataPropRef) {
242
+ if (dataProperties[item].type === 'array' && dataPropRef) {
179
243
  // @ts-ignore
180
- resultData.responses[resbodyIndex].jsonSchema.properties.data.properties[_item].items = transformRefToSchema(dataPropRef);
244
+ resultData.responses[resbodyIndex].jsonSchema.properties.data.properties[item].items = transformRefToSchema(dataPropRef);
181
245
  }
182
246
  }
183
247
  } // 处理request中可能存在的ref
@@ -206,7 +270,7 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
206
270
  up_time: 0,
207
271
  list: []
208
272
  },
209
- _project: _extends({}, _this2.project),
273
+ _project: _extends({}, _this4.project),
210
274
  _url: '',
211
275
  title: resultData.name,
212
276
  status: 'undone',
@@ -241,9 +305,17 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
241
305
  type: item.type === 'string' ? RequestQueryType.string : RequestQueryType.number
242
306
  };
243
307
  })) || [],
244
- req_body_type: resultData.requestBody.jsonSchema ? RequestBodyType.json : RequestBodyType.raw,
308
+ req_body_type: resultData.requestBody.type === 'multipart/form-data' ? RequestBodyType.form : resultData.requestBody.jsonSchema ? RequestBodyType.json : RequestBodyType.raw,
245
309
  req_body_is_json_schema: !!resultData.requestBody.jsonSchema,
246
- req_body_form: [],
310
+ req_body_form: ((_resultData$requestBo3 = resultData.requestBody.parameters) == null ? void 0 : _resultData$requestBo3.map(function (item) {
311
+ return {
312
+ name: item.name,
313
+ desc: item.description,
314
+ example: item.example,
315
+ required: item.required ? Required.true : Required.false,
316
+ type: item.type === 'string' ? RequestFormItemType.text : RequestFormItemType.file
317
+ };
318
+ })) || [],
247
319
  req_body_other: resultData.requestBody.jsonSchema ? JSON.stringify(resultData.requestBody.jsonSchema) : '',
248
320
  res_body_type: ResponseBodyType.json,
249
321
  res_body_is_json_schema: true,
@@ -14,7 +14,7 @@ import { ApifoxToYApiData } from './ApifoxToYApiData';
14
14
  import { castArray, cloneDeepFast, dedent, groupBy, isEmpty, isFunction, last, memoize, noop, omit, uniq, values } from 'vtils';
15
15
  import { Method, RequestBodyType } from './types';
16
16
  import { exec } from 'child_process';
17
- import { getCachedPrettierOptions, getNormalizedRelativePath, getPrettier, getRequestDataJsonSchema, getResponseDataJsonSchema, httpGet, jsonSchemaToType, sortByWeights, throwError } from './utils';
17
+ import { getCachedPrettierOptions, getFilteredCat, getNormalizedRelativePath, getPrettier, getRequestDataJsonSchema, getResponseDataJsonSchema, httpGet, jsonSchemaToType, sortByWeights, throwError } from './utils';
18
18
  import { SwaggerToYApiServer } from './SwaggerToYApiServer';
19
19
 
20
20
  /**
@@ -278,49 +278,24 @@ export var Generator = /*#__PURE__*/function () {
278
278
  _context8.next = 13;
279
279
  return Promise.all(projectConfig.categories.map( /*#__PURE__*/function () {
280
280
  var _ref10 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(categoryConfig, categoryIndex) {
281
- var categoryIds, _categoryIds, excludedCategoryIds, codes, _iterator, _step, _outputFileList$group, groupedCodes;
281
+ var categoryIds, codes, _iterator, _step, _outputFileList$group, groupedCodes;
282
282
 
283
283
  return _regeneratorRuntime.wrap(function _callee7$(_context7) {
284
284
  while (1) {
285
285
  switch (_context7.prev = _context7.next) {
286
286
  case 0:
287
- // 分类处理
288
- // 数组化
289
- categoryIds = castArray(categoryConfig.id); // 全部分类
290
-
291
- // 全部分类
292
- if (categoryIds.includes(0)) {
293
- (_categoryIds = categoryIds).push.apply(_categoryIds, projectInfo.cats.map(function (cat) {
294
- return cat._id;
295
- }));
296
- } // 唯一化
297
-
298
-
299
- // 唯一化
300
- categoryIds = uniq(categoryIds); // 去掉被排除的分类
301
-
302
- // 去掉被排除的分类
303
- excludedCategoryIds = categoryIds.filter(function (id) {
304
- return id < 0;
305
- }).map(Math.abs);
306
- categoryIds = categoryIds.filter(function (id) {
307
- return !excludedCategoryIds.includes(Math.abs(id));
308
- }); // 删除不存在的分类
309
-
310
- // 删除不存在的分类
311
- categoryIds = categoryIds.filter(function (id) {
312
- return !!projectInfo.cats.find(function (cat) {
313
- return cat._id === id;
314
- });
315
- }); // 顺序化
316
-
317
- // 顺序化
318
- categoryIds = categoryIds.sort();
319
- _context7.next = 9;
287
+ categoryIds = [];
288
+
289
+ if (serverConfig.serverType === 'apifox' && apifoxInstance) {
290
+ categoryIds = apifoxInstance.getCats(categoryConfig);
291
+ } else {
292
+ categoryIds = getFilteredCat(categoryConfig, projectInfo.cats);
293
+ }
294
+
295
+ _context7.next = 4;
320
296
  return Promise.all(categoryIds.map( /*#__PURE__*/function () {
321
297
  var _ref11 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(id, categoryIndex2) {
322
- var syntheticalConfig, interfaceList, _apifoxInstance, interfaceCodes, groupedInterfaceCodes;
323
-
298
+ var syntheticalConfig, interfaceList, interfaceCodes, groupedInterfaceCodes;
324
299
  return _regeneratorRuntime.wrap(function _callee6$(_context6) {
325
300
  while (1) {
326
301
  switch (_context6.prev = _context6.next) {
@@ -338,37 +313,27 @@ export var Generator = /*#__PURE__*/function () {
338
313
  // 接口列表
339
314
  interfaceList = [];
340
315
 
341
- if (!(syntheticalConfig.serverType === 'apifox')) {
342
- _context6.next = 15;
316
+ if (!(syntheticalConfig.serverType === 'apifox' && apifoxInstance)) {
317
+ _context6.next = 12;
343
318
  break;
344
319
  }
345
320
 
346
321
  _context6.next = 9;
347
- return (_apifoxInstance = apifoxInstance) == null ? void 0 : _apifoxInstance.getInterfaceList(syntheticalConfig);
322
+ return apifoxInstance.getInterfaceList(syntheticalConfig);
348
323
 
349
324
  case 9:
350
- _context6.t0 = _context6.sent;
351
-
352
- if (_context6.t0) {
353
- _context6.next = 12;
354
- break;
355
- }
356
-
357
- _context6.t0 = [];
358
-
359
- case 12:
360
- interfaceList = _context6.t0;
361
- _context6.next = 18;
325
+ interfaceList = _context6.sent;
326
+ _context6.next = 15;
362
327
  break;
363
328
 
364
- case 15:
365
- _context6.next = 17;
329
+ case 12:
330
+ _context6.next = 14;
366
331
  return _this3.fetchInterfaceList(syntheticalConfig);
367
332
 
368
- case 17:
333
+ case 14:
369
334
  interfaceList = _context6.sent;
370
335
 
371
- case 18:
336
+ case 15:
372
337
  interfaceList = interfaceList.map(function (interfaceInfo) {
373
338
  // 实现 _project 字段
374
339
  interfaceInfo._project = omit(projectInfo, ['cats', 'getMockUrl', 'getDevUrl', 'getProdUrl']); // 预处理
@@ -381,7 +346,7 @@ export var Generator = /*#__PURE__*/function () {
381
346
  interfaceList.sort(function (a, b) {
382
347
  return a._id - b._id;
383
348
  });
384
- _context6.next = 22;
349
+ _context6.next = 19;
385
350
  return Promise.all(interfaceList.map( /*#__PURE__*/function () {
386
351
  var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(interfaceInfo) {
387
352
  var outputFilePath, categoryUID, code, weights;
@@ -417,7 +382,7 @@ export var Generator = /*#__PURE__*/function () {
417
382
  };
418
383
  }()));
419
384
 
420
- case 22:
385
+ case 19:
421
386
  interfaceCodes = _context6.sent;
422
387
  groupedInterfaceCodes = groupBy(interfaceCodes, function (item) {
423
388
  return item.outputFilePath;
@@ -430,11 +395,11 @@ export var Generator = /*#__PURE__*/function () {
430
395
  var text = '';
431
396
 
432
397
  if (syntheticalConfig.withoutUrl !== true) {
433
- 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";
398
+ 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;";
434
399
  }
435
400
 
436
401
  if (syntheticalConfig.ignoreUselessConfigVar !== true) {
437
- text += "const dataKey" + categoryUID + " = " + JSON.stringify(syntheticalConfig.dataKey) + " as any";
402
+ text += "const dataKey" + categoryUID + " = " + JSON.stringify(syntheticalConfig.dataKey) + " as any;";
438
403
  }
439
404
 
440
405
  return dedent(text);
@@ -458,7 +423,7 @@ export var Generator = /*#__PURE__*/function () {
458
423
  };
459
424
  }));
460
425
 
461
- case 25:
426
+ case 22:
462
427
  case "end":
463
428
  return _context6.stop();
464
429
  }
@@ -471,7 +436,7 @@ export var Generator = /*#__PURE__*/function () {
471
436
  };
472
437
  }()));
473
438
 
474
- case 9:
439
+ case 4:
475
440
  codes = _context7.sent.flat();
476
441
 
477
442
  for (_iterator = _createForOfIteratorHelperLoose(values(groupBy(codes, function (item) {
@@ -485,7 +450,7 @@ export var Generator = /*#__PURE__*/function () {
485
450
  }));
486
451
  }
487
452
 
488
- case 11:
453
+ case 6:
489
454
  case "end":
490
455
  return _context7.stop();
491
456
  }
@@ -1096,9 +1061,8 @@ export var Generator = /*#__PURE__*/function () {
1096
1061
 
1097
1062
  case 61:
1098
1063
  requestFunctionExtraInfo = _context17.t5;
1099
- requestHeaders = JSON.stringify((extendedInterfaceInfo.req_headers || []).filter(function (item) {
1100
- return item.name.toLowerCase() !== 'content-type';
1101
- }).reduce(function (res, item) {
1064
+ requestHeaders = JSON.stringify((extendedInterfaceInfo.req_headers || [] // .filter(item => item.name.toLowerCase() !== 'content-type')
1065
+ ).reduce(function (res, item) {
1102
1066
  res[item.name] = item.value;
1103
1067
  return res;
1104
1068
  }, {}));
@@ -1106,11 +1070,11 @@ export var Generator = /*#__PURE__*/function () {
1106
1070
  return "\u63A5\u53E3 " + title + " \u7684 **\u8BF7\u6C42\u7C7B\u578B**";
1107
1071
  }), requestDataType.trim(), genComment(function (title) {
1108
1072
  return "\u63A5\u53E3 " + title + " \u7684 **\u8FD4\u56DE\u7C7B\u578B**";
1109
- }), responseDataType.trim(), syntheticalConfig.typesOnly ? '' : dedent(_templateObject11 || (_templateObject11 = _taggedTemplateLiteralLoose(["\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) {
1073
+ }), responseDataType.trim(), syntheticalConfig.typesOnly ? '' : dedent(_templateObject11 || (_templateObject11 = _taggedTemplateLiteralLoose(["\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) {
1110
1074
  return "\u63A5\u53E3 " + title + " \u7684 **\u8BF7\u6C42\u914D\u7F6E**";
1111
- }), 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 === Method.GET ? RequestBodyType.query : extendedInterfaceInfo.req_body_type
1075
+ }), 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 + " {", 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 === Method.GET ? RequestBodyType.query : extendedInterfaceInfo.req_body_type
1112
1076
  /* istanbul ignore next */
1113
- || 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) {
1077
+ || 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) {
1114
1078
  return "\u63A5\u53E3 " + title + " \u7684 **\u8BF7\u6C42\u51FD\u6570**";
1115
1079
  }), requestFunctionName, COMPRESSOR_TREE_SHAKING_ANNOTATION, isRequestDataOptional ? '?' : '', requestDataTypeName, responseDataTypeName, requestConfigName, requestFunctionName, requestConfigName, !syntheticalConfig.reactHooks || !syntheticalConfig.reactHooks.enabled ? '' : dedent(_templateObject12 || (_templateObject12 = _taggedTemplateLiteralLoose(["\n ", "\n export const ", " = ", " makeRequestHook<", ", ", ", ReturnType<typeof ", ">>(", ")\n "])), genComment(function (title) {
1116
1080
  return "\u63A5\u53E3 " + title + " \u7684 **React Hook**";
package/lib/esm/utils.js CHANGED
@@ -9,10 +9,10 @@ import path from 'path';
9
9
  import prettier from 'prettier';
10
10
  import ProxyAgent from 'proxy-agent';
11
11
  import toJsonSchema from 'to-json-schema';
12
- import { castArray, cloneDeepFast, forOwn, isArray, isEmpty, isObject, mapKeys, memoize, run, traverse } from 'vtils';
12
+ import { castArray, cloneDeepFast, forOwn, isArray, isEmpty, isObject, mapKeys, memoize, run, traverse, uniq } from 'vtils';
13
+ import { Method, RequestBodyType, RequestFormItemType, Required, ResponseBodyType } from './types';
13
14
  import { compile } from 'json-schema-to-typescript';
14
15
  import { FileData } from './helpers';
15
- import { Method, RequestBodyType, RequestFormItemType, Required, ResponseBodyType } from './types';
16
16
  import { URL } from 'url';
17
17
  /**
18
18
  * 抛出错误。
@@ -668,6 +668,9 @@ export var getCachedPrettierOptions = memoize(getPrettierOptions);
668
668
  export function httpGet(_x4, _x5) {
669
669
  return _httpGet.apply(this, arguments);
670
670
  }
671
+ /**
672
+ * 获取过滤后的菜单
673
+ */
671
674
 
672
675
  function _httpGet() {
673
676
  _httpGet = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(url, query) {
@@ -704,4 +707,37 @@ function _httpGet() {
704
707
  }, _callee4);
705
708
  }));
706
709
  return _httpGet.apply(this, arguments);
707
- }
710
+ }
711
+
712
+ export var getFilteredCat = function getFilteredCat(cat, cats) {
713
+ // 分类处理
714
+ // 数组化
715
+ var categoryIds = castArray(cat.id); // 全部分类
716
+
717
+ if (categoryIds.includes(0)) {
718
+ var _categoryIds;
719
+
720
+ (_categoryIds = categoryIds).push.apply(_categoryIds, cats.map(function (c) {
721
+ return c._id;
722
+ }));
723
+ } // 唯一化
724
+
725
+
726
+ categoryIds = uniq(categoryIds); // 去掉被排除的分类
727
+
728
+ var excludedCategoryIds = categoryIds.filter(function (id) {
729
+ return id < 0;
730
+ }).map(Math.abs);
731
+ categoryIds = categoryIds.filter(function (id) {
732
+ return !excludedCategoryIds.includes(Math.abs(id));
733
+ }); // 删除不存在的分类
734
+
735
+ categoryIds = categoryIds.filter(function (id) {
736
+ return !!cats.find(function (c) {
737
+ return c._id === id;
738
+ });
739
+ }); // 顺序化
740
+
741
+ categoryIds = categoryIds.sort();
742
+ return categoryIds;
743
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yapi-apifox-to-typescript",
3
- "version": "4.0.0",
3
+ "version": "4.1.1",
4
4
  "description": "根据 YApi 或 Apifox 的接口定义生成 TypeScript/JavaScript 的接口类型及其请求函数代码。",
5
5
  "keywords": [
6
6
  "yapi",
@@ -37,8 +37,8 @@
37
37
  "docs": "npm run docs:build && gh-pages --dist docs/dist --dest handbook",
38
38
  "docs:build": "dumi build",
39
39
  "docs:dev": "dumi dev",
40
- "release": "standard-version -a && yarn build && git push --follow-tags origin master && npm publish",
41
- "releaseBeta": "standard-version -a --release-as patch --prerelease beta && yarn build && git push --follow-tags origin master && npm publish --tag next",
40
+ "release": "standard-version -a && npm run build && git push --follow-tags origin master && npm publish",
41
+ "releaseBeta": "standard-version -a --release-as patch --prerelease beta && npm run build && git push --follow-tags origin master && npm publish --tag next",
42
42
  "testApi": "ts-node -T src/cli.ts"
43
43
  },
44
44
  "husky": {