yapi-apifox-to-typescript 4.1.3 → 4.2.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.
- package/lib/cjs/ApifoxToYApiData.js +459 -257
- package/lib/cjs/Generator.js +24 -11
- package/lib/esm/ApifoxToYApiData.js +458 -257
- package/lib/esm/Generator.js +24 -11
- package/lib/esm/index.d.ts +26 -0
- package/package.json +3 -3
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import _createForOfIteratorHelperLoose from "@babel/runtime/helpers/esm/createForOfIteratorHelperLoose";
|
|
2
2
|
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
3
4
|
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
4
5
|
import axios from 'axios';
|
|
5
6
|
import { RequestBodyType, RequestFormItemType, RequestParamType, RequestQueryType, Required, ResponseBodyType } from './types';
|
|
6
7
|
import { getFilteredCat } from './utils';
|
|
7
8
|
export var ApifoxToYApiData = /*#__PURE__*/function () {
|
|
8
9
|
function ApifoxToYApiData(options) {
|
|
9
|
-
this.
|
|
10
|
-
this.folders = void 0;
|
|
10
|
+
this.openApiDoc = null;
|
|
11
11
|
this.options = void 0;
|
|
12
12
|
this.project = {
|
|
13
13
|
_id: 0,
|
|
@@ -22,55 +22,333 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
|
|
|
22
22
|
}]
|
|
23
23
|
};
|
|
24
24
|
this.schemas = {};
|
|
25
|
-
this.
|
|
25
|
+
this.interfaceIdCounter = 0;
|
|
26
26
|
this.options = options;
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
var _proto = ApifoxToYApiData.prototype;
|
|
30
|
+
|
|
31
|
+
_proto.resolveRef = function resolveRef(ref) {
|
|
32
|
+
if (!ref.startsWith('#/components/schemas/')) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
var schemaName = ref.replace('#/components/schemas/', '');
|
|
37
|
+
return this.schemas[schemaName] || null;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
_proto.resolveSchema = function resolveSchema(schema) {
|
|
41
|
+
if (!schema) return undefined;
|
|
42
|
+
|
|
43
|
+
if (schema.$ref) {
|
|
44
|
+
return this.resolveRef(schema.$ref) || schema;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return schema;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
_proto.getSchemaType = function getSchemaType(schema) {
|
|
51
|
+
if (!schema) return 'string';
|
|
52
|
+
var resolved = this.resolveSchema(schema);
|
|
53
|
+
if (!resolved) return 'string';
|
|
54
|
+
return resolved.type || 'string';
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
_proto.convertParametersToReqParams = function convertParametersToReqParams(parameters, type) {
|
|
58
|
+
var _this = this;
|
|
59
|
+
|
|
60
|
+
if (parameters === void 0) {
|
|
61
|
+
parameters = [];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (type === 'path') {
|
|
65
|
+
return parameters.filter(function (p) {
|
|
66
|
+
return p.in === 'path';
|
|
67
|
+
}).map(function (p) {
|
|
68
|
+
return {
|
|
69
|
+
name: p.name,
|
|
70
|
+
desc: p.description || '',
|
|
71
|
+
example: p.example || '',
|
|
72
|
+
required: p.required ? Required.true : Required.false,
|
|
73
|
+
type: _this.getSchemaType(p.schema) === 'string' ? RequestParamType.string : RequestParamType.number
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return parameters.filter(function (p) {
|
|
79
|
+
return p.in === 'query';
|
|
80
|
+
}).map(function (p) {
|
|
81
|
+
return {
|
|
82
|
+
name: p.name,
|
|
83
|
+
desc: p.description || '',
|
|
84
|
+
example: p.example || '',
|
|
85
|
+
required: p.required ? Required.true : Required.false,
|
|
86
|
+
type: _this.getSchemaType(p.schema) === 'string' ? RequestQueryType.string : RequestQueryType.number
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
_proto.convertRequestBody = function convertRequestBody(requestBody) {
|
|
92
|
+
if (!(requestBody != null && requestBody.content)) {
|
|
93
|
+
return {
|
|
94
|
+
req_body_type: RequestBodyType.none,
|
|
95
|
+
req_body_is_json_schema: false,
|
|
96
|
+
req_body_form: [],
|
|
97
|
+
req_body_other: '',
|
|
98
|
+
contentType: ''
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
var contentTypes = Object.keys(requestBody.content);
|
|
103
|
+
var jsonContentType = contentTypes.find(function (ct) {
|
|
104
|
+
return ct.includes('application/json');
|
|
105
|
+
});
|
|
106
|
+
var formContentType = contentTypes.find(function (ct) {
|
|
107
|
+
return ct.includes('application/x-www-form-urlencoded') || ct.includes('multipart/form-data');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
if (jsonContentType) {
|
|
111
|
+
var schema = requestBody.content[jsonContentType].schema;
|
|
112
|
+
return {
|
|
113
|
+
req_body_type: RequestBodyType.json,
|
|
114
|
+
req_body_is_json_schema: true,
|
|
115
|
+
req_body_form: [],
|
|
116
|
+
req_body_other: schema ? JSON.stringify(this.normalizeSchema(schema)) : '',
|
|
117
|
+
contentType: jsonContentType
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (formContentType) {
|
|
122
|
+
var _schema = requestBody.content[formContentType].schema;
|
|
123
|
+
var resolvedSchema = this.resolveSchema(_schema);
|
|
124
|
+
var formItems = [];
|
|
125
|
+
|
|
126
|
+
if (resolvedSchema != null && resolvedSchema.properties) {
|
|
127
|
+
var requiredFields = resolvedSchema.required || [];
|
|
128
|
+
|
|
129
|
+
for (var _i = 0, _Object$entries = Object.entries(resolvedSchema.properties); _i < _Object$entries.length; _i++) {
|
|
130
|
+
var _Object$entries$_i = _Object$entries[_i],
|
|
131
|
+
name = _Object$entries$_i[0],
|
|
132
|
+
prop = _Object$entries$_i[1];
|
|
133
|
+
var resolvedProp = this.resolveSchema(prop);
|
|
134
|
+
formItems.push({
|
|
135
|
+
name: name,
|
|
136
|
+
type: (resolvedProp == null ? void 0 : resolvedProp.type) === 'file' || (resolvedProp == null ? void 0 : resolvedProp.format) === 'binary' ? RequestFormItemType.file : RequestFormItemType.text,
|
|
137
|
+
desc: (resolvedProp == null ? void 0 : resolvedProp.description) || '',
|
|
138
|
+
example: (resolvedProp == null ? void 0 : resolvedProp.example) || '',
|
|
139
|
+
required: requiredFields.includes(name) ? Required.true : Required.false
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
req_body_type: RequestBodyType.form,
|
|
146
|
+
req_body_is_json_schema: false,
|
|
147
|
+
req_body_form: formItems,
|
|
148
|
+
req_body_other: '',
|
|
149
|
+
contentType: formContentType
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
req_body_type: RequestBodyType.raw,
|
|
155
|
+
req_body_is_json_schema: false,
|
|
156
|
+
req_body_form: [],
|
|
157
|
+
req_body_other: '',
|
|
158
|
+
contentType: contentTypes[0] || ''
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
_proto.normalizeSchema = function normalizeSchema(schema) {
|
|
163
|
+
var result = _extends({}, schema);
|
|
164
|
+
|
|
165
|
+
if (schema.$ref) {
|
|
166
|
+
var resolved = this.resolveRef(schema.$ref);
|
|
167
|
+
|
|
168
|
+
if (resolved) {
|
|
169
|
+
return this.normalizeSchema(resolved);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (schema.properties) {
|
|
174
|
+
result.properties = {};
|
|
175
|
+
|
|
176
|
+
for (var _i2 = 0, _Object$entries2 = Object.entries(schema.properties); _i2 < _Object$entries2.length; _i2++) {
|
|
177
|
+
var _Object$entries2$_i = _Object$entries2[_i2],
|
|
178
|
+
key = _Object$entries2$_i[0],
|
|
179
|
+
_value = _Object$entries2$_i[1];
|
|
180
|
+
|
|
181
|
+
if (_value) {
|
|
182
|
+
result.properties[key] = this.normalizeSchema(_value);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (schema.items) {
|
|
188
|
+
result.items = this.normalizeSchema(schema.items);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
delete result.$ref;
|
|
192
|
+
return result;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
_proto.convertResponse = function convertResponse(responses) {
|
|
196
|
+
if (responses === void 0) {
|
|
197
|
+
responses = {};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
var successResponse = responses['200'] || responses['201'] || responses['204'];
|
|
201
|
+
|
|
202
|
+
if (!(successResponse != null && successResponse.content)) {
|
|
203
|
+
return {
|
|
204
|
+
res_body_type: ResponseBodyType.json,
|
|
205
|
+
res_body_is_json_schema: false,
|
|
206
|
+
res_body: ''
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
var jsonContent = successResponse.content['application/json'];
|
|
211
|
+
|
|
212
|
+
if (jsonContent != null && jsonContent.schema) {
|
|
213
|
+
return {
|
|
214
|
+
res_body_type: ResponseBodyType.json,
|
|
215
|
+
res_body_is_json_schema: true,
|
|
216
|
+
res_body: JSON.stringify(this.normalizeSchema(jsonContent.schema))
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
res_body_type: ResponseBodyType.json,
|
|
222
|
+
res_body_is_json_schema: false,
|
|
223
|
+
res_body: ''
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
_proto.generateInterfaceId = function generateInterfaceId() {
|
|
228
|
+
this.interfaceIdCounter += 1;
|
|
229
|
+
return this.interfaceIdCounter;
|
|
230
|
+
}
|
|
28
231
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
232
|
+
* 获取接口所属的目录名称
|
|
233
|
+
* Apifox 优先使用 x-apifox-folder 字段,其次才用 tags[0]
|
|
31
234
|
*/
|
|
235
|
+
;
|
|
32
236
|
|
|
237
|
+
_proto.getOperationFolder = function getOperationFolder(operation) {
|
|
238
|
+
// 优先使用 x-apifox-folder
|
|
239
|
+
if (operation['x-apifox-folder']) {
|
|
240
|
+
return operation['x-apifox-folder'];
|
|
241
|
+
} // 其次使用 tags[0]
|
|
33
242
|
|
|
34
|
-
var _proto = ApifoxToYApiData.prototype;
|
|
35
243
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
244
|
+
if (operation.tags && operation.tags.length > 0) {
|
|
245
|
+
return operation.tags[0];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return 'default';
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
_proto.convertOperationToInterface = function convertOperationToInterface(path, method, operation, categoryName, categoryId) {
|
|
252
|
+
var _operation$tags;
|
|
253
|
+
|
|
254
|
+
var parameters = operation.parameters || [];
|
|
255
|
+
var pathParams = this.convertParametersToReqParams(parameters, 'path');
|
|
256
|
+
var queryParams = this.convertParametersToReqParams(parameters, 'query'); // const headerParams = parameters.filter(p => p.in === 'header')
|
|
257
|
+
|
|
258
|
+
var reqHeaders = [];
|
|
259
|
+
var requestBodyInfo = this.convertRequestBody(operation.requestBody);
|
|
260
|
+
|
|
261
|
+
if (requestBodyInfo.contentType) {
|
|
262
|
+
reqHeaders.push({
|
|
263
|
+
name: 'Content-Type',
|
|
264
|
+
value: requestBodyInfo.contentType,
|
|
265
|
+
desc: '',
|
|
266
|
+
example: '',
|
|
267
|
+
required: Required.true
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
var responseInfo = this.convertResponse(operation.responses); // 获取接口所属目录:优先使用 x-apifox-folder,其次用 tags
|
|
272
|
+
|
|
273
|
+
var folderName = this.getOperationFolder(operation);
|
|
274
|
+
var interfaceTags = (_operation$tags = operation.tags) != null && _operation$tags.length ? operation.tags : folderName !== 'default' ? [folderName] : [];
|
|
275
|
+
return {
|
|
276
|
+
_id: this.generateInterfaceId(),
|
|
277
|
+
_category: {
|
|
278
|
+
_id: categoryId,
|
|
279
|
+
_url: '',
|
|
280
|
+
name: categoryName,
|
|
281
|
+
desc: '',
|
|
282
|
+
add_time: 0,
|
|
283
|
+
up_time: 0
|
|
284
|
+
},
|
|
285
|
+
_project: _extends({}, this.project),
|
|
286
|
+
_url: '',
|
|
287
|
+
title: operation.summary || path,
|
|
288
|
+
status: operation.deprecated ? 'undone' : 'done',
|
|
289
|
+
markdown: operation.description || '',
|
|
290
|
+
path: path,
|
|
291
|
+
method: method.toUpperCase(),
|
|
292
|
+
project_id: this.options.projectId,
|
|
293
|
+
catid: categoryId,
|
|
294
|
+
tag: interfaceTags,
|
|
295
|
+
req_headers: reqHeaders,
|
|
296
|
+
req_params: pathParams,
|
|
297
|
+
req_query: queryParams,
|
|
298
|
+
req_body_type: requestBodyInfo.req_body_type,
|
|
299
|
+
req_body_is_json_schema: requestBodyInfo.req_body_is_json_schema,
|
|
300
|
+
req_body_form: requestBodyInfo.req_body_form,
|
|
301
|
+
req_body_other: requestBodyInfo.req_body_other,
|
|
302
|
+
res_body_type: responseInfo.res_body_type,
|
|
303
|
+
res_body_is_json_schema: responseInfo.res_body_is_json_schema,
|
|
304
|
+
res_body: responseInfo.res_body,
|
|
305
|
+
add_time: Math.floor(Date.now() / 1000),
|
|
306
|
+
up_time: Math.floor(Date.now() / 1000),
|
|
307
|
+
uid: 0
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
_proto.getProjectInfo = /*#__PURE__*/function () {
|
|
39
312
|
var _getProjectInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
40
|
-
var _this
|
|
313
|
+
var _this$options$addFold, _this$options$oasVers, _this$options$exportF, _this$openApiDoc, _this$openApiDoc$comp, _this$openApiDoc2, _this$openApiDoc2$inf, _this$openApiDoc3, _this$openApiDoc3$inf;
|
|
41
314
|
|
|
42
|
-
var
|
|
315
|
+
var baseUrl, response;
|
|
43
316
|
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
44
317
|
while (1) {
|
|
45
318
|
switch (_context.prev = _context.next) {
|
|
46
319
|
case 0:
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
_this.
|
|
320
|
+
baseUrl = this.options.serverUrl.replace(/\/$/, '');
|
|
321
|
+
_context.next = 3;
|
|
322
|
+
return axios.post(baseUrl + "/v1/projects/" + this.options.projectId + "/export-openapi?locale=zh-CN", {
|
|
323
|
+
scope: {
|
|
324
|
+
type: 'ALL',
|
|
325
|
+
excludedByTags: []
|
|
326
|
+
},
|
|
327
|
+
options: {
|
|
328
|
+
includeApifoxExtensionProperties: true,
|
|
329
|
+
addFoldersToTags: (_this$options$addFold = this.options.addFoldersToTags) != null ? _this$options$addFold : true
|
|
330
|
+
},
|
|
331
|
+
oasVersion: (_this$options$oasVers = this.options.oasVersion) != null ? _this$options$oasVers : '3.0',
|
|
332
|
+
exportFormat: (_this$options$exportF = this.options.exportFormat) != null ? _this$options$exportF : 'JSON'
|
|
333
|
+
}, {
|
|
334
|
+
headers: {
|
|
335
|
+
'Authorization': "Bearer " + this.options.token,
|
|
336
|
+
'X-Apifox-Api-Version': '2024-03-28',
|
|
337
|
+
'Content-Type': 'application/json'
|
|
338
|
+
}
|
|
60
339
|
});
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.project._id = schemas.data.data[0].projectId;
|
|
340
|
+
|
|
341
|
+
case 3:
|
|
342
|
+
response = _context.sent;
|
|
343
|
+
this.openApiDoc = response.data;
|
|
344
|
+
|
|
345
|
+
if ((_this$openApiDoc = this.openApiDoc) != null && (_this$openApiDoc$comp = _this$openApiDoc.components) != null && _this$openApiDoc$comp.schemas) {
|
|
346
|
+
this.schemas = this.openApiDoc.components.schemas;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
this.project._id = this.options.projectId;
|
|
350
|
+
this.project.name = ((_this$openApiDoc2 = this.openApiDoc) == null ? void 0 : (_this$openApiDoc2$inf = _this$openApiDoc2.info) == null ? void 0 : _this$openApiDoc2$inf.title) || 'ApifoxProject';
|
|
351
|
+
this.project.desc = ((_this$openApiDoc3 = this.openApiDoc) == null ? void 0 : (_this$openApiDoc3$inf = _this$openApiDoc3.info) == null ? void 0 : _this$openApiDoc3$inf.description) || '';
|
|
74
352
|
return _context.abrupt("return", _extends({}, this.project, {
|
|
75
353
|
cats: [],
|
|
76
354
|
getMockUrl: function getMockUrl() {
|
|
@@ -97,253 +375,176 @@ export var ApifoxToYApiData = /*#__PURE__*/function () {
|
|
|
97
375
|
}
|
|
98
376
|
|
|
99
377
|
return getProjectInfo;
|
|
100
|
-
}()
|
|
101
|
-
/**
|
|
102
|
-
* 根据筛选后的权限目录,对当前folder进行筛选
|
|
103
|
-
* 如果传入的cats为空,表明获取所有
|
|
104
|
-
*/
|
|
105
|
-
;
|
|
378
|
+
}();
|
|
106
379
|
|
|
107
|
-
_proto.
|
|
108
|
-
var
|
|
380
|
+
_proto.getTagId = function getTagId(tagName) {
|
|
381
|
+
var hash = 0;
|
|
109
382
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
383
|
+
for (var i = 0; i < tagName.length; i++) {
|
|
384
|
+
var _char = tagName.charCodeAt(i);
|
|
113
385
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (cats.length && !cats.includes(item.folder.id)) return;
|
|
118
|
-
folders.push(item.folder.id);
|
|
386
|
+
hash = (hash << 5) - hash + _char;
|
|
387
|
+
hash = hash & hash;
|
|
388
|
+
}
|
|
119
389
|
|
|
120
|
-
|
|
121
|
-
folders.push.apply(folders, _this2.getValidCat(cats, item.children));
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
return folders;
|
|
390
|
+
return Math.abs(hash);
|
|
125
391
|
}
|
|
126
392
|
/**
|
|
127
|
-
*
|
|
393
|
+
* 从 OpenAPI tags 中获取目录对应的 tag
|
|
394
|
+
* 如果 x-apifox-folder 在 tags 中存在,则返回对应的 tag;否则返回 x-apifox-folder 本身
|
|
128
395
|
*/
|
|
129
396
|
;
|
|
130
397
|
|
|
131
|
-
_proto.
|
|
132
|
-
var
|
|
398
|
+
_proto.getMatchingTag = function getMatchingTag(xApifoxFolder) {
|
|
399
|
+
var _this$openApiDoc4;
|
|
133
400
|
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* 通过目录的ID查找目录
|
|
138
|
-
*/
|
|
139
|
-
;
|
|
401
|
+
if (!((_this$openApiDoc4 = this.openApiDoc) != null && _this$openApiDoc4.tags)) return null; // 先精确匹配
|
|
140
402
|
|
|
141
|
-
|
|
142
|
-
|
|
403
|
+
var exactMatch = this.openApiDoc.tags.find(function (tag) {
|
|
404
|
+
return tag.name === xApifoxFolder;
|
|
405
|
+
});
|
|
406
|
+
if (exactMatch) return exactMatch.name; // 如果 addFoldersToTags=true,可能目录名被添加到 tag 描述或其他地方
|
|
407
|
+
// 或者 tag.name 本身就包含了目录路径
|
|
143
408
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
409
|
+
return null;
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
_proto.getCats = function getCats(cats) {
|
|
413
|
+
var _this2 = this;
|
|
147
414
|
|
|
148
|
-
var
|
|
149
|
-
folders.forEach(function (item) {
|
|
150
|
-
if (folder) return;
|
|
415
|
+
var categoryIds = getFilteredCat(cats, [], true);
|
|
151
416
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
417
|
+
if (categoryIds.length === 0 || categoryIds.includes(0)) {
|
|
418
|
+
var _this$openApiDoc5;
|
|
156
419
|
|
|
157
|
-
if (
|
|
158
|
-
|
|
420
|
+
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
|
+
return [];
|
|
159
423
|
}
|
|
160
|
-
});
|
|
161
|
-
return folder;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* 由于yapi中基本没有处理ref的方案
|
|
165
|
-
* 都是直接由具体的参数值管理,所以这里需要对所有的ref进行转换
|
|
166
|
-
*
|
|
167
|
-
* 在apifox中,对于cat来说,cat的children有可能是folder,对于folder,我们要过滤掉
|
|
168
|
-
*/
|
|
169
|
-
;
|
|
170
424
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
425
|
+
var tagIds = this.openApiDoc.tags.map(function (tag) {
|
|
426
|
+
return _this2.getTagId(tag.name);
|
|
427
|
+
});
|
|
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
|
+
return tagIds;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
console.log("\uD83D\uDCC1 getCats: \u8FD4\u56DE " + categoryIds.length + " \u4E2A\u5206\u7C7B: " + categoryIds.join(', '));
|
|
436
|
+
return categoryIds;
|
|
437
|
+
};
|
|
176
438
|
|
|
177
|
-
|
|
178
|
-
|
|
439
|
+
_proto.getInterfaceList = /*#__PURE__*/function () {
|
|
440
|
+
var _getInterfaceList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(syntheticalConfig) {
|
|
441
|
+
var _this$openApiDoc6,
|
|
442
|
+
_this$openApiDoc7,
|
|
443
|
+
_this3 = this;
|
|
444
|
+
|
|
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;
|
|
446
|
+
|
|
447
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
179
448
|
while (1) {
|
|
180
|
-
switch (
|
|
449
|
+
switch (_context2.prev = _context2.next) {
|
|
181
450
|
case 0:
|
|
182
|
-
|
|
183
|
-
|
|
451
|
+
if ((_this$openApiDoc6 = this.openApiDoc) != null && _this$openApiDoc6.paths) {
|
|
452
|
+
_context2.next = 2;
|
|
453
|
+
break;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return _context2.abrupt("return", []);
|
|
457
|
+
|
|
458
|
+
case 2:
|
|
459
|
+
catId = syntheticalConfig.id;
|
|
460
|
+
interfaces = []; // 用于去重:基于 path + method
|
|
461
|
+
|
|
462
|
+
seenKeys = new Set(); // 调试:获取 tag 名称
|
|
463
|
+
|
|
464
|
+
tagName = 'unknown';
|
|
184
465
|
|
|
185
|
-
if (
|
|
186
|
-
|
|
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
|
+
methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'];
|
|
475
|
+
_i3 = 0, _Object$entries3 = Object.entries(this.openApiDoc.paths);
|
|
476
|
+
|
|
477
|
+
case 10:
|
|
478
|
+
if (!(_i3 < _Object$entries3.length)) {
|
|
479
|
+
_context2.next = 34;
|
|
187
480
|
break;
|
|
188
481
|
}
|
|
189
482
|
|
|
190
|
-
|
|
483
|
+
_Object$entries3$_i = _Object$entries3[_i3], path = _Object$entries3$_i[0], pathItem = _Object$entries3$_i[1];
|
|
484
|
+
_iterator = _createForOfIteratorHelperLoose(methods);
|
|
191
485
|
|
|
192
|
-
case
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
// 处理request中可能存在的ref
|
|
251
|
-
if ((_resultData$requestBo = resultData.requestBody.jsonSchema) != null && _resultData$requestBo['x-apifox-refs'] && (_resultData$requestBo2 = resultData.requestBody.jsonSchema) != null && _resultData$requestBo2['x-apifox-orders']) {
|
|
252
|
-
// @ts-ignore
|
|
253
|
-
reqref = resultData.requestBody.jsonSchema['x-apifox-refs'][resultData.requestBody.jsonSchema['x-apifox-orders'][0]];
|
|
254
|
-
resultData.requestBody.jsonSchema.properties = transformRefToSchema(reqref['$ref']).properties; // @ts-ignore
|
|
255
|
-
|
|
256
|
-
// @ts-ignore
|
|
257
|
-
resultData.requestBody.jsonSchema['required'] = reqref.required;
|
|
258
|
-
delete resultData.requestBody.jsonSchema['x-apifox-refs'];
|
|
259
|
-
delete resultData.requestBody.jsonSchema['x-apifox-orders'];
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return _context2.abrupt("return", {
|
|
263
|
-
_id: resultData.id,
|
|
264
|
-
_category: {
|
|
265
|
-
_id: syntheticalConfig.id,
|
|
266
|
-
_url: '',
|
|
267
|
-
name: cat.name,
|
|
268
|
-
desc: '',
|
|
269
|
-
add_time: 0,
|
|
270
|
-
up_time: 0,
|
|
271
|
-
list: []
|
|
272
|
-
},
|
|
273
|
-
_project: _extends({}, _this4.project),
|
|
274
|
-
_url: '',
|
|
275
|
-
title: resultData.name,
|
|
276
|
-
status: 'undone',
|
|
277
|
-
markdown: resultData.operationId,
|
|
278
|
-
path: resultData.path,
|
|
279
|
-
method: resultData.method.toUpperCase(),
|
|
280
|
-
project_id: resultData.projectId,
|
|
281
|
-
catid: syntheticalConfig.id,
|
|
282
|
-
tag: resultData.tags,
|
|
283
|
-
req_headers: resultData.requestBody.type ? [{
|
|
284
|
-
name: 'Content-Type',
|
|
285
|
-
value: resultData.requestBody.type,
|
|
286
|
-
desc: '',
|
|
287
|
-
example: '',
|
|
288
|
-
required: Required.true
|
|
289
|
-
}] : [],
|
|
290
|
-
req_params: ((_resultData$parameter = resultData.parameters.path) == null ? void 0 : _resultData$parameter.map(function (item) {
|
|
291
|
-
return {
|
|
292
|
-
name: item.name,
|
|
293
|
-
desc: item.description,
|
|
294
|
-
example: '',
|
|
295
|
-
required: item.required ? Required.true : Required.false,
|
|
296
|
-
type: item.type === 'string' ? RequestParamType.string : RequestParamType.number
|
|
297
|
-
};
|
|
298
|
-
})) || [],
|
|
299
|
-
req_query: ((_resultData$parameter2 = resultData.parameters.query) == null ? void 0 : _resultData$parameter2.map(function (item) {
|
|
300
|
-
return {
|
|
301
|
-
name: item.name,
|
|
302
|
-
desc: item.description,
|
|
303
|
-
example: '',
|
|
304
|
-
required: item.required ? Required.true : Required.false,
|
|
305
|
-
type: item.type === 'string' ? RequestQueryType.string : RequestQueryType.number
|
|
306
|
-
};
|
|
307
|
-
})) || [],
|
|
308
|
-
req_body_type: resultData.requestBody.type === 'multipart/form-data' ? RequestBodyType.form : resultData.requestBody.jsonSchema ? RequestBodyType.json : RequestBodyType.raw,
|
|
309
|
-
req_body_is_json_schema: !!resultData.requestBody.jsonSchema,
|
|
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
|
-
})) || [],
|
|
319
|
-
req_body_other: resultData.requestBody.jsonSchema ? JSON.stringify(resultData.requestBody.jsonSchema) : '',
|
|
320
|
-
res_body_type: ResponseBodyType.json,
|
|
321
|
-
res_body_is_json_schema: true,
|
|
322
|
-
res_body: JSON.stringify(resultData.responses[resbodyIndex].jsonSchema),
|
|
323
|
-
add_time: Math.floor(new Date(resultData.createdAt).getTime() / 1000),
|
|
324
|
-
up_time: Math.floor(new Date(resultData.updatedAt).getTime() / 1000),
|
|
325
|
-
uid: 0
|
|
326
|
-
});
|
|
327
|
-
|
|
328
|
-
case 11:
|
|
329
|
-
case "end":
|
|
330
|
-
return _context2.stop();
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}, _callee2);
|
|
334
|
-
}));
|
|
335
|
-
|
|
336
|
-
return function (_x2) {
|
|
337
|
-
return _ref.apply(this, arguments);
|
|
338
|
-
};
|
|
339
|
-
}())));
|
|
340
|
-
|
|
341
|
-
case 5:
|
|
486
|
+
case 13:
|
|
487
|
+
if ((_step = _iterator()).done) {
|
|
488
|
+
_context2.next = 31;
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
method = _step.value;
|
|
493
|
+
operation = pathItem[method];
|
|
494
|
+
|
|
495
|
+
if (operation) {
|
|
496
|
+
_context2.next = 18;
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return _context2.abrupt("continue", 29);
|
|
501
|
+
|
|
502
|
+
case 18:
|
|
503
|
+
// 获取接口的目录信息:优先使用 x-apifox-folder
|
|
504
|
+
xApifoxFolder = this.getOperationFolder(operation);
|
|
505
|
+
tagId = this.getTagId(xApifoxFolder);
|
|
506
|
+
|
|
507
|
+
if (!(tagId === catId)) {
|
|
508
|
+
_context2.next = 29;
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// 去重:基于 path + method
|
|
513
|
+
key = method.toUpperCase() + ":" + path;
|
|
514
|
+
|
|
515
|
+
if (!seenKeys.has(key)) {
|
|
516
|
+
_context2.next = 25;
|
|
517
|
+
break;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
console.log(" \u26A0\uFE0F \u8DF3\u8FC7\u91CD\u590D\u63A5\u53E3: " + key);
|
|
521
|
+
return _context2.abrupt("continue", 29);
|
|
522
|
+
|
|
523
|
+
case 25:
|
|
524
|
+
seenKeys.add(key); // 根据实际的目录名称来确定分类
|
|
525
|
+
|
|
526
|
+
categoryTag = xApifoxFolder;
|
|
527
|
+
interfaceInfo = this.convertOperationToInterface(path, method, operation, categoryTag, catId);
|
|
528
|
+
interfaces.push(interfaceInfo);
|
|
529
|
+
|
|
530
|
+
case 29:
|
|
531
|
+
_context2.next = 13;
|
|
532
|
+
break;
|
|
533
|
+
|
|
534
|
+
case 31:
|
|
535
|
+
_i3++;
|
|
536
|
+
_context2.next = 10;
|
|
537
|
+
break;
|
|
538
|
+
|
|
539
|
+
case 34:
|
|
540
|
+
return _context2.abrupt("return", interfaces);
|
|
541
|
+
|
|
542
|
+
case 35:
|
|
342
543
|
case "end":
|
|
343
|
-
return
|
|
544
|
+
return _context2.stop();
|
|
344
545
|
}
|
|
345
546
|
}
|
|
346
|
-
},
|
|
547
|
+
}, _callee2, this);
|
|
347
548
|
}));
|
|
348
549
|
|
|
349
550
|
function getInterfaceList(_x) {
|