yapi-to-typescript2 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,496 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ exports.__esModule = true;
6
+ exports.swaggerJsonToYApiData = swaggerJsonToYApiData;
7
+
8
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
+
10
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _dayjs = _interopRequireDefault(require("dayjs"));
15
+
16
+ var _swaggerClient = _interopRequireDefault(require("swagger-client"));
17
+
18
+ var _vtils = require("vtils");
19
+
20
+ // @ts-nocheck
21
+ // ref: https://github.com/YMFE/yapi/blob/master/exts/yapi-plugin-import-swagger/run.js
22
+ var SwaggerData, isOAS3;
23
+
24
+ function handlePath(path) {
25
+ if (path === '/') return path;
26
+
27
+ if (path.charAt(0) !== '/') {
28
+ path = "/" + path;
29
+ }
30
+
31
+ if (path.charAt(path.length - 1) === '/') {
32
+ path = path.substr(0, path.length - 1);
33
+ }
34
+
35
+ return path;
36
+ }
37
+
38
+ function openapi2swagger(data) {
39
+ data.swagger = '2.0';
40
+ (0, _vtils.each)(data.paths, function (apis) {
41
+ (0, _vtils.each)(apis, function (api) {
42
+ (0, _vtils.each)(api.responses, function (res) {
43
+ if (res.content && res.content['application/json'] && typeof res.content['application/json'] === 'object') {
44
+ Object.assign(res, res.content['application/json']);
45
+ delete res.content;
46
+ }
47
+
48
+ if (res.content && res.content['application/hal+json'] && typeof res.content['application/hal+json'] === 'object') {
49
+ Object.assign(res, res.content['application/hal+json']);
50
+ delete res.content;
51
+ }
52
+
53
+ if (res.content && res.content['*/*'] && typeof res.content['*/*'] === 'object') {
54
+ Object.assign(res, res.content['*/*']);
55
+ delete res.content;
56
+ }
57
+ });
58
+
59
+ if (api.requestBody) {
60
+ if (!api.parameters) api.parameters = [];
61
+ var body = {
62
+ type: 'object',
63
+ name: 'body',
64
+ in: 'body'
65
+ };
66
+
67
+ try {
68
+ body.schema = api.requestBody.content['application/json'].schema;
69
+ } catch (e) {
70
+ body.schema = {};
71
+ }
72
+
73
+ api.parameters.push(body);
74
+ }
75
+ });
76
+ });
77
+ return data;
78
+ }
79
+
80
+ function handleSwaggerData(_x) {
81
+ return _handleSwaggerData.apply(this, arguments);
82
+ }
83
+
84
+ function _handleSwaggerData() {
85
+ _handleSwaggerData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(res) {
86
+ return _regenerator.default.wrap(function _callee$(_context) {
87
+ while (1) {
88
+ switch (_context.prev = _context.next) {
89
+ case 0:
90
+ return _context.abrupt("return", new Promise(function (resolve) {
91
+ var data = (0, _swaggerClient.default)({
92
+ spec: res
93
+ });
94
+ data.then(function (res) {
95
+ resolve(res.spec);
96
+ });
97
+ }));
98
+
99
+ case 1:
100
+ case "end":
101
+ return _context.stop();
102
+ }
103
+ }
104
+ }, _callee);
105
+ }));
106
+ return _handleSwaggerData.apply(this, arguments);
107
+ }
108
+
109
+ function run(_x2) {
110
+ return _run.apply(this, arguments);
111
+ }
112
+
113
+ function _run() {
114
+ _run = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(res) {
115
+ var interfaceData;
116
+ return _regenerator.default.wrap(function _callee2$(_context2) {
117
+ while (1) {
118
+ switch (_context2.prev = _context2.next) {
119
+ case 0:
120
+ interfaceData = {
121
+ apis: [],
122
+ cats: [],
123
+ basePath: '',
124
+ swaggerData: {}
125
+ };
126
+
127
+ if (typeof res === 'string' && res) {
128
+ try {
129
+ res = JSON.parse(res);
130
+ } catch (e) {
131
+ console.error('json 解析出错', e.message);
132
+ }
133
+ }
134
+
135
+ isOAS3 = res.openapi && String(res.openapi).startsWith('3.');
136
+
137
+ if (isOAS3) {
138
+ res = openapi2swagger(res);
139
+ }
140
+
141
+ _context2.next = 6;
142
+ return handleSwaggerData(res);
143
+
144
+ case 6:
145
+ res = _context2.sent;
146
+ SwaggerData = res;
147
+ interfaceData.swaggerData = SwaggerData;
148
+ interfaceData.basePath = res.basePath || '';
149
+
150
+ if (res.tags && Array.isArray(res.tags)) {
151
+ res.tags.forEach(function (tag) {
152
+ interfaceData.cats.push({
153
+ name: tag.name,
154
+ desc: tag.description
155
+ });
156
+ });
157
+ } else {
158
+ res.tags = [];
159
+ }
160
+
161
+ (0, _vtils.each)(res.paths, function (apis, path) {
162
+ // parameters is common parameters, not a method
163
+ delete apis.parameters;
164
+ (0, _vtils.each)(apis, function (api, method) {
165
+ api.path = path;
166
+ api.method = method;
167
+ var data = null;
168
+
169
+ try {
170
+ data = handleSwagger(api, res.tags);
171
+
172
+ if (data.catname) {
173
+ if (!(0, _vtils.find)(interfaceData.cats, function (item) {
174
+ return item.name === data.catname;
175
+ })) {
176
+ if (res.tags.length === 0) {
177
+ interfaceData.cats.push({
178
+ name: data.catname,
179
+ desc: data.catname
180
+ });
181
+ }
182
+ }
183
+ }
184
+ } catch (err) {
185
+ data = null;
186
+ }
187
+
188
+ if (data) {
189
+ interfaceData.apis.push(data);
190
+ }
191
+ });
192
+ });
193
+ interfaceData.cats = interfaceData.cats.filter(function (catData) {
194
+ var catName = catData.name;
195
+ return (0, _vtils.find)(interfaceData.apis, function (apiData) {
196
+ return apiData.catname === catName;
197
+ });
198
+ });
199
+ return _context2.abrupt("return", interfaceData);
200
+
201
+ case 14:
202
+ case "end":
203
+ return _context2.stop();
204
+ }
205
+ }
206
+ }, _callee2);
207
+ }));
208
+ return _run.apply(this, arguments);
209
+ }
210
+
211
+ function handleSwagger(data, originTags) {
212
+ if (originTags === void 0) {
213
+ originTags = [];
214
+ }
215
+
216
+ var api = {}; //处理基本信息
217
+
218
+ api.method = data.method.toUpperCase();
219
+ api.title = data.summary || data.path;
220
+ api.desc = data.description;
221
+ api.catname = null;
222
+
223
+ if (data.tags && Array.isArray(data.tags)) {
224
+ api.tag = data.tags;
225
+
226
+ var _loop = function _loop(i) {
227
+ if (/v[0-9.]+/.test(data.tags[i])) {
228
+ return "continue";
229
+ } // 如果根路径有 tags,使用根路径 tags,不使用每个接口定义的 tag 做完分类
230
+
231
+
232
+ if (originTags.length > 0 && (0, _vtils.find)(originTags, function (item) {
233
+ return item.name === data.tags[i];
234
+ })) {
235
+ api.catname = data.tags[i];
236
+ return "break";
237
+ }
238
+
239
+ if (originTags.length === 0) {
240
+ api.catname = data.tags[i];
241
+ return "break";
242
+ }
243
+ };
244
+
245
+ for (var i = 0; i < data.tags.length; i++) {
246
+ var _ret = _loop(i);
247
+
248
+ if (_ret === "continue") continue;
249
+ if (_ret === "break") break;
250
+ }
251
+ }
252
+
253
+ api.path = handlePath(data.path);
254
+ api.req_params = [];
255
+ api.req_body_form = [];
256
+ api.req_headers = [];
257
+ api.req_query = [];
258
+ api.req_body_type = 'raw';
259
+ api.res_body_type = 'raw';
260
+
261
+ if (data.produces && data.produces.indexOf('application/json') > -1) {
262
+ api.res_body_type = 'json';
263
+ api.res_body_is_json_schema = true;
264
+ }
265
+
266
+ if (data.consumes && Array.isArray(data.consumes)) {
267
+ if (data.consumes.indexOf('application/x-www-form-urlencoded') > -1 || data.consumes.indexOf('multipart/form-data') > -1) {
268
+ api.req_body_type = 'form';
269
+ } else if (data.consumes.indexOf('application/json') > -1) {
270
+ api.req_body_type = 'json';
271
+ api.req_body_is_json_schema = true;
272
+ }
273
+ } //处理response
274
+
275
+
276
+ api.res_body = handleResponse(data.responses);
277
+
278
+ try {
279
+ JSON.parse(api.res_body);
280
+ api.res_body_type = 'json';
281
+ api.res_body_is_json_schema = true;
282
+ } catch (e) {
283
+ api.res_body_type = 'raw';
284
+ } //处理参数
285
+
286
+
287
+ function simpleJsonPathParse(key, json) {
288
+ if (!key || typeof key !== 'string' || key.indexOf('#/') !== 0 || key.length <= 2) {
289
+ return null;
290
+ }
291
+
292
+ var keys = key.substr(2).split('/');
293
+ keys = keys.filter(function (item) {
294
+ return item;
295
+ });
296
+
297
+ for (var _i = 0, l = keys.length; _i < l; _i++) {
298
+ try {
299
+ json = json[keys[_i]];
300
+ } catch (e) {
301
+ json = '';
302
+ break;
303
+ }
304
+ }
305
+
306
+ return json;
307
+ }
308
+
309
+ if (data.parameters && Array.isArray(data.parameters)) {
310
+ data.parameters.forEach(function (param) {
311
+ if (param && typeof param === 'object' && param.$ref) {
312
+ param = simpleJsonPathParse(param.$ref, {
313
+ parameters: SwaggerData.parameters
314
+ });
315
+ }
316
+
317
+ var defaultParam = {
318
+ name: param.name,
319
+ desc: param.description,
320
+ required: param.required ? '1' : '0'
321
+ };
322
+
323
+ if (param.in) {
324
+ switch (param.in) {
325
+ case 'path':
326
+ defaultParam.type = param.type;
327
+ api.req_params.push(defaultParam);
328
+ break;
329
+
330
+ case 'query':
331
+ defaultParam.type = param.type;
332
+ api.req_query.push(defaultParam);
333
+ break;
334
+
335
+ case 'body':
336
+ handleBodyPamras(param.schema, api);
337
+ break;
338
+
339
+ case 'formData':
340
+ defaultParam.type = param.type === 'file' ? 'file' : 'text';
341
+
342
+ if (param.example) {
343
+ defaultParam.example = param.example;
344
+ }
345
+
346
+ api.req_body_form.push(defaultParam);
347
+ break;
348
+
349
+ case 'header':
350
+ api.req_headers.push(defaultParam);
351
+ break;
352
+
353
+ default:
354
+ break;
355
+ }
356
+ } else {
357
+ api.req_query.push(defaultParam);
358
+ }
359
+ });
360
+ }
361
+
362
+ return api;
363
+ }
364
+
365
+ function isJson(json) {
366
+ try {
367
+ return JSON.parse(json);
368
+ } catch (e) {
369
+ return false;
370
+ }
371
+ }
372
+
373
+ function handleBodyPamras(data, api) {
374
+ api.req_body_other = JSON.stringify(data, null, 2);
375
+
376
+ if (isJson(api.req_body_other)) {
377
+ api.req_body_type = 'json';
378
+ api.req_body_is_json_schema = true;
379
+ }
380
+ }
381
+
382
+ function handleResponse(api) {
383
+ var res_body = '';
384
+
385
+ if (!api || typeof api !== 'object') {
386
+ return res_body;
387
+ }
388
+
389
+ var codes = Object.keys(api);
390
+ var curCode;
391
+
392
+ if (codes.length > 0) {
393
+ if (codes.indexOf('200') > -1) {
394
+ curCode = '200';
395
+ } else curCode = codes[0];
396
+
397
+ var res = api[curCode];
398
+
399
+ if (res && typeof res === 'object') {
400
+ if (res.schema) {
401
+ res_body = JSON.stringify(res.schema, null, 2);
402
+ } else if (res.description) {
403
+ res_body = res.description;
404
+ }
405
+ } else if (typeof res === 'string') {
406
+ res_body = res;
407
+ } else {
408
+ res_body = '';
409
+ }
410
+ } else {
411
+ res_body = '';
412
+ }
413
+
414
+ return res_body;
415
+ }
416
+
417
+ function swaggerJsonToYApiData(_x3) {
418
+ return _swaggerJsonToYApiData.apply(this, arguments);
419
+ }
420
+
421
+ function _swaggerJsonToYApiData() {
422
+ _swaggerJsonToYApiData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(data) {
423
+ var _yapiData$swaggerData;
424
+
425
+ var yapiData, currentTime, project, cats, interfaces;
426
+ return _regenerator.default.wrap(function _callee3$(_context3) {
427
+ while (1) {
428
+ switch (_context3.prev = _context3.next) {
429
+ case 0:
430
+ _context3.next = 2;
431
+ return run(data);
432
+
433
+ case 2:
434
+ yapiData = _context3.sent;
435
+
436
+ // 兼容没有分类的情况
437
+ if (!yapiData.cats.length) {
438
+ yapiData.cats = [{
439
+ name: 'default',
440
+ desc: 'default'
441
+ }];
442
+ yapiData.apis.forEach(function (api) {
443
+ api.catname = 'default';
444
+ });
445
+ }
446
+
447
+ currentTime = (0, _dayjs.default)().unix();
448
+ project = {
449
+ _id: 0,
450
+ name: yapiData.swaggerData.info.title,
451
+ desc: yapiData.swaggerData.info.description || '',
452
+ basepath: yapiData.swaggerData.basePath || '',
453
+ tag: [],
454
+ env: [{
455
+ name: 'local',
456
+ domain: (((_yapiData$swaggerData = yapiData.swaggerData.schemes) == null ? void 0 : _yapiData$swaggerData[0]) || 'http') + "://" + (yapiData.swaggerData.host || '127.0.0.1')
457
+ }]
458
+ };
459
+ cats = yapiData.cats.map(function (cat, index) {
460
+ return {
461
+ _id: index + 1,
462
+ name: cat.name,
463
+ desc: cat.desc,
464
+ add_time: currentTime,
465
+ up_time: currentTime
466
+ };
467
+ });
468
+ interfaces = yapiData.apis.map(function (api, index) {
469
+ var _cats$find;
470
+
471
+ return (0, _extends2.default)({}, api, {
472
+ _id: index + 1,
473
+ project_id: 0,
474
+ catid: ((_cats$find = cats.find(function (cat) {
475
+ return cat.name === api.catname;
476
+ })) == null ? void 0 : _cats$find._id) || -1,
477
+ tag: api.tag || [],
478
+ add_time: currentTime,
479
+ up_time: currentTime
480
+ });
481
+ });
482
+ return _context3.abrupt("return", {
483
+ project: project,
484
+ cats: cats,
485
+ interfaces: interfaces
486
+ });
487
+
488
+ case 9:
489
+ case "end":
490
+ return _context3.stop();
491
+ }
492
+ }
493
+ }, _callee3);
494
+ }));
495
+ return _swaggerJsonToYApiData.apply(this, arguments);
496
+ }
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.ResponseBodyType = exports.Required = exports.RequestQueryType = exports.RequestParamType = exports.RequestFormItemType = exports.RequestBodyType = exports.QueryStringArrayFormat = exports.Method = void 0;
5
+
6
+ /** 请求方式 */
7
+ var Method;
8
+ /** 是否必需 */
9
+
10
+ exports.Method = Method;
11
+
12
+ (function (Method) {
13
+ Method["GET"] = "GET";
14
+ Method["POST"] = "POST";
15
+ Method["PUT"] = "PUT";
16
+ Method["DELETE"] = "DELETE";
17
+ Method["HEAD"] = "HEAD";
18
+ Method["OPTIONS"] = "OPTIONS";
19
+ Method["PATCH"] = "PATCH";
20
+ })(Method || (exports.Method = Method = {}));
21
+
22
+ var Required;
23
+ /** 请求数据类型 */
24
+
25
+ exports.Required = Required;
26
+
27
+ (function (Required) {
28
+ Required["false"] = "0";
29
+ Required["true"] = "1";
30
+ })(Required || (exports.Required = Required = {}));
31
+
32
+ var RequestBodyType;
33
+ /** 请求路径参数类型 */
34
+
35
+ exports.RequestBodyType = RequestBodyType;
36
+
37
+ (function (RequestBodyType) {
38
+ RequestBodyType["query"] = "query";
39
+ RequestBodyType["form"] = "form";
40
+ RequestBodyType["json"] = "json";
41
+ RequestBodyType["text"] = "text";
42
+ RequestBodyType["file"] = "file";
43
+ RequestBodyType["raw"] = "raw";
44
+ RequestBodyType["none"] = "none";
45
+ })(RequestBodyType || (exports.RequestBodyType = RequestBodyType = {}));
46
+
47
+ var RequestParamType;
48
+ /** 请求查询参数类型 */
49
+
50
+ exports.RequestParamType = RequestParamType;
51
+
52
+ (function (RequestParamType) {
53
+ RequestParamType["string"] = "string";
54
+ RequestParamType["number"] = "number";
55
+ })(RequestParamType || (exports.RequestParamType = RequestParamType = {}));
56
+
57
+ var RequestQueryType;
58
+ /** 请求表单条目类型 */
59
+
60
+ exports.RequestQueryType = RequestQueryType;
61
+
62
+ (function (RequestQueryType) {
63
+ RequestQueryType["string"] = "string";
64
+ RequestQueryType["number"] = "number";
65
+ })(RequestQueryType || (exports.RequestQueryType = RequestQueryType = {}));
66
+
67
+ var RequestFormItemType;
68
+ /** 返回数据类型 */
69
+
70
+ exports.RequestFormItemType = RequestFormItemType;
71
+
72
+ (function (RequestFormItemType) {
73
+ RequestFormItemType["text"] = "text";
74
+ RequestFormItemType["file"] = "file";
75
+ })(RequestFormItemType || (exports.RequestFormItemType = RequestFormItemType = {}));
76
+
77
+ var ResponseBodyType;
78
+ /** 查询字符串数组格式化方式 */
79
+
80
+ exports.ResponseBodyType = ResponseBodyType;
81
+
82
+ (function (ResponseBodyType) {
83
+ ResponseBodyType["json"] = "json";
84
+ ResponseBodyType["text"] = "text";
85
+ ResponseBodyType["xml"] = "xml";
86
+ ResponseBodyType["raw"] = "raw";
87
+ })(ResponseBodyType || (exports.ResponseBodyType = ResponseBodyType = {}));
88
+
89
+ var QueryStringArrayFormat;
90
+ /** 接口定义 */
91
+
92
+ exports.QueryStringArrayFormat = QueryStringArrayFormat;
93
+
94
+ (function (QueryStringArrayFormat) {
95
+ QueryStringArrayFormat["brackets"] = "brackets";
96
+ QueryStringArrayFormat["indices"] = "indices";
97
+ QueryStringArrayFormat["repeat"] = "repeat";
98
+ QueryStringArrayFormat["comma"] = "comma";
99
+ QueryStringArrayFormat["json"] = "json";
100
+ })(QueryStringArrayFormat || (exports.QueryStringArrayFormat = QueryStringArrayFormat = {}));