@@ -39,6 +39,7 @@ type Options<T = TypeNode> = {
39
39
useImplementingTypes : boolean ;
40
40
defaultNullableToNull : boolean ;
41
41
nonNull : boolean ;
42
+ typeNamesMapping ?: Record < string , string > ;
42
43
} ;
43
44
44
45
const getTerminateCircularRelationshipsConfig = ( { terminateCircularRelationships } : TypescriptMocksPluginConfig ) =>
@@ -64,6 +65,15 @@ const createNameConverter =
64
65
return `${ prefix } ${ convertName ( value , resolveExternalModuleAndFn ( convention ) , transformUnderscore ) } ` ;
65
66
} ;
66
67
68
+ const renameImports = ( list : string [ ] , typeNamesMapping : Record < string , string > ) => {
69
+ return list . map ( ( type ) => {
70
+ if ( typeNamesMapping && typeNamesMapping [ type ] ) {
71
+ return `${ type } as ${ typeNamesMapping [ type ] } ` ;
72
+ }
73
+ return type ;
74
+ } ) ;
75
+ } ;
76
+
67
77
const toMockName = ( typedName : string , casedName : string , prefix ?: string ) => {
68
78
if ( prefix ) {
69
79
return `${ prefix } ${ casedName } ` ;
@@ -380,14 +390,20 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
380
390
opts . typeNamesConvention ,
381
391
opts . transformUnderscore ,
382
392
) ;
383
- const casedNameWithPrefix = typeNameConverter ( name , opts . typesPrefix ) ;
393
+ const renamedType = renameImports ( [ name ] , opts . typeNamesMapping ) [ 0 ] ;
394
+ const casedNameWithPrefix = typeNameConverter ( renamedType , opts . typesPrefix ) ;
384
395
return `relationshipsToOmit.has('${ casedName } ') ? {} as ${ casedNameWithPrefix } : ${ toMockName (
385
396
name ,
386
397
casedName ,
387
398
opts . prefix ,
388
399
) } ({}, relationshipsToOmit)`;
389
400
} else {
390
- return `relationshipsToOmit.has('${ casedName } ') ? {} as ${ casedName } : ${ toMockName (
401
+ const renamedType = renameImports ( [ name ] , opts . typeNamesMapping ) [ 0 ] ;
402
+ const renamedCasedName = createNameConverter (
403
+ opts . typeNamesConvention ,
404
+ opts . transformUnderscore ,
405
+ ) ( renamedType ) ;
406
+ return `relationshipsToOmit.has('${ casedName } ') ? {} as ${ renamedCasedName } : ${ toMockName (
391
407
name ,
392
408
casedName ,
393
409
opts . prefix ,
@@ -443,10 +459,10 @@ const getMockString = (
443
459
prefix ,
444
460
typesPrefix = '' ,
445
461
transformUnderscore : boolean ,
446
- newTypeNames ?: Record < string , string > ,
462
+ typeNamesMapping ?: Record < string , string > ,
447
463
) => {
448
464
const typeNameConverter = createNameConverter ( typeNamesConvention , transformUnderscore ) ;
449
- const NewTypeName = newTypeNames [ typeName ] || typeName ;
465
+ const NewTypeName = typeNamesMapping [ typeName ] || typeName ;
450
466
const casedName = typeNameConverter ( typeName ) ;
451
467
const casedNameWithPrefix = typeNameConverter ( NewTypeName , typesPrefix ) ;
452
468
const typename = addTypename ? `\n __typename: '${ typeName } ',` : '' ;
@@ -491,7 +507,7 @@ const getImportTypes = ({
491
507
transformUnderscore,
492
508
enumsAsTypes,
493
509
useTypeImports,
494
- newTypeNames ,
510
+ typeNamesMapping ,
495
511
} : {
496
512
typeNamesConvention : NamingConvention ;
497
513
definitions : any ;
@@ -502,24 +518,21 @@ const getImportTypes = ({
502
518
transformUnderscore : boolean ;
503
519
enumsAsTypes : boolean ;
504
520
useTypeImports : boolean ;
505
- newTypeNames ?: Record < string , string > ;
521
+ typeNamesMapping ?: Record < string , string > ;
506
522
} ) => {
507
523
const typenameConverter = createNameConverter ( typeNamesConvention , transformUnderscore ) ;
508
524
const typeImports = typesPrefix ?. endsWith ( '.' )
509
525
? [ typesPrefix . slice ( 0 , - 1 ) ]
510
526
: definitions
511
527
. filter ( ( { typeName } : { typeName : string } ) => ! ! typeName )
512
528
. map ( ( { typeName } : { typeName : string } ) => typenameConverter ( typeName , typesPrefix ) ) ;
513
- const renamedTypeImports = typeImports . map ( ( type ) => {
514
- if ( newTypeNames [ type ] ) {
515
- return `${ type } as ${ newTypeNames [ type ] } ` ;
516
- }
517
- return type ;
518
- } ) ;
529
+
519
530
const enumTypes = enumsPrefix ?. endsWith ( '.' )
520
531
? [ enumsPrefix . slice ( 0 , - 1 ) ]
521
532
: types . filter ( ( { type } ) => type === 'enum' ) . map ( ( { name } ) => typenameConverter ( name , enumsPrefix ) ) ;
522
533
534
+ const renamedTypeImports = renameImports ( typeImports , typeNamesMapping ) ;
535
+
523
536
if ( ! enumsAsTypes || useTypeImports ) {
524
537
renamedTypeImports . push ( ...enumTypes ) ;
525
538
}
@@ -576,7 +589,7 @@ export interface TypescriptMocksPluginConfig {
576
589
useImplementingTypes ?: boolean ;
577
590
defaultNullableToNull ?: boolean ;
578
591
useTypeImports ?: boolean ;
579
- newTypeNames ?: Record < string , string > ;
592
+ typeNamesMapping ?: Record < string , string > ;
580
593
}
581
594
582
595
interface TypeItem {
@@ -627,7 +640,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
627
640
const useImplementingTypes = config . useImplementingTypes ?? false ;
628
641
const defaultNullableToNull = config . defaultNullableToNull ?? false ;
629
642
const generatorLocale = config . locale || 'en' ;
630
- const newTypeNames = config . newTypeNames || { } ;
643
+ const typeNamesMapping = config . typeNamesMapping || { } ;
631
644
632
645
// List of types that are enums
633
646
const types : TypeItem [ ] = [ ] ;
@@ -707,6 +720,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
707
720
useImplementingTypes,
708
721
defaultNullableToNull,
709
722
nonNull : false ,
723
+ typeNamesMapping : config . typeNamesMapping ,
710
724
} ) ;
711
725
712
726
return ` ${ fieldName } : overrides && overrides.hasOwnProperty('${ fieldName } ') ? overrides.${ fieldName } ! : ${ value } ,` ;
@@ -745,6 +759,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
745
759
useImplementingTypes,
746
760
defaultNullableToNull,
747
761
nonNull : false ,
762
+ typeNamesMapping : config . typeNamesMapping ,
748
763
} ) ;
749
764
750
765
return ` ${ field . name . value } : overrides && overrides.hasOwnProperty('${ field . name . value } ') ? overrides.${ field . name . value } ! : ${ value } ,` ;
@@ -761,7 +776,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
761
776
config . prefix ,
762
777
config . typesPrefix ,
763
778
transformUnderscore ,
764
- newTypeNames ,
779
+ typeNamesMapping ,
765
780
) ;
766
781
} ,
767
782
} ;
@@ -785,7 +800,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
785
800
config . prefix ,
786
801
config . typesPrefix ,
787
802
transformUnderscore ,
788
- newTypeNames ,
803
+ typeNamesMapping ,
789
804
) ;
790
805
} ,
791
806
} ;
@@ -807,7 +822,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
807
822
config . prefix ,
808
823
config . typesPrefix ,
809
824
transformUnderscore ,
810
- newTypeNames ,
825
+ typeNamesMapping ,
811
826
) ;
812
827
} ,
813
828
} ;
@@ -830,7 +845,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
830
845
transformUnderscore : transformUnderscore ,
831
846
useTypeImports : config . useTypeImports ,
832
847
enumsAsTypes,
833
- newTypeNames ,
848
+ typeNamesMapping ,
834
849
} ) ;
835
850
// Function that will generate the mocks.
836
851
// We generate it after having visited because we need to distinct types from enums
0 commit comments