yapi-apifox-to-typescript 4.2.3 → 5.0.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.
@@ -1,483 +1,279 @@
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
-
1
+ import { each, find } from "./mini-ut.js";
2
+ import dayjs from "dayjs";
3
+ import swagger from "swagger-client";
4
+ //#region src/swaggerJsonToYApiData.ts
5
+ var SwaggerData;
6
+ var isOAS3;
11
7
  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;
8
+ if (path === "/") return path;
9
+ if (path.charAt(0) !== "/") path = `/${path}`;
10
+ if (path.charAt(path.length - 1) === "/") path = path.substr(0, path.length - 1);
11
+ return path;
23
12
  }
24
-
25
13
  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;
14
+ data.swagger = "2.0";
15
+ each(data.paths, (apis) => {
16
+ each(apis, (api) => {
17
+ each(api.responses, (res) => {
18
+ if (res.content && res.content["application/json"] && typeof res.content["application/json"] === "object") {
19
+ Object.assign(res, res.content["application/json"]);
20
+ delete res.content;
21
+ }
22
+ if (res.content && res.content["application/hal+json"] && typeof res.content["application/hal+json"] === "object") {
23
+ Object.assign(res, res.content["application/hal+json"]);
24
+ delete res.content;
25
+ }
26
+ if (res.content && res.content["*/*"] && typeof res.content["*/*"] === "object") {
27
+ Object.assign(res, res.content["*/*"]);
28
+ delete res.content;
29
+ }
30
+ });
31
+ if (api.requestBody) {
32
+ if (!api.parameters) api.parameters = [];
33
+ const body = {
34
+ type: "object",
35
+ name: "body",
36
+ in: "body"
37
+ };
38
+ try {
39
+ body.schema = api.requestBody.content["application/json"].schema;
40
+ } catch (e) {
41
+ body.schema = {};
42
+ }
43
+ api.parameters.push(body);
44
+ }
45
+ });
46
+ });
47
+ return data;
65
48
  }
66
-
67
- function handleSwaggerData(_x) {
68
- return _handleSwaggerData.apply(this, arguments);
49
+ async function handleSwaggerData(res) {
50
+ return new Promise((resolve) => {
51
+ swagger({ spec: res }).then((res) => {
52
+ resolve(res.spec);
53
+ });
54
+ });
69
55
  }
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);
56
+ async function run(res) {
57
+ const interfaceData = {
58
+ apis: [],
59
+ cats: [],
60
+ basePath: "",
61
+ swaggerData: {}
62
+ };
63
+ if (typeof res === "string" && res) try {
64
+ res = JSON.parse(res);
65
+ } catch (e) {
66
+ console.error("json 解析出错", e.message);
67
+ }
68
+ isOAS3 = res.openapi && String(res.openapi).startsWith("3.");
69
+ if (isOAS3) res = openapi2swagger(res);
70
+ res = await handleSwaggerData(res);
71
+ SwaggerData = res;
72
+ interfaceData.swaggerData = SwaggerData;
73
+ interfaceData.basePath = res.basePath || "";
74
+ if (res.tags && Array.isArray(res.tags)) res.tags.forEach((tag) => {
75
+ interfaceData.cats.push({
76
+ name: tag.name,
77
+ desc: tag.description
78
+ });
79
+ });
80
+ else res.tags = [];
81
+ each(res.paths, (apis, path) => {
82
+ delete apis.parameters;
83
+ each(apis, (api, method) => {
84
+ api.path = path;
85
+ api.method = method;
86
+ let data = null;
87
+ try {
88
+ data = handleSwagger(api, res.tags);
89
+ if (data.catname) {
90
+ if (!find(interfaceData.cats, (item) => item.name === data.catname)) {
91
+ if (res.tags.length === 0) interfaceData.cats.push({
92
+ name: data.catname,
93
+ desc: data.catname
94
+ });
95
+ }
96
+ }
97
+ } catch (err) {
98
+ data = null;
99
+ }
100
+ if (data) interfaceData.apis.push(data);
101
+ });
102
+ });
103
+ interfaceData.cats = interfaceData.cats.filter((catData) => {
104
+ const catName = catData.name;
105
+ return find(interfaceData.apis, (apiData) => {
106
+ return apiData.catname === catName;
107
+ });
108
+ });
109
+ return interfaceData;
94
110
  }
