@@ -22,7 +22,6 @@ import { rimrafSync } from 'rimraf';
22
22
23
23
import {
24
24
PriorityRule ,
25
- SchemaObjectFormat ,
26
25
SchemaObjectType ,
27
26
displayReactQueryMode ,
28
27
} from '../config' ;
@@ -108,6 +107,7 @@ export default class ServiceGenerator {
108
107
protected config : GenerateServiceProps ;
109
108
protected openAPIData : OpenAPIObject ;
110
109
protected schemaList : ISchemaItem [ ] = [ ] ;
110
+ protected interfaceTPConfigs : ITypeItem [ ] = [ ] ;
111
111
112
112
constructor ( config : GenerateServiceProps , openAPIData : OpenAPIObject ) {
113
113
this . config = {
@@ -273,58 +273,6 @@ export default class ServiceGenerator {
273
273
const reactQueryMode = this . config . reactQueryMode ;
274
274
const reactQueryFileName = displayReactQueryFileName ( reactQueryMode ) ;
275
275
276
- // 处理重复的 typeName
277
- const interfaceTPConfigs = this . getInterfaceTPConfigs ( ) ;
278
- handleDuplicateTypeNames ( interfaceTPConfigs ) ;
279
-
280
- // 生成 ts 类型声明
281
- if ( ! isGenJavaScript ) {
282
- this . genFileFromTemplate (
283
- `${ interfaceFileName } .ts` ,
284
- TypescriptFileType . interface ,
285
- {
286
- nullable : this . config . nullable ,
287
- list : interfaceTPConfigs ,
288
- }
289
- ) ;
290
- }
291
-
292
- // 生成枚举翻译
293
- const enums = filter ( interfaceTPConfigs , ( item ) => item . isEnum ) ;
294
- if ( ! isGenJavaScript && ! isOnlyGenTypeScriptType && ! isEmpty ( enums ) ) {
295
- this . genFileFromTemplate (
296
- `${ displayEnumLabelFileName } .ts` ,
297
- TypescriptFileType . displayEnumLabel ,
298
- {
299
- list : enums ,
300
- namespace : this . config . namespace ,
301
- interfaceFileName : interfaceFileName ,
302
- }
303
- ) ;
304
- }
305
-
306
- const displayTypeLabels = filter (
307
- interfaceTPConfigs ,
308
- ( item ) => ! item . isEnum
309
- ) ;
310
- // 生成 type 翻译
311
- if (
312
- ! isGenJavaScript &&
313
- ! isOnlyGenTypeScriptType &&
314
- this . config . isDisplayTypeLabel &&
315
- ! isEmpty ( displayTypeLabels )
316
- ) {
317
- this . genFileFromTemplate (
318
- `${ displayTypeLabelFileName } .ts` ,
319
- TypescriptFileType . displayTypeLabel ,
320
- {
321
- list : displayTypeLabels ,
322
- namespace : this . config . namespace ,
323
- interfaceFileName : interfaceFileName ,
324
- }
325
- ) ;
326
- }
327
-
328
276
if ( ! isOnlyGenTypeScriptType ) {
329
277
const prettierError = [ ] ;
330
278
@@ -392,6 +340,58 @@ export default class ServiceGenerator {
392
340
}
393
341
}
394
342
343
+ // 处理重复的 typeName
344
+ this . interfaceTPConfigs = this . getInterfaceTPConfigs ( ) ;
345
+ handleDuplicateTypeNames ( this . interfaceTPConfigs ) ;
346
+
347
+ // 生成 ts 类型声明
348
+ if ( ! isGenJavaScript ) {
349
+ this . genFileFromTemplate (
350
+ `${ interfaceFileName } .ts` ,
351
+ TypescriptFileType . interface ,
352
+ {
353
+ nullable : this . config . nullable ,
354
+ list : this . interfaceTPConfigs ,
355
+ }
356
+ ) ;
357
+ }
358
+
359
+ // 生成枚举翻译
360
+ const enums = filter ( this . interfaceTPConfigs , ( item ) => item . isEnum ) ;
361
+ if ( ! isGenJavaScript && ! isOnlyGenTypeScriptType && ! isEmpty ( enums ) ) {
362
+ this . genFileFromTemplate (
363
+ `${ displayEnumLabelFileName } .ts` ,
364
+ TypescriptFileType . displayEnumLabel ,
365
+ {
366
+ list : enums ,
367
+ namespace : this . config . namespace ,
368
+ interfaceFileName : interfaceFileName ,
369
+ }
370
+ ) ;
371
+ }
372
+
373
+ const displayTypeLabels = filter (
374
+ this . interfaceTPConfigs ,
375
+ ( item ) => ! item . isEnum
376
+ ) ;
377
+ // 生成 type 翻译
378
+ if (
379
+ ! isGenJavaScript &&
380
+ ! isOnlyGenTypeScriptType &&
381
+ this . config . isDisplayTypeLabel &&
382
+ ! isEmpty ( displayTypeLabels )
383
+ ) {
384
+ this . genFileFromTemplate (
385
+ `${ displayTypeLabelFileName } .ts` ,
386
+ TypescriptFileType . displayTypeLabel ,
387
+ {
388
+ list : displayTypeLabels ,
389
+ namespace : this . config . namespace ,
390
+ interfaceFileName : interfaceFileName ,
391
+ }
392
+ ) ;
393
+ }
394
+
395
395
if (
396
396
! isOnlyGenTypeScriptType &&
397
397
this . config . isGenJsonSchemas &&
@@ -443,7 +443,7 @@ export default class ServiceGenerator {
443
443
444
444
private getInterfaceTPConfigs ( ) {
445
445
const schemas = this . openAPIData . components ?. schemas ;
446
- const lastTypes : Array < ITypeItem > = [ ] ;
446
+ const lastTypes : Array < ITypeItem > = this . interfaceTPConfigs ;
447
447
const includeTags = this . config ?. includeTags || [ ] ;
448
448
449
449
// 强行替换掉请求参数params的类型,生成方法对应的 xxxxParams 类型
@@ -637,6 +637,28 @@ export default class ServiceGenerator {
637
637
tmpFunctionRD [ functionName ] = 1 ;
638
638
}
639
639
640
+ if ( body ?. isAnonymous ) {
641
+ const bodyName = upperFirst ( `${ functionName } Body` ) ;
642
+ this . interfaceTPConfigs . push ( {
643
+ typeName : bodyName ,
644
+ type : body ?. type ,
645
+ isEnum : false ,
646
+ props : [ ] ,
647
+ } ) ;
648
+ body . type = `${ this . config . namespace } .${ bodyName } ` ;
649
+ }
650
+
651
+ if ( response ?. isAnonymous ) {
652
+ const responseName = upperFirst ( `${ functionName } Response` ) ;
653
+ this . interfaceTPConfigs . push ( {
654
+ typeName : responseName ,
655
+ type : response ?. type ,
656
+ isEnum : false ,
657
+ props : [ ] ,
658
+ } ) ;
659
+ response . type = `${ this . config . namespace } .${ responseName } ` ;
660
+ }
661
+
640
662
let formattedPath = newApi . path . replace (
641
663
/ : ( [ ^ / ] * ) | { ( [ ^ } ] * ) } / gi,
642
664
( _ , str , str2 ) => `$\{${ str || str2 } }`
@@ -909,49 +931,21 @@ export default class ServiceGenerator {
909
931
// 如果 requestBody 有 required 属性,则正常展示;如果没有,默认非必填
910
932
const required =
911
933
typeof requestBody ?. required === 'boolean' ? requestBody . required : false ;
912
-
913
- if ( schema . type === 'object' && schema . properties ) {
914
- const propertiesList = keys ( schema . properties )
915
- . map ( ( propertyKey ) => {
916
- const propertyObj = schema . properties [
917
- propertyKey
918
- ] as ArraySchemaObject ;
919
-
920
- if (
921
- propertyObj &&
922
- ! [ SchemaObjectFormat . binary , SchemaObjectFormat . base64 ] . includes (
923
- propertyObj . format as SchemaObjectFormat
924
- ) &&
925
- ! isBinaryArraySchemaObject ( propertyObj )
926
- ) {
927
- // 测试了很多用例,很少有用例走到这里
928
- return {
929
- key : propertyKey ,
930
- schema : {
931
- ...( propertyObj as ArraySchemaObject ) ,
932
- type : this . getType ( propertyObj , this . config . namespace ) ,
933
- required : schema . required ?. includes ( propertyKey ) ?? false ,
934
- } ,
935
- } ;
936
- }
937
-
938
- return null ;
939
- } )
940
- . filter ( ( p ) => p ) ;
941
-
942
- return {
943
- mediaType,
944
- ...schema ,
945
- required,
946
- propertiesList,
947
- } ;
948
- }
949
-
950
- return {
934
+ const bodySchema = {
951
935
mediaType,
952
936
required,
953
937
type : this . getType ( schema , this . config . namespace ) ,
938
+ isAnonymous : false ,
954
939
} ;
940
+
941
+ // 具名 body 场景
942
+ if ( isReferenceObject ( schema ) ) {
943
+ bodySchema . type = `${ this . config . namespace } .${ bodySchema . type } ` ;
944
+ } else {
945
+ bodySchema . isAnonymous = true ;
946
+ }
947
+
948
+ return bodySchema ;
955
949
}
956
950
957
951
private getFileTP ( requestBody : RequestBodyObject ) {
@@ -1011,6 +1005,7 @@ export default class ServiceGenerator {
1011
1005
const defaultResponse = {
1012
1006
mediaType : '*/*' ,
1013
1007
type : 'unknown' ,
1008
+ isAnonymous : false ,
1014
1009
} ;
1015
1010
1016
1011
if ( ! response ) {
@@ -1029,6 +1024,11 @@ export default class ServiceGenerator {
1029
1024
1030
1025
let schema = ( resContent [ mediaType ] . schema ||
1031
1026
DEFAULT_SCHEMA ) as SchemaObject ;
1027
+ const responseSchema = {
1028
+ mediaType,
1029
+ type : 'unknown' ,
1030
+ isAnonymous : false ,
1031
+ } ;
1032
1032
1033
1033
if ( isReferenceObject ( schema ) ) {
1034
1034
const refName = getLastRefName ( schema . $ref ) ;
@@ -1041,19 +1041,20 @@ export default class ServiceGenerator {
1041
1041
resContent [ mediaType ] . schema ||
1042
1042
DEFAULT_SCHEMA ) as SchemaObject ;
1043
1043
}
1044
+
1045
+ responseSchema . type = `${ this . config . namespace } .${ this . getType ( schema , this . config . namespace ) } ` ;
1044
1046
}
1045
1047
1046
1048
if ( isSchemaObject ( schema ) ) {
1047
1049
keys ( schema . properties ) . map ( ( fieldName ) => {
1048
1050
schema . properties [ fieldName ] [ 'required' ] =
1049
1051
schema . required ?. includes ( fieldName ) ?? false ;
1050
1052
} ) ;
1053
+ responseSchema . isAnonymous = true ;
1054
+ responseSchema . type = this . getType ( schema , this . config . namespace ) ;
1051
1055
}
1052
1056
1053
- return {
1054
- mediaType,
1055
- type : this . getType ( schema , this . config . namespace ) ,
1056
- } ;
1057
+ return responseSchema ;
1057
1058
}
1058
1059
1059
1060
private getParamsTP (
0 commit comments