Skip to content

Commit cb2dd36

Browse files
feat: support only generate typescript type
1 parent e7a47bc commit cb2dd36

File tree

8 files changed

+75
-27
lines changed

8 files changed

+75
-27
lines changed

.changeset/pink-bikes-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': minor
3+
---
4+
5+
feat: support only generate typescript type

README-en_US.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ $ openapi --help
150150
--authorization <string> docs authorization
151151
--nullable <boolean> null instead of optional (default: false)
152152
--isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
153+
--isOnlyGenTypeScriptType <boolean>only generate typescript type (default: false)
153154
--isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
154155
-h, --help display help for command
155156
```
@@ -177,6 +178,7 @@ openapi -i ./spec.json -o ./apis
177178
| authorization || string | - | docs authorization |
178179
| nullable | no | boolean | false | null instead of optional |
179180
| isTranslateToEnglishTag | no | boolean | false | translate chinese tag name to english tag name |
181+
| isOnlyGenTypeScriptType | no | boolean | false | only generate typescript type |
180182
| isCamelCase | no | boolean | true | camelCase naming of controller files and request client |
181183
| hook | no | [Custom Hook](#Custom-Hook) | - | custom hook |
182184

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ $ openapi --help
150150
--authorization <string> docs authorization
151151
--nullable <boolean> null instead of optional (default: false)
152152
--isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
153+
--isOnlyGenTypeScriptType <boolean>only generate typescript type (default: false)
153154
--isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
154155
-h, --help display help for command
155156
```
@@ -177,6 +178,7 @@ openapi --i ./spec.json --o ./apis
177178
| authorization || string | - | 文档权限凭证 |
178179
| nullable || boolean | false | 使用 null 代替可选 |
179180
| isTranslateToEnglishTag || boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |
181+
| isOnlyGenTypeScriptType || boolean | false | 仅生成 typescript 类型 |
180182
| isCamelCase || boolean | true | 小驼峰命名文件和请求函数 |
181183
| hook || [Custom Hook](#Custom-Hook) | - | 自定义 hook |
182184

src/bin/openapi.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ const params = program
4545
'translate chinese tag name to english tag name',
4646
false
4747
)
48+
.option(
49+
'--isOnlyGenTypeScriptType <boolean>',
50+
'only generate typescript type',
51+
false
52+
)
4853
.option(
4954
'--isCamelCase <boolean>',
5055
'camelCase naming of controller files and request client',
@@ -83,6 +88,8 @@ async function run() {
8388
nullable: JSON.parse(params.nullable as string) === true,
8489
isTranslateToEnglishTag:
8590
JSON.parse(params.isTranslateToEnglishTag as string) === true,
91+
isOnlyGenTypeScriptType:
92+
JSON.parse(params.isOnlyGenTypeScriptType as string) === true,
8693
isCamelCase: JSON.parse(params.isCamelCase as string) === true,
8794
});
8895
process.exit(0);

