yapi-apifox-to-typescript 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/lib/cjs/ApifoxToYApiData.js +299 -0
- package/lib/cjs/Generator.js +1202 -0
- package/lib/cjs/SwaggerToYApiServer.js +336 -0
- package/lib/cjs/cli.js +285 -0
- package/lib/cjs/helpers.js +218 -0
- package/lib/cjs/index.js +19 -0
- package/lib/cjs/swaggerJsonToYApiData.js +496 -0
- package/lib/cjs/types.js +100 -0
- package/lib/cjs/utils.js +766 -0
- package/lib/esm/ApifoxToYApiData.js +285 -0
- package/lib/esm/Generator.js +1177 -0
- package/lib/esm/SwaggerToYApiServer.js +317 -0
- package/lib/esm/cli.js +257 -0
- package/lib/esm/helpers.js +204 -0
- package/lib/esm/index.d.ts +867 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/swaggerJsonToYApiData.js +483 -0
- package/lib/esm/types.js +79 -0
- package/lib/esm/utils.js +707 -0
- package/package.json +108 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
4
|
+
import axios from 'axios';
|
|
5
|
+
import { RequestBodyType, RequestParamType, RequestQueryType, Required, ResponseBodyType } from './types';
|
|
6
|
+
export var ApifoxToYApiData = /*#__PURE__*/function () {
|
|
7
|
+
function ApifoxToYApiData(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
this.schemas = void 0;
|
|
10
|
+
this.folders = void 0;
|
|
11
|
+
this.project = {
|
|
12
|
+
_id: 0,
|
|
13
|
+
_url: '',
|
|
14
|
+
name: 'ApifoxProject',
|
|
15
|
+
desc: '',
|
|
16
|
+
basepath: '',
|
|
17
|
+
tag: [],
|
|
18
|
+
env: [{
|
|
19
|
+
name: '',
|
|
20
|
+
domain: ''
|
|
21
|
+
}]
|
|
22
|
+
};
|
|
23
|
+
this.schemas = {};
|
|
24
|
+
this.folders = [];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* http-api-tree 这个接口拿到所有的folder(category)
|
|
28
|
+
* data-schemas 这个接口拿到所用通用的schema
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
var _proto = ApifoxToYApiData.prototype;
|
|
33
|
+
|
|
34
|
+
_proto.getProjectInfo =
|
|
35
|
+
/*#__PURE__*/
|
|
36
|
+
function () {
|
|
37
|
+
var _getProjectInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
38
|
+
var _this = this;
|
|
39
|
+
|
|
40
|
+
var folders, schemas, cats;
|
|
41
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
42
|
+
while (1) {
|
|
43
|
+
switch (_context.prev = _context.next) {
|
|
44
|
+
case 0:
|
|
45
|
+
_context.next = 2;
|
|
46
|
+
return axios.get("https://apifox.com/api/v1/shared-docs/" + this.options.token + "/http-api-tree");
|
|
47
|
+
|
|
48
|
+
case 2:
|
|
49
|
+
folders = _context.sent;
|
|
50
|
+
_context.next = 5;
|
|
51
|
+
return axios.get("https://apifox.com/api/v1/shared-docs/" + this.options.token + "/data-schemas");
|
|
52
|
+
|
|
53
|
+
case 5:
|
|
54
|
+
schemas = _context.sent;
|
|
55
|
+
// 初始化
|
|
56
|
+
// this.schemas = schemas.data.data
|
|
57
|
+
schemas.data.data.forEach(function (item) {
|
|
58
|
+
_this.schemas[item.id] = item.jsonSchema;
|
|
59
|
+
});
|
|
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
|
+
});
|
|
72
|
+
this.project._id = schemas.data.data[0].projectId;
|
|
73
|
+
return _context.abrupt("return", _extends({}, this.project, {
|
|
74
|
+
cats: cats,
|
|
75
|
+
getMockUrl: function getMockUrl() {
|
|
76
|
+
return '';
|
|
77
|
+
},
|
|
78
|
+
getDevUrl: function getDevUrl() {
|
|
79
|
+
return '';
|
|
80
|
+
},
|
|
81
|
+
getProdUrl: function getProdUrl() {
|
|
82
|
+
return '';
|
|
83
|
+
}
|
|
84
|
+
}));
|
|
85
|
+
|
|
86
|
+
case 11:
|
|
87
|
+
case "end":
|
|
88
|
+
return _context.stop();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}, _callee, this);
|
|
92
|
+
}));
|
|
93
|
+
|
|
94
|
+
function getProjectInfo() {
|
|
95
|
+
return _getProjectInfo.apply(this, arguments);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return getProjectInfo;
|
|
99
|
+
}()
|
|
100
|
+
/**
|
|
101
|
+
* 由于yapi中基本没有处理ref的方案
|
|
102
|
+
* 都是直接由具体的参数值管理,所以这里需要对所有的ref进行转换
|
|
103
|
+
* TODO:目前这里只考虑了一个层级,如果是多个层级,需要再递归处理
|
|
104
|
+
*/
|
|
105
|
+
;
|
|
106
|
+
|
|
107
|
+
_proto.getInterfaceList =
|
|
108
|
+
/*#__PURE__*/
|
|
109
|
+
function () {
|
|
110
|
+
var _getInterfaceList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(syntheticalConfig) {
|
|
111
|
+
var _this2 = this;
|
|
112
|
+
|
|
113
|
+
var cat, transformRefToSchema;
|
|
114
|
+
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
115
|
+
while (1) {
|
|
116
|
+
switch (_context3.prev = _context3.next) {
|
|
117
|
+
case 0:
|
|
118
|
+
// syntheticalConfig.id 就是catId
|
|
119
|
+
cat = this.folders.find(function (item) {
|
|
120
|
+
return item.folder.id === syntheticalConfig.id;
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
if (cat) {
|
|
124
|
+
_context3.next = 3;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return _context3.abrupt("return", []);
|
|
129
|
+
|
|
130
|
+
case 3:
|
|
131
|
+
transformRefToSchema = function transformRefToSchema(refString) {
|
|
132
|
+
var refId = refString.split('/').pop();
|
|
133
|
+
return _this2.schemas[refId];
|
|
134
|
+
};
|
|
135
|
+
|
|
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;
|
|
139
|
+
|
|
140
|
+
var result, resultData, resbodyIndex, respref, dataProperties, _item, _dataProperties$_item, dataPropRef, reqref;
|
|
141
|
+
|
|
142
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
143
|
+
while (1) {
|
|
144
|
+
switch (_context2.prev = _context2.next) {
|
|
145
|
+
case 0:
|
|
146
|
+
_context2.next = 2;
|
|
147
|
+
return axios.get("https://apifox.com/api/v1/shared-docs/" + _this2.options.token + "/http-apis/" + item.api.id);
|
|
148
|
+
|
|
149
|
+
case 2:
|
|
150
|
+
result = _context2.sent;
|
|
151
|
+
resultData = result.data.data; // 根据result对应的param查询制定的schema
|
|
152
|
+
|
|
153
|
+
// 根据result对应的param查询制定的schema
|
|
154
|
+
resbodyIndex = resultData.responses.findIndex(function (item) {
|
|
155
|
+
return item.code === 200;
|
|
156
|
+
}); // 处理response重可能存在ref
|
|
157
|
+
|
|
158
|
+
// 处理response重可能存在ref
|
|
159
|
+
respref = (_resultData$responses = resultData.responses[resbodyIndex].jsonSchema) == null ? void 0 : (_resultData$responses2 = _resultData$responses.properties) == null ? void 0 : (_resultData$responses3 = _resultData$responses2.data) == null ? void 0 : (_resultData$responses4 = _resultData$responses3.items) == null ? void 0 : _resultData$responses4.$ref;
|
|
160
|
+
|
|
161
|
+
if (respref) {
|
|
162
|
+
// @ts-ignore
|
|
163
|
+
resultData.responses[resbodyIndex].jsonSchema.properties.data.items = transformRefToSchema(respref);
|
|
164
|
+
} // 处理data中可能存在ref的问题
|
|
165
|
+
// 这里只考虑了一层,如果存在多层,需要递归处理
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
// 处理data中可能存在ref的问题
|
|
169
|
+
// 这里只考虑了一层,如果存在多层,需要递归处理
|
|
170
|
+
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
|
+
|
|
172
|
+
if (dataProperties) {
|
|
173
|
+
for (_item in dataProperties) {
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
dataPropRef = (_dataProperties$_item = dataProperties[_item].items) == null ? void 0 : _dataProperties$_item['$ref']; // @ts-ignore
|
|
176
|
+
|
|
177
|
+
// @ts-ignore
|
|
178
|
+
if (dataProperties[_item].type === 'array' && dataPropRef) {
|
|
179
|
+
// @ts-ignore
|
|
180
|
+
resultData.responses[resbodyIndex].jsonSchema.properties.data.properties[_item].items = transformRefToSchema(dataPropRef);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
} // 处理request中可能存在的ref
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
// 处理request中可能存在的ref
|
|
187
|
+
if ((_resultData$requestBo = resultData.requestBody.jsonSchema) != null && _resultData$requestBo['x-apifox-refs'] && (_resultData$requestBo2 = resultData.requestBody.jsonSchema) != null && _resultData$requestBo2['x-apifox-orders']) {
|
|
188
|
+
// @ts-ignore
|
|
189
|
+
reqref = resultData.requestBody.jsonSchema['x-apifox-refs'][resultData.requestBody.jsonSchema['x-apifox-orders'][0]];
|
|
190
|
+
resultData.requestBody.jsonSchema.properties = transformRefToSchema(reqref['$ref']).properties; // @ts-ignore
|
|
191
|
+
|
|
192
|
+
// @ts-ignore
|
|
193
|
+
resultData.requestBody.jsonSchema['required'] = reqref.required;
|
|
194
|
+
delete resultData.requestBody.jsonSchema['x-apifox-refs'];
|
|
195
|
+
delete resultData.requestBody.jsonSchema['x-apifox-orders'];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return _context2.abrupt("return", {
|
|
199
|
+
_id: resultData.id,
|
|
200
|
+
_category: {
|
|
201
|
+
_id: syntheticalConfig.id,
|
|
202
|
+
_url: '',
|
|
203
|
+
name: cat.name,
|
|
204
|
+
desc: '',
|
|
205
|
+
add_time: 0,
|
|
206
|
+
up_time: 0,
|
|
207
|
+
list: []
|
|
208
|
+
},
|
|
209
|
+
_project: _extends({}, _this2.project),
|
|
210
|
+
_url: '',
|
|
211
|
+
title: resultData.name,
|
|
212
|
+
status: 'undone',
|
|
213
|
+
markdown: resultData.operationId,
|
|
214
|
+
path: resultData.path,
|
|
215
|
+
method: resultData.method.toUpperCase(),
|
|
216
|
+
project_id: resultData.projectId,
|
|
217
|
+
catid: syntheticalConfig.id,
|
|
218
|
+
tag: resultData.tags,
|
|
219
|
+
req_headers: resultData.requestBody.type ? [{
|
|
220
|
+
name: 'Content-Type',
|
|
221
|
+
value: resultData.requestBody.type,
|
|
222
|
+
desc: '',
|
|
223
|
+
example: '',
|
|
224
|
+
required: Required.true
|
|
225
|
+
}] : [],
|
|
226
|
+
req_params: ((_resultData$parameter = resultData.parameters.path) == null ? void 0 : _resultData$parameter.map(function (item) {
|
|
227
|
+
return {
|
|
228
|
+
name: item.name,
|
|
229
|
+
desc: item.description,
|
|
230
|
+
example: '',
|
|
231
|
+
required: item.required ? Required.true : Required.false,
|
|
232
|
+
type: item.type === 'string' ? RequestParamType.string : RequestParamType.number
|
|
233
|
+
};
|
|
234
|
+
})) || [],
|
|
235
|
+
req_query: ((_resultData$parameter2 = resultData.parameters.query) == null ? void 0 : _resultData$parameter2.map(function (item) {
|
|
236
|
+
return {
|
|
237
|
+
name: item.name,
|
|
238
|
+
desc: item.description,
|
|
239
|
+
example: '',
|
|
240
|
+
required: item.required ? Required.true : Required.false,
|
|
241
|
+
type: item.type === 'string' ? RequestQueryType.string : RequestQueryType.number
|
|
242
|
+
};
|
|
243
|
+
})) || [],
|
|
244
|
+
req_body_type: resultData.requestBody.jsonSchema ? RequestBodyType.json : RequestBodyType.raw,
|
|
245
|
+
req_body_is_json_schema: !!resultData.requestBody.jsonSchema,
|
|
246
|
+
req_body_form: [],
|
|
247
|
+
req_body_other: resultData.requestBody.jsonSchema ? JSON.stringify(resultData.requestBody.jsonSchema) : '',
|
|
248
|
+
res_body_type: ResponseBodyType.json,
|
|
249
|
+
res_body_is_json_schema: true,
|
|
250
|
+
res_body: JSON.stringify(resultData.responses[resbodyIndex].jsonSchema),
|
|
251
|
+
add_time: Math.floor(new Date(resultData.createdAt).getTime() / 1000),
|
|
252
|
+
up_time: Math.floor(new Date(resultData.updatedAt).getTime() / 1000),
|
|
253
|
+
uid: 0
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
case 11:
|
|
257
|
+
case "end":
|
|
258
|
+
return _context2.stop();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}, _callee2);
|
|
262
|
+
}));
|
|
263
|
+
|
|
264
|
+
return function (_x2) {
|
|
265
|
+
return _ref.apply(this, arguments);
|
|
266
|
+
};
|
|
267
|
+
}())));
|
|
268
|
+
|
|
269
|
+
case 5:
|
|
270
|
+
case "end":
|
|
271
|
+
return _context3.stop();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}, _callee3, this);
|
|
275
|
+
}));
|
|
276
|
+
|
|
277
|
+
function getInterfaceList(_x) {
|
|
278
|
+
return _getInterfaceList.apply(this, arguments);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return getInterfaceList;
|
|
282
|
+
}();
|
|
283
|
+
|
|
284
|
+
return ApifoxToYApiData;
|
|
285
|
+
}();
|