swagger2api-v3 1.1.8 → 1.1.10
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 +6 -4
- package/dist/.swagger2api.schema.json +4 -0
- package/dist/cli/index.js +20 -19
- package/dist/config/validator.js +2 -0
- package/dist/core/generator.d.ts +18 -0
- package/dist/core/generator.js +73 -25
- package/dist/core/parser.d.ts +14 -0
- package/dist/core/parser.js +76 -54
- package/dist/index.js +22 -19
- package/dist/types/index.d.ts +8 -0
- package/dist/utils/comment.d.ts +18 -3
- package/dist/utils/comment.js +1 -1
- package/dist/utils/file.js +4 -3
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/logger.d.ts +115 -0
- package/dist/utils/logger.js +261 -0
- package/dist/utils/type.d.ts +25 -11
- package/dist/utils/type.js +37 -45
- package/package.json +23 -21
package/dist/utils/type.js
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidIdentifier = isValidIdentifier;
|
|
4
|
+
exports.formatTsPropertyName = formatTsPropertyName;
|
|
3
5
|
exports.stripNullFromUnion = stripNullFromUnion;
|
|
4
6
|
exports.swaggerTypeToTsType = swaggerTypeToTsType;
|
|
7
|
+
exports.getRefName = getRefName;
|
|
5
8
|
exports.swaggerParameterToSchema = swaggerParameterToSchema;
|
|
6
|
-
exports.generateParameterTypes = generateParameterTypes;
|
|
7
9
|
exports.getResponseType = getResponseType;
|
|
8
10
|
exports.getSchemaFromContent = getSchemaFromContent;
|
|
9
11
|
const naming_1 = require("./naming");
|
|
12
|
+
/**
|
|
13
|
+
* 判断字符串是否为合法 TypeScript 标识符
|
|
14
|
+
* @param name 属性名
|
|
15
|
+
* @returns 是否为合法标识符
|
|
16
|
+
*/
|
|
17
|
+
function isValidIdentifier(name) {
|
|
18
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 格式化 TypeScript 属性名
|
|
22
|
+
* @param name 属性名
|
|
23
|
+
* @returns 可用于类型声明的属性名
|
|
24
|
+
*/
|
|
25
|
+
function formatTsPropertyName(name) {
|
|
26
|
+
return isValidIdentifier(name) ? name : JSON.stringify(name);
|
|
27
|
+
}
|
|
10
28
|
/**
|
|
11
29
|
* 移除联合类型中的顶层 null 类型
|
|
12
30
|
* @param typeStr 类型字符串
|
|
@@ -78,8 +96,8 @@ function swaggerTypeToTsType(schema, schemas) {
|
|
|
78
96
|
else if (schema.allOf) {
|
|
79
97
|
const refSchema = schema.allOf.find((item) => item.$ref);
|
|
80
98
|
const secondSchema = schema.allOf.find((item) => !item.$ref);
|
|
81
|
-
if (refSchema && secondSchema) {
|
|
82
|
-
const refName = refSchema.$ref
|
|
99
|
+
if (refSchema && secondSchema && schema.allOf.length === 2) {
|
|
100
|
+
const refName = getRefName(refSchema.$ref || '');
|
|
83
101
|
const sanitizedRefName = (0, naming_1.sanitizeTypeName)(refName || '');
|
|
84
102
|
if (secondSchema.properties) {
|
|
85
103
|
const propertyTypes = [];
|
|
@@ -97,7 +115,7 @@ function swaggerTypeToTsType(schema, schemas) {
|
|
|
97
115
|
let type = swaggerTypeToTsType(value, schemas);
|
|
98
116
|
if (optional === '?')
|
|
99
117
|
type = stripNullFromUnion(type);
|
|
100
|
-
return `${key}${optional}: ${type}`;
|
|
118
|
+
return `${formatTsPropertyName(key)}${optional}: ${type}`;
|
|
101
119
|
})
|
|
102
120
|
.join('; ')} }`;
|
|
103
121
|
baseType = `${sanitizedRefName}<${combinedType}>`;
|
|
@@ -112,9 +130,10 @@ function swaggerTypeToTsType(schema, schemas) {
|
|
|
112
130
|
}
|
|
113
131
|
else {
|
|
114
132
|
const types = schema.allOf
|
|
115
|
-
.map((item) => swaggerTypeToTsType(item))
|
|
133
|
+
.map((item) => swaggerTypeToTsType(item, schemas))
|
|
116
134
|
.filter((type) => type !== 'any');
|
|
117
|
-
|
|
135
|
+
const uniqueTypes = Array.from(new Set(types));
|
|
136
|
+
baseType = uniqueTypes.length > 0 ? uniqueTypes.join(' & ') : 'any';
|
|
118
137
|
}
|
|
119
138
|
}
|
|
120
139
|
else if (schema.anyOf || schema.oneOf) {
|
|
@@ -132,7 +151,7 @@ function swaggerTypeToTsType(schema, schemas) {
|
|
|
132
151
|
}
|
|
133
152
|
}
|
|
134
153
|
else if (schema.$ref) {
|
|
135
|
-
const refName = schema.$ref
|
|
154
|
+
const refName = getRefName(schema.$ref);
|
|
136
155
|
baseType = (0, naming_1.sanitizeTypeName)(refName || 'any');
|
|
137
156
|
if (schemas && refName) {
|
|
138
157
|
const referencedSchema = schemas[refName];
|
|
@@ -145,7 +164,7 @@ function swaggerTypeToTsType(schema, schemas) {
|
|
|
145
164
|
const itemSchema = schema.items;
|
|
146
165
|
const itemType = swaggerTypeToTsType(itemSchema, schemas);
|
|
147
166
|
if (itemSchema?.$ref && schemas) {
|
|
148
|
-
const refName = itemSchema.$ref
|
|
167
|
+
const refName = getRefName(itemSchema.$ref);
|
|
149
168
|
const referencedSchema = refName ? schemas[refName] : undefined;
|
|
150
169
|
baseType =
|
|
151
170
|
referencedSchema?.type === 'array' ? itemType : `${itemType}[]`;
|
|
@@ -162,7 +181,7 @@ function swaggerTypeToTsType(schema, schemas) {
|
|
|
162
181
|
let type = swaggerTypeToTsType(value, schemas);
|
|
163
182
|
if (optional === '?')
|
|
164
183
|
type = stripNullFromUnion(type);
|
|
165
|
-
return ` ${key}${optional}: ${type};`;
|
|
184
|
+
return ` ${formatTsPropertyName(key)}${optional}: ${type};`;
|
|
166
185
|
})
|
|
167
186
|
.join('\n');
|
|
168
187
|
baseType = `{\n${properties}\n}`;
|
|
@@ -211,6 +230,14 @@ function swaggerTypeToTsType(schema, schemas) {
|
|
|
211
230
|
}
|
|
212
231
|
return baseType;
|
|
213
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* 从 $ref 字符串中提取引用名称
|
|
235
|
+
* @param ref OpenAPI $ref 字符串
|
|
236
|
+
* @returns 引用名称
|
|
237
|
+
*/
|
|
238
|
+
function getRefName(ref) {
|
|
239
|
+
return ref.split('/').pop() || '';
|
|
240
|
+
}
|
|
214
241
|
/**
|
|
215
242
|
* 将枚举值转换为 TypeScript 字面量类型
|
|
216
243
|
* @param value 枚举值
|
|
@@ -220,7 +247,7 @@ function toLiteralType(value) {
|
|
|
220
247
|
if (value === null)
|
|
221
248
|
return 'null';
|
|
222
249
|
if (typeof value === 'string')
|
|
223
|
-
return
|
|
250
|
+
return JSON.stringify(value);
|
|
224
251
|
return String(value);
|
|
225
252
|
}
|
|
226
253
|
/**
|
|
@@ -239,41 +266,6 @@ function swaggerParameterToSchema(parameter) {
|
|
|
239
266
|
enum: parameter.enum
|
|
240
267
|
};
|
|
241
268
|
}
|
|
242
|
-
/**
|
|
243
|
-
* 从 OpenAPI 参数生成 TypeScript 参数类型
|
|
244
|
-
* @param parameters OpenAPI 参数数组
|
|
245
|
-
* @returns TypeScript参数类型定义
|
|
246
|
-
*/
|
|
247
|
-
function generateParameterTypes(parameters) {
|
|
248
|
-
if (!parameters || parameters.length === 0) {
|
|
249
|
-
return '';
|
|
250
|
-
}
|
|
251
|
-
const queryParams = parameters.filter((param) => param.in === 'query');
|
|
252
|
-
const pathParams = parameters.filter((param) => param.in === 'path');
|
|
253
|
-
const bodyParams = parameters.filter((param) => param.in === 'body');
|
|
254
|
-
const types = [];
|
|
255
|
-
if (pathParams.length > 0) {
|
|
256
|
-
const pathType = pathParams
|
|
257
|
-
.map((param) => `${param.name}: ${swaggerTypeToTsType(swaggerParameterToSchema(param))}`)
|
|
258
|
-
.join(', ');
|
|
259
|
-
types.push(`pathParams: { ${pathType} }`);
|
|
260
|
-
}
|
|
261
|
-
if (queryParams.length > 0) {
|
|
262
|
-
const queryType = queryParams
|
|
263
|
-
.map((param) => {
|
|
264
|
-
const optional = param.required ? '' : '?';
|
|
265
|
-
return `${param.name}${optional}: ${swaggerTypeToTsType(swaggerParameterToSchema(param))}`;
|
|
266
|
-
})
|
|
267
|
-
.join(', ');
|
|
268
|
-
types.push(`queryParams${queryParams.every((param) => !param.required) ? '?' : ''}: { ${queryType} }`);
|
|
269
|
-
}
|
|
270
|
-
if (bodyParams.length > 0) {
|
|
271
|
-
const bodyParam = bodyParams[0];
|
|
272
|
-
const bodyType = swaggerTypeToTsType(bodyParam.schema);
|
|
273
|
-
types.push(`data: ${bodyType}`);
|
|
274
|
-
}
|
|
275
|
-
return types.join(', ');
|
|
276
|
-
}
|
|
277
269
|
/**
|
|
278
270
|
* 获取响应类型
|
|
279
271
|
* @param responses OpenAPI 响应对象
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swagger2api-v3",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "A command-line tool for generating TypeScript API interfaces from Swagger
|
|
3
|
+
"version": "1.1.10",
|
|
4
|
+
"description": "A command-line tool for generating TypeScript API interfaces from OpenAPI 3.x / Swagger 3.0 documentation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"type": "commonjs",
|
|
@@ -18,20 +18,21 @@
|
|
|
18
18
|
"test:watch": "jest --watch",
|
|
19
19
|
"test:coverage": "jest --coverage",
|
|
20
20
|
"test:ci": "jest --ci --coverage --watchAll=false",
|
|
21
|
-
"
|
|
21
|
+
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
|
|
22
|
+
"build": "tsc && pnpm run copy:schema",
|
|
22
23
|
"copy:schema": "node build/copy-schema.js",
|
|
23
24
|
"clean": "rimraf dist",
|
|
24
|
-
"prebuild": "
|
|
25
|
+
"prebuild": "pnpm run clean",
|
|
25
26
|
"start": "node dist/cli/index.js",
|
|
26
|
-
"dev": "
|
|
27
|
+
"dev": "pnpm run build && pnpm start",
|
|
27
28
|
"lint": "prettier --write .",
|
|
28
|
-
"generate": "
|
|
29
|
-
"init": "
|
|
30
|
-
"validate": "
|
|
31
|
-
"swagger:generate": "
|
|
32
|
-
"swagger:run": "
|
|
33
|
-
"swagger:init": "
|
|
34
|
-
"swagger:validate": "
|
|
29
|
+
"generate": "pnpm run build && node dist/cli/index.js generate",
|
|
30
|
+
"init": "pnpm run build && node dist/cli/index.js init",
|
|
31
|
+
"validate": "pnpm run build && node dist/cli/index.js validate",
|
|
32
|
+
"swagger:generate": "pnpm run generate",
|
|
33
|
+
"swagger:run": "pnpm run generate",
|
|
34
|
+
"swagger:init": "pnpm run init",
|
|
35
|
+
"swagger:validate": "pnpm run validate"
|
|
35
36
|
},
|
|
36
37
|
"keywords": [
|
|
37
38
|
"swagger",
|
|
@@ -54,16 +55,17 @@
|
|
|
54
55
|
},
|
|
55
56
|
"packageManager": "pnpm@10.11.0",
|
|
56
57
|
"devDependencies": {
|
|
57
|
-
"@types/jest": "^
|
|
58
|
-
"@types/node": "^24.
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
58
|
+
"@types/jest": "^30.0.0",
|
|
59
|
+
"@types/node": "^24.13.2",
|
|
60
|
+
"conventional-changelog-cli": "^5.0.0",
|
|
61
|
+
"jest": "^30.4.2",
|
|
62
|
+
"prettier": "^3.8.4",
|
|
63
|
+
"rimraf": "^6.1.3",
|
|
64
|
+
"ts-jest": "^29.4.11",
|
|
65
|
+
"typescript": "^6.0.3"
|
|
64
66
|
},
|
|
65
67
|
"dependencies": {
|
|
66
|
-
"axios": "^1.
|
|
67
|
-
"commander": "^12.
|
|
68
|
+
"axios": "^1.18.1",
|
|
69
|
+
"commander": "^12.1.0"
|
|
68
70
|
}
|
|
69
71
|
}
|