@@ -17,6 +17,7 @@ type Options<T = TypeNode> = {
17
17
terminateCircularRelationships : boolean ;
18
18
prefix : string | undefined ;
19
19
typesPrefix : string ;
20
+ enumsPrefix : string ;
20
21
currentType : T ;
21
22
customScalars ?: ScalarMap ;
22
23
} ;
@@ -108,7 +109,7 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
108
109
// It's an enum
109
110
const typenameConverter = createNameConverter ( opts . typenamesConvention ) ;
110
111
const value = foundType . values ? foundType . values [ 0 ] : '' ;
111
- return `${ typenameConverter ( foundType . name ) } .${ updateTextCase (
112
+ return `${ typenameConverter ( foundType . name , opts . enumsPrefix ) } .${ updateTextCase (
112
113
value ,
113
114
opts . enumValuesConvention ,
114
115
) } `;
@@ -242,23 +243,34 @@ const getImportTypes = ({
242
243
types,
243
244
typesFile,
244
245
typesPrefix,
246
+ enumsPrefix,
245
247
} : {
246
248
typenamesConvention : NamingConvention ;
247
249
definitions : any ;
248
250
types : TypeItem [ ] ;
249
251
typesFile : string ;
250
252
typesPrefix : string ;
253
+ enumsPrefix : string ;
251
254
} ) => {
252
255
const typenameConverter = createNameConverter ( typenamesConvention ) ;
253
- const enumTypes = types . filter ( ( { type } ) => type === 'enum' ) ;
254
- const typeImports = definitions
255
- . filter ( ( { typeName } : { typeName : string } ) => ! ! typeName )
256
- . map ( ( { typeName } : { typeName : string } ) => typenameConverter ( typeName , typesPrefix ) ) ;
256
+ const typeImports = typesPrefix ?. endsWith ( '.' )
257
+ ? [ typesPrefix . slice ( 0 , - 1 ) ]
258
+ : definitions
259
+ . filter ( ( { typeName } : { typeName : string } ) => ! ! typeName )
260
+ . map ( ( { typeName } : { typeName : string } ) => typenameConverter ( typeName , typesPrefix ) ) ;
261
+ const enumTypes = enumsPrefix ?. endsWith ( '.' )
262
+ ? [ enumsPrefix . slice ( 0 , - 1 ) ]
263
+ : types . filter ( ( { type } ) => type === 'enum' ) . map ( ( { name } ) => typenameConverter ( name , enumsPrefix ) ) ;
264
+
265
+ typeImports . push ( ...enumTypes ) ;
266
+
267
+ function onlyUnique ( value , index , self ) {
268
+ return self . indexOf ( value ) === index ;
269
+ }
257
270
258
- typeImports . push ( ...enumTypes . map ( ( { name } ) => typenameConverter ( name ) ) ) ;
259
271
return typesFile
260
272
? `/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars,no-prototype-builtins */
261
- import { ${ typeImports . join ( ', ' ) } } from '${ typesFile } ';\n`
273
+ import { ${ typeImports . filter ( onlyUnique ) . join ( ', ' ) } } from '${ typesFile } ';\n`
262
274
: '' ;
263
275
} ;
264
276
@@ -281,6 +293,7 @@ export interface TypescriptMocksPluginConfig {
281
293
scalars ?: ScalarMap ;
282
294
terminateCircularRelationships ?: boolean ;
283
295
typesPrefix ?: string ;
296
+ enumsPrefix ?: string ;
284
297
}
285
298
286
299
interface TypeItem {
@@ -339,6 +352,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
339
352
terminateCircularRelationships : ! ! config . terminateCircularRelationships ,
340
353
prefix : config . prefix ,
341
354
typesPrefix : config . typesPrefix ,
355
+ enumsPrefix : config . enumsPrefix ,
342
356
currentType : node . type ,
343
357
customScalars : config . scalars ,
344
358
} ) ;
@@ -365,6 +379,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
365
379
terminateCircularRelationships : ! ! config . terminateCircularRelationships ,
366
380
prefix : config . prefix ,
367
381
typesPrefix : config . typesPrefix ,
382
+ enumsPrefix : config . enumsPrefix ,
368
383
currentType : field . type ,
369
384
customScalars : config . scalars ,
370
385
} ) ;
@@ -453,6 +468,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
453
468
types,
454
469
typesFile,
455
470
typesPrefix : config . typesPrefix ,
471
+ enumsPrefix : config . enumsPrefix ,
456
472
} ) ;
457
473
// List of function that will generate the mock.
458
474
// We generate it after having visited because we need to distinct types from enums
0 commit comments