open-api-typescript-request-generator 0.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.
Files changed (71) hide show
  1. package/README.md +26 -0
  2. package/bin/apits-gener +4 -0
  3. package/es/Generator.d.ts +43 -0
  4. package/es/Generator.js +892 -0
  5. package/es/cli.d.ts +19 -0
  6. package/es/cli.js +547 -0
  7. package/es/console.d.ts +48 -0
  8. package/es/console.js +151 -0
  9. package/es/constants.d.ts +22 -0
  10. package/es/constants.js +39 -0
  11. package/es/dependenciesHandler.d.ts +16 -0
  12. package/es/dependenciesHandler.js +270 -0
  13. package/es/genIndex.d.ts +23 -0
  14. package/es/genIndex.js +354 -0
  15. package/es/genRequest.d.ts +3 -0
  16. package/es/genRequest.js +216 -0
  17. package/es/getOutputPath.d.ts +5 -0
  18. package/es/getOutputPath.js +28 -0
  19. package/es/helpers.d.ts +63 -0
  20. package/es/helpers.js +458 -0
  21. package/es/index.d.ts +2 -0
  22. package/es/index.js +34 -0
  23. package/es/requestYapiData.d.ts +157 -0
  24. package/es/requestYapiData.js +1010 -0
  25. package/es/responseDataJsonSchemaHandler.d.ts +11 -0
  26. package/es/responseDataJsonSchemaHandler.js +193 -0
  27. package/es/server/mock.d.ts +1 -0
  28. package/es/server/mock.js +7 -0
  29. package/es/server/swaggerJsonToYApiData.d.ts +10 -0
  30. package/es/server/swaggerJsonToYApiData.js +614 -0
  31. package/es/spinner.d.ts +15 -0
  32. package/es/spinner.js +58 -0
  33. package/es/types.d.ts +736 -0
  34. package/es/types.js +143 -0
  35. package/es/utils.d.ts +97 -0
  36. package/es/utils.js +783 -0
  37. package/lib/Generator.d.ts +43 -0
  38. package/lib/Generator.js +892 -0
  39. package/lib/cli.d.ts +19 -0
  40. package/lib/cli.js +547 -0
  41. package/lib/console.d.ts +48 -0
  42. package/lib/console.js +151 -0
  43. package/lib/constants.d.ts +22 -0
  44. package/lib/constants.js +39 -0
  45. package/lib/dependenciesHandler.d.ts +16 -0
  46. package/lib/dependenciesHandler.js +270 -0
  47. package/lib/genIndex.d.ts +23 -0
  48. package/lib/genIndex.js +354 -0
  49. package/lib/genRequest.d.ts +3 -0
  50. package/lib/genRequest.js +216 -0
  51. package/lib/getOutputPath.d.ts +5 -0
  52. package/lib/getOutputPath.js +28 -0
  53. package/lib/helpers.d.ts +63 -0
  54. package/lib/helpers.js +458 -0
  55. package/lib/index.d.ts +2 -0
  56. package/lib/index.js +34 -0
  57. package/lib/requestYapiData.d.ts +157 -0
  58. package/lib/requestYapiData.js +1010 -0
  59. package/lib/responseDataJsonSchemaHandler.d.ts +11 -0
  60. package/lib/responseDataJsonSchemaHandler.js +193 -0
  61. package/lib/server/mock.d.ts +1 -0
  62. package/lib/server/mock.js +7 -0
  63. package/lib/server/swaggerJsonToYApiData.d.ts +10 -0
  64. package/lib/server/swaggerJsonToYApiData.js +614 -0
  65. package/lib/spinner.d.ts +15 -0
  66. package/lib/spinner.js +58 -0
  67. package/lib/types.d.ts +736 -0
  68. package/lib/types.js +143 -0
  69. package/lib/utils.d.ts +97 -0
  70. package/lib/utils.js +783 -0
  71. package/package.json +106 -0
