openapi-ts-request 0.11.1 → 0.12.1
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 +2 -0
- package/dist/bin/openapi.js +2 -0
- package/dist/generator/serviceGenarator.js +30 -20
- package/dist/generator/util.d.ts +1 -0
- package/dist/generator/util.js +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -150,6 +150,7 @@ $ openapi --help
|
|
|
150
150
|
--authorization <string> docs authorization
|
|
151
151
|
--nullable <boolean> null instead of optional (default: false)
|
|
152
152
|
--isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
|
|
153
|
+
--isOnlyGenTypeScriptType <boolean>only generate typescript type (default: false)
|
|
153
154
|
--isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
|
|
154
155
|
-h, --help display help for command
|
|
155
156
|
```
|
|
@@ -177,6 +178,7 @@ openapi --i ./spec.json --o ./apis
|
|
|
177
178
|
| authorization | 否 | string | - | 文档权限凭证 |
|
|
178
179
|
| nullable | 否 | boolean | false | 使用 null 代替可选 |
|
|
179
180
|
| isTranslateToEnglishTag | 否 | boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |
|
|
181
|
+
| isOnlyGenTypeScriptType | 否 | boolean | false | 仅生成 typescript 类型 |
|
|
180
182
|
| isCamelCase | 否 | boolean | true | 小驼峰命名文件和请求函数 |
|
|
181
183
|
| hook | 否 | [Custom Hook](#Custom-Hook) | - | 自定义 hook |
|
|
182
184
|
|
package/dist/bin/openapi.js
CHANGED
|
@@ -23,6 +23,7 @@ const params = commander_1.program
|
|
|
23
23
|
.option('--authorization <string>', 'docs authorization')
|
|
24
24
|
.option('--nullable <boolean>', 'null instead of optional', false)
|
|
25
25
|
.option('--isTranslateToEnglishTag <boolean>', 'translate chinese tag name to english tag name', false)
|
|
26
|
+
.option('--isOnlyGenTypeScriptType <boolean>', 'only generate typescript type', false)
|
|
26
27
|
.option('--isCamelCase <boolean>', 'camelCase naming of controller files and request client', true)
|
|
27
28
|
.parse(process.argv)
|
|
28
29
|
.opts();
|
|
@@ -51,6 +52,7 @@ function run() {
|
|
|
51
52
|
authorization: params.authorization,
|
|
52
53
|
nullable: JSON.parse(params.nullable) === true,
|
|
53
54
|
isTranslateToEnglishTag: JSON.parse(params.isTranslateToEnglishTag) === true,
|
|
55
|
+
isOnlyGenTypeScriptType: JSON.parse(params.isOnlyGenTypeScriptType) === true,
|
|
54
56
|
isCamelCase: JSON.parse(params.isCamelCase) === true,
|
|
55
57
|
});
|
|
56
58
|
process.exit(0);
|
|
@@ -80,7 +80,7 @@ class ServiceGenerator {
|
|
|
80
80
|
});
|
|
81
81
|
// 生成枚举翻译
|
|
82
82
|
const enums = (0, lodash_1.filter)(interfaceTPConfigs, (item) => item.isEnum);
|
|
83
|
-
if (!(0, lodash_1.isEmpty)(enums)) {
|
|
83
|
+
if (!this.config.isOnlyGenTypeScriptType && !(0, lodash_1.isEmpty)(enums)) {
|
|
84
84
|
this.genFileFromTemplate(`${config_1.displayEnumLabelFileName}.ts`, config_1.TypescriptFileType.displayEnumLabel, {
|
|
85
85
|
list: enums,
|
|
86
86
|
namespace: this.config.namespace,
|
|
@@ -89,23 +89,29 @@ class ServiceGenerator {
|
|
|
89
89
|
}
|
|
90
90
|
const displayTypeLabels = (0, lodash_1.filter)(interfaceTPConfigs, (item) => !item.isEnum);
|
|
91
91
|
// 生成 type 翻译
|
|
92
|
-
if (this.config.
|
|
92
|
+
if (!this.config.isOnlyGenTypeScriptType &&
|
|
93
|
+
this.config.isDisplayTypeLabel &&
|
|
94
|
+
!(0, lodash_1.isEmpty)(displayTypeLabels)) {
|
|
93
95
|
this.genFileFromTemplate(`${config_1.displayTypeLabelFileName}.ts`, config_1.TypescriptFileType.displayTypeLabel, {
|
|
94
96
|
list: displayTypeLabels,
|
|
95
97
|
namespace: this.config.namespace,
|
|
96
98
|
interfaceFileName: config_1.interfaceFileName,
|
|
97
99
|
});
|
|
98
100
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
(
|
|
101
|
+
if (!this.config.isOnlyGenTypeScriptType) {
|
|
102
|
+
const prettierError = [];
|
|
103
|
+
// 生成 service controller 文件
|
|
104
|
+
this.getServiceTPConfigs().forEach((tp) => {
|
|
105
|
+
const hasError = this.genFileFromTemplate((0, util_1.getFinalFileName)(`${tp.className}.ts`), config_1.TypescriptFileType.serviceController, Object.assign({ namespace: this.config.namespace, requestOptionsType: this.config.requestOptionsType, requestImportStatement: this.config.requestImportStatement, interfaceFileName: config_1.interfaceFileName }, tp));
|
|
106
|
+
prettierError.push(hasError);
|
|
107
|
+
});
|
|
108
|
+
if (prettierError.includes(true)) {
|
|
109
|
+
(0, log_1.default)('🚥 格式化失败,请检查 service controller 文件内可能存在的语法错误');
|
|
110
|
+
}
|
|
107
111
|
}
|
|
108
|
-
if (this.config.
|
|
112
|
+
if (!this.config.isOnlyGenTypeScriptType &&
|
|
113
|
+
this.config.isGenJsonSchemas &&
|
|
114
|
+
!(0, lodash_1.isEmpty)(this.schemaList)) {
|
|
109
115
|
// 处理重复的 schemaName
|
|
110
116
|
(0, util_1.handleDuplicateTypeNames)(this.schemaList);
|
|
111
117
|
// 生成 schema 文件
|
|
@@ -118,11 +124,15 @@ class ServiceGenerator {
|
|
|
118
124
|
list: this.classNameList,
|
|
119
125
|
namespace: this.config.namespace,
|
|
120
126
|
interfaceFileName: config_1.interfaceFileName,
|
|
121
|
-
isGenJsonSchemas: this.config.
|
|
127
|
+
isGenJsonSchemas: !this.config.isOnlyGenTypeScriptType &&
|
|
128
|
+
this.config.isGenJsonSchemas &&
|
|
129
|
+
!(0, lodash_1.isEmpty)(this.schemaList),
|
|
122
130
|
schemaFileName: config_1.schemaFileName,
|
|
123
|
-
isDisplayEnumLabel: !(0, lodash_1.isEmpty)(enums),
|
|
131
|
+
isDisplayEnumLabel: !this.config.isOnlyGenTypeScriptType && !(0, lodash_1.isEmpty)(enums),
|
|
124
132
|
displayEnumLabelFileName: config_1.displayEnumLabelFileName,
|
|
125
|
-
isDisplayTypeLabel: this.config.
|
|
133
|
+
isDisplayTypeLabel: !this.config.isOnlyGenTypeScriptType &&
|
|
134
|
+
this.config.isDisplayTypeLabel &&
|
|
135
|
+
!(0, lodash_1.isEmpty)(displayTypeLabels),
|
|
126
136
|
displayTypeLabelFileName: config_1.displayTypeLabelFileName,
|
|
127
137
|
});
|
|
128
138
|
// 打印日志
|
|
@@ -617,11 +627,11 @@ class ServiceGenerator {
|
|
|
617
627
|
const enumArray = schemaObject.enum;
|
|
618
628
|
let enumStr = '';
|
|
619
629
|
let enumLabelTypeStr = '';
|
|
620
|
-
if (
|
|
621
|
-
enumStr = `{${(0, lodash_1.map)(enumArray, (value) =>
|
|
630
|
+
if (config_1.numberEnum.includes(schemaObject.type) || (0, util_1.isAllNumeric)(enumArray)) {
|
|
631
|
+
enumStr = `{${(0, lodash_1.map)(enumArray, (value) => `NUMBER_${value}=${Number(value)}`).join(',')}}`;
|
|
622
632
|
}
|
|
623
633
|
else {
|
|
624
|
-
enumStr = `{${(0, lodash_1.map)(enumArray, (value) =>
|
|
634
|
+
enumStr = `{${(0, lodash_1.map)(enumArray, (value) => `${value}="${value}"`).join(',')}}`;
|
|
625
635
|
}
|
|
626
636
|
// 翻译枚举
|
|
627
637
|
if (schemaObject['x-enum-varnames'] && schemaObject['x-enum-comments']) {
|
|
@@ -637,11 +647,11 @@ class ServiceGenerator {
|
|
|
637
647
|
}).join(',')}}`;
|
|
638
648
|
}
|
|
639
649
|
else {
|
|
640
|
-
if (
|
|
641
|
-
enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) =>
|
|
650
|
+
if (config_1.numberEnum.includes(schemaObject.type) || (0, util_1.isAllNumeric)(enumArray)) {
|
|
651
|
+
enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => `NUMBER_${value}:${Number(value)}`).join(',')}}`;
|
|
642
652
|
}
|
|
643
653
|
else {
|
|
644
|
-
enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) =>
|
|
654
|
+
enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => `${value}:"${value}"`).join(',')}}`;
|
|
645
655
|
}
|
|
646
656
|
}
|
|
647
657
|
return {
|
package/dist/generator/util.d.ts
CHANGED
|
@@ -19,3 +19,4 @@ export declare function isNonArraySchemaObject(schema: unknown): schema is NonAr
|
|
|
19
19
|
export declare function isArraySchemaObject(schema: unknown): schema is ArraySchemaObject;
|
|
20
20
|
export declare function isBinaryArraySchemaObject(schema: unknown): schema is BinaryArraySchemaObject;
|
|
21
21
|
export declare function resolveRefs(obj: OpenAPIObject, fields: string[]): unknown;
|
|
22
|
+
export declare function isAllNumeric(arr: any): boolean;
|
package/dist/generator/util.js
CHANGED
|
@@ -19,6 +19,7 @@ exports.isNonArraySchemaObject = isNonArraySchemaObject;
|
|
|
19
19
|
exports.isArraySchemaObject = isArraySchemaObject;
|
|
20
20
|
exports.isBinaryArraySchemaObject = isBinaryArraySchemaObject;
|
|
21
21
|
exports.resolveRefs = resolveRefs;
|
|
22
|
+
exports.isAllNumeric = isAllNumeric;
|
|
22
23
|
const tslib_1 = require("tslib");
|
|
23
24
|
const lodash_1 = require("lodash");
|
|
24
25
|
const reserved_words_1 = tslib_1.__importDefault(require("reserved-words"));
|
|
@@ -327,3 +328,6 @@ function resolveRefs(obj, fields) {
|
|
|
327
328
|
return s;
|
|
328
329
|
}, obj);
|
|
329
330
|
}
|
|
331
|
+
function isAllNumeric(arr) {
|
|
332
|
+
return (0, lodash_1.every)(arr, (item) => (0, lodash_1.isString)(item) && /^[0-9]+$/.test(item));
|
|
333
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -21,7 +21,7 @@ function generateService(_a) {
|
|
|
21
21
|
yield (0, util_1.translateChineseModuleNodeToEnglish)(openAPI);
|
|
22
22
|
}
|
|
23
23
|
const requestImportStatement = (0, util_1.getImportStatement)(requestLibPath);
|
|
24
|
-
const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', nullable: false, isCamelCase: true, isDisplayTypeLabel: false, isGenJsonSchemas: false, allowedTags: allowedTags
|
|
24
|
+
const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', nullable: false, isCamelCase: true, isDisplayTypeLabel: false, isGenJsonSchemas: false, isOnlyGenTypeScriptType: false, allowedTags: allowedTags
|
|
25
25
|
? (0, lodash_1.map)(allowedTags, (item) => item.toLowerCase())
|
|
26
26
|
: null }, rest), openAPI);
|
|
27
27
|
serviceGenerator.genFile();
|
package/package.json
CHANGED