95
-
96
- function run(_x2) {
97
- return _run.apply(this, arguments);
111
+ function handleSwagger(data, originTags = []) {
112
+ const api = {};
113
+ api.method = data.method.toUpperCase();
114
+ api.title = data.summary || data.path;
115
+ api.desc = data.description;
116
+ api.catname = null;
117
+ if (data.tags && Array.isArray(data.tags)) {
118
+ api.tag = data.tags;
119
+ for (let i = 0; i < data.tags.length; i++) {
120
+ if (/v[0-9.]+/.test(data.tags[i])) continue;
121
+ if (originTags.length > 0 && find(originTags, (item) => {
122
+ return item.name === data.tags[i];
123
+ })) {
124
+ api.catname = data.tags[i];
125
+ break;
126
+ }
127
+ if (originTags.length === 0) {
128
+ api.catname = data.tags[i];
129
+ break;
130
+ }
131
+ }
132
+ }
133
+ api.path = handlePath(data.path);
134
+ api.req_params = [];
135
+ api.req_body_form = [];
136
+ api.req_headers = [];
137
+ api.req_query = [];
138
+ api.req_body_type = "raw";
139
+ api.res_body_type = "raw";
140
+ if (data.produces && data.produces.indexOf("application/json") > -1) {
141
+ api.res_body_type = "json";
142
+ api.res_body_is_json_schema = true;
143
+ }
144
+ if (data.consumes && Array.isArray(data.consumes)) {
145
+ if (data.consumes.indexOf("application/x-www-form-urlencoded") > -1 || data.consumes.indexOf("multipart/form-data") > -1) api.req_body_type = "form";
146
+ else if (data.consumes.indexOf("application/json") > -1) {
147
+ api.req_body_type = "json";
148
+ api.req_body_is_json_schema = true;
149
+ }
150
+ }
151
+ api.res_body = handleResponse(data.responses);
152
+ try {
153
+ JSON.parse(api.res_body);
154
+ api.res_body_type = "json";
155
+ api.res_body_is_json_schema = true;
156
+ } catch (e) {
157
+ api.res_body_type = "raw";
158
+ }
159
+ function simpleJsonPathParse(key, json) {
160
+ if (!key || typeof key !== "string" || key.indexOf("#/") !== 0 || key.length <= 2) return null;
161
+ let keys = key.substr(2).split("/");
162
+ keys = keys.filter((item) => {
163
+ return item;
164
+ });
165
+ for (let i = 0, l = keys.length; i < l; i++) try {
166
+ json = json[keys[i]];
167
+ } catch (e) {
168
+ json = "";
169
+ break;
170
+ }
171
+ return json;
172
+ }
173
+ if (data.parameters && Array.isArray(data.parameters)) data.parameters.forEach((param) => {
174
+ if (param && typeof param === "object" && param.$ref) param = simpleJsonPathParse(param.$ref, { parameters: SwaggerData.parameters });
175
+ const defaultParam = {
176
+ name: param.name,
177
+ desc: param.description,
178
+ required: param.required ? "1" : "0"
179
+ };
180
+ if (param.in) switch (param.in) {
181
+ case "path":
182
+ defaultParam.type = param.type;
183
+ api.req_params.push(defaultParam);
184
+ break;
185
+ case "query":
186
+ defaultParam.type = param.type;
187
+ api.req_query.push(defaultParam);
188
+ break;
189
+ case "body":
190
+ handleBodyPamras(param.schema, api);
191
+ break;
192
+ case "formData":
193
+ defaultParam.type = param.type === "file" ? "file" : "text";
194
+ if (param.example) defaultParam.example = param.example;
195
+ api.req_body_form.push(defaultParam);
196
+ break;
197
+ case "header": api.req_headers.push(defaultParam);
198
+ }
199
+ else api.req_query.push(defaultParam);
200
+ });
201
+ return api;
98
202
  }
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
203
  function isJson(json) {
353
- try {
354
- return JSON.parse(json);
355
- } catch (e) {
356
- return false;
357
- }
204
+ try {
205
+ return JSON.parse(json);
206
+ } catch (e) {
207
+ return false;
208
+ }
358
209
  }
359
-
360
210
  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
- }
211
+ api.req_body_other = JSON.stringify(data, null, 2);
212
+ if (isJson(api.req_body_other)) {
213
+ api.req_body_type = "json";
214
+ api.req_body_is_json_schema = true;
215
+ }
367
216
  }
368
-
369
217
  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;
218
+ let res_body = "";
219
+ if (!api || typeof api !== "object") return res_body;
220
+ const codes = Object.keys(api);
221
+ let curCode;
222
+ if (codes.length > 0) {
223
+ if (codes.indexOf("200") > -1) curCode = "200";
224
+ else curCode = codes[0];
225
+ const res = api[curCode];
226
+ if (res && typeof res === "object") {
227
+ if (res.schema) res_body = JSON.stringify(res.schema, null, 2);
228
+ else if (res.description) res_body = res.description;
229
+ } else if (typeof res === "string") res_body = res;
230
+ else res_body = "";
231
+ } else res_body = "";
232
+ return res_body;
402
233
  }
403
-
404
- export function swaggerJsonToYApiData(_x3) {
405
- return _swaggerJsonToYApiData.apply(this, arguments);
234
+ async function swaggerJsonToYApiData(data) {
235
+ const yapiData = await run(data);
236
+ if (!yapiData.cats.length) {
237
+ yapiData.cats = [{
238
+ name: "default",
239
+ desc: "default"
240
+ }];
241
+ yapiData.apis.forEach((api) => {
242
+ api.catname = "default";
243
+ });
244
+ }
245
+ const currentTime = dayjs().unix();
246
+ const project = {
247
+ _id: 0,
248
+ name: yapiData.swaggerData.info.title,
249
+ desc: yapiData.swaggerData.info.description || "",
250
+ basepath: yapiData.swaggerData.basePath || "",
251
+ tag: [],
252
+ env: [{
253
+ name: "local",
254
+ domain: `${yapiData.swaggerData.schemes?.[0] || "http"}://${yapiData.swaggerData.host || "127.0.0.1"}`
255
+ }]
256
+ };
257
+ const cats = yapiData.cats.map((cat, index) => ({
258
+ _id: index + 1,
259
+ name: cat.name,
260
+ desc: cat.desc,
261
+ add_time: currentTime,
262
+ up_time: currentTime
263
+ }));
264
+ return {
265
+ project,
266
+ cats,
267
+ interfaces: yapiData.apis.map((api, index) => ({
268
+ ...api,
269
+ _id: index + 1,
270
+ project_id: 0,
271
+ catid: cats.find((cat) => cat.name === api.catname)?._id || -1,
272
+ tag: api.tag || [],
273
+ add_time: currentTime,
274
+ up_time: currentTime
275
+ }))
276
+ };
406
277
  }
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
- }
278
+ //#endregion
279
+ export { swaggerJsonToYApiData };