yapi-to-typescript
Version:
根据 YApi 的接口定义生成 TypeScript/JavaScript 的接口类型及其请求函数代码。
317 lines (267 loc) • 10.5 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import getAvailablePort from 'get-port';
import http from 'http';
import onExit from 'signal-exit';
import url from 'url';
import { httpGet } from './utils';
import { isEmpty } from 'vtils';
import { swaggerJsonToYApiData } from './swaggerJsonToYApiData';
export var SwaggerToYApiServer = /*#__PURE__*/function () {
function SwaggerToYApiServer(options) {
this.options = options;
this.port = 0;
this.swaggerJson = {};
this.httpServer = null;
this.yapiData = {};
}
var _proto = SwaggerToYApiServer.prototype;
_proto.getPort = /*#__PURE__*/function () {
var _getPort = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!(this.port === 0)) {
_context.next = 4;
break;
}
_context.next = 3;
return getAvailablePort({
port: 50505
});
case 3:
this.port = _context.sent;
case 4:
return _context.abrupt("return", this.port);
case 5:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function getPort() {
return _getPort.apply(this, arguments);
}
return getPort;
}();
_proto.getUrl = /*#__PURE__*/function () {
var _getUrl = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return this.getPort();
case 2:
_context2.t0 = _context2.sent;
return _context2.abrupt("return", "http://127.0.0.1:" + _context2.t0);
case 4:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
function getUrl() {
return _getUrl.apply(this, arguments);
}
return getUrl;
}();
_proto.getSwaggerJson = /*#__PURE__*/function () {
var _getSwaggerJson = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
var res;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (!isEmpty(this.swaggerJson)) {
_context3.next = 5;
break;
}
_context3.next = 3;
return httpGet(this.options.swaggerJsonUrl);
case 3:
res = _context3.sent;
this.swaggerJson = res;
case 5:
return _context3.abrupt("return", this.swaggerJson);
case 6:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
function getSwaggerJson() {
return _getSwaggerJson.apply(this, arguments);
}
return getSwaggerJson;
}();
_proto.getYApiData = /*#__PURE__*/function () {
var _getYApiData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
if (!isEmpty(this.yapiData)) {
_context4.next = 8;
break;
}
_context4.t0 = swaggerJsonToYApiData;
_context4.next = 4;
return this.getSwaggerJson();
case 4:
_context4.t1 = _context4.sent;
_context4.next = 7;
return (0, _context4.t0)(_context4.t1);
case 7:
this.yapiData = _context4.sent;
case 8:
return _context4.abrupt("return", this.yapiData);
case 9:
case "end":
return _context4.stop();
}
}
}, _callee4, this);
}));
function getYApiData() {
return _getYApiData.apply(this, arguments);
}
return getYApiData;
}();
_proto.start = /*#__PURE__*/function () {
var _start = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
var _this = this;
var yapiData;
return _regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return this.getYApiData();
case 2:
yapiData = _context7.sent;
_context7.next = 5;
return new Promise( /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(resolve) {
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.t0 = http.createServer( /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(req, res) {
var _url$parse, pathname;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_url$parse = url.parse(req.url || ''), pathname = _url$parse.pathname;
res.setHeader('Content-Type', 'application/json');
if (pathname.includes('/api/plugin/export')) {
res.end(JSON.stringify(yapiData.cats.map(function (cat) {
return _extends({}, cat, {
list: yapiData.interfaces.filter(function (item) {
return item.catid === cat._id;
})
});
})));
} else if (pathname.includes('/api/interface/getCatMenu')) {
res.end(JSON.stringify({
errcode: 0,
errmsg: '成功!',
data: yapiData.cats
}));
} else if (pathname.includes('/api/project/get')) {
res.end(JSON.stringify({
errcode: 0,
errmsg: '成功!',
data: yapiData.project
}));
} else {
res.end('404');
}
case 3:
case "end":
return _context5.stop();
}
}
}, _callee5);
}));
return function (_x2, _x3) {
return _ref2.apply(this, arguments);
};
}());
_context6.next = 3;
return _this.getPort();
case 3:
_context6.t1 = _context6.sent;
_context6.t2 = function () {
onExit(function () {
return _this.stop();
});
resolve();
};
_this.httpServer = _context6.t0.listen.call(_context6.t0, _context6.t1, '127.0.0.1', _context6.t2);
case 6:
case "end":
return _context6.stop();
}
}
}, _callee6);
}));
return function (_x) {
return _ref.apply(this, arguments);
};
}());
case 5:
return _context7.abrupt("return", this.getUrl());
case 6:
case "end":
return _context7.stop();
}
}
}, _callee7, this);
}));
function start() {
return _start.apply(this, arguments);
}
return start;
}();
_proto.stop = /*#__PURE__*/function () {
var _stop = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
var _this2 = this;
return _regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
return _context8.abrupt("return", new Promise(function (resolve, reject) {
if (!_this2.httpServer) {
resolve();
} else {
_this2.httpServer.close(function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
}
}));
case 1:
case "end":
return _context8.stop();
}
}
}, _callee8);
}));
function stop() {
return _stop.apply(this, arguments);
}
return stop;
}();
return SwaggerToYApiServer;
}();