Skip to content

Commit 591f5b7

Browse files
authored
feat: add enumsPrefix option (#56)
Similar to `typesPrefix`, the option `enumsPrefix` is used to add a prefix to all enum type usages Also add union type in unit tests to verify the behaviour Fixes #51
1 parent bfc7fdf commit 591f5b7

File tree

4 files changed

+397
-9
lines changed

4 files changed

+397
-9
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ Setting the `typesPrefix` to `Api.` will create the following mock data
105105
export const aUser = (overrides?: Partial<Api.User>): Api.User => {
106106
```
107107
108+
### enumsPrefix (`string`, defaultValue: '')
109+
110+
Similar to `typesPrefix`, but for enum types
111+
112+
```
113+
declare namespace Api {
114+
enum Status {
115+
...
116+
}
117+
}
118+
```
119+
120+
Setting the `enumsPrefix` to `Api.` will create the following mock data
121+
122+
```
123+
export const aUser = (overrides?: Partial<User>): User => {
124+
status: overrides && overrides.hasOwnProperty('status') ? overrides.status! : Api.Status.Online,
125+
}
126+
```
127+
108128
## Example of usage
109129
110130
**codegen.yml**

src/index.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Options<T = TypeNode> = {
1717
terminateCircularRelationships: boolean;
1818
prefix: string | undefined;
1919
typesPrefix: string;
20+
enumsPrefix: string;
2021
currentType: T;
2122
customScalars?: ScalarMap;
2223
};
@@ -108,7 +109,7 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
108109
// It's an enum
109110
const typenameConverter = createNameConverter(opts.typenamesConvention);
110111
const value = foundType.values ? foundType.values[0] : '';
111-
return `${typenameConverter(foundType.name)}.${updateTextCase(
112+
return `${typenameConverter(foundType.name, opts.enumsPrefix)}.${updateTextCase(
112113
value,
113114
opts.enumValuesConvention,
114115
)}`;
@@ -242,23 +243,34 @@ const getImportTypes = ({
242243
types,
243244
typesFile,
244245
typesPrefix,
246+
enumsPrefix,
245247
}: {
246248
typenamesConvention: NamingConvention;
247249
definitions: any;
248250
types: TypeItem[];
249251
typesFile: string;
250252
typesPrefix: string;
253+
enumsPrefix: string;
251254
}) => {
252255
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+
}
257270

258-
typeImports.push(...enumTypes.map(({ name }) => typenameConverter(name)));
259271
return typesFile
260272
? `/* 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`
262274
: '';
263275
};
264276

@@ -281,6 +293,7 @@ export interface TypescriptMocksPluginConfig {
281293
scalars?: ScalarMap;
282294
terminateCircularRelationships?: boolean;
283295
typesPrefix?: string;
296+
enumsPrefix?: string;
284297
}
285298

286299
interface TypeItem {
@@ -339,6 +352,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
339352
terminateCircularRelationships: !!config.terminateCircularRelationships,
340353
prefix: config.prefix,
341354
typesPrefix: config.typesPrefix,
355+
enumsPrefix: config.enumsPrefix,
342356
currentType: node.type,
343357
customScalars: config.scalars,
344358
});
@@ -365,6 +379,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
365379
terminateCircularRelationships: !!config.terminateCircularRelationships,
366380
prefix: config.prefix,
367381
typesPrefix: config.typesPrefix,
382+
enumsPrefix: config.enumsPrefix,
368383
currentType: field.type,
369384
customScalars: config.scalars,
370385
});
@@ -453,6 +468,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
453468
types,
454469
typesFile,
455470
typesPrefix: config.typesPrefix,
471+
enumsPrefix: config.enumsPrefix,
456472
});
457473
// List of function that will generate the mock.
458474
// We generate it after having visited because we need to distinct types from enums

0 commit comments

Comments
 (0)