swagger2api-v3 1.1.6 → 1.1.8
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 +15 -1
- package/dist/.swagger2api.schema.json +120 -0
- package/dist/cli/index.js +119 -47
- package/dist/config/validator.d.ts +7 -0
- package/dist/config/validator.js +218 -0
- package/dist/core/generator.d.ts +7 -1
- package/dist/core/generator.js +26 -44
- package/dist/core/parser.d.ts +13 -1
- package/dist/core/parser.js +54 -40
- package/dist/index.d.ts +6 -0
- package/dist/index.js +21 -13
- package/dist/types/index.d.ts +75 -38
- package/dist/utils/comment.d.ts +8 -0
- package/dist/utils/comment.js +47 -0
- package/dist/utils/file.d.ts +23 -0
- package/dist/utils/file.js +98 -0
- package/dist/utils/index.d.ts +4 -99
- package/dist/utils/index.js +6 -572
- package/dist/utils/naming.d.ts +51 -0
- package/dist/utils/naming.js +124 -0
- package/dist/utils/type.d.ts +39 -0
- package/dist/utils/type.js +331 -0
- package/package.json +3 -2
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateApiComment = generateApiComment;
|
|
4
|
+
/**
|
|
5
|
+
* 生成接口注释
|
|
6
|
+
* @param operation Swagger操作对象
|
|
7
|
+
* @param parameters 参数列表
|
|
8
|
+
* @returns 注释字符串
|
|
9
|
+
*/
|
|
10
|
+
function generateApiComment(operation, parameters) {
|
|
11
|
+
const comments = ['/**'];
|
|
12
|
+
if (operation.summary) {
|
|
13
|
+
comments.push(` * ${operation.summary}`);
|
|
14
|
+
}
|
|
15
|
+
if (operation.description && operation.description !== operation.summary) {
|
|
16
|
+
comments.push(` * ${operation.description}`);
|
|
17
|
+
}
|
|
18
|
+
if (parameters && parameters.length > 0) {
|
|
19
|
+
const queryParams = parameters.filter((param) => param.in === 'query');
|
|
20
|
+
const pathParams = parameters.filter((param) => param.in === 'path');
|
|
21
|
+
const bodyParams = parameters.filter((param) => param.in === 'body');
|
|
22
|
+
const hasParams = queryParams.length > 0 || pathParams.length > 0;
|
|
23
|
+
const hasData = bodyParams.length > 0;
|
|
24
|
+
if (hasParams || hasData) {
|
|
25
|
+
comments.push(' *');
|
|
26
|
+
if (hasParams) {
|
|
27
|
+
const paramDescriptions = [...pathParams, ...queryParams]
|
|
28
|
+
.map((param) => param.description || '')
|
|
29
|
+
.filter((description) => description)
|
|
30
|
+
.join(', ');
|
|
31
|
+
const description = paramDescriptions || '请求参数';
|
|
32
|
+
comments.push(` * @param params ${description}`);
|
|
33
|
+
}
|
|
34
|
+
if (hasData) {
|
|
35
|
+
const dataParam = bodyParams[0];
|
|
36
|
+
const description = dataParam?.description || '请求数据';
|
|
37
|
+
comments.push(` * @param data ${description}`);
|
|
38
|
+
}
|
|
39
|
+
comments.push(` * @param config 可选的请求配置`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (operation.deprecated) {
|
|
43
|
+
comments.push(' * @deprecated');
|
|
44
|
+
}
|
|
45
|
+
comments.push(' */');
|
|
46
|
+
return comments.join('\n');
|
|
47
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SwaggerDocument } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* 确保目录存在,如果不存在则创建
|
|
4
|
+
* @param dirPath 目录路径
|
|
5
|
+
*/
|
|
6
|
+
export declare function ensureDirectoryExists(dirPath: string): void;
|
|
7
|
+
/**
|
|
8
|
+
* 删除目录及其所有内容
|
|
9
|
+
* @param dirPath 目录路径
|
|
10
|
+
*/
|
|
11
|
+
export declare function removeDirectory(dirPath: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* 读取Swagger文档
|
|
14
|
+
* @param input 文件路径或URL
|
|
15
|
+
* @returns Swagger文档对象
|
|
16
|
+
*/
|
|
17
|
+
export declare function loadSwaggerDocument(input: string): Promise<SwaggerDocument>;
|
|
18
|
+
/**
|
|
19
|
+
* 写入文件
|
|
20
|
+
* @param filePath 文件路径
|
|
21
|
+
* @param content 文件内容
|
|
22
|
+
*/
|
|
23
|
+
export declare function writeFile(filePath: string, content: string): void;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.ensureDirectoryExists = ensureDirectoryExists;
|
|
40
|
+
exports.removeDirectory = removeDirectory;
|
|
41
|
+
exports.loadSwaggerDocument = loadSwaggerDocument;
|
|
42
|
+
exports.writeFile = writeFile;
|
|
43
|
+
const fs = __importStar(require("fs"));
|
|
44
|
+
const path = __importStar(require("path"));
|
|
45
|
+
const axios_1 = __importDefault(require("axios"));
|
|
46
|
+
/**
|
|
47
|
+
* 确保目录存在,如果不存在则创建
|
|
48
|
+
* @param dirPath 目录路径
|
|
49
|
+
*/
|
|
50
|
+
function ensureDirectoryExists(dirPath) {
|
|
51
|
+
if (!fs.existsSync(dirPath)) {
|
|
52
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 删除目录及其所有内容
|
|
57
|
+
* @param dirPath 目录路径
|
|
58
|
+
*/
|
|
59
|
+
function removeDirectory(dirPath) {
|
|
60
|
+
if (fs.existsSync(dirPath)) {
|
|
61
|
+
fs.rmSync(dirPath, { recursive: true, force: true });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 读取Swagger文档
|
|
66
|
+
* @param input 文件路径或URL
|
|
67
|
+
* @returns Swagger文档对象
|
|
68
|
+
*/
|
|
69
|
+
async function loadSwaggerDocument(input) {
|
|
70
|
+
try {
|
|
71
|
+
if (input.startsWith('http://') || input.startsWith('https://')) {
|
|
72
|
+
const { data } = await axios_1.default.get(input);
|
|
73
|
+
console.log('Loaded from URL:', input);
|
|
74
|
+
if (data.components?.schemas) {
|
|
75
|
+
console.log('Schemas count:', Object.keys(data.components.schemas).length);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
console.log('No schemas in loaded data');
|
|
79
|
+
}
|
|
80
|
+
return data;
|
|
81
|
+
}
|
|
82
|
+
const content = fs.readFileSync(input, 'utf-8');
|
|
83
|
+
return JSON.parse(content);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
throw new Error(`Failed to load Swagger document from ${input}: ${error}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 写入文件
|
|
91
|
+
* @param filePath 文件路径
|
|
92
|
+
* @param content 文件内容
|
|
93
|
+
*/
|
|
94
|
+
function writeFile(filePath, content) {
|
|
95
|
+
const dir = path.dirname(filePath);
|
|
96
|
+
ensureDirectoryExists(dir);
|
|
97
|
+
fs.writeFileSync(filePath, content, 'utf-8');
|
|
98
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,99 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 将路径转换为小驼峰命名的函数名
|
|
7
|
-
* @param method HTTP方法
|
|
8
|
-
* @param path 接口路径
|
|
9
|
-
* @returns 小驼峰命名的函数名
|
|
10
|
-
*/
|
|
11
|
-
export declare function pathToFunctionName(method: string, path: string): string;
|
|
12
|
-
/**
|
|
13
|
-
* 将字符串转换为kebab-case
|
|
14
|
-
* @param str 输入字符串
|
|
15
|
-
* @returns kebab-case字符串
|
|
16
|
-
*/
|
|
17
|
-
export declare function toKebabCase(str: string): string;
|
|
18
|
-
/**
|
|
19
|
-
* 将字符串转换为PascalCase
|
|
20
|
-
* @param str 输入字符串
|
|
21
|
-
* @returns PascalCase字符串
|
|
22
|
-
*/
|
|
23
|
-
export declare function toPascalCase(str: string): string;
|
|
24
|
-
/**
|
|
25
|
-
* 将字符串转换为camelCase
|
|
26
|
-
* @param str 输入字符串
|
|
27
|
-
* @returns camelCase字符串
|
|
28
|
-
*/
|
|
29
|
-
export declare function toCamelCase(str: string): string;
|
|
30
|
-
/**
|
|
31
|
-
* 从方法名中移除指定的前缀
|
|
32
|
-
* @param methodName 方法名
|
|
33
|
-
* @param prefixes 需要移除的前缀数组
|
|
34
|
-
* @returns 移除前缀后的方法名
|
|
35
|
-
*/
|
|
36
|
-
export declare function stripMethodNamePrefixes(methodName: string, prefixes?: string[]): string;
|
|
37
|
-
/**
|
|
38
|
-
* 从函数名中移除 HTTP method 后缀
|
|
39
|
-
* @param functionName 函数名
|
|
40
|
-
* @param method HTTP 方法
|
|
41
|
-
* @returns 移除后缀后的函数名
|
|
42
|
-
*/
|
|
43
|
-
export declare function removeMethodSuffix(functionName: string, method: string): string;
|
|
44
|
-
export declare function stripNullFromUnion(typeStr: string): string;
|
|
45
|
-
/**
|
|
46
|
-
* 将Swagger类型转换为TypeScript类型
|
|
47
|
-
* @param schema Swagger模式
|
|
48
|
-
* @param schemas 可选的 schemas 上下文,用于查找被引用的类型定义
|
|
49
|
-
* @returns TypeScript类型字符串
|
|
50
|
-
*/
|
|
51
|
-
export declare function swaggerTypeToTsType(schema: any, schemas?: any): string;
|
|
52
|
-
/**
|
|
53
|
-
* 从Swagger参数生成TypeScript参数类型
|
|
54
|
-
* @param parameters Swagger参数数组
|
|
55
|
-
* @returns TypeScript参数类型定义
|
|
56
|
-
*/
|
|
57
|
-
export declare function generateParameterTypes(parameters: SwaggerParameter[]): string;
|
|
58
|
-
/**
|
|
59
|
-
* 确保目录存在,如果不存在则创建
|
|
60
|
-
* @param dirPath 目录路径
|
|
61
|
-
*/
|
|
62
|
-
export declare function ensureDirectoryExists(dirPath: string): void;
|
|
63
|
-
/**
|
|
64
|
-
* 删除目录及其所有内容
|
|
65
|
-
* @param dirPath 目录路径
|
|
66
|
-
*/
|
|
67
|
-
export declare function removeDirectory(dirPath: string): void;
|
|
68
|
-
/**
|
|
69
|
-
* 读取Swagger文档
|
|
70
|
-
* @param input 文件路径或URL
|
|
71
|
-
* @returns Swagger文档对象
|
|
72
|
-
*/
|
|
73
|
-
export declare function loadSwaggerDocument(input: string): Promise<SwaggerDocument>;
|
|
74
|
-
/**
|
|
75
|
-
* 写入文件
|
|
76
|
-
* @param filePath 文件路径
|
|
77
|
-
* @param content 文件内容
|
|
78
|
-
*/
|
|
79
|
-
export declare function writeFile(filePath: string, content: string): void;
|
|
80
|
-
/**
|
|
81
|
-
* 生成接口注释
|
|
82
|
-
* @param operation Swagger操作对象
|
|
83
|
-
* @param parameters 参数列表
|
|
84
|
-
* @returns 注释字符串
|
|
85
|
-
*/
|
|
86
|
-
export declare function generateApiComment(operation: any, parameters: SwaggerParameter[]): string;
|
|
87
|
-
/**
|
|
88
|
-
* 清理文件名,移除非法字符
|
|
89
|
-
* @param filename 文件名
|
|
90
|
-
* @returns 清理后的文件名
|
|
91
|
-
*/
|
|
92
|
-
export declare function sanitizeFilename(filename: string): string;
|
|
93
|
-
/**
|
|
94
|
-
* 获取响应类型
|
|
95
|
-
* @param responses Swagger响应对象
|
|
96
|
-
* @returns TypeScript类型字符串
|
|
97
|
-
*/
|
|
98
|
-
export declare function getResponseType(responses: any): string;
|
|
99
|
-
export declare function sanitizeTypeName(name: string): string;
|
|
1
|
+
export * from './naming';
|
|
2
|
+
export * from './type';
|
|
3
|
+
export * from './file';
|
|
4
|
+
export * from './comment';
|