yapi-apifox-to-typescript 4.2.3 → 5.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,23 @@
1
+ import { AsyncReturnType } from './mini-ut';
2
+ import { swaggerJsonToYApiData } from './swaggerJsonToYApiData';
3
+ import { OpenAPIV2 as SwaggerType } from 'openapi-types';
4
+ export interface SwaggerToYApiServerOptions {
5
+ /** 远程 Swagger/OpenAPI JSON 地址(serverType = 'swagger' 时使用) */
6
+ swaggerJsonUrl?: string;
7
+ /** 本地 Swagger/OpenAPI JSON 文件路径(serverType = 'local' 时使用) */
8
+ swaggerJsonPath?: string;
9
+ }
10
+ export declare class SwaggerToYApiServer {
11
+ private readonly options;
12
+ private port;
13
+ private swaggerJson;
14
+ private httpServer;
15
+ private yapiData;
16
+ constructor(options: SwaggerToYApiServerOptions);
17
+ getPort(): Promise<number>;
18
+ getUrl(): Promise<string>;
19
+ getSwaggerJson(): Promise<SwaggerType.Document>;
20
+ getYApiData(): Promise<AsyncReturnType<typeof swaggerJsonToYApiData>>;
21
+ start(): Promise<string>;
22
+ stop(): Promise<void>;
23
+ }
@@ -1,317 +1,119 @@
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
- import getAvailablePort from 'get-port';
5
- import http from 'http';
6
- import onExit from 'signal-exit';
7
- import url from 'url';
8
- import { httpGet } from './utils';
9
- import { isEmpty } from 'vtils';
10
- import { swaggerJsonToYApiData } from './swaggerJsonToYApiData';
11
- export var SwaggerToYApiServer = /*#__PURE__*/function () {
12
- function SwaggerToYApiServer(options) {
13
- this.options = options;
14
- this.port = 0;
15
- this.swaggerJson = {};
16
- this.httpServer = null;
17
- this.yapiData = {};
18
- }
19
-
20
- var _proto = SwaggerToYApiServer.prototype;
21
-
22
- _proto.getPort = /*#__PURE__*/function () {
23
- var _getPort = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
24
- return _regeneratorRuntime.wrap(function _callee$(_context) {
25
- while (1) {
26
- switch (_context.prev = _context.next) {
27
- case 0:
28
- if (!(this.port === 0)) {
29
- _context.next = 4;
30
- break;
31
- }
32
-
33
- _context.next = 3;
34
- return getAvailablePort({
35
- port: 50505
36
- });
37
-
38
- case 3:
39
- this.port = _context.sent;
40
-
41
- case 4:
42
- return _context.abrupt("return", this.port);
43
-
44
- case 5:
45
- case "end":
46
- return _context.stop();
47
- }
48
- }
49
- }, _callee, this);
50
- }));
51
-
52
- function getPort() {
53
- return _getPort.apply(this, arguments);
54
- }
55
-
56
- return getPort;
57
- }();
58
-
59
- _proto.getUrl = /*#__PURE__*/function () {
60
- var _getUrl = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
61
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
62
- while (1) {
63
- switch (_context2.prev = _context2.next) {
64
- case 0:
65
- _context2.next = 2;
66
- return this.getPort();
67
-
68
- case 2:
69
- _context2.t0 = _context2.sent;
70
- return _context2.abrupt("return", "http://127.0.0.1:" + _context2.t0);
71
-
72
- case 4:
73
- case "end":
74
- return _context2.stop();
75
- }
76
- }
77
- }, _callee2, this);
78
- }));
79
-
80
- function getUrl() {
81
- return _getUrl.apply(this, arguments);
82
- }
83
-
84
- return getUrl;
85
- }();
86
-
87
- _proto.getSwaggerJson = /*#__PURE__*/function () {
88
- var _getSwaggerJson = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
89
- var res;
90
- return _regeneratorRuntime.wrap(function _callee3$(_context3) {
91
- while (1) {
92
- switch (_context3.prev = _context3.next) {
93
- case 0:
94
- if (!isEmpty(this.swaggerJson)) {
95
- _context3.next = 5;
96
- break;
97
- }
98
-
99
- _context3.next = 3;
100
- return httpGet(this.options.swaggerJsonUrl);
101
-
102
- case 3:
103
- res = _context3.sent;
104
- this.swaggerJson = res;
105
-
106
- case 5:
107
- return _context3.abrupt("return", this.swaggerJson);
108
-
109
- case 6:
110
- case "end":
111
- return _context3.stop();
112
- }
113
- }
114
- }, _callee3, this);
115
- }));
116
-
117
- function getSwaggerJson() {
118
- return _getSwaggerJson.apply(this, arguments);
119
- }
120
-
121
- return getSwaggerJson;
122
- }();
123
-
124
- _proto.getYApiData = /*#__PURE__*/function () {
125
- var _getYApiData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
126
- return _regeneratorRuntime.wrap(function _callee4$(_context4) {
127
- while (1) {
128
- switch (_context4.prev = _context4.next) {
129
- case 0:
130
- if (!isEmpty(this.yapiData)) {
131
- _context4.next = 8;
132
- break;
133
- }
134
-
135
- _context4.t0 = swaggerJsonToYApiData;
136
- _context4.next = 4;
137
- return this.getSwaggerJson();
138
-
139
- case 4:
140
- _context4.t1 = _context4.sent;
141
- _context4.next = 7;
142
- return (0, _context4.t0)(_context4.t1);
143
-
144
- case 7:
145
- this.yapiData = _context4.sent;
146
-
147
- case 8:
148
- return _context4.abrupt("return", this.yapiData);
149
-
150
- case 9:
151
- case "end":
152
- return _context4.stop();
153
- }
154
- }
155
- }, _callee4, this);
156
- }));
157
-
158
- function getYApiData() {
159
- return _getYApiData.apply(this, arguments);
160
- }
161
-
162
- return getYApiData;
163
- }();
164
-
165
- _proto.start = /*#__PURE__*/function () {
166
- var _start = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
167
- var _this = this;
168
-
169
- var yapiData;
170
- return _regeneratorRuntime.wrap(function _callee7$(_context7) {
171
- while (1) {
172
- switch (_context7.prev = _context7.next) {
173
- case 0:
174
- _context7.next = 2;
175
- return this.getYApiData();
176
-
177
- case 2:
178
- yapiData = _context7.sent;
179
- _context7.next = 5;
180
- return new Promise( /*#__PURE__*/function () {
181
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(resolve) {
182
- return _regeneratorRuntime.wrap(function _callee6$(_context6) {
183
- while (1) {
184
- switch (_context6.prev = _context6.next) {
185
- case 0:
186
- _context6.t0 = http.createServer( /*#__PURE__*/function () {
187
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(req, res) {
188
- var _url$parse, pathname;
189
-
190
- return _regeneratorRuntime.wrap(function _callee5$(_context5) {
191
- while (1) {
192
- switch (_context5.prev = _context5.next) {
193
- case 0:
194
- _url$parse = url.parse(req.url || ''), pathname = _url$parse.pathname;
195
- res.setHeader('Content-Type', 'application/json');
196
-
197
- if (pathname.includes('/api/plugin/export')) {
198
- res.end(JSON.stringify(yapiData.cats.map(function (cat) {
199
- return _extends({}, cat, {
200
- list: yapiData.interfaces.filter(function (item) {
201
- return item.catid === cat._id;
202
- })
203
- });
204
- })));
205
- } else if (pathname.includes('/api/interface/getCatMenu')) {
206
- res.end(JSON.stringify({
207
- errcode: 0,
208
- errmsg: '成功!',
209
- data: yapiData.cats
210
- }));
211
- } else if (pathname.includes('/api/project/get')) {
212
- res.end(JSON.stringify({
213
- errcode: 0,
214
- errmsg: '成功!',
215
- data: yapiData.project
216
- }));
217
- } else {
218
- res.end('404');
219
- }
220
-
221
- case 3:
222
- case "end":
223
- return _context5.stop();
224
- }
225
- }
226
- }, _callee5);
227
- }));
228
-
229
- return function (_x2, _x3) {
230
- return _ref2.apply(this, arguments);
231
- };
232
- }());
233
- _context6.next = 3;
234
- return _this.getPort();
235
-
236
- case 3:
237
- _context6.t1 = _context6.sent;
238
-
239
- _context6.t2 = function () {
240
- onExit(function () {
241
- return _this.stop();
242
- });
243
- resolve();
244
- };
245
-
246
- _this.httpServer = _context6.t0.listen.call(_context6.t0, _context6.t1, '127.0.0.1', _context6.t2);
247
-
248
- case 6:
249
- case "end":
250
- return _context6.stop();
251
- }
252
- }
253
- }, _callee6);
254
- }));
255
-
256
- return function (_x) {
257
- return _ref.apply(this, arguments);
258
- };
259
- }());
260
-
261
- case 5:
262
- return _context7.abrupt("return", this.getUrl());
263
-
264
- case 6:
265
- case "end":
266
- return _context7.stop();
267
- }
268
- }
269
- }, _callee7, this);
270
- }));
271
-
272
- function start() {
273
- return _start.apply(this, arguments);
274
- }
275
-
276
- return start;
277
- }();
278
-
279
- _proto.stop = /*#__PURE__*/function () {
280
- var _stop = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
281
- var _this2 = this;
282
-
283
- return _regeneratorRuntime.wrap(function _callee8$(_context8) {
284
- while (1) {
285
- switch (_context8.prev = _context8.next) {
286
- case 0:
287
- return _context8.abrupt("return", new Promise(function (resolve, reject) {
288
- if (!_this2.httpServer) {
289
- resolve();
290
- } else {
291
- _this2.httpServer.close(function (err) {
292
- if (err) {
293
- reject(err);
294
- } else {
295
- resolve();
296
- }
297
- });
298
- }
299
- }));
300
-
301
- case 1:
302
- case "end":
303
- return _context8.stop();
304
- }
305
- }
306
- }, _callee8);
307
- }));
308
-
309
- function stop() {
310
- return _stop.apply(this, arguments);
311
- }
312
-
313
- return stop;
314
- }();
315
-
316
- return SwaggerToYApiServer;
317
- }();
1
+ import { isEmpty } from "./mini-ut.js";
2
+ import { httpGet } from "./utils.js";
3
+ import { swaggerJsonToYApiData } from "./swaggerJsonToYApiData.js";
4
+ import fs from "fs-extra";
5
+ import path from "path";
6
+ import url from "url";
7
+ import getAvailablePort from "get-port";
8
+ import http from "http";
9
+ import { onExit } from "signal-exit";
10
+ //#region src/SwaggerToYApiServer.ts
11
+ var SwaggerToYApiServer = class {
12
+ constructor(options) {
13
+ this.options = options;
14
+ this.port = 0;
15
+ this.swaggerJson = {};
16
+ this.httpServer = null;
17
+ this.yapiData = {};
18
+ }
19
+ async getPort() {
20
+ if (this.port === 0) this.port = await getAvailablePort({ port: 50505 });
21
+ return this.port;
22
+ }
23
+ async getUrl() {
24
+ return `http://127.0.0.1:${await this.getPort()}`;
25
+ }
26
+ async getSwaggerJson() {
27
+ if (isEmpty(this.swaggerJson)) {
28
+ if (this.options.swaggerJsonPath) {
29
+ const absPath = path.resolve(this.options.swaggerJsonPath);
30
+ if (!await fs.pathExists(absPath)) throw new Error(`本地 OpenAPI/Swagger JSON 文件不存在: ${absPath}`);
31
+ if ((await fs.stat(absPath)).isDirectory()) {
32
+ const files = (await fs.readdir(absPath)).filter((f) => f.endsWith(".json")).sort();
33
+ if (files.length === 0) throw new Error(`目录中没有找到 JSON 文件: ${absPath}`);
34
+ const docs = await Promise.all(files.map(async (f) => {
35
+ const raw = await fs.readFile(path.join(absPath, f), "utf-8");
36
+ try {
37
+ return JSON.parse(raw);
38
+ } catch (err) {
39
+ throw new Error(`解析本地 OpenAPI/Swagger JSON 失败: ${path.join(absPath, f)}\n${err.message}`);
40
+ }
41
+ }));
42
+ this.swaggerJson = docs.reduce((acc, doc, i) => {
43
+ if (i === 0) return { ...doc };
44
+ return {
45
+ ...acc,
46
+ paths: {
47
+ ...acc.paths,
48
+ ...doc.paths
49
+ },
50
+ components: {
51
+ ...acc.components,
52
+ schemas: {
53
+ ...acc.components?.schemas,
54
+ ...doc.components?.schemas
55
+ }
56
+ }
57
+ };
58
+ });
59
+ } else {
60
+ const raw = await fs.readFile(absPath, "utf-8");
61
+ try {
62
+ this.swaggerJson = JSON.parse(raw);
63
+ } catch (err) {
64
+ throw new Error(`解析本地 OpenAPI/Swagger JSON 失败: ${absPath}\n${err.message}`);
65
+ }
66
+ }
67
+ } else if (this.options.swaggerJsonUrl) {
68
+ const res = await httpGet(this.options.swaggerJsonUrl);
69
+ this.swaggerJson = res;
70
+ } else throw new Error("SwaggerToYApiServer: 必须提供 swaggerJsonUrl 或 swaggerJsonPath");
71
+ }
72
+ return this.swaggerJson;
73
+ }
74
+ async getYApiData() {
75
+ if (isEmpty(this.yapiData)) this.yapiData = await swaggerJsonToYApiData(await this.getSwaggerJson());
76
+ return this.yapiData;
77
+ }
78
+ async start() {
79
+ const yapiData = await this.getYApiData();
80
+ await new Promise(async (resolve) => {
81
+ this.httpServer = http.createServer(async (req, res) => {
82
+ const { pathname } = url.parse(req.url || "");
83
+ res.setHeader("Content-Type", "application/json");
84
+ if (pathname.includes("/api/plugin/export")) res.end(JSON.stringify(yapiData.cats.map((cat) => ({
85
+ ...cat,
86
+ list: yapiData.interfaces.filter((item) => item.catid === cat._id)
87
+ }))));
88
+ else if (pathname.includes("/api/interface/getCatMenu")) res.end(JSON.stringify({
89
+ errcode: 0,
90
+ errmsg: "成功!",
91
+ data: yapiData.cats
92
+ }));
93
+ else if (pathname.includes("/api/project/get")) res.end(JSON.stringify({
94
+ errcode: 0,
95
+ errmsg: "成功!",
96
+ data: yapiData.project
97
+ }));
98
+ else res.end("404");
99
+ }).listen(await this.getPort(), "127.0.0.1", () => {
100
+ onExit(() => {
101
+ this.stop();
102
+ });
103
+ resolve();
104
+ });
105
+ });
106
+ return this.getUrl();
107
+ }
108
+ async stop() {
109
+ return new Promise((resolve, reject) => {
110
+ if (!this.httpServer) resolve();
111
+ else this.httpServer.close((err) => {
112
+ if (err) reject(err);
113
+ else resolve();
114
+ });
115
+ });
116
+ }
117
+ };
118
+ //#endregion
119
+ export { SwaggerToYApiServer };
@@ -0,0 +1,7 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) {
3
+ if (typeof require !== "undefined") return require.apply(this, arguments);
4
+ throw Error("Calling `require` for \"" + x + "\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.");
5
+ });
6
+ //#endregion
7
+ export { __require };
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ export declare function run(cmd: string | undefined, options?: {
3
+ configFile?: string;
4
+ }): Promise<void>;