@@ -20,7 +20,10 @@ const createNameConverter = (convention: NamingConvention) => (value: string) =>
20
20
}
21
21
} ;
22
22
23
- const toMockName = ( name : string ) => {
23
+ const toMockName = ( name : string , prefix ?: string ) => {
24
+ if ( prefix ) {
25
+ return `${ prefix } ${ name } ` ;
26
+ }
24
27
const isVowel = name . match ( / ^ [ A E I O ] / ) ;
25
28
return isVowel ? `an${ name } ` : `a${ name } ` ;
26
29
} ;
@@ -59,6 +62,7 @@ const getNamedType = (
59
62
types : TypeItem [ ] ,
60
63
typenamesConvention : NamingConvention ,
61
64
enumValuesConvention : NamingConvention ,
65
+ prefix ?: string ,
62
66
namedType ?: NamedTypeNode ,
63
67
) : string | number | boolean => {
64
68
if ( ! namedType ) {
@@ -98,6 +102,7 @@ const getNamedType = (
98
102
types ,
99
103
typenamesConvention ,
100
104
enumValuesConvention ,
105
+ prefix ,
101
106
foundType . types && foundType . types [ 0 ] ,
102
107
) ;
103
108
case 'scalar' :
@@ -108,7 +113,7 @@ const getNamedType = (
108
113
throw `foundType is unknown: ${ foundType . name } : ${ foundType . type } ` ;
109
114
}
110
115
}
111
- return `${ toMockName ( name ) } ()` ;
116
+ return `${ toMockName ( name , prefix ) } ()` ;
112
117
}
113
118
}
114
119
} ;
@@ -119,6 +124,7 @@ const generateMockValue = (
119
124
types : TypeItem [ ] ,
120
125
typenamesConvention : NamingConvention ,
121
126
enumValuesConvention : NamingConvention ,
127
+ prefix : string | undefined ,
122
128
currentType : TypeNode ,
123
129
) : string | number | boolean => {
124
130
switch ( currentType . kind ) {
@@ -129,6 +135,7 @@ const generateMockValue = (
129
135
types ,
130
136
typenamesConvention ,
131
137
enumValuesConvention ,
138
+ prefix ,
132
139
currentType as NamedTypeNode ,
133
140
) ;
134
141
case 'NonNullType' :
@@ -138,6 +145,7 @@ const generateMockValue = (
138
145
types ,
139
146
typenamesConvention ,
140
147
enumValuesConvention ,
148
+ prefix ,
141
149
currentType . type ,
142
150
) ;
143
151
case 'ListType' : {
@@ -147,6 +155,7 @@ const generateMockValue = (
147
155
types ,
148
156
typenamesConvention ,
149
157
enumValuesConvention ,
158
+ prefix ,
150
159
currentType . type ,
151
160
) ;
152
161
return `[${ value } ]` ;
@@ -159,11 +168,12 @@ const getMockString = (
159
168
fields : string ,
160
169
typenamesConvention : NamingConvention ,
161
170
addTypename = false ,
171
+ prefix ,
162
172
) => {
163
173
const casedName = createNameConverter ( typenamesConvention ) ( typeName ) ;
164
174
const typename = addTypename ? `\n __typename: '${ casedName } ',` : '' ;
165
175
return `
166
- export const ${ toMockName ( casedName ) } = (overrides?: Partial<${ casedName } >): ${ casedName } => {
176
+ export const ${ toMockName ( casedName , prefix ) } = (overrides?: Partial<${ casedName } >): ${ casedName } => {
167
177
return {${ typename }
168
178
${ fields }
169
179
};
@@ -175,6 +185,7 @@ export interface TypescriptMocksPluginConfig {
175
185
enumValues ?: NamingConvention ;
176
186
typenames ?: NamingConvention ;
177
187
addTypename ?: boolean ;
188
+ prefix ?: string ;
178
189
}
179
190
180
191
interface TypeItem {
@@ -230,6 +241,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
230
241
types ,
231
242
typenamesConvention ,
232
243
enumValuesConvention ,
244
+ config . prefix ,
233
245
node . type ,
234
246
) ;
235
247
@@ -252,6 +264,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
252
264
types ,
253
265
typenamesConvention ,
254
266
enumValuesConvention ,
267
+ config . prefix ,
255
268
field . type ,
256
269
) ;
257
270
@@ -260,7 +273,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
260
273
. join ( '\n' )
261
274
: '' ;
262
275
263
- return getMockString ( fieldName , mockFields , typenamesConvention , false ) ;
276
+ return getMockString ( fieldName , mockFields , typenamesConvention , false , config . prefix ) ;
264
277
} ,
265
278
} ;
266
279
} ,
@@ -278,7 +291,13 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
278
291
mockFn : ( ) => {
279
292
const mockFields = fields ? fields . map ( ( { mockFn } : any ) => mockFn ( typeName ) ) . join ( '\n' ) : '' ;
280
293
281
- return getMockString ( typeName , mockFields , typenamesConvention , ! ! config . addTypename ) ;
294
+ return getMockString (
295
+ typeName ,
296
+ mockFields ,
297
+ typenamesConvention ,
298
+ ! ! config . addTypename ,
299
+ config . prefix ,
300
+ ) ;
282
301
} ,
283
302
} ;
284
303
} ,
0 commit comments