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