Skip to content

Commit 5c9b4cb

Browse files
Add config for prefixing types (#29)
Related with: #28 Allows adding a prefix to the generated types, useful when your types are globally exported (i.e when using the generator add plugin) so you don't need to import them into your files and can call them directly.
1 parent b6e14a7 commit 5c9b4cb

File tree

5 files changed

+120
-28
lines changed

5 files changed

+120
-28
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ Allows you to define mappings for your custom scalars. Allows you to map any Gra
3737
[casual](https://github.com/boo1ean/casual#embedded-generators) embedded generator (string or
3838
function key)
3939

40+
### typesPrefix (`string`, defaultValue: '')
41+
42+
Useful if you have globally exported types under a certain namespace.
43+
e.g If the types file is something like this
44+
45+
```
46+
declare namespace Api {
47+
type User {
48+
...
49+
}
50+
}
51+
```
52+
53+
Setting the typesPrefix to `Api.` will create the following mock data
54+
55+
```
56+
export const aUser = (overrides?: Partial<Api.User>): Api.User => {
57+
```
58+
4059
## Example of usage
4160

4261
**codegen.yml**

src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,16 @@ const getMockString = (
192192
typenamesConvention: NamingConvention,
193193
addTypename = false,
194194
prefix,
195+
typesPrefix = '',
195196
) => {
196197
const casedName = createNameConverter(typenamesConvention)(typeName);
197198
const typename = addTypename ? `\n __typename: '${casedName}',` : '';
198199
return `
199-
export const ${toMockName(typeName, casedName, prefix)} = (overrides?: Partial<${casedName}>): ${casedName} => {
200+
export const ${toMockName(
201+
typeName,
202+
casedName,
203+
prefix,
204+
)} = (overrides?: Partial<${typesPrefix}${casedName}>): ${typesPrefix}${casedName} => {
200205
return {${typename}
201206
${fields}
202207
};
@@ -212,6 +217,7 @@ export interface TypescriptMocksPluginConfig {
212217
addTypename?: boolean;
213218
prefix?: string;
214219
scalars?: ScalarMap;
220+
typesPrefix?: string;
215221
}
216222

217223
interface TypeItem {
@@ -301,7 +307,14 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
301307
.join('\n')
302308
: '';
303309

304-
return getMockString(fieldName, mockFields, typenamesConvention, false, config.prefix);
310+
return getMockString(
311+
fieldName,
312+
mockFields,
313+
typenamesConvention,
314+
false,
315+
config.prefix,
316+
config.typesPrefix,
317+
);
305318
},
306319
};
307320
},
@@ -325,6 +338,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
325338
typenamesConvention,
326339
!!config.addTypename,
327340
config.prefix,
341+
config.typesPrefix,
328342
);
329343
},
330344
};

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,43 @@ export const mockUser = (overrides?: Partial<User>): User => {
3737
"
3838
`;
3939
40+
exports[`should add typesPrefix to all types when option is specified 1`] = `
41+
"
42+
export const anAbcType = (overrides?: Partial<Api.AbcType>): Api.AbcType => {
43+
return {
44+
abc: overrides && overrides.hasOwnProperty('abc') ? overrides.abc! : 'sit',
45+
};
46+
};
47+
48+
export const anAvatar = (overrides?: Partial<Api.Avatar>): Api.Avatar => {
49+
return {
50+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '0550ff93-dd31-49b4-8c38-ff1cb68bdc38',
51+
url: overrides && overrides.hasOwnProperty('url') ? overrides.url! : 'aliquid',
52+
};
53+
};
54+
55+
export const anUpdateUserInput = (overrides?: Partial<Api.UpdateUserInput>): Api.UpdateUserInput => {
56+
return {
57+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
58+
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
59+
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(),
60+
};
61+
};
62+
63+
export const aUser = (overrides?: Partial<Api.User>): Api.User => {
64+
return {
65+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 'a5756f00-41a6-422a-8a7d-d13ee6a63750',
66+
creationDate: overrides && overrides.hasOwnProperty('creationDate') ? overrides.creationDate! : '1970-01-09T16:33:21.532Z',
67+
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'libero',
68+
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(),
69+
status: overrides && overrides.hasOwnProperty('status') ? overrides.status! : Status.Online,
70+
customStatus: overrides && overrides.hasOwnProperty('customStatus') ? overrides.customStatus! : AbcStatus.HasXyzStatus,
71+
scalarValue: overrides && overrides.hasOwnProperty('scalarValue') ? overrides.scalarValue! : 'neque',
72+
};
73+
};
74+
"
75+
`;
76+
4077
exports[`should correctly generate the \`casual\` data for a non-string scalar mapping 1`] = `
4178
"
4279
export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {

tests/typescript-mock-data.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,12 @@ it('should correctly generate the `casual` data for a non-string scalar mapping'
193193
expect(result).toContain(JSON.stringify([41, 98, 185]));
194194
expect(result).toMatchSnapshot();
195195
});
196+
197+
it('should add typesPrefix to all types when option is specified', async () => {
198+
const result = await plugin(testSchema, [], { typesPrefix: 'Api.' });
199+
200+
expect(result).toBeDefined();
201+
expect(result).toMatch(/: Api.User/);
202+
expect(result).not.toMatch(/: User/);
203+
expect(result).toMatchSnapshot();
204+
});

yarn.lock

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
resolved "https://registry.yarnpkg.com/@auto-it/bot-list/-/bot-list-9.25.0.tgz#1147225d4763282a98abf4cbbc0e324eee4b038d"
88
integrity sha512-XUgJir4roCPdUUX/5wo26dyj1eFZbYKIjP1SuVJDskZ4x3HOwhggBK5WBKkJFv5rGrWnAtVfZpOju4Qzez7GgQ==
99

10-
"@auto-it/bot-list@^9.46.0":
11-
version "9.46.0"
12-
resolved "https://registry.yarnpkg.com/@auto-it/bot-list/-/bot-list-9.46.0.tgz#eef1fe0ae08673816efd3470a284c9ce7c4249c5"
13-
integrity sha512-8qoTX5T2KCnpMzPSz9lkASmWhVI0V49UcqY8TW9mhQCnwApDi9Uy4OZ9gSApf8I/+FR3aNwmRPiZjkvLXy7Avw==
10+
"@auto-it/bot-list@^9.49.4":
11+
version "9.49.4"
12+
resolved "https://registry.yarnpkg.com/@auto-it/bot-list/-/bot-list-9.49.4.tgz#bc936cfb7bfac4361a3f57f3be71b0dca8d0239f"
13+
integrity sha512-/XqOWNtHZFRou/qrpT56y9uVzgr4YiXGaFExNRbldS0ID0sk0bbDS3QxR09jYu71UGmPrJfpyEtlWf5jP5xyYA==
1414

1515
"@auto-it/conventional-commits@^9.25.0":
1616
version "9.25.0"
@@ -61,12 +61,12 @@
6161
typescript-memoize "^1.0.0-alpha.3"
6262
url-join "^4.0.0"
6363

64-
"@auto-it/core@^9.46.0":
65-
version "9.46.0"
66-
resolved "https://registry.yarnpkg.com/@auto-it/core/-/core-9.46.0.tgz#2ef366011e17e04fe424d49e0bafe6a9da3261a0"
67-
integrity sha512-bEhFLHjilCXOlQ9zmrqZ2h+ZBgr2GKbBMViLRb2TQhDeWXOjAHJPcGKk1w9rPRDzfOkoXPKBahKQdljqzGErXA==
64+
"@auto-it/core@^9.49.4":
65+
version "9.49.4"
66+
resolved "https://registry.yarnpkg.com/@auto-it/core/-/core-9.49.4.tgz#2244c28f259a6a1ea2ba35214bd3f068647d17f8"
67+
integrity sha512-fsKL3a0p8meWZlzLao2g6GWA1RWRlkofhsS1ffAgixEgD0jNeFCgm9DFDkOzpklbParCz35D/IzldYbgHjI5Bg==
6868
dependencies:
69-
"@auto-it/bot-list" "^9.46.0"
69+
"@auto-it/bot-list" "^9.49.4"
7070
"@octokit/graphql" "^4.4.0"
7171
"@octokit/plugin-enterprise-compatibility" "^1.2.2"
7272
"@octokit/plugin-retry" "^3.0.1"
@@ -93,6 +93,7 @@
9393
node-fetch "2.6.0"
9494
parse-author "^2.0.0"
9595
parse-github-url "1.0.2"
96+
pretty-ms "^7.0.0"
9697
semver "^7.0.0"
9798
signale "^1.4.0"
9899
tapable "^2.0.0-beta.2"
@@ -102,12 +103,12 @@
102103
typescript-memoize "^1.0.0-alpha.3"
103104
url-join "^4.0.0"
104105

105-
"@auto-it/npm@^9.46.0":
106-
version "9.46.0"
107-
resolved "https://registry.yarnpkg.com/@auto-it/npm/-/npm-9.46.0.tgz#aa2b191e12e4caae363563343b29e2d1363fb977"
108-
integrity sha512-8YvVDOlRLr+rff9YNb/6aJ6wGjd152mnB1icUSG/FZyf5LsdKIAGm1R2pK6M5MDDi6xD8XVT/dL5P4tLwbQJUw==
106+
"@auto-it/npm@^9.49.4":
107+
version "9.49.4"
108+
resolved "https://registry.yarnpkg.com/@auto-it/npm/-/npm-9.49.4.tgz#55e9498bfb660653e084b74c11998aec8c071de9"
109+
integrity sha512-25xGeRDoSMATYJv3EJxLCHpdYCVN8BMnAha20WNSultlt5yCU1ZBHp6zK+UUX+eB0ch8Q7UNh9J0pG08fm1X1g==
109110
dependencies:
110-
"@auto-it/core" "^9.46.0"
111+
"@auto-it/core" "^9.49.4"
111112
await-to-js "^2.1.1"
112113
env-ci "^5.0.1"
113114
fp-ts "^2.5.3"
@@ -122,12 +123,12 @@
122123
url-join "^4.0.0"
123124
user-home "^2.0.0"
124125

125-
"@auto-it/released@^9.46.0":
126-
version "9.46.0"
127-
resolved "https://registry.yarnpkg.com/@auto-it/released/-/released-9.46.0.tgz#2088a797b280528d2289133f1c5bbdc032f69b14"
128-
integrity sha512-sQp+G/TR92xNWo4ILfD6CRVOA4uF1MjbqPTdTTFTlkDUlNZgG6fi3CbfxT0ugp5nrg+lAKLwAhoYxex8wx3daA==
126+
"@auto-it/released@^9.49.4":
127+
version "9.49.4"
128+
resolved "https://registry.yarnpkg.com/@auto-it/released/-/released-9.49.4.tgz#267cb6e4b449cc9107a21102f20ee4ceb1c59165"
129+
integrity sha512-tQ8N8MCreJC033FZwnJDE4SIKJrv8/toTVKbrkfjqNnq7xuCsYPRpP6R+HzqEhWpmN48A0alycMfwD9OHVSV3Q==
129130
dependencies:
130-
"@auto-it/core" "^9.46.0"
131+
"@auto-it/core" "^9.49.4"
131132
deepmerge "^4.0.0"
132133
fp-ts "^2.5.3"
133134
io-ts "^2.1.2"
@@ -1443,14 +1444,14 @@ author-regex@^1.0.0:
14431444
resolved "https://registry.yarnpkg.com/author-regex/-/author-regex-1.0.0.tgz#d08885be6b9bbf9439fe087c76287245f0a81450"
14441445
integrity sha1-0IiFvmubv5Q5/gh8dihyRfCoFFA=
14451446

1446-
auto@^9.46.0:
1447-
version "9.46.0"
1448-
resolved "https://registry.yarnpkg.com/auto/-/auto-9.46.0.tgz#6018864d74b9e6dcb5c37e6a31cae0d1d7d909b2"
1449-
integrity sha512-LnTbvRBZA0IxqBYVShtxgxAFWq9oVCq+h2PLYjZtpOXBJn+5LNMxNjEOv918efacRWgVE/KAaAauuIhWQagsqw==
1447+
auto@^9.47.0:
1448+
version "9.49.4"
1449+
resolved "https://registry.yarnpkg.com/auto/-/auto-9.49.4.tgz#00c420289593769cca2518878bdf304ff97bd9b0"
1450+
integrity sha512-mnoYllKK28cQNhUFn0nlHpa0DYarOxBqmhtocI1hkIojIHy32M+7mINV5ELKKhbJ9ZEbXQPLgOW8inUAGiQtXg==
14501451
dependencies:
1451-
"@auto-it/core" "^9.46.0"
1452-
"@auto-it/npm" "^9.46.0"
1453-
"@auto-it/released" "^9.46.0"
1452+
"@auto-it/core" "^9.49.4"
1453+
"@auto-it/npm" "^9.49.4"
1454+
"@auto-it/released" "^9.49.4"
14541455
await-to-js "^2.1.1"
14551456
chalk "^4.0.0"
14561457
command-line-application "^0.10.1"
@@ -4804,6 +4805,11 @@ parse-json@^5.0.0:
48044805
json-parse-better-errors "^1.0.1"
48054806
lines-and-columns "^1.1.6"
48064807

4808+
parse-ms@^2.1.0:
4809+
version "2.1.0"
4810+
resolved "https://registry.yarnpkg.com/parse-ms/-/parse-ms-2.1.0.tgz#348565a753d4391fa524029956b172cb7753097d"
4811+
integrity sha512-kHt7kzLoS9VBZfUsiKjv43mr91ea+U05EyKkEtqp7vNbHxmaVuEqN7XxeEVnGrMtYOAxGrDElSi96K7EgO1zCA==
4812+
48074813
48084814
version "5.1.0"
48094815
resolved "https://registry.yarnpkg.com/parse5/-/parse5-5.1.0.tgz#c59341c9723f414c452975564c7c00a68d58acd2"
@@ -4979,6 +4985,13 @@ pretty-format@^25.1.0, pretty-format@^25.2.6:
49794985
ansi-styles "^4.0.0"
49804986
react-is "^16.12.0"
49814987

4988+
pretty-ms@^7.0.0:
4989+
version "7.0.0"
4990+
resolved "https://registry.yarnpkg.com/pretty-ms/-/pretty-ms-7.0.0.tgz#45781273110caf35f55cab21a8a9bd403a233dc0"
4991+
integrity sha512-J3aPWiC5e9ZeZFuSeBraGxSkGMOvulSWsxDByOcbD1Pr75YL3LSNIKIb52WXbCLE1sS5s4inBBbryjF4Y05Ceg==
4992+
dependencies:
4993+
parse-ms "^2.1.0"
4994+
49824995
progress@^2.0.0:
49834996
version "2.0.3"
49844997
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"

0 commit comments

Comments
 (0)