src/generator/serviceGenarator.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export default class ServiceGenerator {
183183

184184
// 生成枚举翻译
185185
const enums = filter(interfaceTPConfigs, (item) => item.isEnum);
186-
if (!isEmpty(enums)) {
186+
if (!this.config.isOnlyGenTypeScriptType && !isEmpty(enums)) {
187187
this.genFileFromTemplate(
188188
`${displayEnumLabelFileName}.ts`,
189189
TypescriptFileType.displayEnumLabel,
@@ -200,7 +200,11 @@ export default class ServiceGenerator {
200200
(item) => !item.isEnum
201201
);
202202
// 生成 type 翻译
203-
if (this.config.isDisplayTypeLabel && !isEmpty(displayTypeLabels)) {
203+
if (
204+
!this.config.isOnlyGenTypeScriptType &&
205+
this.config.isDisplayTypeLabel &&
206+
!isEmpty(displayTypeLabels)
207+
) {
204208
this.genFileFromTemplate(
205209
`${displayTypeLabelFileName}.ts`,
206210
TypescriptFileType.displayTypeLabel,
@@ -212,30 +216,38 @@ export default class ServiceGenerator {
212216
);
213217
}
214218

215-
const prettierError = [];
216-
217-
// 生成 service controller 文件
218-
this.getServiceTPConfigs().forEach((tp) => {
219-
const hasError = this.genFileFromTemplate(
220-
getFinalFileName(`${tp.className}.ts`),
221-
TypescriptFileType.serviceController,
222-
{
223-
namespace: this.config.namespace,
224-
requestOptionsType: this.config.requestOptionsType,
225-
requestImportStatement: this.config.requestImportStatement,
226-
interfaceFileName: interfaceFileName,
227-
...tp,
228-
}
229-
);
219+
if (!this.config.isOnlyGenTypeScriptType) {
220+
const prettierError = [];
221+
222+
// 生成 service controller 文件
223+
this.getServiceTPConfigs().forEach((tp) => {
224+
const hasError = this.genFileFromTemplate(
225+
getFinalFileName(`${tp.className}.ts`),
226+
TypescriptFileType.serviceController,
227+
{
228+
namespace: this.config.namespace,
229+
requestOptionsType: this.config.requestOptionsType,
230+
requestImportStatement: this.config.requestImportStatement,
231+
interfaceFileName: interfaceFileName,
232+
...tp,
233+
}
234+
);
230235

231-
prettierError.push(hasError);
232-
});
236+
prettierError.push(hasError);
237+
});
233238

234-
if (prettierError.includes(true)) {
235-
log('🚥 格式化失败,请检查 service controller 文件内可能存在的语法错误');
239+
if (prettierError.includes(true)) {
240+
log(
241+
'🚥 格式化失败,请检查 service controller 文件内可能存在的语法错误'
242+
);
243+
}
236244
}
237245

238-
if (this.config.isGenJsonSchemas && !isEmpty(this.schemaList)) {
246+
if (
247+
!this.config.isOnlyGenTypeScriptType &&
248+
this.config.isGenJsonSchemas &&
249+
!isEmpty(this.schemaList)
250+
) {
239251
// 处理重复的 schemaName
240252
handleDuplicateTypeNames(this.schemaList);
241253
// 生成 schema 文件
@@ -257,12 +269,17 @@ export default class ServiceGenerator {
257269
namespace: this.config.namespace,
258270
interfaceFileName: interfaceFileName,
259271
isGenJsonSchemas:
260-
this.config.isGenJsonSchemas && !isEmpty(this.schemaList),
272+
!this.config.isOnlyGenTypeScriptType &&
273+
this.config.isGenJsonSchemas &&
274+
!isEmpty(this.schemaList),
261275
schemaFileName: schemaFileName,
262-
isDisplayEnumLabel: !isEmpty(enums),
276+
isDisplayEnumLabel:
277+
!this.config.isOnlyGenTypeScriptType && !isEmpty(enums),
263278
displayEnumLabelFileName: displayEnumLabelFileName,
264279
isDisplayTypeLabel:
265-
this.config.isDisplayTypeLabel && !isEmpty(displayTypeLabels),
280+
!this.config.isOnlyGenTypeScriptType &&
281+
this.config.isDisplayTypeLabel &&
282+
!isEmpty(displayTypeLabels),
266283
displayTypeLabelFileName: displayTypeLabelFileName,
267284
}
268285
);

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export type GenerateServiceProps = {
8787
* 是否将中文 tag 名称翻译成英文 tag 名称
8888
*/
8989
isTranslateToEnglishTag?: boolean;
90+
/**
91+
* 仅仅生成类型,不生成请求函数
92+
*/
93+
isOnlyGenTypeScriptType?: boolean;
9094
/**
9195
* 模板文件、请求函数采用小驼峰命名
9296
*/
@@ -208,6 +212,7 @@ export async function generateService({
208212
isCamelCase: true,
209213
isDisplayTypeLabel: false,
210214
isGenJsonSchemas: false,
215+
isOnlyGenTypeScriptType: false,
211216
allowedTags: allowedTags
212217
? map(allowedTags, (item) => item.toLowerCase())
213218
: null,

test/genOpenapi.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { generateService } = require('../dist/index');
22

33
generateService({
4-
schemaPath: `${__dirname}/example-files/openapi-schemas-enum-array.json`,
5-
serversPath: './apis/schemas-enum-array',
4+
schemaPath: `${__dirname}/example-files/openapi.json`,
5+
serversPath: './apis/only-generate-typescript-type',
6+
isOnlyGenTypeScriptType: true,
67
});

test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ const gen = async () => {
158158
serversPath: './apis/schemas-enum-array'
159159
});
160160

161+
// 测试 只生成 typescript 类型,不生成请求函数
162+
await openAPI.generateService({
163+
schemaPath: `${__dirname}/example-files/openapi.json`,
164+
serversPath: './apis/only-generate-typescript-type',
165+
isOnlyGenTypeScriptType: true,
166+
isDisplayTypeLabel: true,
167+
isGenJsonSchemas: true,
168+
});
169+
161170
// check 文件生成
162171
const fileControllerStr = fs.readFileSync(
163172
path.join(__dirname, 'apis/file/fileController.ts'),

0 commit comments

Comments
 (0)