openapi-ts-request 1.10.1 → 1.12.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 +4 -0
- package/dist/bin/openapi.js +4 -0
- package/dist/generator/config.d.ts +6 -1
- package/dist/generator/config.js +6 -1
- package/dist/generator/serviceGenarator.d.ts +20 -8
- package/dist/generator/serviceGenarator.js +272 -289
- package/dist/generator/serviceGeneratorHelper.d.ts +184 -0
- package/dist/generator/serviceGeneratorHelper.js +532 -0
- package/dist/generator/type.d.ts +2 -0
- package/dist/generator/util.d.ts +7 -0
- package/dist/generator/util.js +49 -1
- package/dist/index.d.ts +12 -0
- package/package.json +2 -2
- package/templates/enum.njk +14 -0
- package/templates/moduleType.njk +63 -0
- package/templates/typeIndex.njk +17 -0
package/README.md
CHANGED
|
@@ -187,6 +187,7 @@ $ openapi --help
|
|
|
187
187
|
-cfn, --configFileName <string> config file name
|
|
188
188
|
-cfp, --configFilePath <string> config file path
|
|
189
189
|
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch" (default: "axios")
|
|
190
|
+
--isSplitTypesByModule <boolean> split types by module, generates {module}.type.ts, common.type.ts, enum.ts, types.ts (default: false)
|
|
190
191
|
-f, --full <boolean> full replacement (default: true)
|
|
191
192
|
--enableLogging <boolean> open the log (default: false)
|
|
192
193
|
--priorityRule <string> priority rule, include/exclude/both (default: "include")
|
|
@@ -210,6 +211,7 @@ $ openapi --help
|
|
|
210
211
|
--isOnlyGenTypeScriptType <boolean> only generate typescript type (default: false)
|
|
211
212
|
--isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
|
|
212
213
|
--isSupportParseEnumDesc <boolean> parse enum description to generate enum label (default: false)
|
|
214
|
+
--supportParseEnumDescByReg <string> custom regex for parsing enum description
|
|
213
215
|
-h, --help display help for command
|
|
214
216
|
```
|
|
215
217
|
|
|
@@ -226,6 +228,7 @@ openapi --i ./spec.json --o ./apis
|
|
|
226
228
|
| schemaPath | 是 | string | - | Swagger2/OpenAPI3 地址 |
|
|
227
229
|
| serversPath | 否 | string | './src/apis' | 运行结果文件夹路径 |
|
|
228
230
|
| requestLibPath | 否 | string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
|
|
231
|
+
| isSplitTypesByModule | 否 | boolean | false | 按模块拆分类型文件,开启后会生成:{module}.type.ts(各模块类型)、common.type.ts(公共类型)、enum.ts(枚举类型)、types.ts(统一导出) |
|
|
229
232
|
| full | 否 | boolean | true | 是否全量替换 |
|
|
230
233
|
| describe | 否 | string | - | 描述信息,在用 cli 可交互运行方式时会用到 |
|
|
231
234
|
| enableLogging | 否 | boolean | false | 是否开启日志 |
|
|
@@ -251,6 +254,7 @@ openapi --i ./spec.json --o ./apis
|
|
|
251
254
|
| isOnlyGenTypeScriptType | 否 | boolean | false | 仅生成 typescript 类型 |
|
|
252
255
|
| isCamelCase | 否 | boolean | true | 小驼峰命名文件和请求函数 |
|
|
253
256
|
| isSupportParseEnumDesc | 否 | boolean | false | 解析枚举描述生成枚举标签,格式参考:`系统用户角色:User(普通用户)=0,Agent(经纪人)=1,Admin(管理员)=2` |
|
|
257
|
+
| supportParseEnumDescByReg | 否 | string \| RegExp | - | 自定义正则表达式,用于解析 description 中的枚举定义。如果设置了此参数,将使用此正则表达式替代默认的 parseDescriptionEnum 方法。例如:`/([^\s=<>/&;]+(?:\s+[^\s=<>/&;]+)*)\s*=\s*(\d+)/g` 可以匹配 "普通 = 0" 或 "SampleMaker = 1" 这样的格式 |
|
|
254
258
|
| binaryMediaTypes | 否 | string[] | - | 自定义二进制媒体类型列表,默认包含:['application/octet-stream', 'application/pdf', 'image/*', 'video/*', 'audio/*'] |
|
|
255
259
|
| hook | 否 | [Custom Hook](#Custom-Hook) | - | 自定义 hook |
|
|
256
260
|
|
package/dist/bin/openapi.js
CHANGED
|
@@ -19,6 +19,7 @@ const params = commander_1.program
|
|
|
19
19
|
.option('-cfn, --configFileName <string>', 'config file name')
|
|
20
20
|
.option('-cfp, --configFilePath <string>', 'config file path')
|
|
21
21
|
.option('--requestLibPath <string>', 'custom request lib path, for example: "@/request", "node-fetch" (default: "axios")')
|
|
22
|
+
.option('--isSplitTypesByModule <boolean>', 'split types by module, generates {module}.type.ts, common.type.ts, enum.ts, types.ts', false)
|
|
22
23
|
.option('-f, --full <boolean>', 'full replacement', true)
|
|
23
24
|
.option('--enableLogging <boolean>', 'open the log', false)
|
|
24
25
|
.option('--priorityRule <string>', 'priority rule, include/exclude/both (default: "include")')
|
|
@@ -42,6 +43,7 @@ const params = commander_1.program
|
|
|
42
43
|
.option('--isOnlyGenTypeScriptType <boolean>', 'only generate typescript type', false)
|
|
43
44
|
.option('--isCamelCase <boolean>', 'camelCase naming of controller files and request client', true)
|
|
44
45
|
.option('--isSupportParseEnumDesc <boolean>', 'parse enum description to generate enum label', false)
|
|
46
|
+
.option('--supportParseEnumDescByReg <string>', 'custom regex for parsing enum description')
|
|
45
47
|
.parse(process.argv)
|
|
46
48
|
.opts();
|
|
47
49
|
function getPath(path) {
|
|
@@ -80,6 +82,8 @@ const baseGenerate = (_params_) => {
|
|
|
80
82
|
isOnlyGenTypeScriptType: JSON.parse(_params_.isOnlyGenTypeScriptType) === true,
|
|
81
83
|
isCamelCase: JSON.parse(_params_.isCamelCase) === true,
|
|
82
84
|
isSupportParseEnumDesc: JSON.parse(_params_.isSupportParseEnumDesc) === true,
|
|
85
|
+
supportParseEnumDescByReg: _params_.supportParseEnumDescByReg,
|
|
86
|
+
isSplitTypesByModule: JSON.parse(_params_.isSplitTypesByModule) === true,
|
|
83
87
|
};
|
|
84
88
|
return options;
|
|
85
89
|
};
|
|
@@ -5,6 +5,8 @@ export declare const interfaceFileName = "types";
|
|
|
5
5
|
export declare const displayEnumLabelFileName = "displayEnumLabel";
|
|
6
6
|
export declare const displayTypeLabelFileName = "displayTypeLabel";
|
|
7
7
|
export declare const schemaFileName = "schema";
|
|
8
|
+
export declare const enumFileName = "enum";
|
|
9
|
+
export declare const commonTypeFileName = "common.type";
|
|
8
10
|
export declare const displayReactQueryFileName: (reactQueryMode: IReactQueryMode) => string;
|
|
9
11
|
export declare enum TypescriptFileType {
|
|
10
12
|
interface = "interface",
|
|
@@ -13,7 +15,10 @@ export declare enum TypescriptFileType {
|
|
|
13
15
|
displayEnumLabel = "displayEnumLabel",
|
|
14
16
|
displayTypeLabel = "displayTypeLabel",
|
|
15
17
|
schema = "schema",
|
|
16
|
-
reactQuery = "reactQuery"
|
|
18
|
+
reactQuery = "reactQuery",
|
|
19
|
+
moduleType = "moduleType",
|
|
20
|
+
typeIndex = "typeIndex",
|
|
21
|
+
enum = "enum"
|
|
17
22
|
}
|
|
18
23
|
export declare const DEFAULT_SCHEMA: SchemaObject;
|
|
19
24
|
export declare const DEFAULT_PATH_PARAM: ParameterObject & Dictionary<unknown>;
|
package/dist/generator/config.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LangType = exports.lineBreakReg = exports.numberEnum = exports.parametersIn = exports.parametersInsEnum = exports.methods = exports.DEFAULT_PATH_PARAM = exports.DEFAULT_SCHEMA = exports.TypescriptFileType = exports.displayReactQueryFileName = exports.schemaFileName = exports.displayTypeLabelFileName = exports.displayEnumLabelFileName = exports.interfaceFileName = exports.serviceEntryFileName = void 0;
|
|
3
|
+
exports.LangType = exports.lineBreakReg = exports.numberEnum = exports.parametersIn = exports.parametersInsEnum = exports.methods = exports.DEFAULT_PATH_PARAM = exports.DEFAULT_SCHEMA = exports.TypescriptFileType = exports.displayReactQueryFileName = exports.commonTypeFileName = exports.enumFileName = exports.schemaFileName = exports.displayTypeLabelFileName = exports.displayEnumLabelFileName = exports.interfaceFileName = exports.serviceEntryFileName = void 0;
|
|
4
4
|
exports.serviceEntryFileName = 'index';
|
|
5
5
|
exports.interfaceFileName = 'types';
|
|
6
6
|
exports.displayEnumLabelFileName = 'displayEnumLabel';
|
|
7
7
|
exports.displayTypeLabelFileName = 'displayTypeLabel';
|
|
8
8
|
exports.schemaFileName = 'schema';
|
|
9
|
+
exports.enumFileName = 'enum';
|
|
10
|
+
exports.commonTypeFileName = 'common.type';
|
|
9
11
|
const displayReactQueryFileName = (reactQueryMode) => {
|
|
10
12
|
return {
|
|
11
13
|
react: 'reactquery',
|
|
@@ -22,6 +24,9 @@ var TypescriptFileType;
|
|
|
22
24
|
TypescriptFileType["displayTypeLabel"] = "displayTypeLabel";
|
|
23
25
|
TypescriptFileType["schema"] = "schema";
|
|
24
26
|
TypescriptFileType["reactQuery"] = "reactQuery";
|
|
27
|
+
TypescriptFileType["moduleType"] = "moduleType";
|
|
28
|
+
TypescriptFileType["typeIndex"] = "typeIndex";
|
|
29
|
+
TypescriptFileType["enum"] = "enum";
|
|
25
30
|
})(TypescriptFileType || (exports.TypescriptFileType = TypescriptFileType = {}));
|
|
26
31
|
exports.DEFAULT_SCHEMA = {
|
|
27
32
|
type: 'object',
|
|
@@ -8,6 +8,7 @@ export default class ServiceGenerator {
|
|
|
8
8
|
protected openAPIData: OpenAPIObject;
|
|
9
9
|
protected schemaList: ISchemaItem[];
|
|
10
10
|
protected interfaceTPConfigs: ITypeItem[];
|
|
11
|
+
protected typeModuleMap: Map<string, Set<string>>;
|
|
11
12
|
constructor(config: GenerateServiceProps, openAPIData: OpenAPIObject);
|
|
12
13
|
genFile(): void;
|
|
13
14
|
private getInterfaceTPConfigs;
|
|
@@ -31,14 +32,6 @@ export default class ServiceGenerator {
|
|
|
31
32
|
* @returns 多状态码响应类型定义字符串,如果没有响应则返回 null
|
|
32
33
|
*/
|
|
33
34
|
private getResponsesType;
|
|
34
|
-
/**
|
|
35
|
-
* 解析响应条目,提取每个状态码对应的类型和描述信息
|
|
36
|
-
*
|
|
37
|
-
* @param responses OpenAPI 响应对象
|
|
38
|
-
* @param components OpenAPI 组件对象,用于解析引用类型
|
|
39
|
-
* @returns 响应条目数组,包含状态码、类型和描述
|
|
40
|
-
*/
|
|
41
|
-
private parseResponseEntries;
|
|
42
35
|
/**
|
|
43
36
|
* 从响应内容中提取 TypeScript 类型
|
|
44
37
|
* 处理不同的媒体类型和 schema 类型
|
|
@@ -71,4 +64,23 @@ export default class ServiceGenerator {
|
|
|
71
64
|
* @param reg 规则
|
|
72
65
|
*/
|
|
73
66
|
private matches;
|
|
67
|
+
/**
|
|
68
|
+
* 记录类型被某个模块使用
|
|
69
|
+
* @param typeName 类型名称
|
|
70
|
+
* @param moduleName 模块名称
|
|
71
|
+
*/
|
|
72
|
+
private markTypeUsedByModule;
|
|
73
|
+
/**
|
|
74
|
+
* 获取模块需要导入的类型
|
|
75
|
+
* @param moduleTypes 模块类型列表
|
|
76
|
+
* @param commonTypes 公共类型列表
|
|
77
|
+
* @param enumTypes 枚举类型列表
|
|
78
|
+
* @returns 导入信息
|
|
79
|
+
*/
|
|
80
|
+
private getModuleImports;
|
|
81
|
+
/**
|
|
82
|
+
* 分组类型:按模块、公共类型和枚举
|
|
83
|
+
* @returns 分组后的类型
|
|
84
|
+
*/
|
|
85
|
+
private groupTypesByModule;
|
|
74
86
|
}
|