@fxri/swagger2api 1.0.11 → 1.0.13
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.
- package/README.md +21 -9
- package/dist/api/api.d.ts +1 -0
- package/dist/api/api.js +2 -0
- package/dist/api/index.d.ts +1217 -1217
- package/dist/api/index.js +1214 -1214
- package/dist/index.js +18 -8
- package/dist/utils/common.d.ts +19 -4
- package/dist/utils/common.js +48 -9
- package/package.json +6 -1
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import path from "node:path";
|
|
|
7
7
|
import prompts from "prompts";
|
|
8
8
|
import { getParam, getParamList, isUrl, saveFile } from "./utils/index.js";
|
|
9
9
|
/** 配置集合 */
|
|
10
|
-
|
|
10
|
+
const confs = {
|
|
11
11
|
/** 文件 */
|
|
12
12
|
file: path.resolve(getParam(["--config", "-c"]) ?? ".swaggerrc"),
|
|
13
13
|
/** 参数 */
|
|
@@ -17,19 +17,22 @@ try {
|
|
|
17
17
|
confs.param = JSON.parse(fs.readFileSync(confs.file, "utf-8"));
|
|
18
18
|
}
|
|
19
19
|
catch (error) { }
|
|
20
|
-
//
|
|
21
|
-
confs.param
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
// 检查快速模式
|
|
21
|
+
if (!getParam(["--quick", "-q"], false) || [confs.param.url, confs.param.output, confs.param.name].includes(undefined)) {
|
|
22
|
+
// 输入配置
|
|
23
|
+
confs.param = await prompts([
|
|
24
|
+
{ name: "url", type: "text", message: "Swagger docs 地址:", initial: confs.param.url ?? "https://example.com/v3/api-docs" },
|
|
25
|
+
{ name: "output", type: "text", message: "输出地址:", initial: confs.param.output ?? "./src/api" },
|
|
26
|
+
{ name: "name", type: "text", message: "文件名:", initial: confs.param.name ?? "index" }
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
26
29
|
// 检查配置
|
|
27
30
|
if ([confs.param.url, confs.param.output, confs.param.name].includes(undefined)) {
|
|
28
31
|
console.log("失败:手动退出");
|
|
29
32
|
process.exit();
|
|
30
33
|
}
|
|
31
34
|
/** 数据集合 */
|
|
32
|
-
|
|
35
|
+
const datas = {
|
|
33
36
|
/** 用户目录 */
|
|
34
37
|
home: os.homedir(),
|
|
35
38
|
/** 脚本 */
|
|
@@ -73,11 +76,18 @@ function onGenerate(data) {
|
|
|
73
76
|
console.log("就绪:", ...Object.entries(datas.enum).map((item) => item.join("-")));
|
|
74
77
|
// 保存文档
|
|
75
78
|
saveFile(datas.file, JSON.stringify(data));
|
|
79
|
+
// 获取移除文件参数
|
|
80
|
+
const rd = getParam(["--remove-d.ts", "-rd"], false);
|
|
76
81
|
// 生成接口
|
|
77
82
|
const param = ["generate", "-n", `${confs.param.name}.ts`, "-o", path.resolve(confs.param.output), "-p", datas.file, ...getParamList(false)];
|
|
78
83
|
console.log("执行:", execSync(`${datas.script} ${param.join(" ")}`, { cwd: datas.home }).toString());
|
|
79
84
|
// 删除文档
|
|
80
85
|
fs.unlinkSync(datas.file);
|
|
86
|
+
// 移除使用--js参数时生成的d.ts文件
|
|
87
|
+
if (rd) {
|
|
88
|
+
console.log("移除:", path.resolve(confs.param.output, `${confs.param.name}.d.ts`));
|
|
89
|
+
execSync(`npx rimraf ${path.resolve(confs.param.output, `${confs.param.name}.d.ts`)}`, { cwd: datas.home }).toString();
|
|
90
|
+
}
|
|
81
91
|
// 保存配置
|
|
82
92
|
saveFile(confs.file, JSON.stringify(confs.param, null, 4));
|
|
83
93
|
}
|
package/dist/utils/common.d.ts
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 获取参数
|
|
3
|
+
* @param name 参数名称
|
|
4
|
+
* @param next true取索引值,false取索引键
|
|
5
|
+
* @param use 配置已使用
|
|
6
|
+
* @return 选中的参数
|
|
7
|
+
*/
|
|
8
|
+
export declare function getParam(name: string | string[], next?: boolean, use?: boolean): string | boolean;
|
|
9
|
+
/**
|
|
10
|
+
* 获取参数列表
|
|
11
|
+
* @param used 是否已使用
|
|
12
|
+
* @return 参数数组
|
|
13
|
+
*/
|
|
4
14
|
export declare function getParamList(used?: boolean): string[];
|
|
5
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* 保存文件
|
|
17
|
+
* @param file 文件地址
|
|
18
|
+
* @param data 数据
|
|
19
|
+
* @return 无返回数据
|
|
20
|
+
*/
|
|
6
21
|
export declare function saveFile(file: any, data: any): void;
|
package/dist/utils/common.js
CHANGED
|
@@ -1,29 +1,68 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
/** 数据集合 */
|
|
4
|
-
|
|
4
|
+
const datas = {
|
|
5
5
|
/** 参数 */
|
|
6
6
|
param: process.argv.filter((_item, index) => index > 1),
|
|
7
7
|
/** 已使用 */
|
|
8
8
|
used: {}
|
|
9
9
|
};
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* 获取参数
|
|
12
|
+
* @param name 参数名称
|
|
13
|
+
* @param next true取索引值,false取索引键
|
|
14
|
+
* @param use 配置已使用
|
|
15
|
+
* @return 选中的参数
|
|
16
|
+
*/
|
|
11
17
|
export function getParam(name, next = true, use = true) {
|
|
12
18
|
// 处理名称
|
|
13
19
|
if (!Array.isArray(name))
|
|
14
20
|
name = [name];
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
//
|
|
18
|
-
|
|
21
|
+
// 配置数据
|
|
22
|
+
let data;
|
|
23
|
+
// 查询参数
|
|
24
|
+
datas.param.forEach((item, index) => {
|
|
25
|
+
// 查询索引
|
|
26
|
+
if (!name.includes(item)) {
|
|
27
|
+
// 空值
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
else if (next) {
|
|
31
|
+
// 取索引值
|
|
32
|
+
data = datas.param[index + 1];
|
|
33
|
+
// 配置已使用
|
|
34
|
+
use && Object.assign(datas.used, { [index + 1]: true });
|
|
35
|
+
}
|
|
36
|
+
else if (["true", "false"].includes(datas.param[index + 1])) {
|
|
37
|
+
// 取索引键,但存在索引值的话,则取索引值
|
|
38
|
+
data = datas.param[index + 1].toLowerCase() == "true";
|
|
39
|
+
// 配置已使用
|
|
40
|
+
use && Object.assign(datas.used, { [index + 1]: true });
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
// 取索引键
|
|
44
|
+
data = true;
|
|
45
|
+
}
|
|
46
|
+
// 配置已使用
|
|
47
|
+
use && Object.assign(datas.used, { [index]: true });
|
|
48
|
+
});
|
|
19
49
|
// 返回参数
|
|
20
|
-
return
|
|
50
|
+
return data;
|
|
21
51
|
}
|
|
22
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* 获取参数列表
|
|
54
|
+
* @param used 是否已使用
|
|
55
|
+
* @return 参数数组
|
|
56
|
+
*/
|
|
23
57
|
export function getParamList(used) {
|
|
24
58
|
return typeof used == "boolean" ? datas.param.filter((_item, index) => (used ? datas.used[index] : !datas.used[index])) : datas.param;
|
|
25
59
|
}
|
|
26
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* 保存文件
|
|
62
|
+
* @param file 文件地址
|
|
63
|
+
* @param data 数据
|
|
64
|
+
* @return 无返回数据
|
|
65
|
+
*/
|
|
27
66
|
export function saveFile(file, data) {
|
|
28
67
|
// 获取目录
|
|
29
68
|
const dir = path.dirname(file);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fxri/swagger2api",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13",
|
|
4
4
|
"description": "基于 swagger-typescript-api,将 Swagger 文档自动转为 TypeScript 格式的 API 文件。",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -28,6 +28,11 @@
|
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^24.2.0",
|
|
30
30
|
"@types/prompts": "^2.4.9",
|
|
31
|
+
"@typescript-eslint/eslint-plugin": "^6.6.0",
|
|
32
|
+
"@typescript-eslint/parser": "^6.6.0",
|
|
33
|
+
"eslint": "^8.11.0",
|
|
34
|
+
"eslint-config-prettier": "^9.0.0",
|
|
35
|
+
"eslint-plugin-vue": "^9.0.0",
|
|
31
36
|
"rimraf": "^6.0.1",
|
|
32
37
|
"typescript": "^5.9.3"
|
|
33
38
|
},
|