@@ -0,0 +1,614 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
+
5
+ var __assign = void 0 && (void 0).__assign || function () {
6
+ __assign = Object.assign || function (t) {
7
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
8
+ s = arguments[i];
9
+
10
+ for (var p in s) {
11
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
12
+ }
13
+ }
14
+
15
+ return t;
16
+ };
17
+
18
+ return __assign.apply(this, arguments);
19
+ };
20
+
21
+ var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) {
23
+ return value instanceof P ? value : new P(function (resolve) {
24
+ resolve(value);
25
+ });
26
+ }
27
+
28
+ return new (P || (P = Promise))(function (resolve, reject) {
29
+ function fulfilled(value) {
30
+ try {
31
+ step(generator.next(value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ }
36
+
37
+ function rejected(value) {
38
+ try {
39
+ step(generator["throw"](value));
40
+ } catch (e) {
41
+ reject(e);
42
+ }
43
+ }
44
+
45
+ function step(result) {
46
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
47
+ }
48
+
49
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
50
+ });
51
+ };
52
+
53
+ var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
54
+ var _ = {
55
+ label: 0,
56
+ sent: function sent() {
57
+ if (t[0] & 1) throw t[1];
58
+ return t[1];
59
+ },
60
+ trys: [],
61
+ ops: []
62
+ },
63
+ f,
64
+ y,
65
+ t,
66
+ g;
67
+ return g = {
68
+ next: verb(0),
69
+ "throw": verb(1),
70
+ "return": verb(2)
71
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
72
+ return this;
73
+ }), g;
74
+
75
+ function verb(n) {
76
+ return function (v) {
77
+ return step([n, v]);
78
+ };
79
+ }
80
+
81
+ function step(op) {
82
+ if (f) throw new TypeError("Generator is already executing.");
83
+
84
+ while (_) {
85
+ try {
86
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
87
+ if (y = 0, t) op = [op[0] & 2, t.value];
88
+
89
+ switch (op[0]) {
90
+ case 0:
91
+ case 1:
92
+ t = op;
93
+ break;
94
+
95
+ case 4:
96
+ _.label++;
97
+ return {
98
+ value: op[1],
99
+ done: false
100
+ };
101
+
102
+ case 5:
103
+ _.label++;
104
+ y = op[1];
105
+ op = [0];
106
+ continue;
107
+
108
+ case 7:
109
+ op = _.ops.pop();
110
+
111
+ _.trys.pop();
112
+
113
+ continue;
114
+
115
+ default:
116
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
117
+ _ = 0;
118
+ continue;
119
+ }
120
+
121
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
122
+ _.label = op[1];
123
+ break;
124
+ }
125
+
126
+ if (op[0] === 6 && _.label < t[1]) {
127
+ _.label = t[1];
128
+ t = op;
129
+ break;
130
+ }
131
+
132
+ if (t && _.label < t[2]) {
133
+ _.label = t[2];
134
+
135
+ _.ops.push(op);
136
+
137
+ break;
138
+ }
139
+
140
+ if (t[2]) _.ops.pop();
141
+
142
+ _.trys.pop();
143
+
144
+ continue;
145
+ }
146
+
147
+ op = body.call(thisArg, _);
148
+ } catch (e) {
149
+ op = [6, e];
150
+ y = 0;
151
+ } finally {
152
+ f = t = 0;
153
+ }
154
+ }
155
+
156
+ if (op[0] & 5) throw op[1];
157
+ return {
158
+ value: op[0] ? op[1] : void 0,
159
+ done: true
160
+ };
161
+ }
162
+ };
163
+
164
+ var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
165
+ return mod && mod.__esModule ? mod : {
166
+ "default": mod
167
+ };
168
+ };
169
+
170
+ Object.defineProperty(exports, "__esModule", {
171
+ value: true
172
+ });
173
+ exports.swaggerJsonToYApiData = void 0;
174
+
175
+ var dayjs_1 = __importDefault(require("dayjs"));
176
+
177
+ var swagger_client_1 = __importDefault(require("swagger-client"));
178
+
179
+ var vtils_1 = require("vtils");
180
+
181
+ var SwaggerData;
182
+ var isOAS3;
183
+
184
+ function handlePath(path) {
185
+ if (path === '/') return path;
186
+
187
+ if (path.charAt(0) !== '/') {
188
+ path = "/".concat(path);
189
+ }
190
+
191
+ if (path.charAt(path.length - 1) === '/') {
192
+ path = path.substr(0, path.length - 1);
193
+ }
194
+
195
+ return path;
196
+ }
197
+
198
+ function openapi3Format(data) {
199
+ data.swagger = '2.0';
200
+ (0, vtils_1.each)(data.paths, function (apis) {
201
+ (0, vtils_1.each)(apis, function (api) {
202
+ (0, vtils_1.each)(api.responses, function (res) {
203
+ if (res.content && res.content['application/json'] && _typeof(res.content['application/json']) === 'object') {
204
+ Object.assign(res, res.content['application/json']);
205
+ delete res.content;
206
+ }
207
+
208
+ if (res.content && res.content['application/hal+json'] && _typeof(res.content['application/hal+json']) === 'object') {
209
+ Object.assign(res, res.content['application/hal+json']);
210
+ delete res.content;
211
+ }
212
+
213
+ if (res.content && res.content['*/*'] && _typeof(res.content['*/*']) === 'object') {
214
+ Object.assign(res, res.content['*/*']);
215
+ delete res.content;
216
+ }
217
+ });
218
+
219
+ if (api.requestBody) {
220
+ if (!api.parameters) api.parameters = [];
221
+ var body = {
222
+ type: 'object',
223
+ name: 'body',
224
+ in: 'body',
225
+ schema: {}
226
+ };
227
+
228
+ try {
229
+ body.schema = api.requestBody.content['application/json'].schema;
230
+ } catch (e) {
231
+ body.schema = {};
232
+ }
233
+
234
+ api.parameters.push(body);
235
+ }
236
+ });
237
+ });
238
+ return data;
239
+ }
240
+
241
+ function openapi2ToSwaggerData(openapiData) {
242
+ return __awaiter(this, void 0, void 0, function () {
243
+ return __generator(this, function (_a) {
244
+ return [2
245
+ /*return*/
246
+ , openapiData];
247
+ });
248
+ });
249
+ }
250
+
251
+ function parseOpenapi(res) {
252
+ return __awaiter(this, void 0, void 0, function () {
253
+ var interfaceData;
254
+ return __generator(this, function (_a) {
255
+ switch (_a.label) {
256
+ case 0:
257
+ interfaceData = {
258
+ apis: [],
259
+ cats: [],
260
+ basePath: '',
261
+ swaggerData: {}
262
+ };
263
+
264
+ if (typeof res === 'string' && res) {
265
+ try {
266
+ res = JSON.parse(res);
267
+ } catch (e) {
268
+ console.error('json 解析出错', e.message);
269
+ }
270
+ }
271
+
272
+ isOAS3 = res.openapi && String(res.openapi).startsWith('3.');
273
+
274
+ if (isOAS3) {
275
+ res = openapi3Format(res);
276
+ }
277
+
278
+ return [4
279
+ /*yield*/
280
+ , openapi2ToSwaggerData(res)];
281
+
282
+ case 1:
283
+ res = _a.sent();
284
+ SwaggerData = res;
285
+ interfaceData.swaggerData = SwaggerData;
286
+ interfaceData.basePath = res.basePath || '';
287
+
288
+ if (res.tags && Array.isArray(res.tags)) {
289
+ res.tags.forEach(function (tag) {
290
+ interfaceData.cats.push({
291
+ name: tag.name,
292
+ desc: tag.description
293
+ });
294
+ });
295
+ } else {
296
+ res.tags = [];
297
+ }
298
+
299
+ (0, vtils_1.each)(res.paths, function (apis, path) {
300
+ // parameters is common parameters, not a method
301
+ delete apis.parameters;
302
+ (0, vtils_1.each)(apis, function (api, method) {
303
+ api.path = path;
304
+ api.method = method;
305
+ var data = null;
306
+
307
+ try {
308
+ data = handleSwagger(api, res.tags);
309
+
310
+ if (data.catname) {
311
+ if (!(0, vtils_1.find)(interfaceData.cats, function (item) {
312
+ return item.name === data.catname;
313
+ })) {
314
+ if (res.tags.length === 0) {
315
+ interfaceData.cats.push({
316
+ name: data.catname,
317
+ desc: data.catname
318
+ });
319
+ }
320
+ }
321
+ }
322
+ } catch (err) {
323
+ data = null;
324
+ }
325
+
326
+ if (data) {
327
+ interfaceData.apis.push(data);
328
+ }
329
+ });
330
+ });
331
+ interfaceData.cats = interfaceData.cats.filter(function (catData) {
332
+ var catName = catData.name;
333
+ return (0, vtils_1.find)(interfaceData.apis, function (apiData) {
334
+ return apiData.catname === catName;
335
+ });
336
+ });
337
+ return [2
338
+ /*return*/
339
+ , interfaceData];
340
+ }
341
+ });
342
+ });
343
+ }
344
+
345
+ function handleSwagger(data, originTags) {
346
+ if (originTags === void 0) {
347
+ originTags = [];
348
+ }
349
+
350
+ var api = {}; // 处理基本信息
351
+
352
+ api.method = data.method.toUpperCase();
353
+ api.title = data.summary || data.path;
354
+ api.desc = data.description;
355
+ api.catname = null;
356
+
357
+ if (data.tags && Array.isArray(data.tags)) {
358
+ api.tag = data.tags;
359
+
360
+ var _loop_1 = function _loop_1(i) {
361
+ if (/v[0-9.]+/.test(data.tags[i])) {
362
+ return "continue";
363
+ } // 如果根路径有 tags,使用根路径 tags,不使用每个接口定义的 tag 做完分类
364
+
365
+
366
+ if (originTags.length > 0 && (0, vtils_1.find)(originTags, function (item) {
367
+ return item.name === data.tags[i];
368
+ })) {
369
+ api.catname = data.tags[i];
370
+ return "break";
371
+ }
372
+
373
+ if (originTags.length === 0) {
374
+ api.catname = data.tags[i];
375
+ return "break";
376
+ }
377
+ };
378
+
379
+ for (var i = 0; i < data.tags.length; i++) {
380
+ var state_1 = _loop_1(i);
381
+
382
+ if (state_1 === "break") break;
383
+ }
384
+ }
385
+
386
+ api.path = handlePath(data.path);
387
+ api.req_params = [];
388
+ api.req_body_form = [];
389
+ api.req_headers = [];
390
+ api.req_query = [];
391
+ api.req_body_type = 'raw';
392
+ api.res_body_type = 'raw';
393
+
394
+ if (data.produces && data.produces.indexOf('application/json') > -1) {
395
+ api.res_body_type = 'json';
396
+ api.res_body_is_json_schema = true;
397
+ }
398
+
399
+ if (data.consumes && Array.isArray(data.consumes)) {
400
+ if (data.consumes.indexOf('application/x-www-form-urlencoded') > -1 || data.consumes.indexOf('multipart/form-data') > -1) {
401
+ api.req_body_type = 'form';
402
+ } else if (data.consumes.indexOf('application/json') > -1) {
403
+ api.req_body_type = 'json';
404
+ api.req_body_is_json_schema = true;
405
+ }
406
+ } // 处理response
407
+
408
+
409
+ api.res_body = handleResponse(data.responses);
410
+
411
+ try {
412
+ JSON.parse(api.res_body);
413
+ api.res_body_type = 'json';
414
+ api.res_body_is_json_schema = true;
415
+ } catch (e) {
416
+ api.res_body_type = 'raw';
417
+ } // 处理参数
418
+
419
+
420
+ function simpleJsonPathParse(key, json) {
421
+ if (!key || typeof key !== 'string' || key.indexOf('#/') !== 0 || key.length <= 2) {
422
+ return null;
423
+ }
424
+
425
+ var keys = key.substr(2).split('/');
426
+ keys = keys.filter(function (item) {
427
+ return item;
428
+ });
429
+
430
+ for (var i = 0, l = keys.length; i < l; i++) {
431
+ try {
432
+ json = json[keys[i]];
433
+ } catch (e) {
434
+ json = '';
435
+ break;
436
+ }
437
+ }
438
+
439
+ return json;
440
+ }
441
+
442
+ if (data.parameters && Array.isArray(data.parameters)) {
443
+ data.parameters.forEach(function (param) {
444
+ if (param && _typeof(param) === 'object' && param.$ref) {
445
+ param = simpleJsonPathParse(param.$ref, {
446
+ parameters: SwaggerData.parameters
447
+ });
448
+ }
449
+
450
+ var defaultParam = {
451
+ name: param.name,
452
+ desc: param.description,
453
+ type: param.type || param.schema.type,
454
+ required: param.required ? '1' : '0',
455
+ example: ''
456
+ };
457
+
458
+ if (param.in) {
459
+ switch (param.in) {
460
+ case 'path':
461
+ api.req_params.push(defaultParam);
462
+ break;
463
+
464
+ case 'query':
465
+ api.req_query.push(defaultParam);
466
+ break;
467
+
468
+ case 'body':
469
+ handleBodyPamras(param.schema, api);
470
+ break;
471
+
472
+ case 'formData':
473
+ defaultParam.type = param.type === 'file' ? 'file' : 'text';
474
+
475
+ if (param.example) {
476
+ defaultParam.example = param.example;
477
+ }
478
+
479
+ api.req_body_form.push(defaultParam);
480
+ break;
481
+
482
+ case 'header':
483
+ api.req_headers.push(defaultParam);
484
+ break;
485
+
486
+ default:
487
+ break;
488
+ }
489
+ } else {
490
+ api.req_query.push(defaultParam);
491
+ }
492
+ });
493
+ }
494
+
495
+ return api;
496
+ }
497
+
498
+ function isJson(json) {
499
+ try {
500
+ return JSON.parse(json);
501
+ } catch (e) {
502
+ return false;
503
+ }
504
+ }
505
+
506
+ function handleBodyPamras(data, api) {
507
+ api.req_body_other = JSON.stringify(data, null, 2);
508
+
509
+ if (isJson(api.req_body_other)) {
510
+ api.req_body_type = 'json';
511
+ api.req_body_is_json_schema = true;
512
+ }
513
+ }
514
+
515
+ function handleResponse(api) {
516
+ var res_body = '';
517
+
518
+ if (!api || _typeof(api) !== 'object') {
519
+ return res_body;
520
+ }
521
+
522
+ var codes = Object.keys(api);
523
+ var curCode;
524
+
525
+ if (codes.length > 0) {
526
+ if (codes.indexOf('200') > -1) {
527
+ curCode = '200';
528
+ } else curCode = codes[0];
529
+
530
+ var res = api[curCode];
531
+
532
+ if (res && _typeof(res) === 'object') {
533
+ if (res.schema) {
534
+ res_body = JSON.stringify(res.schema, null, 2);
535
+ } else if (res.description) {
536
+ res_body = res.description;
537
+ }
538
+ } else if (typeof res === 'string') {
539
+ res_body = res;
540
+ } else {
541
+ res_body = '';
542
+ }
543
+ } else {
544
+ res_body = '';
545
+ }
546
+
547
+ return res_body;
548
+ }
549
+ /**
550
+ *
551
+ * @param data
552
+ * @description data is openapiV3 json
553
+ * @returns
554
+ */
555
+
556
+
557
+ function swaggerJsonToYApiData(data) {
558
+ return __awaiter(this, void 0, void 0, function () {
559
+ var yapiData, currentTime, cats, interfaces;
560
+ return __generator(this, function (_a) {
561
+ switch (_a.label) {
562
+ case 0:
563
+ return [4
564
+ /*yield*/
565
+ , parseOpenapi(data)];
566
+
567
+ case 1:
568
+ yapiData = _a.sent(); // 兼容没有分类的情况
569
+
570
+ if (!yapiData.cats.length) {
571
+ yapiData.cats = [{
572
+ name: 'default',
573
+ desc: 'default'
574
+ }];
575
+ yapiData.apis.forEach(function (api) {
576
+ api.catname = 'default';
577
+ });
578
+ }
579
+
580
+ currentTime = (0, dayjs_1.default)().unix();
581
+ cats = yapiData.cats.map(function (cat, index) {
582
+ return {
583
+ _id: index + 1,
584
+ name: cat.name,
585
+ desc: cat.desc,
586
+ add_time: currentTime,
587
+ up_time: currentTime
588
+ };
589
+ });
590
+ interfaces = yapiData.apis.map(function (api, index) {
591
+ var _a;
592
+
593
+ return __assign(__assign({}, api), {
594
+ _id: index + 1,
595
+ project_id: 0,
596
+ catid: ((_a = cats.find(function (cat) {
597
+ return cat.name === api.catname;
598
+ })) === null || _a === void 0 ? void 0 : _a._id) || -1,
599
+ tag: api.tag || [],
600
+ add_time: currentTime,
601
+ up_time: currentTime
602
+ });
603
+ });
604
+ return [2
605
+ /*return*/
606
+ , {
607
+ interfaces: interfaces
608
+ }];
609
+ }
610
+ });
611
+ });
612
+ }
613
+
614
+ exports.swaggerJsonToYApiData = swaggerJsonToYApiData;
@@ -0,0 +1,15 @@
1
+ import { Ora } from 'ora';
2
+ declare class Spin {
3
+ spinnerInstance: Ora;
4
+ constructor();
5
+ clear(): void;
6
+ start(text?: string): void;
7
+ succeed(text?: string): void;
8
+ fail(text?: string): void;
9
+ render(): void;
10
+ frame(): void;
11
+ info(text?: string): void;
12
+ stop(): void;
13
+ }
14
+ export declare const spinnerInstance: Spin;
15
+ export {};
package/lib/spinner.js ADDED
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
4
+ return mod && mod.__esModule ? mod : {
5
+ "default": mod
6
+ };
7
+ };
8
+
9
+ Object.defineProperty(exports, "__esModule", {
10
+ value: true
11
+ });
12
+ exports.spinnerInstance = void 0;
13
+
14
+ var ora_1 = __importDefault(require("ora"));
15
+
16
+ var Spin =
17
+ /** @class */
18
+ function () {
19
+ function Spin() {
20
+ this.spinnerInstance = (0, ora_1.default)();
21
+ }
22
+
23
+ Spin.prototype.clear = function () {
24
+ this.spinnerInstance.clear();
25
+ };
26
+
27
+ Spin.prototype.start = function (text) {
28
+ this.spinnerInstance.start(text);
29
+ };
30
+
31
+ Spin.prototype.succeed = function (text) {
32
+ this.spinnerInstance.succeed(text);
33
+ };
34
+
35
+ Spin.prototype.fail = function (text) {
36
+ this.spinnerInstance.fail(text);
37
+ };
38
+
39
+ Spin.prototype.render = function () {
40
+ this.spinnerInstance.render();
41
+ };
42
+
43
+ Spin.prototype.frame = function () {
44
+ this.spinnerInstance.frame();
45
+ };
46
+
47
+ Spin.prototype.info = function (text) {
48
+ this.spinnerInstance.info(text);
49
+ };
50
+
51
+ Spin.prototype.stop = function () {
52
+ this.spinnerInstance.stop();
53
+ };
54
+
55
+ return Spin;
56
+ }();
57
+
58
+ exports.spinnerInstance = new Spin();