@@ -443,10 +443,12 @@ const getMockString = (
443
443
prefix ,
444
444
typesPrefix = '' ,
445
445
transformUnderscore : boolean ,
446
+ newTypeNames ?: Record < string , string > ,
446
447
) => {
447
448
const typeNameConverter = createNameConverter ( typeNamesConvention , transformUnderscore ) ;
449
+ const NewTypeName = newTypeNames [ typeName ] || typeName ;
448
450
const casedName = typeNameConverter ( typeName ) ;
449
- const casedNameWithPrefix = typeNameConverter ( typeName , typesPrefix ) ;
451
+ const casedNameWithPrefix = typeNameConverter ( NewTypeName , typesPrefix ) ;
450
452
const typename = addTypename ? `\n __typename: '${ typeName } ',` : '' ;
451
453
const typenameReturnType = addTypename ? `{ __typename: '${ typeName } ' } & ` : '' ;
452
454
@@ -489,6 +491,7 @@ const getImportTypes = ({
489
491
transformUnderscore,
490
492
enumsAsTypes,
491
493
useTypeImports,
494
+ newTypeNames,
492
495
} : {
493
496
typeNamesConvention : NamingConvention ;
494
497
definitions : any ;
@@ -499,19 +502,26 @@ const getImportTypes = ({
499
502
transformUnderscore : boolean ;
500
503
enumsAsTypes : boolean ;
501
504
useTypeImports : boolean ;
505
+ newTypeNames ?: Record < string , string > ;
502
506
} ) => {
503
507
const typenameConverter = createNameConverter ( typeNamesConvention , transformUnderscore ) ;
504
508
const typeImports = typesPrefix ?. endsWith ( '.' )
505
509
? [ typesPrefix . slice ( 0 , - 1 ) ]
506
510
: definitions
507
511
. filter ( ( { typeName } : { typeName : string } ) => ! ! typeName )
508
512
. 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
+ } ) ;
509
519
const enumTypes = enumsPrefix ?. endsWith ( '.' )
510
520
? [ enumsPrefix . slice ( 0 , - 1 ) ]
511
521
: types . filter ( ( { type } ) => type === 'enum' ) . map ( ( { name } ) => typenameConverter ( name , enumsPrefix ) ) ;
512
522
513
523
if ( ! enumsAsTypes || useTypeImports ) {
514
- typeImports . push ( ...enumTypes ) ;
524
+ renamedTypeImports . push ( ...enumTypes ) ;
515
525
}
516
526
517
527
function onlyUnique ( value , index , self ) {
@@ -520,7 +530,9 @@ const getImportTypes = ({
520
530
521
531
const importPrefix = `import ${ useTypeImports ? 'type ' : '' } ` ;
522
532
523
- return typesFile ? `${ importPrefix } { ${ typeImports . filter ( onlyUnique ) . join ( ', ' ) } } from '${ typesFile } ';\n` : '' ;
533
+ return typesFile
534
+ ? `${ importPrefix } { ${ renamedTypeImports . filter ( onlyUnique ) . join ( ', ' ) } } from '${ typesFile } ';\n`
535
+ : '' ;
524
536
} ;
525
537
526
538
type GeneratorName = keyof Casual . Casual | keyof Casual . functions | string ;
@@ -564,6 +576,7 @@ export interface TypescriptMocksPluginConfig {
564
576
useImplementingTypes ?: boolean ;
565
577
defaultNullableToNull ?: boolean ;
566
578
useTypeImports ?: boolean ;
579
+ newTypeNames ?: Record < string , string > ;
567
580
}
568
581
569
582
interface TypeItem {
@@ -614,6 +627,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
614
627
const useImplementingTypes = config . useImplementingTypes ?? false ;
615
628
const defaultNullableToNull = config . defaultNullableToNull ?? false ;
616
629
const generatorLocale = config . locale || 'en' ;
630
+ const newTypeNames = config . newTypeNames || { } ;
617
631
618
632
// List of types that are enums
619
633
const types : TypeItem [ ] = [ ] ;
@@ -747,6 +761,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
747
761
config . prefix ,
748
762
config . typesPrefix ,
749
763
transformUnderscore ,
764
+ newTypeNames ,
750
765
) ;
751
766
} ,
752
767
} ;
@@ -770,6 +785,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
770
785
config . prefix ,
771
786
config . typesPrefix ,
772
787
transformUnderscore ,
788
+ newTypeNames ,
773
789
) ;
774
790
} ,
775
791
} ;
@@ -791,6 +807,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
791
807
config . prefix ,
792
808
config . typesPrefix ,
793
809
transformUnderscore ,
810
+ newTypeNames ,
794
811
) ;
795
812
} ,
796
813
} ;
@@ -813,6 +830,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
813
830
transformUnderscore : transformUnderscore ,
814
831
useTypeImports : config . useTypeImports ,
815
832
enumsAsTypes,
833
+ newTypeNames,
816
834
} ) ;
817
835
// Function that will generate the mocks.
818
836
// We generate it after having visited because we need to distinct types from enums
0 commit comments