@fxri/swagger2api 1.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.
- package/.swaggerrc +5 -0
- package/README.md +82 -0
- package/dist/api/index.d.ts +18474 -0
- package/dist/api/index.js +10908 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +84 -0
- package/dist/utils/common.d.ts +4 -0
- package/dist/utils/common.js +16 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/method.d.ts +2 -0
- package/dist/utils/method.js +7 -0
- package/package.json +43 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { execSync } from "node:child_process";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import prompts from "prompts";
|
|
6
|
+
import { getParam, isUrl, saveFile } from "./utils/index.js";
|
|
7
|
+
/** 配置集合 */
|
|
8
|
+
let confs = { file: path.resolve(getParam("--config") ?? ".swaggerrc"), param: {} };
|
|
9
|
+
try {
|
|
10
|
+
confs.param = JSON.parse(fs.readFileSync(confs.file, "utf-8"));
|
|
11
|
+
}
|
|
12
|
+
catch (error) { }
|
|
13
|
+
// 输入配置
|
|
14
|
+
confs.param = await prompts([
|
|
15
|
+
{ name: "url", type: "text", message: "Swagger docs 地址:", initial: confs.param.url ?? "https://example.com/v3/api-docs" },
|
|
16
|
+
{ name: "output", type: "text", message: "输出地址:", initial: confs.param.output ?? "./src/api" },
|
|
17
|
+
{ name: "name", type: "text", message: "文件名:", initial: confs.param.name ?? "index" }
|
|
18
|
+
]);
|
|
19
|
+
// 检查配置
|
|
20
|
+
if ([confs.param.url, confs.param.output, confs.param.name].includes(undefined)) {
|
|
21
|
+
console.log("失败:手动退出");
|
|
22
|
+
process.exit();
|
|
23
|
+
}
|
|
24
|
+
/** 数据集合 */
|
|
25
|
+
let datas = {
|
|
26
|
+
/** 文件 */
|
|
27
|
+
file: `${confs.param.output}/${confs.param.name}-${new Date().getTime()}.json`,
|
|
28
|
+
/** 枚举 */
|
|
29
|
+
enum: {}
|
|
30
|
+
};
|
|
31
|
+
/** 生成 */
|
|
32
|
+
function onGenerate(data) {
|
|
33
|
+
// 检查格式
|
|
34
|
+
if (typeof data?.paths != "object")
|
|
35
|
+
return console.log("失败:缺少paths字段");
|
|
36
|
+
// 处理数据
|
|
37
|
+
Object.entries(data.paths).forEach((item) => {
|
|
38
|
+
Object.keys(item[1]).forEach((item2) => {
|
|
39
|
+
data.paths[item[0]][item2].operationId = `${item[0]}/${item2}`
|
|
40
|
+
.replace(/([^\}]\/)get$/, "$1query")
|
|
41
|
+
.replace(/\/\{.+?\}/, "")
|
|
42
|
+
.replace(/^\/[^\/]*/, "");
|
|
43
|
+
const last = data.paths[item[0]][item2].operationId.split("/");
|
|
44
|
+
if (typeof datas.enum[last.slice(-1)] == "number") {
|
|
45
|
+
datas.enum[last.slice(-1)]++;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
datas.enum[last.slice(-1)] = 1;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
console.log("就绪:", ...Object.entries(datas.enum).map((item) => item.join("-")));
|
|
53
|
+
// 保存文档
|
|
54
|
+
saveFile(datas.file, JSON.stringify(data));
|
|
55
|
+
// 生成接口
|
|
56
|
+
const param = ["--axios", `-n ${confs.param.name}.ts`, `-o ${confs.param.output}`, `-p ${datas.file}`, ...process.argv.filter((_item, index) => index > 1)];
|
|
57
|
+
console.log("执行:", execSync(`swagger-typescript-api generate ${param.join(" ")}`).toString());
|
|
58
|
+
// 删除文档
|
|
59
|
+
fs.unlinkSync(datas.file);
|
|
60
|
+
// 保存配置
|
|
61
|
+
saveFile(confs.file, JSON.stringify(confs.param, null, 4));
|
|
62
|
+
}
|
|
63
|
+
/** 运行 */
|
|
64
|
+
function onRun(url = confs.param.url) {
|
|
65
|
+
// 检查地址
|
|
66
|
+
if (isUrl(url)) {
|
|
67
|
+
// 请求文档
|
|
68
|
+
axios
|
|
69
|
+
.get(url)
|
|
70
|
+
.then((res) => onGenerate(res.data))
|
|
71
|
+
.catch((err) => console.log("失败:", err.status ? `接口异常,状态码${err.status}` : err));
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
try {
|
|
75
|
+
// 读取文档
|
|
76
|
+
onGenerate(JSON.parse(fs.readFileSync(path.resolve(url), "utf8")));
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
console.log("失败:", error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// 执行脚本
|
|
84
|
+
onRun();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/** 获取参数 */
|
|
4
|
+
export function getParam(name) {
|
|
5
|
+
!Array.isArray(name) && (name = [name]);
|
|
6
|
+
const index = process.argv.findIndex((item) => name.includes(item));
|
|
7
|
+
return index >= 0 ? process.argv[index + 1] : undefined;
|
|
8
|
+
}
|
|
9
|
+
/** 保存文件 */
|
|
10
|
+
export function saveFile(file, data) {
|
|
11
|
+
const dir = path.dirname(file);
|
|
12
|
+
if (!fs.existsSync(dir)) {
|
|
13
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
14
|
+
}
|
|
15
|
+
fs.writeFileSync(file, data);
|
|
16
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// +----------------------------------------------------------------------
|
|
2
|
+
// | Author 唐启云 <tqy@fxri.net>
|
|
3
|
+
// +----------------------------------------------------------------------
|
|
4
|
+
// | Category 方弦研究所
|
|
5
|
+
// +----------------------------------------------------------------------
|
|
6
|
+
/** 校验URL */
|
|
7
|
+
export const isUrl = (data) => /^(https?:\/\/|:?\/\/)/.test(data);
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fxri/swagger2api",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "基于 swagger-typescript-api,将 Swagger 文档自动转为 TypeScript 格式的 API 文件",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "唐启云",
|
|
8
|
+
"email": "tqy@fxri.net",
|
|
9
|
+
"url": "https://fxri.net:444/"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"bin": {
|
|
14
|
+
"@fxri/swagger2api": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
".swaggerrc",
|
|
20
|
+
"package.json"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"axios": "^1.12.2",
|
|
25
|
+
"prompts": "^2.4.2",
|
|
26
|
+
"swagger-typescript-api": "^13.2.14"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^24.2.0",
|
|
30
|
+
"@types/prompts": "^2.4.9",
|
|
31
|
+
"rimraf": "^6.0.1",
|
|
32
|
+
"typescript": "^5.9.3"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"registry": "https://registry.npmjs.org/"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"dev": "pnpm build && node ./dist/index.js",
|
|
39
|
+
"prebuild": "rimraf dist",
|
|
40
|
+
"build": "tsc",
|
|
41
|
+
"release": "pnpm build && pnpm publish"
|
|
42
|
+
}
|
|
43
|
+
}
|