Skip to content

Commit dba9178

Browse files
committed
feat(faker): milestone 1 implemented
1 parent 1a03e97 commit dba9178

26 files changed

Lines changed: 818 additions & 3 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { faker, type Faker } from '@faker-js/faker';
4+
5+
import type { Bar, Foo } from '../types.gen';
6+
7+
export const fakeFoo = (options?: {
8+
faker?: Faker;
9+
}): Foo => ({
10+
name: (options?.faker ?? faker).string.sample(),
11+
age: (options?.faker ?? faker).number.int(),
12+
active: (options?.faker ?? faker).datatype.boolean()
13+
});
14+
15+
export const fakeBar = (options?: {
16+
faker?: Faker;
17+
}): Bar => (options?.faker ?? faker).helpers.arrayElement(['baz', 'qux']);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type { Bar, ClientOptions, Foo } from './types.gen';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type ClientOptions = {
4+
baseUrl: `${string}://${string}` | (string & {});
5+
};
6+
7+
export type Foo = {
8+
name: string;
9+
age: number;
10+
active?: boolean;
11+
};
12+
13+
export type Bar = 'baz' | 'qux';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { faker, type Faker } from '@faker-js/faker';
4+
5+
export const fakeFoo = (options?: {
6+
faker?: Faker;
7+
}) => ({
8+
name: (options?.faker ?? faker).string.sample(),
9+
age: (options?.faker ?? faker).number.int(),
10+
active: (options?.faker ?? faker).datatype.boolean()
11+
});
12+
13+
export const fakeBar = (options?: {
14+
faker?: Faker;
15+
}) => (options?.faker ?? faker).helpers.arrayElement(['baz', 'qux']);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { faker, type Faker } from '@faker-js/faker';
4+
5+
import type { Active, Anything, Nothing, NumericEnum, Pet, PetList, PetOrTag, Price, Quantity, StatusWithNull, StringOrNumber, Tag, Tags } from '../types.gen';
6+
7+
export const fakePrice = (options?: {
8+
faker?: Faker;
9+
}): Price => (options?.faker ?? faker).number.float();
10+
11+
export const fakeQuantity = (options?: {
12+
faker?: Faker;
13+
}): Quantity => (options?.faker ?? faker).number.int();
14+
15+
export const fakeActive = (options?: {
16+
faker?: Faker;
17+
}): Active => (options?.faker ?? faker).datatype.boolean();
18+
19+
export const fakeNothing = (): Nothing => null;
20+
21+
export const fakeAnything = (): Anything => undefined;
22+
23+
export const fakeStatusWithNull = (options?: {
24+
faker?: Faker;
25+
}): StatusWithNull => (options?.faker ?? faker).helpers.arrayElement([
26+
'active',
27+
'inactive',
28+
null
29+
]);
30+
31+
export const fakeNumericEnum = (options?: {
32+
faker?: Faker;
33+
}): NumericEnum => (options?.faker ?? faker).helpers.arrayElement([
34+
1,
35+
2,
36+
3
37+
]);
38+
39+
export const fakeTags = (options?: {
40+
faker?: Faker;
41+
}): Tags => (options?.faker ?? faker).helpers.multiple(() => (options?.faker ?? faker).string.sample());
42+
43+
export const fakeTag = (options?: {
44+
faker?: Faker;
45+
}): Tag => ({
46+
id: (options?.faker ?? faker).number.int(),
47+
label: (options?.faker ?? faker).string.sample()
48+
});
49+
50+
export const fakePet = (options?: {
51+
faker?: Faker;
52+
}): Pet => ({
53+
name: (options?.faker ?? faker).string.sample(),
54+
tag: fakeTag(options)
55+
});
56+
57+
export const fakePetOrTag = (options?: {
58+
faker?: Faker;
59+
}): PetOrTag => fakePet(options);
60+
61+
export const fakeStringOrNumber = (options?: {
62+
faker?: Faker;
63+
}): StringOrNumber => (options?.faker ?? faker).string.sample();
64+
65+
export const fakePetList = (options?: {
66+
faker?: Faker;
67+
}): PetList => (options?.faker ?? faker).helpers.multiple(() => fakePet(options));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type { Active, Anything, ClientOptions, Nothing, NumericEnum, Pet, PetList, PetOrTag, Price, Quantity, StatusWithNull, StringOrNumber, Tag, Tags } from './types.gen';
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type ClientOptions = {
4+
baseUrl: `${string}://${string}` | (string & {});
5+
};
6+
7+
export type Price = number;
8+
9+
export type Quantity = number;
10+
11+
export type Active = boolean;
12+
13+
export type Nothing = null;
14+
15+
export type Anything = unknown;
16+
17+
export type StatusWithNull = 'active' | 'inactive' | null;
18+
19+
export type NumericEnum = 1 | 2 | 3;
20+
21+
export type Tags = Array<string>;
22+
23+
export type Pet = {
24+
name: string;
25+
tag?: Tag;
26+
};
27+
28+
export type Tag = {
29+
id: number;
30+
label: string;
31+
};
32+
33+
export type PetOrTag = Pet | Tag;
34+
35+
export type StringOrNumber = string | number;
36+
37+
export type PetList = Array<Pet>;

packages/openapi-ts-tests/faker/v1/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"typecheck": "tsgo --noEmit"
88
},
99
"devDependencies": {
10+
"@faker-js/faker": "^9.9.0",
1011
"@hey-api/openapi-ts": "workspace:*",
1112
"typescript": "5.9.3"
1213
},

packages/openapi-ts-tests/faker/v1/test/3.1.x.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ describe(`OpenAPI ${version}`, () => {
2222
}),
2323
description: 'generates faker factories for basic schemas',
2424
},
25+
{
26+
config: createConfig({
27+
input: 'faker-basic.yaml',
28+
output: 'faker-basic-typed',
29+
plugins: ['@hey-api/typescript', '@faker-js/faker'],
30+
}),
31+
description: 'generates typed faker factories when typescript plugin is active',
32+
},
33+
{
34+
config: createConfig({
35+
input: 'faker-m1.yaml',
36+
output: 'faker-m1',
37+
plugins: ['@hey-api/typescript', '@faker-js/faker'],
38+
}),
39+
description: 'handles number, null, enum-with-null, array, $ref, and union schemas',
40+
},
2541
];
2642

2743
it.each(scenarios)('$description', async ({ config }) => {

packages/openapi-ts/src/plugins/@faker-js/faker/config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { definePluginConfig, mappers } from '@hey-api/shared';
22

33
import { Api } from './api';
4-
// import { handler } from './plugin';
4+
import { handler } from './plugin';
55
import type { FakerJsFakerPlugin } from './types';
66

77
export const defaultConfig: FakerJsFakerPlugin['Config'] = {
@@ -10,8 +10,7 @@ export const defaultConfig: FakerJsFakerPlugin['Config'] = {
1010
case: 'camelCase',
1111
includeInEntry: false,
1212
},
13-
// handler,
14-
handler: () => {},
13+
handler,
1514
name: '@faker-js/faker',
1615
resolveConfig: (plugin, context) => {
1716
plugin.config.definitions = context.valueToObject({

0 commit comments

Comments
 (0)