@fxri/swagger2api 1.0.12 → 1.0.14
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 +16 -8
- package/dist/api/api.d.ts +1 -0
- package/dist/api/api.js +2 -0
- package/dist/index.js +11 -8
- package/dist/utils/common.d.ts +19 -4
- package/dist/utils/common.js +48 -9
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -48,10 +48,13 @@ npx swagger2api
|
|
|
48
48
|
| `output` | string | `'./src/api'` | 生成代码的输出目录 |
|
|
49
49
|
| `name` | string | `'index'` | 接口文件名称,默认后缀为ts |
|
|
50
50
|
| `--config,-c` | string | `'.swaggerrc'` | 配置文件 |
|
|
51
|
-
| `--
|
|
52
|
-
| `--
|
|
51
|
+
| `--quick,-q` | boolean | `false` | 快速模式,直接使用配置文件参数,若配置文件不存在或者参数不全,则不会进入该模式 |
|
|
52
|
+
| `--convert-get,-cg` | boolean | `false` | 转换无{.+}get为query |
|
|
53
|
+
| `--remove-param,-rp` | boolean | `false` | 移除{.+}参数 |
|
|
53
54
|
| `--remove-prefix-index,-rpi` | number | `-1` | 移除前缀索引 |
|
|
54
|
-
| `--remove-d.ts,-rd` |
|
|
55
|
+
| `--remove-d.ts,-rd` | boolean | `false` | 移除使用--js参数时生成的d.ts文件 |
|
|
56
|
+
|
|
57
|
+
扩展:[更多 swagger-typescript-api 配置选项](https://fig.io/manual/swagger-typescript-api)
|
|
55
58
|
|
|
56
59
|
## 🔧 CLI 命令
|
|
57
60
|
|
|
@@ -60,14 +63,19 @@ npx swagger2api
|
|
|
60
63
|
npx swagger2api [--config,-c <path>]
|
|
61
64
|
```
|
|
62
65
|
|
|
66
|
+
```bash
|
|
67
|
+
# 快速模式,直接使用配置文件参数,若配置文件不存在或者参数不全,则不会进入该模式
|
|
68
|
+
npx swagger2api [--quick,-q] [--quick,-q <boolean>]
|
|
69
|
+
```
|
|
70
|
+
|
|
63
71
|
```bash
|
|
64
72
|
# 转换无{.+}get为query
|
|
65
|
-
npx swagger2api [--convert-get,-cg]
|
|
73
|
+
npx swagger2api [--convert-get,-cg] [--convert-get,-cg <boolean>]
|
|
66
74
|
```
|
|
67
75
|
|
|
68
76
|
```bash
|
|
69
77
|
# 移除{.+}参数
|
|
70
|
-
npx swagger2api [--remove-param,-rp]
|
|
78
|
+
npx swagger2api [--remove-param,-rp] [--remove-param,-rp <boolean>]
|
|
71
79
|
```
|
|
72
80
|
|
|
73
81
|
```bash
|
|
@@ -77,19 +85,19 @@ npx swagger2api [--remove-prefix-index,-rpi <number>]
|
|
|
77
85
|
|
|
78
86
|
```bash
|
|
79
87
|
# 移除使用--js参数时生成的d.ts文件
|
|
80
|
-
npx swagger2api [--remove-d.ts,-rd]
|
|
88
|
+
npx swagger2api [--remove-d.ts,-rd] [--remove-d.ts,-rd <boolean>]
|
|
81
89
|
```
|
|
82
90
|
|
|
83
91
|
## 📝 PNPM 脚本
|
|
84
92
|
|
|
85
93
|
在 `package.json` 中添加:
|
|
86
94
|
|
|
87
|
-
生成 axios http
|
|
95
|
+
生成 axios http 客户端,生成有关请求响应的附加信息,移除第一个前缀索引,使用快速模式
|
|
88
96
|
|
|
89
97
|
```json
|
|
90
98
|
{
|
|
91
99
|
"scripts": {
|
|
92
|
-
"api": "swagger2api --axios --responses --remove-prefix-index 0",
|
|
100
|
+
"api": "swagger2api --axios --responses --remove-prefix-index 0 --quick",
|
|
93
101
|
}
|
|
94
102
|
}
|
|
95
103
|
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/api/api.js
ADDED
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
|
/** 脚本 */
|
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.14",
|
|
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
|
},
|