@@ -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,12 @@ const getMockString = (
443
459
prefix ,
444
460
typesPrefix = '' ,
445
461
transformUnderscore : boolean ,
462
+ typeNamesMapping ?: Record < string , string > ,
446
463
) => {
447
464
const typeNameConverter = createNameConverter ( typeNamesConvention , transformUnderscore ) ;
465
+ const NewTypeName = typeNamesMapping [ typeName ] || typeName ;
448
466
const casedName = typeNameConverter ( typeName ) ;
449
- const casedNameWithPrefix = typeNameConverter ( typeName , typesPrefix ) ;
467
+ const casedNameWithPrefix = typeNameConverter ( NewTypeName , typesPrefix ) ;
450
468
const typename = addTypename ? `\n __typename: '${ typeName } ',` : '' ;
451
469
const typenameReturnType = addTypename ? `{ __typename: '${ typeName } ' } & ` : '' ;
452
470
@@ -489,6 +507,7 @@ const getImportTypes = ({
489
507
transformUnderscore,
490
508
enumsAsTypes,
491
509
useTypeImports,
510
+ typeNamesMapping,
492
511
} : {
493
512
typeNamesConvention : NamingConvention ;
494
513
definitions : any ;
@@ -499,19 +518,23 @@ const getImportTypes = ({
499
518
transformUnderscore : boolean ;
500
519
enumsAsTypes : boolean ;
501
520
useTypeImports : boolean ;
521
+ typeNamesMapping ?: Record < string , string > ;
502
522
} ) => {
503
523
const typenameConverter = createNameConverter ( typeNamesConvention , transformUnderscore ) ;
504
524
const typeImports = typesPrefix ?. endsWith ( '.' )
505
525
? [ typesPrefix . slice ( 0 , - 1 ) ]
506
526
: definitions
507
527
. filter ( ( { typeName } : { typeName : string } ) => ! ! typeName )
508
528
. map ( ( { typeName } : { typeName : string } ) => typenameConverter ( typeName , typesPrefix ) ) ;
529
+
509
530
const enumTypes = enumsPrefix ?. endsWith ( '.' )
510
531
? [ enumsPrefix . slice ( 0 , - 1 ) ]
511
532
: types . filter ( ( { type } ) => type === 'enum' ) . map ( ( { name } ) => typenameConverter ( name , enumsPrefix ) ) ;
512
533
534
+ const renamedTypeImports = renameImports ( typeImports , typeNamesMapping ) ;
535
+
513
536
if ( ! enumsAsTypes || useTypeImports ) {
514
- typeImports . push ( ...enumTypes ) ;
537
+ renamedTypeImports . push ( ...enumTypes ) ;
515
538
}
516
539
517
540
function onlyUnique ( value , index , self ) {
@@ -520,7 +543,9 @@ const getImportTypes = ({
520
543
521
544
const importPrefix = `import ${ useTypeImports ? 'type ' : '' } ` ;
522
545
523
- return typesFile ? `${ importPrefix } { ${ typeImports . filter ( onlyUnique ) . join ( ', ' ) } } from '${ typesFile } ';\n` : '' ;
546
+ return typesFile
547
+ ? `${ importPrefix } { ${ renamedTypeImports . filter ( onlyUnique ) . join ( ', ' ) } } from '${ typesFile } ';\n`
548
+ : '' ;
524
549
} ;
525
550
526
551
type GeneratorName = keyof Casual . Casual | keyof Casual . functions | string ;
@@ -564,6 +589,7 @@ export interface TypescriptMocksPluginConfig {
564
589
useImplementingTypes ?: boolean ;
565
590
defaultNullableToNull ?: boolean ;
566
591
useTypeImports ?: boolean ;
592
+ typeNamesMapping ?: Record < string , string > ;
567
593
}
568
594
569
595
interface TypeItem {
@@ -614,6 +640,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
614
640
const useImplementingTypes = config . useImplementingTypes ?? false ;
615
641
const defaultNullableToNull = config . defaultNullableToNull ?? false ;
616
642
const generatorLocale = config . locale || 'en' ;
643
+ const typeNamesMapping = config . typeNamesMapping || { } ;
617
644
618
645
// List of types that are enums
619
646
const types : TypeItem [ ] = [ ] ;
@@ -693,6 +720,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
693
720
useImplementingTypes,
694
721
defaultNullableToNull,
695
722
nonNull : false ,
723
+ typeNamesMapping : config . typeNamesMapping ,
696
724
} ) ;
697
725
698
726
return ` ${ fieldName } : overrides && overrides.hasOwnProperty('${ fieldName } ') ? overrides.${ fieldName } ! : ${ value } ,` ;
@@ -731,6 +759,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
731
759
useImplementingTypes,
732
760
defaultNullableToNull,
733
761
nonNull : false ,
762
+ typeNamesMapping : config . typeNamesMapping ,
734
763
} ) ;
735
764
736
765
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
747
776
config . prefix ,
748
777
config . typesPrefix ,
749
778
transformUnderscore ,
779
+ typeNamesMapping ,
750
780
) ;
751
781
} ,
752
782
} ;
@@ -770,6 +800,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
770
800
config . prefix ,
771
801
config . typesPrefix ,
772
802
transformUnderscore ,
803
+ typeNamesMapping ,
773
804
) ;
774
805
} ,
775
806
} ;
@@ -791,6 +822,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
791
822
config . prefix ,
792
823
config . typesPrefix ,
793
824
transformUnderscore ,
825
+ typeNamesMapping ,
794
826
) ;
795
827
} ,
796
828
} ;
@@ -813,6 +845,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
813
845
transformUnderscore : transformUnderscore ,
814
846
useTypeImports : config . useTypeImports ,
815
847
enumsAsTypes,
848
+ typeNamesMapping,
816
849
} ) ;
817
850
// Function that will generate the mocks.
818
851
// We generate it after having visited because we need to distinct types from enums
0 commit comments