yapi-to-typescript2 1.0.0 → 1.0.2

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,20 @@
1
+ import { AsyncReturnType } from 'vtils/types';
2
+ import { swaggerJsonToYApiData } from './swaggerJsonToYApiData';
3
+ import { OpenAPIV2 as SwaggerType } from 'openapi-types';
4
+ export interface SwaggerToYApiServerOptions {
5
+ swaggerJsonUrl: string;
6
+ }
7
+ export declare class SwaggerToYApiServer {
8
+ private readonly options;
9
+ private port;
10
+ private swaggerJson;
11
+ private httpServer;
12
+ private yapiData;
13
+ constructor(options: SwaggerToYApiServerOptions);
14
+ getPort(): Promise<number>;
15
+ getUrl(): Promise<string>;
16
+ getSwaggerJson(): Promise<SwaggerType.Document>;
17
+ getYApiData(): Promise<AsyncReturnType<typeof swaggerJsonToYApiData>>;
18
+ start(): Promise<string>;
19
+ stop(): Promise<void>;
20
+ }
@@ -1,336 +1,228 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
- exports.__esModule = true;
6
- exports.SwaggerToYApiServer = void 0;
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 _getPort2 = _interopRequireDefault(require("get-port"));
15
-
16
- var _http = _interopRequireDefault(require("http"));
17
-
18
- var _signalExit = _interopRequireDefault(require("signal-exit"));
19
-
20
- var _url = _interopRequireDefault(require("url"));
21
-
22
- var _utils = require("./utils");
23
-
24
- var _vtils = require("vtils");
25
-
26
- var _swaggerJsonToYApiData = require("./swaggerJsonToYApiData");
27
-
28
- var SwaggerToYApiServer = /*#__PURE__*/function () {
29
- function SwaggerToYApiServer(options) {
30
- this.options = options;
31
- this.port = 0;
32
- this.swaggerJson = {};
33
- this.httpServer = null;
34
- this.yapiData = {};
35
- }
36
-
37
- var _proto = SwaggerToYApiServer.prototype;
38
-
39
- _proto.getPort = /*#__PURE__*/function () {
40
- var _getPort = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
41
- return _regenerator.default.wrap(function _callee$(_context) {
42
- while (1) {
43
- switch (_context.prev = _context.next) {
44
- case 0:
45
- if (!(this.port === 0)) {
46
- _context.next = 4;
47
- break;
48
- }
49
-
50
- _context.next = 3;
51
- return (0, _getPort2.default)({
52
- port: 50505
53
- });
54
-
55
- case 3:
56
- this.port = _context.sent;
57
-
58
- case 4:
59
- return _context.abrupt("return", this.port);
60
-
61
- case 5:
62
- case "end":
63
- return _context.stop();
64
- }
65
- }
66
- }, _callee, this);
67
- }));
68
-
69
- function getPort() {
70
- return _getPort.apply(this, arguments);
71
- }
72
-
73
- return getPort;
74
- }();
75
-
76
- _proto.getUrl = /*#__PURE__*/function () {
77
- var _getUrl = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
78
- return _regenerator.default.wrap(function _callee2$(_context2) {
79
- while (1) {
80
- switch (_context2.prev = _context2.next) {
81
- case 0:
82
- _context2.next = 2;
83
- return this.getPort();
84
-
85
- case 2:
86
- _context2.t0 = _context2.sent;
87
- return _context2.abrupt("return", "http://127.0.0.1:" + _context2.t0);
88
-
89
- case 4:
90
- case "end":
91
- return _context2.stop();
92
- }
93
- }
94
- }, _callee2, this);
95
- }));
96
-
97
- function getUrl() {
98
- return _getUrl.apply(this, arguments);
99
- }
100
-
101
- return getUrl;
102
- }();
103
-
104
- _proto.getSwaggerJson = /*#__PURE__*/function () {
105
- var _getSwaggerJson = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3() {
106
- var res;
107
- return _regenerator.default.wrap(function _callee3$(_context3) {
108
- while (1) {
109
- switch (_context3.prev = _context3.next) {
110
- case 0:
111
- if (!(0, _vtils.isEmpty)(this.swaggerJson)) {
112
- _context3.next = 5;
113
- break;
114
- }
115
-
116
- _context3.next = 3;
117
- return (0, _utils.httpGet)(this.options.swaggerJsonUrl);
118
-
119
- case 3:
120
- res = _context3.sent;
121
- this.swaggerJson = res;
122
-
123
- case 5:
124
- return _context3.abrupt("return", this.swaggerJson);
125
-
126
- case 6:
127
- case "end":
128
- return _context3.stop();
129
- }
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
130
8
  }
131
- }, _callee3, this);
132
- }));
133
-
134
- function getSwaggerJson() {
135
- return _getSwaggerJson.apply(this, arguments);
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
136
47
  }
