Skip to content

Commit 40b741f

Browse files
committed
faker for request data
1 parent 08c9e76 commit 40b741f

23 files changed

Lines changed: 956 additions & 13 deletions

File tree

docs/openapi-ts/plugins/faker.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The Faker plugin for Hey API generates factory functions from your OpenAPI spec
2323

2424
- Faker v10 support
2525
- seamless integration with `@hey-api/openapi-ts` ecosystem
26-
- factory functions for reusable schema definitions and operation responses
26+
- factory functions for reusable schema definitions, operation requests, and operation responses
2727
- smart property name inference for realistic data (e.g. `email` → `faker.internet.email()`)
2828
- constraint and format awareness (min/max, string formats, array bounds)
2929
- optional property and default value handling
@@ -87,6 +87,59 @@ export default {
8787

8888
You can customize the naming and casing pattern for `definitions` factories using the `.name` and `.case` options.
8989

90+
## Requests
91+
92+
A single factory function is generated per operation for request data. It combines the request body, path parameters, query parameters, and headers into one object. Only keys with actual data are included.
93+
94+
::: code-group
95+
96+
```ts [example]
97+
// body + path + query combined into one factory
98+
export const fakeUpdatePetRequest = (options?: Options): UpdatePetData => {
99+
const f = options?.faker ?? faker;
100+
return {
101+
body: {
102+
name: f.string.sample(),
103+
tag: fakeTag(options),
104+
},
105+
path: {
106+
id: f.string.uuid(),
107+
},
108+
query: {
109+
dryRun: f.datatype.boolean(),
110+
},
111+
};
112+
};
113+
114+
// body only
115+
export const fakeCreatePetRequest = (options?: Options): CreatePetData => {
116+
const f = options?.faker ?? faker;
117+
return {
118+
body: {
119+
name: f.string.sample(),
120+
},
121+
};
122+
};
123+
```
124+
125+
```js [config]
126+
export default {
127+
input: 'hey-api/backend', // sign up at app.heyapi.dev
128+
output: 'src/client',
129+
plugins: [
130+
// ...other plugins
131+
{
132+
name: '@faker-js/faker',
133+
requests: true, // [!code ++]
134+
},
135+
],
136+
};
137+
```
138+
139+
:::
140+
141+
You can customize the naming and casing pattern for `requests` factories using the `.name` and `.case` options.
142+
90143
## Responses
91144

92145
A factory function is generated for operation responses. When an operation has a single success response, the factory is unsuffixed. When multiple responses exist, each factory is suffixed with the status code.

packages/openapi-ts-tests/faker/v10/__snapshots__/2.0.x/faker-locale/@faker-js/faker.gen.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,23 +333,70 @@ export const fakeDocument = (options?: Options) => {
333333
};
334334
};
335335

336+
export const fakeListPetsRequest = (options?: Options) => {
337+
const f = options?.faker ?? faker;
338+
return { query: {
339+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { limit: f.number.int({ min: 1, max: 100 }) }
340+
} };
341+
};
342+
336343
export const fakeListPetsResponse = (options?: Options) => {
337344
const f = options?.faker ?? faker;
338345
return f.helpers.multiple(() => fakePet(options));
339346
};
340347

348+
export const fakeCreatePetRequest = (options?: Options) => {
349+
const f = options?.faker ?? faker;
350+
return { body: {
351+
name: f.string.sample(),
352+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { tag: f.string.sample() }
353+
} };
354+
};
355+
341356
export const fakeCreatePetResponse201 = (options?: Options) => fakePet(options);
342357

343358
export const fakeCreatePetResponse204 = () => undefined;
344359

360+
export const fakeDeletePetRequest = (options?: Options) => {
361+
const f = options?.faker ?? faker;
362+
return { path: {
363+
id: f.string.uuid()
364+
} };
365+
};
366+
345367
export const fakeDeletePetResponse204 = () => undefined;
346368

347369
export const fakeDeletePetResponse404 = (options?: Options) => fakeError(options);
348370

371+
export const fakeGetPetRequest = (options?: Options) => {
372+
const f = options?.faker ?? faker;
373+
return { path: {
374+
id: f.string.uuid()
375+
} };
376+
};
377+
349378
export const fakeGetPetResponse200 = (options?: Options) => fakePet(options);
350379

