@@ -39,6 +39,7 @@ type Options<T = TypeNode> = {
3939 useImplementingTypes : boolean ;
4040 defaultNullableToNull : boolean ;
4141 nonNull : boolean ;
42+ typeNamesMapping ?: Record < string , string > ;
4243} ;
4344
4445const getTerminateCircularRelationshipsConfig = ( { terminateCircularRelationships } : TypescriptMocksPluginConfig ) =>
@@ -64,6 +65,15 @@ const createNameConverter =
6465 return `${ prefix } ${ convertName ( value , resolveExternalModuleAndFn ( convention ) , transformUnderscore ) } ` ;
6566 } ;
6667
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+
6777const toMockName = ( typedName : string , casedName : string , prefix ?: string ) => {
6878 if ( prefix ) {
6979 return `${ prefix } ${ casedName } ` ;
@@ -380,14 +390,20 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
380390 opts . typeNamesConvention ,
381391 opts . transformUnderscore ,
382392 ) ;
383- const casedNameWithPrefix = typeNameConverter ( name , opts . typesPrefix ) ;
393+ const renamedType = renameImports ( [ name ] , opts . typeNamesMapping ) [ 0 ] ;
394+ const casedNameWithPrefix = typeNameConverter ( renamedType , opts . typesPrefix ) ;
384395 return `relationshipsToOmit.has('${ casedName } ') ? {} as ${ casedNameWithPrefix } : ${ toMockName (
385396 name ,
386397 casedName ,
387398 opts . prefix ,
388399 ) } ({}, relationshipsToOmit)`;
389400 } 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 (
391407 name ,
392408 casedName ,
393409 opts . prefix ,
@@ -443,10 +459,12 @@ const getMockString = (
443459 prefix ,
444460 typesPrefix = '' ,
445461 transformUnderscore : boolean ,
462+ typeNamesMapping ?: Record < string , string > ,
446463) => {
447464 const typeNameConverter = createNameConverter ( typeNamesConvention , transformUnderscore ) ;
465+ const NewTypeName = typeNamesMapping [ typeName ] || typeName ;
448466 const casedName = typeNameConverter ( typeName ) ;
449- const casedNameWithPrefix = typeNameConverter ( typeName , typesPrefix ) ;
467+ const casedNameWithPrefix = typeNameConverter ( NewTypeName , typesPrefix ) ;
450468 const typename = addTypename ? `\n __typename: '${ typeName } ',` : '' ;
451469 const typenameReturnType = addTypename ? `{ __typename: '${ typeName } ' } & ` : '' ;
452470
@@ -489,6 +507,7 @@ const getImportTypes = ({
489507 transformUnderscore,
490508 enumsAsTypes,
491509 useTypeImports,
510+ typeNamesMapping,
492511} : {
493512 typeNamesConvention : NamingConvention ;
494513 definitions : any ;
@@ -499,19 +518,23 @@ const getImportTypes = ({
499518 transformUnderscore : boolean ;
500519 enumsAsTypes : boolean ;
501520 useTypeImports : boolean ;
521+ typeNamesMapping ?: Record < string , string > ;
502522} ) => {
503523 const typenameConverter = createNameConverter ( typeNamesConvention , transformUnderscore ) ;
504524 const typeImports = typesPrefix ?. endsWith ( '.' )
505525 ? [ typesPrefix . slice ( 0 , - 1 ) ]
506526 : definitions
507527 . filter ( ( { typeName } : { typeName : string } ) => ! ! typeName )
508528 . map ( ( { typeName } : { typeName : string } ) => typenameConverter ( typeName , typesPrefix ) ) ;
529+
509530 const enumTypes = enumsPrefix ?. endsWith ( '.' )
510531 ? [ enumsPrefix . slice ( 0 , - 1 ) ]
511532 : types . filter ( ( { type } ) => type === 'enum' ) . map ( ( { name } ) => typenameConverter ( name , enumsPrefix ) ) ;
512533
534+ const renamedTypeImports = renameImports ( typeImports , typeNamesMapping ) ;
535+
513536 if ( ! enumsAsTypes || useTypeImports ) {
514- typeImports . push ( ...enumTypes ) ;
537+ renamedTypeImports . push ( ...enumTypes ) ;
515538 }
516539
517540 function onlyUnique ( value , index , self ) {
@@ -520,7 +543,9 @@ const getImportTypes = ({
520543
521544 const importPrefix = `import ${ useTypeImports ? 'type ' : '' } ` ;
522545
523- return typesFile ? `${ importPrefix } { ${ typeImports . filter ( onlyUnique ) . join ( ', ' ) } } from '${ typesFile } ';\n` : '' ;
546+ return typesFile
547+ ? `${ importPrefix } { ${ renamedTypeImports . filter ( onlyUnique ) . join ( ', ' ) } } from '${ typesFile } ';\n`
548+ : '' ;
524549} ;
525550
526551type GeneratorName = keyof Casual . Casual | keyof Casual . functions | string ;
@@ -564,6 +589,7 @@ export interface TypescriptMocksPluginConfig {
564589 useImplementingTypes ?: boolean ;
565590 defaultNullableToNull ?: boolean ;
566591 useTypeImports ?: boolean ;
592+ typeNamesMapping ?: Record < string , string > ;
567593}
568594
569595interface TypeItem {
@@ -614,6 +640,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
614640 const useImplementingTypes = config . useImplementingTypes ?? false ;
615641 const defaultNullableToNull = config . defaultNullableToNull ?? false ;
616642 const generatorLocale = config . locale || 'en' ;
643+ const typeNamesMapping = config . typeNamesMapping || { } ;
617644
618645 // List of types that are enums
619646 const types : TypeItem [ ] = [ ] ;
@@ -693,6 +720,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
693720 useImplementingTypes,
694721 defaultNullableToNull,
695722 nonNull : false ,
723+ typeNamesMapping : config . typeNamesMapping ,
696724 } ) ;
697725
698726 return ` ${ fieldName } : overrides && overrides.hasOwnProperty('${ fieldName } ') ? overrides.${ fieldName } ! : ${ value } ,` ;
@@ -731,6 +759,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
731759 useImplementingTypes,
732760 defaultNullableToNull,
733761 nonNull : false ,
762+ typeNamesMapping : config . typeNamesMapping ,
734763 } ) ;
735764
736765 return ` ${ field . name . value } : overrides && overrides.hasOwnProperty('${ field . name . value } ') ? overrides.${ field . name . value } ! : ${ value } ,` ;
@@ -747,6 +776,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
747776 config . prefix ,
748777 config . typesPrefix ,
749778 transformUnderscore ,
779+ typeNamesMapping ,
750780 ) ;
751781 } ,
752782 } ;
@@ -770,6 +800,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
770800 config . prefix ,
771801 config . typesPrefix ,
772802 transformUnderscore ,
803+ typeNamesMapping ,
773804 ) ;
774805 } ,
775806 } ;
@@ -791,6 +822,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
791822 config . prefix ,
792823 config . typesPrefix ,
793824 transformUnderscore ,
825+ typeNamesMapping ,
794826 ) ;
795827 } ,
796828 } ;
@@ -813,6 +845,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
813845 transformUnderscore : transformUnderscore ,
814846 useTypeImports : config . useTypeImports ,
815847 enumsAsTypes,
848+ typeNamesMapping,
816849 } ) ;
817850 // Function that will generate the mocks.
818851 // We generate it after having visited because we need to distinct types from enums
0 commit comments