137
-
138
- return getSwaggerJson;
139
- }();
140
-
141
- _proto.getYApiData = /*#__PURE__*/function () {
142
- var _getYApiData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4() {
143
- return _regenerator.default.wrap(function _callee4$(_context4) {
144
- while (1) {
145
- switch (_context4.prev = _context4.next) {
146
- case 0:
147
- if (!(0, _vtils.isEmpty)(this.yapiData)) {
148
- _context4.next = 8;
149
- break;
150
- }
151
-
152
- _context4.t0 = _swaggerJsonToYApiData.swaggerJsonToYApiData;
153
- _context4.next = 4;
154
- return this.getSwaggerJson();
155
-
156
- case 4:
157
- _context4.t1 = _context4.sent;
158
- _context4.next = 7;
159
- return (0, _context4.t0)(_context4.t1);
160
-
161
- case 7:
162
- this.yapiData = _context4.sent;
163
-
164
- case 8:
165
- return _context4.abrupt("return", this.yapiData);
166
-
167
- case 9:
168
- case "end":
169
- return _context4.stop();
170
- }
171
- }
172
- }, _callee4, this);
173
- }));
174
-
175
- function getYApiData() {
176
- return _getYApiData.apply(this, arguments);
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.SwaggerToYApiServer = void 0;
54
+ var get_port_1 = __importDefault(require("get-port"));
55
+ var http_1 = __importDefault(require("http"));
56
+ var signal_exit_1 = __importDefault(require("signal-exit"));
57
+ var url_1 = __importDefault(require("url"));
58
+ var utils_1 = require("./utils");
59
+ var vtils_1 = require("vtils");
60
+ var swaggerJsonToYApiData_1 = require("./swaggerJsonToYApiData");
61
+ var SwaggerToYApiServer = /** @class */ (function () {
62
+ function SwaggerToYApiServer(options) {
63
+ this.options = options;
64
+ this.port = 0;
65
+ this.swaggerJson = {};
66
+ this.httpServer = null;
67
+ this.yapiData = {};
177
68
  }
178
-
179
- return getYApiData;
180
- }();
181
-
182
- _proto.start = /*#__PURE__*/function () {
183
- var _start = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7() {
184
- var _this = this;
185
-
186
- var yapiData;
187
- return _regenerator.default.wrap(function _callee7$(_context7) {
188
- while (1) {
189
- switch (_context7.prev = _context7.next) {
190
- case 0:
191
- _context7.next = 2;
192
- return this.getYApiData();
193
-
194
- case 2:
195
- yapiData = _context7.sent;
196
- _context7.next = 5;
197
- return new Promise( /*#__PURE__*/function () {
198
- var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(resolve) {
199
- return _regenerator.default.wrap(function _callee6$(_context6) {
200
- while (1) {
201
- switch (_context6.prev = _context6.next) {
202
- case 0:
203
- _context6.t0 = _http.default.createServer( /*#__PURE__*/function () {
204
- var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(req, res) {
205
- var _url$parse, pathname;
206
-
207
- return _regenerator.default.wrap(function _callee5$(_context5) {
208
- while (1) {
209
- switch (_context5.prev = _context5.next) {
210
- case 0:
211
- _url$parse = _url.default.parse(req.url || ''), pathname = _url$parse.pathname;
212
- res.setHeader('Content-Type', 'application/json');
213
-
214
- if (pathname.includes('/api/plugin/export')) {
215
- res.end(JSON.stringify(yapiData.cats.map(function (cat) {
216
- return (0, _extends2.default)({}, cat, {
217
- list: yapiData.interfaces.filter(function (item) {
218
- return item.catid === cat._id;
219
- })
220
- });
221
- })));
222
- } else if (pathname.includes('/api/interface/getCatMenu')) {
223
- res.end(JSON.stringify({
224
- errcode: 0,
225
- errmsg: '成功!',
226
- data: yapiData.cats
227
- }));
228
- } else if (pathname.includes('/api/project/get')) {
229
- res.end(JSON.stringify({
230
- errcode: 0,
231
- errmsg: '成功!',
232
- data: yapiData.project
233
- }));
234
- } else {
235
- res.end('404');
236
- }
237
-
238
- case 3:
239
- case "end":
240
- return _context5.stop();
241
- }
69
+ SwaggerToYApiServer.prototype.getPort = function () {
70
+ return __awaiter(this, void 0, void 0, function () {
71
+ var _a;
72
+ return __generator(this, function (_b) {
73
+ switch (_b.label) {
74
+ case 0:
75
+ if (!(this.port === 0)) return [3 /*break*/, 2];
76
+ _a = this;
77
+ return [4 /*yield*/, (0, get_port_1.default)({
78
+ port: 50505,
79
+ })];
80
+ case 1:
81
+ _a.port = _b.sent();
82
+ _b.label = 2;
83
+ case 2: return [2 /*return*/, this.port];
84
+ }
85
+ });
86
+ });
87
+ };
88
+ SwaggerToYApiServer.prototype.getUrl = function () {
89
+ return __awaiter(this, void 0, void 0, function () {
90
+ var _a;
91
+ return __generator(this, function (_b) {
92
+ switch (_b.label) {
93
+ case 0:
94
+ _a = "http://127.0.0.1:".concat;
95
+ return [4 /*yield*/, this.getPort()];
96
+ case 1: return [2 /*return*/, _a.apply("http://127.0.0.1:", [_b.sent()])];
97
+ }
98
+ });
99
+ });
100
+ };
101
+ SwaggerToYApiServer.prototype.getSwaggerJson = function () {
102
+ return __awaiter(this, void 0, void 0, function () {
103
+ var res;
104
+ return __generator(this, function (_a) {
105
+ switch (_a.label) {
106
+ case 0:
107
+ if (!(0, vtils_1.isEmpty)(this.swaggerJson)) return [3 /*break*/, 2];
108
+ return [4 /*yield*/, (0, utils_1.httpGet)(this.options.swaggerJsonUrl)];
109
+ case 1:
110
+ res = _a.sent();
111
+ this.swaggerJson = res;
112
+ _a.label = 2;
113
+ case 2: return [2 /*return*/, this.swaggerJson];
114
+ }
115
+ });
116
+ });
117
+ };
118
+ SwaggerToYApiServer.prototype.getYApiData = function () {
119
+ return __awaiter(this, void 0, void 0, function () {
120
+ var _a, _b;
121
+ return __generator(this, function (_c) {
122
+ switch (_c.label) {
123
+ case 0:
124
+ if (!(0, vtils_1.isEmpty)(this.yapiData)) return [3 /*break*/, 3];
125
+ _a = this;
126
+ _b = swaggerJsonToYApiData_1.swaggerJsonToYApiData;
127
+ return [4 /*yield*/, this.getSwaggerJson()];
128
+ case 1: return [4 /*yield*/, _b.apply(void 0, [_c.sent()])];
129
+ case 2:
130
+ _a.yapiData = _c.sent();
131
+ _c.label = 3;
132
+ case 3: return [2 /*return*/, this.yapiData];
133
+ }
134
+ });
135
+ });
136
+ };
137
+ SwaggerToYApiServer.prototype.start = function () {
138
+ return __awaiter(this, void 0, void 0, function () {
139
+ var yapiData;
140
+ var _this = this;
141
+ return __generator(this, function (_a) {
142
+ switch (_a.label) {
143
+ case 0: return [4 /*yield*/, this.getYApiData()
144
+ // eslint-disable-next-line no-async-promise-executor
145
+ ];
146
+ case 1:
147
+ yapiData = _a.sent();
148
+ // eslint-disable-next-line no-async-promise-executor
149
+ return [4 /*yield*/, new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () {
150
+ var _a, _b, _c;
151
+ var _this = this;
152
+ return __generator(this, function (_d) {
153
+ switch (_d.label) {
154
+ case 0:
155
+ _a = this;
156
+ _c = (_b = http_1.default
157
+ .createServer(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
158
+ var pathname;
159
+ return __generator(this, function (_a) {
160
+ pathname = url_1.default.parse(req.url || '').pathname;
161
+ res.setHeader('Content-Type', 'application/json');
162
+ if (pathname.includes('/api/plugin/export')) {
163
+ res.end(JSON.stringify(yapiData.cats.map(function (cat) { return (__assign(__assign({}, cat), { list: yapiData.interfaces.filter(function (item) { return item.catid === cat._id; }) })); })));
164
+ }
165
+ else if (pathname.includes('/api/interface/getCatMenu')) {
166
+ res.end(JSON.stringify({
167
+ errcode: 0,
168
+ errmsg: '成功!',
169
+ data: yapiData.cats,
170
+ }));
171
+ }
172
+ else if (pathname.includes('/api/project/get')) {
173
+ res.end(JSON.stringify({
174
+ errcode: 0,
175
+ errmsg: '成功!',
176
+ data: yapiData.project,
177
+ }));
178
+ }
179
+ else {
180
+ res.end('404');
181
+ }
182
+ return [2 /*return*/];
183
+ });
184
+ }); }))
185
+ .listen;
186
+ return [4 /*yield*/, this.getPort()];
187
+ case 1:
188
+ _a.httpServer = _c.apply(_b, [_d.sent(), '127.0.0.1', function () {
189
+ (0, signal_exit_1.default)(function () { return _this.stop(); });
190
+ resolve();
191
+ }]);
192
+ return [2 /*return*/];
193
+ }
194
+ });
195
+ }); })];
196
+ case 2:
197
+ // eslint-disable-next-line no-async-promise-executor
198
+ _a.sent();
199
+ return [2 /*return*/, this.getUrl()];
200
+ }
201
+ });
202
+ });
203
+ };
204
+ SwaggerToYApiServer.prototype.stop = function () {
205
+ return __awaiter(this, void 0, void 0, function () {
206
+ var _this = this;
207
+ return __generator(this, function (_a) {
208
+ return [2 /*return*/, new Promise(function (resolve, reject) {
209
+ if (!_this.httpServer) {
210
+ resolve();
211
+ }
212
+ else {
213
+ _this.httpServer.close(function (err) {
214
+ if (err) {
215
+ reject(err);
216
+ }
217
+ else {
218
+ resolve();
242
219
  }
243
- }, _callee5);
244
- }));
245
-
246
- return function (_x2, _x3) {
247
- return _ref2.apply(this, arguments);
248
- };
249
- }());
250
- _context6.next = 3;
251
- return _this.getPort();
252
-
253
- case 3:
254
- _context6.t1 = _context6.sent;
255
-
256
- _context6.t2 = function () {
257
- (0, _signalExit.default)(function () {
258
- return _this.stop();
259
220
  });
260
- resolve();
261
- };
262
-
263
- _this.httpServer = _context6.t0.listen.call(_context6.t0, _context6.t1, '127.0.0.1', _context6.t2);
264
-
265
- case 6:
266
- case "end":
267
- return _context6.stop();
268
- }
269
- }
270
- }, _callee6);
271
- }));
272
-
273
- return function (_x) {
274
- return _ref.apply(this, arguments);
275
- };
276
- }());
277
-
278
- case 5:
279
- return _context7.abrupt("return", this.getUrl());
280
-
281
- case 6:
282
- case "end":
283
- return _context7.stop();
284
- }
285
- }
286
- }, _callee7, this);
287
- }));
288
-
289
- function start() {
290
- return _start.apply(this, arguments);
291
- }
292
-
293
- return start;
294
- }();
295
-
296
- _proto.stop = /*#__PURE__*/function () {
297
- var _stop = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee8() {
298
- var _this2 = this;
299
-
300
- return _regenerator.default.wrap(function _callee8$(_context8) {
301
- while (1) {
302
- switch (_context8.prev = _context8.next) {
303
- case 0:
304
- return _context8.abrupt("return", new Promise(function (resolve, reject) {
305
- if (!_this2.httpServer) {
306
- resolve();
307
- } else {
308
- _this2.httpServer.close(function (err) {
309
- if (err) {
310
- reject(err);
311
- } else {
312
- resolve();
313
- }
314
- });
315
- }
316
- }));
317
-
318
- case 1:
319
- case "end":
320
- return _context8.stop();
321
- }
322
- }
323
- }, _callee8);
324
- }));
325
-
326
- function stop() {
327
- return _stop.apply(this, arguments);
328
- }
329
-
330
- return stop;
331
- }();
332
-
333
- return SwaggerToYApiServer;
334
- }();
335
-
336
- exports.SwaggerToYApiServer = SwaggerToYApiServer;
221
+ }
222
+ })];
223
+ });
224
+ });
225
+ };
226
+ return SwaggerToYApiServer;
227
+ }());
228
+ exports.SwaggerToYApiServer = SwaggerToYApiServer;
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ export declare function run(cmd: string | undefined, options?: {
3
+ configFile?: string;
4
+ }): Promise<void>;