351380
export const fakeGetPetResponse404 = (options?: Options) => fakeError(options);
352381

382+
export const fakeUpdatePetRequest = (options?: Options) => {
383+
const f = options?.faker ?? faker;
384+
return {
385+
body: {
386+
name: f.string.sample(),
387+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { tag: fakeTag(options) }
388+
},
389+
path: {
390+
id: f.string.uuid()
391+
},
392+
query: {
393+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { dryRun: f.datatype.boolean() }
394+
}
395+
};
396+
};
397+
398+
export const fakeUpdatePetResponse = (options?: Options) => fakePet(options);
399+
353400
export const fakeHealthCheckResponse = (options?: Options) => {
354401
const f = options?.faker ?? faker;
355402
return f.string.sample();

packages/openapi-ts-tests/faker/v10/__snapshots__/2.0.x/faker-typed/@faker-js/faker.gen.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { faker, type Faker } from '@faker-js/faker';
44

5-
import type { Active, Address, BoundedFloat, BoundedInt, Company, Config, CreatePetResponses, DateOnly, DateTime, DefaultBool, DefaultInt, DefaultOverridesConstraints, DefaultString, DeletePetErrors, DeletePetResponses, Document, Email, EmailWithLength, Error, ExclusiveFloat, ExclusiveFloatNarrow, ExclusiveInt, GetPetErrors, GetPetResponses, HealthCheckResponse, IPv4Address, IPv6Address, ListPetsResponse, MaxOnlyArray, MaxOnlyInt, MaxOnlyString, MinOnlyArray, MinOnlyNumber, MinOnlyString, NullableInt, NullableString, NumericEnum, ObjectWithDefaultProp, Person, PersonList, PersonProfile, PersonWithConstraints, Pet, PetList, PetWithOwner, Price, Quantity, ShortName, Tag, TagList, Tags, Team, UniqueId, User, UserProfile, Website, ZipCode } from '../types.gen';
5+
import type { Active, Address, BoundedFloat, BoundedInt, Company, Config, CreatePetData, CreatePetResponses, DateOnly, DateTime, DefaultBool, DefaultInt, DefaultOverridesConstraints, DefaultString, DeletePetData, DeletePetErrors, DeletePetResponses, Document, Email, EmailWithLength, Error, ExclusiveFloat, ExclusiveFloatNarrow, ExclusiveInt, GetPetData, GetPetErrors, GetPetResponses, HealthCheckResponse, IPv4Address, IPv6Address, ListPetsData, ListPetsResponse, MaxOnlyArray, MaxOnlyInt, MaxOnlyString, MinOnlyArray, MinOnlyNumber, MinOnlyString, NullableInt, NullableString, NumericEnum, ObjectWithDefaultProp, Person, PersonList, PersonProfile, PersonWithConstraints, Pet, PetList, PetWithOwner, Price, Quantity, ShortName, Tag, TagList, Tags, Team, UniqueId, UpdatePetData, UpdatePetResponse, User, UserProfile, Website, ZipCode } from '../types.gen';
66

77
export type Options = {
88
faker?: Faker;
@@ -334,23 +334,70 @@ export const fakeDocument = (options?: Options): Document => {
334334
};
335335
};
336336

337+
export const fakeListPetsRequest = (options?: Options): ListPetsData => {
338+
const f = options?.faker ?? faker;
339+
return { query: {
340+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { limit: f.number.int({ min: 1, max: 100 }) }
341+
} };
342+
};
343+
337344
export const fakeListPetsResponse = (options?: Options): ListPetsResponse => {
338345
const f = options?.faker ?? faker;
339346
return f.helpers.multiple(() => fakePet(options));
340347
};
341348

349+
export const fakeCreatePetRequest = (options?: Options): CreatePetData => {
350+
const f = options?.faker ?? faker;
351+
return { body: {
352+
name: f.string.sample(),
353+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { tag: f.string.sample() }
354+
} };
355+
};
356+
342357
export const fakeCreatePetResponse201 = (options?: Options): CreatePetResponses[201] => fakePet(options);
343358

344359
export const fakeCreatePetResponse204 = (): CreatePetResponses[204] => undefined;
345360

361+
export const fakeDeletePetRequest = (options?: Options): DeletePetData => {
362+
const f = options?.faker ?? faker;
363+
return { path: {
364+
id: f.string.uuid()
365+
} };
366+
};
367+
346368
export const fakeDeletePetResponse204 = (): DeletePetResponses[204] => undefined;
347369

348370
export const fakeDeletePetResponse404 = (options?: Options): DeletePetErrors[404] => fakeError(options);
349371

372+
export const fakeGetPetRequest = (options?: Options): GetPetData => {
373+
const f = options?.faker ?? faker;
374+
return { path: {
375+
id: f.string.uuid()
376+
} };
377+
};
378+
350379
export const fakeGetPetResponse200 = (options?: Options): GetPetResponses[200] => fakePet(options);
351380

352381
export const fakeGetPetResponse404 = (options?: Options): GetPetErrors[404] => fakeError(options);
353382

383+
export const fakeUpdatePetRequest = (options?: Options): UpdatePetData => {
384+
const f = options?.faker ?? faker;
385+
return {
386+
body: {
387+
name: f.string.sample(),
388+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { tag: fakeTag(options) }
389+
},
390+
path: {
391+
id: f.string.uuid()
392+
},
393+
query: {
394+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { dryRun: f.datatype.boolean() }
395+
}
396+
};
397+
};
398+
399+
export const fakeUpdatePetResponse = (options?: Options): UpdatePetResponse => fakePet(options);
400+
354401
export const fakeHealthCheckResponse = (options?: Options): HealthCheckResponse => {
355402
const f = options?.faker ?? faker;
356403
return f.string.sample();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
export type { Active, Address, BoundedFloat, BoundedInt, ClientOptions, Company, Config, CreatePetData, CreatePetResponse, CreatePetResponses, DateOnly, DateTime, DefaultBool, DefaultInt, DefaultOverridesConstraints, DefaultString, DeletePetData, DeletePetError, DeletePetErrors, DeletePetResponses, Document, Email, EmailWithLength, Error, ExclusiveFloat, ExclusiveFloatNarrow, ExclusiveInt, GetPetData, GetPetError, GetPetErrors, GetPetResponse, GetPetResponses, HealthCheckData, HealthCheckResponse, HealthCheckResponses, IPv4Address, IPv6Address, ListPetsData, ListPetsResponse, ListPetsResponses, MaxOnlyArray, MaxOnlyInt, MaxOnlyString, MinOnlyArray, MinOnlyNumber, MinOnlyString, NullableInt, NullableString, NumericEnum, ObjectWithDefaultProp, Person, PersonList, PersonProfile, PersonWithConstraints, Pet, PetList, PetWithOwner, Price, Quantity, ShortName, Tag, TagList, Tags, Team, UniqueId, User, UserProfile, Website, ZipCode } from './types.gen';
3+
export type { Active, Address, BoundedFloat, BoundedInt, ClientOptions, Company, Config, CreatePetData, CreatePetResponse, CreatePetResponses, DateOnly, DateTime, DefaultBool, DefaultInt, DefaultOverridesConstraints, DefaultString, DeletePetData, DeletePetError, DeletePetErrors, DeletePetResponses, Document, Email, EmailWithLength, Error, ExclusiveFloat, ExclusiveFloatNarrow, ExclusiveInt, GetPetData, GetPetError, GetPetErrors, GetPetResponse, GetPetResponses, HealthCheckData, HealthCheckResponse, HealthCheckResponses, IPv4Address, IPv6Address, ListPetsData, ListPetsResponse, ListPetsResponses, MaxOnlyArray, MaxOnlyInt, MaxOnlyString, MinOnlyArray, MinOnlyNumber, MinOnlyString, NullableInt, NullableString, NumericEnum, ObjectWithDefaultProp, Person, PersonList, PersonProfile, PersonWithConstraints, Pet, PetList, PetWithOwner, Price, Quantity, ShortName, Tag, TagList, Tags, Team, UniqueId, UpdatePetData, UpdatePetResponse, UpdatePetResponses, User, UserProfile, Website, ZipCode } from './types.gen';

packages/openapi-ts-tests/faker/v10/__snapshots__/2.0.x/faker-typed/types.gen.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ export type Document = {
165165
export type ListPetsData = {
166166
body?: never;
167167
path?: never;
168-
query?: never;
168+
query?: {
169+
limit?: number;
170+
};
169171
url: '/pets';
170172
};
171173

@@ -179,7 +181,10 @@ export type ListPetsResponses = {
179181
export type ListPetsResponse = ListPetsResponses[keyof ListPetsResponses];
180182

181183
export type CreatePetData = {
182-
body?: never;
184+
body: {
185+
name: string;
186+
tag?: string;
187+
};
183188
path?: never;
184189
query?: never;
185190
url: '/pets';
@@ -250,6 +255,29 @@ export type GetPetResponses = {
250255

251256
export type GetPetResponse = GetPetResponses[keyof GetPetResponses];
252257

258+
export type UpdatePetData = {
259+
body: {
260+
name: string;
261+
tag?: Tag;
262+
};
263+
path: {
264+
id: string;
265+
};
266+
query?: {
267+
dryRun?: boolean;
268+
};
269+
url: '/pets/{id}';
270+
};
271+
272+
export type UpdatePetResponses = {
273+
/**
274+
* Updated pet
275+
*/
276+
200: Pet;
277+
};
278+
279+
export type UpdatePetResponse = UpdatePetResponses[keyof UpdatePetResponses];
280+
253281
export type HealthCheckData = {
254282
body?: never;
255283
path?: never;

packages/openapi-ts-tests/faker/v10/__snapshots__/2.0.x/faker/@faker-js/faker.gen.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,23 +332,70 @@ export const fakeDocument = (options?: Options) => {
332332
};
333333
};
334334

335+
export const fakeListPetsRequest = (options?: Options) => {
336+
const f = options?.faker ?? faker;
337+
return { query: {
338+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { limit: f.number.int({ min: 1, max: 100 }) }
339+
} };
340+
};
341+
335342
export const fakeListPetsResponse = (options?: Options) => {
336343
const f = options?.faker ?? faker;
337344
return f.helpers.multiple(() => fakePet(options));
338345
};
339346

347+
export const fakeCreatePetRequest = (options?: Options) => {
348+
const f = options?.faker ?? faker;
349+
return { body: {
350+
name: f.string.sample(),
351+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { tag: f.string.sample() }
352+
} };
353+
};
354+
340355
export const fakeCreatePetResponse201 = (options?: Options) => fakePet(options);
341356

342357
export const fakeCreatePetResponse204 = () => undefined;
343358

359+
export const fakeDeletePetRequest = (options?: Options) => {
360+
const f = options?.faker ?? faker;
361+
return { path: {
362+
id: f.string.uuid()
363+
} };
364+
};
365+
344366
export const fakeDeletePetResponse204 = () => undefined;
345367

346368
export const fakeDeletePetResponse404 = (options?: Options) => fakeError(options);
347369

370+
export const fakeGetPetRequest = (options?: Options) => {
371+
const f = options?.faker ?? faker;
372+
return { path: {
373+
id: f.string.uuid()
374+
} };
375+
};
376+
348377
export const fakeGetPetResponse200 = (options?: Options) => fakePet(options);
349378

350379
export const fakeGetPetResponse404 = (options?: Options) => fakeError(options);
351380

381+
export const fakeUpdatePetRequest = (options?: Options) => {
382+
const f = options?.faker ?? faker;
383+
return {
384+
body: {
385+
name: f.string.sample(),
386+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { tag: fakeTag(options) }
387+
},
388+
path: {
389+
id: f.string.uuid()
390+
},
391+
query: {
392+
...!resolveCondition(options?.includeOptional ?? true, f) ? {} : { dryRun: f.datatype.boolean() }
393+
}
394+
};
395+
};
396+
397+
export const fakeUpdatePetResponse = (options?: Options) => fakePet(options);
398+
352399
export const fakeHealthCheckResponse = (options?: Options) => {
353400
const f = options?.faker ?? faker;
354401
return f.string.sample();

0 commit comments

Comments
 (0)