Skip to content

Commit 69fd16c

Browse files
author
s.vanriessen
committed
feat: rename types in config
1 parent 52b4a5d commit 69fd16c

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

src/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,12 @@ const getMockString = (
443443
prefix,
444444
typesPrefix = '',
445445
transformUnderscore: boolean,
446+
newTypeNames?: Record<string, string>,
446447
) => {
447448
const typeNameConverter = createNameConverter(typeNamesConvention, transformUnderscore);
449+
const NewTypeName = newTypeNames[typeName] || typeName;
448450
const casedName = typeNameConverter(typeName);
449-
const casedNameWithPrefix = typeNameConverter(typeName, typesPrefix);
451+
const casedNameWithPrefix = typeNameConverter(NewTypeName, typesPrefix);
450452
const typename = addTypename ? `\n __typename: '${typeName}',` : '';
451453
const typenameReturnType = addTypename ? `{ __typename: '${typeName}' } & ` : '';
452454

@@ -489,6 +491,7 @@ const getImportTypes = ({
489491
transformUnderscore,
490492
enumsAsTypes,
491493
useTypeImports,
494+
newTypeNames,
492495
}: {
493496
typeNamesConvention: NamingConvention;
494497
definitions: any;
@@ -499,19 +502,26 @@ const getImportTypes = ({
499502
transformUnderscore: boolean;
500503
enumsAsTypes: boolean;
501504
useTypeImports: boolean;
505+
newTypeNames?: Record<string, string>;
502506
}) => {
503507
const typenameConverter = createNameConverter(typeNamesConvention, transformUnderscore);
504508
const typeImports = typesPrefix?.endsWith('.')
505509
? [typesPrefix.slice(0, -1)]
506510
: definitions
507511
.filter(({ typeName }: { typeName: string }) => !!typeName)
508512
.map(({ typeName }: { typeName: string }) => typenameConverter(typeName, typesPrefix));
513+
const renamedTypeImports = typeImports.map((type) => {
514+
if (newTypeNames[type]) {
515+
return `${type} as ${newTypeNames[type]}`;
516+
}
517+
return type;
518+
});
509519
const enumTypes = enumsPrefix?.endsWith('.')
510520
? [enumsPrefix.slice(0, -1)]
511521
: types.filter(({ type }) => type === 'enum').map(({ name }) => typenameConverter(name, enumsPrefix));
512522

513523
if (!enumsAsTypes || useTypeImports) {
514-
typeImports.push(...enumTypes);
524+
renamedTypeImports.push(...enumTypes);
515525
}
516526

517527
function onlyUnique(value, index, self) {
@@ -520,7 +530,9 @@ const getImportTypes = ({
520530

521531
const importPrefix = `import ${useTypeImports ? 'type ' : ''}`;
522532

523-
return typesFile ? `${importPrefix}{ ${typeImports.filter(onlyUnique).join(', ')} } from '${typesFile}';\n` : '';
533+
return typesFile
534+
? `${importPrefix}{ ${renamedTypeImports.filter(onlyUnique).join(', ')} } from '${typesFile}';\n`
535+
: '';
524536
};
525537

526538
type GeneratorName = keyof Casual.Casual | keyof Casual.functions | string;
@@ -564,6 +576,7 @@ export interface TypescriptMocksPluginConfig {
564576
useImplementingTypes?: boolean;
565577
defaultNullableToNull?: boolean;
566578
useTypeImports?: boolean;
579+
newTypeNames?: Record<string, string>;
567580
}
568581

569582
interface TypeItem {
@@ -614,6 +627,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
614627
const useImplementingTypes = config.useImplementingTypes ?? false;
615628
const defaultNullableToNull = config.defaultNullableToNull ?? false;
616629
const generatorLocale = config.locale || 'en';
630+
const newTypeNames = config.newTypeNames || {};
617631

618632
// List of types that are enums
619633
const types: TypeItem[] = [];
@@ -747,6 +761,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
747761
config.prefix,
748762
config.typesPrefix,
749763
transformUnderscore,
764+
newTypeNames,
750765
);
751766
},
752767
};
@@ -770,6 +785,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
770785
config.prefix,
771786
config.typesPrefix,
772787
transformUnderscore,
788+
newTypeNames,
773789
);
774790
},
775791
};
@@ -791,6 +807,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
791807
config.prefix,
792808
config.typesPrefix,
793809
transformUnderscore,
810+
newTypeNames,
794811
);
795812
},
796813
};
@@ -813,6 +830,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
813830
transformUnderscore: transformUnderscore,
814831
useTypeImports: config.useTypeImports,
815832
enumsAsTypes,
833+
newTypeNames,
816834
});
817835
// Function that will generate the mocks.
818836
// We generate it after having visited because we need to distinct types from enums

tests/useTypeImports/__snapshots__/spec.ts.snap

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`should support useTypeImports 1`] = `
4-
"import type { Avatar, User, WithAvatar, CamelCaseThing, PrefixedResponse, AbcType, ListType, UpdateUserInput, Mutation, Query, AbcStatus, Status, PrefixedEnum } from './types/graphql';
4+
"import type { Avatar, User, Partial, WithAvatar, CamelCaseThing, PrefixedResponse, AbcType, ListType, UpdateUserInput, Mutation, Query, AbcStatus, Status, PrefixedEnum } from './types/graphql';
55
66
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
77
return {
@@ -25,6 +25,12 @@ export const aUser = (overrides?: Partial<User>): User => {
2525
};
2626
};
2727
28+
export const aPartial = (overrides?: Partial<Partial>): Partial => {
29+
return {
30+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '262c8866-bf76-4ccf-b606-2a0b4742f81f',
31+
};
32+
};
33+
2834
export const aWithAvatar = (overrides?: Partial<WithAvatar>): WithAvatar => {
2935
return {
3036
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '99f515e7-21e0-461d-b823-0d4c7f4dafc5',

tests/useTypeImports/schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default buildSchema(/* GraphQL */ `
2222
prefixedEnum: Prefixed_Enum
2323
}
2424
25+
type Partial {
26+
id: ID!
27+
}
28+
2529
interface WithAvatar {
2630
id: ID!
2731
avatar: Avatar

tests/useTypeImports/spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@ it('should support useTypeImports', async () => {
66

77
expect(result).toBeDefined();
88
expect(result).toContain(
9-
"import type { Avatar, User, WithAvatar, CamelCaseThing, PrefixedResponse, AbcType, ListType, UpdateUserInput, Mutation, Query, AbcStatus, Status, PrefixedEnum } from './types/graphql';",
9+
"import type { Avatar, User, Partial, WithAvatar, CamelCaseThing, PrefixedResponse, AbcType, ListType, UpdateUserInput, Mutation, Query, AbcStatus, Status, PrefixedEnum } from './types/graphql';",
1010
);
1111
expect(result).toMatchSnapshot();
1212
});
13+
14+
it('should support useTypeImports', async () => {
15+
const result = await plugin(testSchema, [], {
16+
typesFile: './types/graphql.ts',
17+
useTypeImports: true,
18+
newTypeNames: { Partial: 'RenamedPartial' },
19+
});
20+
21+
expect(result).toBeDefined();
22+
expect(result).toContain(
23+
"import type { Avatar, User, Partial as RenamedPartial, WithAvatar, CamelCaseThing, PrefixedResponse, AbcType, ListType, UpdateUserInput, Mutation, Query, AbcStatus, Status, PrefixedEnum } from './types/graphql';",
24+
);
25+
});

0 commit comments

Comments
 (0)