Skip to content

Commit 028ccf7

Browse files
emabardeois
andauthored
feat: rename typenames to typeNames (#105)
BREAKING CHANGE: rename `typenames` to `typeNames` An error will be thrown if the old config is used Co-authored-by: Corentin Ardeois <[email protected]>
1 parent a76842d commit 028ccf7

File tree

4 files changed

+35
-31
lines changed

4 files changed

+35
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ keep all GraphQL names as-is. Available case functions in `change-case-all` are
4040
`localeLowerCase`, `lowerCaseFirst`, `spongeCase`, `titleCase`, `upperCase`, `localeUpperCase` and `upperCaseFirst`
4141
[See more](https://github.com/btxtiger/change-case-all)
4242

43-
### typenames (`string`, defaultValue: `change-case-all#pascalCase`)
43+
### typeNames (`string`, defaultValue: `change-case-all#pascalCase`)
4444

4545
Changes the case of types. The format of the converter must be a valid `module#method`. You can also use `keep` to
4646
keep all GraphQL names as-is. Available case functions in `change-case-all` are `camelCase`, `capitalCase`, `constantCase`,

src/index.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Options<T = TypeNode> = {
1212
typeName: string;
1313
fieldName: string;
1414
types: TypeItem[];
15-
typenamesConvention: NamingConvention;
15+
typeNamesConvention: NamingConvention;
1616
enumValuesConvention: NamingConvention;
1717
terminateCircularRelationships: boolean;
1818
prefix: string | undefined;
@@ -175,7 +175,7 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
175175
});
176176
if (!opts.dynamicValues) mockValueGenerator.seed(hashedString(opts.typeName + opts.fieldName));
177177
const name = opts.currentType.name.value;
178-
const casedName = createNameConverter(opts.typenamesConvention, opts.transformUnderscore)(name);
178+
const casedName = createNameConverter(opts.typeNamesConvention, opts.transformUnderscore)(name);
179179
switch (name) {
180180
case 'String': {
181181
const customScalar = opts.customScalars ? getScalarDefinition(opts.customScalars['String']) : null;
@@ -204,7 +204,7 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
204204
case 'enum': {
205205
// It's an enum
206206
const typenameConverter = createNameConverter(
207-
opts.typenamesConvention,
207+
opts.typeNamesConvention,
208208
opts.transformUnderscore,
209209
);
210210
const enumConverter = createNameConverter(opts.enumValuesConvention, opts.transformUnderscore);
@@ -279,16 +279,16 @@ const generateMockValue = (opts: Options): string | number | boolean => {
279279
const getMockString = (
280280
typeName: string,
281281
fields: string,
282-
typenamesConvention: NamingConvention,
282+
typeNamesConvention: NamingConvention,
283283
terminateCircularRelationships: boolean,
284284
addTypename = false,
285285
prefix,
286286
typesPrefix = '',
287287
transformUnderscore: boolean,
288288
) => {
289-
const typenameConverter = createNameConverter(typenamesConvention, transformUnderscore);
290-
const casedName = typenameConverter(typeName);
291-
const casedNameWithPrefix = typenameConverter(typeName, typesPrefix);
289+
const typeNameConverter = createNameConverter(typeNamesConvention, transformUnderscore);
290+
const casedName = typeNameConverter(typeName);
291+
const casedNameWithPrefix = typeNameConverter(typeName, typesPrefix);
292292
const typename = addTypename ? `\n __typename: '${typeName}',` : '';
293293
const typenameReturnType = addTypename ? `{ __typename: '${typeName}' } & ` : '';
294294

@@ -319,23 +319,23 @@ ${fields}
319319
};
320320

321321
const getImportTypes = ({
322-
typenamesConvention,
322+
typeNamesConvention,
323323
definitions,
324324
types,
325325
typesFile,
326326
typesPrefix,
327327
enumsPrefix,
328328
transformUnderscore,
329329
}: {
330-
typenamesConvention: NamingConvention;
330+
typeNamesConvention: NamingConvention;
331331
definitions: any;
332332
types: TypeItem[];
333333
typesFile: string;
334334
typesPrefix: string;
335335
enumsPrefix: string;
336336
transformUnderscore: boolean;
337337
}) => {
338-
const typenameConverter = createNameConverter(typenamesConvention, transformUnderscore);
338+
const typenameConverter = createNameConverter(typeNamesConvention, transformUnderscore);
339339
const typeImports = typesPrefix?.endsWith('.')
340340
? [typesPrefix.slice(0, -1)]
341341
: definitions
@@ -367,7 +367,7 @@ type ScalarMap = {
367367
export interface TypescriptMocksPluginConfig {
368368
typesFile?: string;
369369
enumValues?: NamingConvention;
370-
typenames?: NamingConvention;
370+
typeNames?: NamingConvention;
371371
addTypename?: boolean;
372372
prefix?: string;
373373
scalars?: ScalarMap;
@@ -413,8 +413,12 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
413413
const printedSchema = printSchema(schema); // Returns a string representation of the schema
414414
const astNode = parse(printedSchema); // Transforms the string into ASTNode
415415

416+
if ('typenames' in config) {
417+
throw new Error('Config `typenames` was renamed to `typeNames`. Please update your config');
418+
}
419+
416420
const enumValuesConvention = config.enumValues || 'change-case-all#pascalCase';
417-
const typenamesConvention = config.typenames || 'change-case-all#pascalCase';
421+
const typeNamesConvention = config.typeNames || 'change-case-all#pascalCase';
418422
const transformUnderscore = config.transformUnderscore ?? true;
419423
const listElementCount = config.listElementCount > 0 ? config.listElementCount : 1;
420424
const dynamicValues = !!config.dynamicValues;
@@ -452,7 +456,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
452456
typeName,
453457
fieldName,
454458
types,
455-
typenamesConvention,
459+
typeNamesConvention,
456460
enumValuesConvention,
457461
terminateCircularRelationships: !!config.terminateCircularRelationships,
458462
prefix: config.prefix,
@@ -483,7 +487,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
483487
typeName: fieldName,
484488
fieldName: field.name.value,
485489
types,
486-
typenamesConvention,
490+
typeNamesConvention,
487491
enumValuesConvention,
488492
terminateCircularRelationships: !!config.terminateCircularRelationships,
489493
prefix: config.prefix,
@@ -505,7 +509,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
505509
return getMockString(
506510
fieldName,
507511
mockFields,
508-
typenamesConvention,
512+
typeNamesConvention,
509513
!!config.terminateCircularRelationships,
510514
false,
511515
config.prefix,
@@ -528,7 +532,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
528532
return getMockString(
529533
typeName,
530534
mockFields,
531-
typenamesConvention,
535+
typeNamesConvention,
532536
!!config.terminateCircularRelationships,
533537
!!config.addTypename,
534538
config.prefix,
@@ -549,7 +553,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
549553
return getMockString(
550554
typeName,
551555
mockFields,
552-
typenamesConvention,
556+
typeNamesConvention,
553557
!!config.terminateCircularRelationships,
554558
!!config.addTypename,
555559
config.prefix,
@@ -575,7 +579,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
575579
const typesFile = config.typesFile ? config.typesFile.replace(/\.[\w]+$/, '') : null;
576580

577581
const typesFileImport = getImportTypes({
578-
typenamesConvention,
582+
typeNamesConvention,
579583
definitions,
580584
types,
581585
typesFile,

tests/__snapshots__/typescript-mock-data.spec.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ export const aQuery = (overrides?: Partial<Query>): Query => {
15741574
"
15751575
`;
15761576

1577-
exports[`should generate mock data with PascalCase enum values if typenames is "pascal-case#pascalCase" 1`] = `
1577+
exports[`should generate mock data with PascalCase enum values if typeNames is "pascal-case#pascalCase" 1`] = `
15781578
"
15791579
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
15801580
return {
@@ -1809,7 +1809,7 @@ export const aQuery = (overrides?: Partial<Query>): Query => {
18091809
"
18101810
`;
18111811

1812-
exports[`should generate mock data with as-is types and enums if typenames is "keep" 1`] = `
1812+
exports[`should generate mock data with as-is types and enums if typeNames is "keep" 1`] = `
18131813
"
18141814
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
18151815
return {
@@ -2052,7 +2052,7 @@ export const aQuery = (overrides?: Partial<Query>): Query => {
20522052
"
20532053
`;
20542054

2055-
exports[`should generate mock data with upperCase types and enums if typenames is "upper-case#upperCase" 1`] = `
2055+
exports[`should generate mock data with upperCase types and enums if typeNames is "upper-case#upperCase" 1`] = `
20562056
"
20572057
export const anAVATAR = (overrides?: Partial<AVATAR>): AVATAR => {
20582058
return {
@@ -2130,7 +2130,7 @@ export const aQUERY = (overrides?: Partial<QUERY>): QUERY => {
21302130
"
21312131
`;
21322132

2133-
exports[`should generate mock data with upperCase types and imports if typenames is "upper-case#upperCase" 1`] = `
2133+
exports[`should generate mock data with upperCase types and imports if typeNames is "upper-case#upperCase" 1`] = `
21342134
"import { AVATAR, USER, WITHAVATAR, CAMELCASETHING, PREFIXED_RESPONSE, ABCTYPE, LISTTYPE, UPDATEUSERINPUT, MUTATION, QUERY, ABCSTATUS, STATUS, PREFIXED_ENUM } from './types/graphql';
21352135

21362136
export const anAVATAR = (overrides?: Partial<AVATAR>): AVATAR => {

tests/typescript-mock-data.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ it('should generate mock data with PascalCase types and enums by default', async
191191
expect(result).toMatchSnapshot();
192192
});
193193

194-
it('should generate mock data with PascalCase enum values if typenames is "pascal-case#pascalCase"', async () => {
195-
const result = await plugin(testSchema, [], { typenames: 'pascal-case#pascalCase' });
194+
it('should generate mock data with PascalCase enum values if typeNames is "pascal-case#pascalCase"', async () => {
195+
const result = await plugin(testSchema, [], { typeNames: 'pascal-case#pascalCase' });
196196

197197
expect(result).toBeDefined();
198198
expect(result).toMatch(/Abc(Type|Status)/);
@@ -201,8 +201,8 @@ it('should generate mock data with PascalCase enum values if typenames is "pasca
201201
expect(result).toMatchSnapshot();
202202
});
203203

204-
it('should generate mock data with upperCase types and enums if typenames is "upper-case#upperCase"', async () => {
205-
const result = await plugin(testSchema, [], { typenames: 'upper-case#upperCase' });
204+
it('should generate mock data with upperCase types and enums if typeNames is "upper-case#upperCase"', async () => {
205+
const result = await plugin(testSchema, [], { typeNames: 'upper-case#upperCase' });
206206

207207
expect(result).toBeDefined();
208208
expect(result).not.toMatch(/Abc(Type|Status)/);
@@ -211,8 +211,8 @@ it('should generate mock data with upperCase types and enums if typenames is "up
211211
expect(result).toMatchSnapshot();
212212
});
213213

214-
it('should generate mock data with upperCase types and imports if typenames is "upper-case#upperCase"', async () => {
215-
const result = await plugin(testSchema, [], { typenames: 'upper-case#upperCase', typesFile: './types/graphql.ts' });
214+
it('should generate mock data with upperCase types and imports if typeNames is "upper-case#upperCase"', async () => {
215+
const result = await plugin(testSchema, [], { typeNames: 'upper-case#upperCase', typesFile: './types/graphql.ts' });
216216

217217
expect(result).toBeDefined();
218218
expect(result).not.toMatch(/Abc(Type|Status)/);
@@ -221,8 +221,8 @@ it('should generate mock data with upperCase types and imports if typenames is "
221221
expect(result).toMatchSnapshot();
222222
});
223223

224-
it('should generate mock data with as-is types and enums if typenames is "keep"', async () => {
225-
const result = await plugin(testSchema, [], { typenames: 'keep' });
224+
it('should generate mock data with as-is types and enums if typeNames is "keep"', async () => {
225+
const result = await plugin(testSchema, [], { typeNames: 'keep' });
226226

227227
expect(result).toBeDefined();
228228
expect(result).not.toMatch(/Abc(Type|Status)/);

0 commit comments

Comments
 (0)