swagger2api-v3 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/README.md +471 -0
- package/dist/api/authcontroller/index.d.ts +11 -0
- package/dist/api/authcontroller/index.js +21 -0
- package/dist/api/index.d.ts +8 -0
- package/dist/api/index.js +24 -0
- package/dist/api/menucontroller/index.d.ts +9 -0
- package/dist/api/menucontroller/index.js +18 -0
- package/dist/api/types.d.ts +65 -0
- package/dist/api/types.js +2 -0
- package/dist/api/usercontroller/index.d.ts +41 -0
- package/dist/api/usercontroller/index.js +73 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +200 -0
- package/dist/core/generator.d.ts +97 -0
- package/dist/core/generator.js +412 -0
- package/dist/core/parser.d.ts +61 -0
- package/dist/core/parser.js +224 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +155 -0
- package/dist/types/index.d.ts +363 -0
- package/dist/types/index.js +5 -0
- package/dist/utils/index.d.ts +82 -0
- package/dist/utils/index.js +318 -0
- package/dist/utils/request.d.ts +17 -0
- package/dist/utils/request.js +49 -0
- package/package.json +68 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* UserController API 接口
|
|
4
|
+
* 此文件由 swagger2api 自动生成,请勿手动修改
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.userControllerDetail1 = exports.userControllerDetail = exports.userControllerUpdate = exports.userControllerCreate = exports.userControllerList = void 0;
|
|
8
|
+
const request_1 = require("../../utils/request");
|
|
9
|
+
/**
|
|
10
|
+
* 用户列表
|
|
11
|
+
*
|
|
12
|
+
* @param body
|
|
13
|
+
*/
|
|
14
|
+
const userControllerList = (data, config) => {
|
|
15
|
+
return request_1.request.post({
|
|
16
|
+
url: '/admin/system/user/list',
|
|
17
|
+
data,
|
|
18
|
+
...config
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
exports.userControllerList = userControllerList;
|
|
22
|
+
/**
|
|
23
|
+
* 创建用户
|
|
24
|
+
*
|
|
25
|
+
* @param body
|
|
26
|
+
*/
|
|
27
|
+
const userControllerCreate = (data, config) => {
|
|
28
|
+
return request_1.request.post({
|
|
29
|
+
url: '/admin/system/user/create',
|
|
30
|
+
data,
|
|
31
|
+
...config
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
exports.userControllerCreate = userControllerCreate;
|
|
35
|
+
/**
|
|
36
|
+
* 更新用户
|
|
37
|
+
*
|
|
38
|
+
* @param body
|
|
39
|
+
*/
|
|
40
|
+
const userControllerUpdate = (data, config) => {
|
|
41
|
+
return request_1.request.post({
|
|
42
|
+
url: '/admin/system/user/update',
|
|
43
|
+
data,
|
|
44
|
+
...config
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
exports.userControllerUpdate = userControllerUpdate;
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
* @param pageNum 页码
|
|
51
|
+
* @param pageSize 每页数量
|
|
52
|
+
* @param username 用户名
|
|
53
|
+
*/
|
|
54
|
+
const userControllerDetail = (params, config) => {
|
|
55
|
+
return request_1.request.get({
|
|
56
|
+
url: '/admin/system/user/detail',
|
|
57
|
+
params,
|
|
58
|
+
...config
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
exports.userControllerDetail = userControllerDetail;
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
* @param id
|
|
65
|
+
*/
|
|
66
|
+
const userControllerDetail1 = (params, config) => {
|
|
67
|
+
return request_1.request.get({
|
|
68
|
+
url: '/admin/system/user/detail1',
|
|
69
|
+
params,
|
|
70
|
+
...config
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
exports.userControllerDetail1 = userControllerDetail1;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
const commander_1 = require("commander");
|
|
38
|
+
const path = __importStar(require("path"));
|
|
39
|
+
const fs = __importStar(require("fs"));
|
|
40
|
+
const index_1 = require("../index");
|
|
41
|
+
const program = new commander_1.Command();
|
|
42
|
+
// 版本信息
|
|
43
|
+
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf-8'));
|
|
44
|
+
program
|
|
45
|
+
.name('swagger2api')
|
|
46
|
+
.description('从 Swagger/OpenAPI 文档生成 TypeScript API 接口')
|
|
47
|
+
.version(packageJson.version);
|
|
48
|
+
// generate 命令
|
|
49
|
+
program
|
|
50
|
+
.command('generate')
|
|
51
|
+
.alias('gen')
|
|
52
|
+
.description('根据配置文件生成 API 接口')
|
|
53
|
+
.option('-c, --config <path>', '配置文件路径', '.swagger.config.ts')
|
|
54
|
+
.option('-i, --input <path>', 'Swagger JSON 文件路径或 URL')
|
|
55
|
+
.option('-o, --output <path>', '输出目录')
|
|
56
|
+
.option('--no-types', '不生成类型文件')
|
|
57
|
+
.option('--no-group', '不按标签分组')
|
|
58
|
+
.action(async (options) => {
|
|
59
|
+
try {
|
|
60
|
+
if (options.input && options.output) {
|
|
61
|
+
// 使用命令行参数直接生成
|
|
62
|
+
const config = {
|
|
63
|
+
input: options.input,
|
|
64
|
+
output: options.output,
|
|
65
|
+
generator: 'typescript',
|
|
66
|
+
groupByTags: options.group !== false,
|
|
67
|
+
options: {
|
|
68
|
+
generateModels: options.types !== false,
|
|
69
|
+
generateApis: true,
|
|
70
|
+
generateIndex: true,
|
|
71
|
+
useAxios: true,
|
|
72
|
+
addComments: true,
|
|
73
|
+
prettify: true
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const { generate } = await Promise.resolve().then(() => __importStar(require('../index')));
|
|
77
|
+
await generate(config);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
// 使用配置文件生成
|
|
81
|
+
await (0, index_1.generateFromConfig)(options.config);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
console.error('❌ 生成失败:', error);
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
// init 命令
|
|
90
|
+
program
|
|
91
|
+
.command('init')
|
|
92
|
+
.description('初始化配置文件')
|
|
93
|
+
.option('-f, --force', '强制覆盖已存在的配置文件')
|
|
94
|
+
.action(async (options) => {
|
|
95
|
+
const configPath = path.resolve(process.cwd(), '.swagger.config.ts');
|
|
96
|
+
if (fs.existsSync(configPath) && !options.force) {
|
|
97
|
+
console.error('❌ 配置文件已存在,使用 --force 参数强制覆盖');
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
const configTemplate = `/**
|
|
101
|
+
* Swagger2API 配置文件
|
|
102
|
+
* 用于配置从 Swagger JSON 生成前端接口的参数
|
|
103
|
+
*/
|
|
104
|
+
const config = {
|
|
105
|
+
// Swagger JSON 文件路径或 URL
|
|
106
|
+
input: 'https://petstore.swagger.io/v2/swagger.json',
|
|
107
|
+
|
|
108
|
+
// 输出目录
|
|
109
|
+
output: './src/api',
|
|
110
|
+
|
|
111
|
+
// request 导入路径模板
|
|
112
|
+
importTemplate: "import { request } from '@/utils/request';",
|
|
113
|
+
|
|
114
|
+
// 生成器类型
|
|
115
|
+
generator: 'typescript',
|
|
116
|
+
|
|
117
|
+
// 按标签分组生成文件
|
|
118
|
+
groupByTags: true,
|
|
119
|
+
|
|
120
|
+
// 是否覆盖更新,默认为true。为true时会先删除输出目录下的所有文件
|
|
121
|
+
overwrite: true,
|
|
122
|
+
|
|
123
|
+
// 接口路径公共前缀,默认为空字符串
|
|
124
|
+
prefix: '',
|
|
125
|
+
|
|
126
|
+
// 代码格式化命令(可选)
|
|
127
|
+
lint: 'prettier --write',
|
|
128
|
+
|
|
129
|
+
// 生成选项
|
|
130
|
+
options: {
|
|
131
|
+
// 是否添加注释
|
|
132
|
+
addComments: true
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
module.exports = config;
|
|
137
|
+
`;
|
|
138
|
+
try {
|
|
139
|
+
fs.writeFileSync(configPath, configTemplate, 'utf-8');
|
|
140
|
+
console.log('✅ 配置文件已创建:', configPath);
|
|
141
|
+
console.log('💡 请根据需要修改配置文件,然后运行 swagger2api generate');
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.error('❌ 创建配置文件失败:', error);
|
|
145
|
+
process.exit(1);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
// validate 命令
|
|
149
|
+
program
|
|
150
|
+
.command('validate')
|
|
151
|
+
.description('验证配置文件')
|
|
152
|
+
.option('-c, --config <path>', '配置文件路径', '.swagger.config.ts')
|
|
153
|
+
.action(async (options) => {
|
|
154
|
+
const configPath = path.resolve(process.cwd(), options.config);
|
|
155
|
+
try {
|
|
156
|
+
const configModule = await Promise.resolve(`${configPath}`).then(s => __importStar(require(s)));
|
|
157
|
+
const config = configModule.default || configModule;
|
|
158
|
+
const { Swagger2API } = await Promise.resolve().then(() => __importStar(require('../index')));
|
|
159
|
+
const swagger2api = new Swagger2API(config);
|
|
160
|
+
if (swagger2api.validateConfig()) {
|
|
161
|
+
console.log('✅ 配置文件验证通过');
|
|
162
|
+
// 尝试加载 Swagger 文档
|
|
163
|
+
try {
|
|
164
|
+
const { loadSwaggerDocument } = await Promise.resolve().then(() => __importStar(require('../utils')));
|
|
165
|
+
const document = await loadSwaggerDocument(config.input);
|
|
166
|
+
console.log(`✅ Swagger 文档加载成功: ${document.info.title} v${document.info.version}`);
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
console.warn('⚠️ Swagger 文档加载失败:', error);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
process.exit(1);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
console.error('❌ 配置文件验证失败:', error);
|
|
178
|
+
process.exit(1);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
// run 命令(别名,兼容性)
|
|
182
|
+
program
|
|
183
|
+
.command('run')
|
|
184
|
+
.description('运行生成器(generate 命令的别名)')
|
|
185
|
+
.option('-c, --config <path>', '配置文件路径', '.swagger.config.ts')
|
|
186
|
+
.action(async (options) => {
|
|
187
|
+
try {
|
|
188
|
+
await (0, index_1.generateFromConfig)(options.config);
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
console.error('❌ 生成失败:', error);
|
|
192
|
+
process.exit(1);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
// 解析命令行参数
|
|
196
|
+
program.parse();
|
|
197
|
+
// 如果没有提供命令,显示帮助信息
|
|
198
|
+
if (!process.argv.slice(2).length) {
|
|
199
|
+
program.outputHelp();
|
|
200
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { SwaggerConfig, ApiInfo, TypeInfo } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* 代码生成器
|
|
4
|
+
*/
|
|
5
|
+
export declare class CodeGenerator {
|
|
6
|
+
private config;
|
|
7
|
+
constructor(config: SwaggerConfig);
|
|
8
|
+
/**
|
|
9
|
+
* 生成所有文件
|
|
10
|
+
* @param apis API接口数组
|
|
11
|
+
* @param types 类型定义数组
|
|
12
|
+
* @param groupedApis 按标签分组的API
|
|
13
|
+
*/
|
|
14
|
+
generateAll(apis: ApiInfo[], types: TypeInfo[], groupedApis: Map<string, ApiInfo[]>): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* 生成类型定义文件
|
|
17
|
+
* @param types 类型定义数组
|
|
18
|
+
*/
|
|
19
|
+
private generateTypesFile;
|
|
20
|
+
/**
|
|
21
|
+
* 生成类型文件内容
|
|
22
|
+
* @param types 类型定义数组
|
|
23
|
+
* @returns 类型文件内容
|
|
24
|
+
*/
|
|
25
|
+
private generateTypesContent;
|
|
26
|
+
/**
|
|
27
|
+
* 按标签生成API文件
|
|
28
|
+
* @param groupedApis 按标签分组的API
|
|
29
|
+
* @param types 类型定义数组
|
|
30
|
+
*/
|
|
31
|
+
private generateApiFilesByTags;
|
|
32
|
+
/**
|
|
33
|
+
* 生成单个API文件
|
|
34
|
+
* @param apis API接口数组
|
|
35
|
+
* @param types 类型定义数组
|
|
36
|
+
*/
|
|
37
|
+
private generateSingleApiFile;
|
|
38
|
+
/**
|
|
39
|
+
* 生成API文件内容
|
|
40
|
+
* @param apis API接口数组
|
|
41
|
+
* @param types 类型定义数组
|
|
42
|
+
* @param tag 标签名称(可选)
|
|
43
|
+
* @returns API文件内容
|
|
44
|
+
*/
|
|
45
|
+
private generateApiFileContent;
|
|
46
|
+
/**
|
|
47
|
+
* 生成单个API函数
|
|
48
|
+
* @param api API接口信息
|
|
49
|
+
* @returns API函数代码
|
|
50
|
+
*/
|
|
51
|
+
private generateApiFunction;
|
|
52
|
+
/**
|
|
53
|
+
* 生成直接参数形式
|
|
54
|
+
* @param parameters Swagger参数数组
|
|
55
|
+
* @returns 函数参数字符串
|
|
56
|
+
*/
|
|
57
|
+
private generateDirectParameters;
|
|
58
|
+
/**
|
|
59
|
+
* 从schema获取类型
|
|
60
|
+
* @param schema Swagger schema
|
|
61
|
+
* @returns 类型字符串
|
|
62
|
+
*/
|
|
63
|
+
private getTypeFromSchema;
|
|
64
|
+
/**
|
|
65
|
+
* 收集API数组中实际使用的类型
|
|
66
|
+
* @param apis API接口数组
|
|
67
|
+
* @returns 使用的类型名称数组
|
|
68
|
+
*/
|
|
69
|
+
private collectUsedTypes;
|
|
70
|
+
/**
|
|
71
|
+
* 判断是否为基础类型
|
|
72
|
+
* @param type 类型名称
|
|
73
|
+
* @returns 是否为基础类型
|
|
74
|
+
*/
|
|
75
|
+
private isPrimitiveType;
|
|
76
|
+
/**
|
|
77
|
+
* 生成请求配置
|
|
78
|
+
* @param api API接口信息
|
|
79
|
+
* @returns 请求配置代码
|
|
80
|
+
*/
|
|
81
|
+
private generateRequestConfig;
|
|
82
|
+
/**
|
|
83
|
+
* 生成入口文件
|
|
84
|
+
* @param groupedApis 按标签分组的API
|
|
85
|
+
*/
|
|
86
|
+
private generateIndexFile;
|
|
87
|
+
/**
|
|
88
|
+
* 获取标签对应的文件名
|
|
89
|
+
* @param tag 标签名
|
|
90
|
+
* @returns 文件名
|
|
91
|
+
*/
|
|
92
|
+
private getTagFileName;
|
|
93
|
+
/**
|
|
94
|
+
* 在所有文件生成完成后运行lint命令
|
|
95
|
+
*/
|
|
96
|
+
private runLintOnAllFiles;
|
|
97
|
+
}
|