swagger2api-v3 1.1.7 → 1.1.9

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.
@@ -5,6 +5,8 @@
5
5
  * Swagger配置接口
6
6
  */
7
7
  export interface SwaggerConfig {
8
+ /** 本地 JSON Schema 路径,用于编辑器提示 */
9
+ $schema?: string;
8
10
  /** Swagger JSON 文件路径或 URL */
9
11
  input: string;
10
12
  /** 输出目录 */
@@ -33,65 +35,96 @@ export interface SwaggerConfig {
33
35
  methodNameIgnorePrefix?: string[];
34
36
  /** 是否在生成的方法名中添加 HTTP method 后缀,默认为 true。true: userListPost, false: userList */
35
37
  addMethodSuffix?: boolean;
38
+ /** 接口过滤配置 */
39
+ filter?: FilterConfig;
40
+ /** 自定义生成文件头部注释 */
41
+ headerComment?: string;
42
+ }
43
+ /**
44
+ * 接口过滤配置
45
+ */
46
+ export interface FilterConfig {
47
+ /** 包含规则 */
48
+ include?: TagFilterConfig;
49
+ /** 排除规则 */
50
+ exclude?: TagFilterConfig;
51
+ }
52
+ /**
53
+ * 标签过滤配置
54
+ */
55
+ export interface TagFilterConfig {
56
+ /** 需要匹配的标签名称 */
57
+ tags?: string[];
36
58
  }
37
59
  /**
38
60
  * 标签分组配置
39
61
  */
40
62
  export interface TagGroupingConfig {
41
63
  /** 启用标签分组 */
42
- enabled: boolean;
64
+ enabled?: boolean;
43
65
  /** 为每个标签创建子目录 */
44
- createSubDirectories: boolean;
66
+ createSubDirectories?: boolean;
45
67
  /** 文件命名方式 */
46
- fileNaming: 'tag' | 'kebab-case' | 'camelCase';
68
+ fileNaming?: 'tag' | 'kebab-case' | 'camelCase';
47
69
  }
48
70
  /**
49
71
  * 生成选项
50
72
  */
51
73
  export interface GenerationOptions {
52
74
  /** 是否生成数据模型 */
53
- generateModels: boolean;
75
+ generateModels?: boolean;
54
76
  /** 是否生成 API 接口 */
55
- generateApis: boolean;
77
+ generateApis?: boolean;
56
78
  /** 是否生成入口文件 */
57
- generateIndex: boolean;
79
+ generateIndex?: boolean;
58
80
  /** 是否使用 Axios */
59
- useAxios: boolean;
81
+ useAxios?: boolean;
60
82
  /** 是否添加详细注释 */
61
- addComments: boolean;
83
+ addComments?: boolean;
62
84
  /** 是否格式化代码 */
63
- prettify: boolean;
85
+ prettify?: boolean;
64
86
  }
65
87
  /**
66
88
  * 注释配置
67
89
  */
68
90
  export interface CommentConfig {
69
91
  /** 包含接口描述 */
70
- includeDescription: boolean;
92
+ includeDescription?: boolean;
71
93
  /** 包含参数信息 */
72
- includeParameters: boolean;
94
+ includeParameters?: boolean;
73
95
  /** 包含返回值信息 */
74
- includeResponses: boolean;
96
+ includeResponses?: boolean;
75
97
  /** 包含示例 */
76
- includeExamples: boolean;
98
+ includeExamples?: boolean;
77
99
  }
78
100
  /**
79
- * Swagger文档结构
101
+ * OpenAPI 文档结构
80
102
  */
81
103
  export interface SwaggerDocument {
82
- swagger?: string;
83
- openapi?: string;
104
+ openapi: string;
84
105
  info: SwaggerInfo;
85
- host?: string;
86
- basePath?: string;
87
- schemes?: string[];
88
- consumes?: string[];
89
- produces?: string[];
106
+ servers?: OpenAPIServer[];
90
107
  paths: SwaggerPaths;
91
- definitions?: SwaggerDefinitions;
92
108
  components?: SwaggerComponents;
93
109
  tags?: SwaggerTag[];
94
110
  }
111
+ /**
112
+ * OpenAPI Server 对象
113
+ */
114
+ export interface OpenAPIServer {
115
+ /** Server URL */
116
+ url: string;
117
+ /** Server 描述 */
118
+ description?: string;
119
+ /** Server URL 变量 */
120
+ variables?: {
121
+ [name: string]: {
122
+ default: string;
123
+ enum?: string[];
124
+ description?: string;
125
+ };
126
+ };
127
+ }
95
128
  /**
96
129
  * Swagger信息
97
130
  */
@@ -145,8 +178,6 @@ export interface SwaggerOperation {
145
178
  summary?: string;
146
179
  description?: string;
147
180
  operationId?: string;
148
- consumes?: string[];
149
- produces?: string[];
150
181
  parameters?: SwaggerParameter[];
151
182
  requestBody?: SwaggerRequestBody;
152
183
  responses: SwaggerResponses;
@@ -156,6 +187,8 @@ export interface SwaggerOperation {
156
187
  * Swagger请求体 (OpenAPI 3.0)
157
188
  */
158
189
  export interface SwaggerRequestBody {
190
+ /** 本地引用路径 */
191
+ $ref?: string;
159
192
  description?: string;
160
193
  content: {
161
194
  [mediaType: string]: {
@@ -169,11 +202,13 @@ export interface SwaggerRequestBody {
169
202
  required?: boolean;
170
203
  }
171
204
  /**
172
- * Swagger参数
205
+ * OpenAPI 参数
173
206
  */
174
207
  export interface SwaggerParameter {
208
+ /** 本地引用路径 */
209
+ $ref?: string;
175
210
  name: string;
176
- in: 'query' | 'header' | 'path' | 'formData' | 'body';
211
+ in: 'query' | 'header' | 'path' | 'body' | 'cookie';
177
212
  description?: string;
178
213
  required?: boolean;
179
214
  type?: string;
@@ -184,17 +219,25 @@ export interface SwaggerParameter {
184
219
  default?: any;
185
220
  }
186
221
  /**
187
- * Swagger响应
222
+ * OpenAPI 响应
188
223
  */
189
224
  export interface SwaggerResponses {
190
225
  [statusCode: string]: SwaggerResponse;
191
226
  }
192
227
  /**
193
- * Swagger响应项
228
+ * OpenAPI 响应项
194
229
  */
195
230
  export interface SwaggerResponse {
196
231
  description: string;
197
- schema?: SwaggerSchema;
232
+ content?: {
233
+ [mediaType: string]: {
234
+ schema?: SwaggerSchema;
235
+ example?: any;
236
+ examples?: {
237
+ [name: string]: any;
238
+ };
239
+ };
240
+ };
198
241
  headers?: {
199
242
  [name: string]: SwaggerHeader;
200
243
  };
@@ -214,10 +257,10 @@ export interface SwaggerHeader {
214
257
  default?: any;
215
258
  }
216
259
  /**
217
- * Swagger模式
260
+ * OpenAPI Schema
218
261
  */
219
262
  export interface SwaggerSchema {
220
- type?: string;
263
+ type?: string | string[];
221
264
  format?: string;
222
265
  title?: string;
223
266
  description?: string;
@@ -243,6 +286,10 @@ export interface SwaggerSchema {
243
286
  externalDocs?: SwaggerExternalDocs;
244
287
  nullable?: boolean;
245
288
  deprecated?: boolean;
289
+ /** OpenAPI 扩展字段:枚举变量名 */
290
+ 'x-enum-varnames'?: string[];
291
+ /** OpenAPI 扩展字段:枚举名称 */
292
+ 'x-enumNames'?: string[];
246
293
  }
247
294
  /**
248
295
  * Swagger项目
@@ -255,12 +302,6 @@ export interface SwaggerItems {
255
302
  default?: any;
256
303
  $ref?: string;
257
304
  }
258
- /**
259
- * Swagger定义
260
- */
261
- export interface SwaggerDefinitions {
262
- [name: string]: SwaggerSchema;
263
- }
264
305
  /**
265
306
  * Swagger组件 (OpenAPI 3.0)
266
307
  */
@@ -348,7 +389,7 @@ export interface ParameterInfo {
348
389
  /** 参数类型 */
349
390
  type: string;
350
391
  /** 参数位置 */
351
- in: 'query' | 'header' | 'path' | 'formData' | 'body';
392
+ in: 'query' | 'header' | 'path' | 'body' | 'cookie';
352
393
  /** 是否必需 */
353
394
  required: boolean;
354
395
  /** 参数描述 */
@@ -0,0 +1,23 @@
1
+ import { ParameterInfo } from '../types';
2
+ /**
3
+ * 注释生成需要的操作信息
4
+ */
5
+ export interface ApiCommentOperation {
6
+ /** 接口摘要 */
7
+ summary?: string;
8
+ /** 接口描述 */
9
+ description?: string;
10
+ /** 是否废弃 */
11
+ deprecated?: boolean;
12
+ }
13
+ /**
14
+ * 注释生成需要的参数信息
15
+ */
16
+ export type ApiCommentParameter = Pick<ParameterInfo, 'in' | 'description'>;
17
+ /**
18
+ * 生成接口注释
19
+ * @param operation 接口操作信息
20
+ * @param parameters 参数列表
21
+ * @returns 注释字符串
22
+ */
23
+ export declare function generateApiComment(operation: ApiCommentOperation, parameters: ApiCommentParameter[]): string;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateApiComment = generateApiComment;
4
+ /**
5
+ * 生成接口注释
6
+ * @param operation 接口操作信息
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
+ }
@@ -1,99 +1,4 @@
1
- import { SwaggerDocument, SwaggerParameter } from '../types';
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';