Skip to content

Commit

Permalink
feat: faker seed
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Dec 21, 2023
1 parent 5e90513 commit ffd0439
Show file tree
Hide file tree
Showing 45 changed files with 1,001 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-donuts-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-faker": minor
---

Faker with `seed` option to specify the overall seed that Faker needs to use. This can be a number or an array of numbers. See https://fakerjs.dev/api/faker.html#seed
46 changes: 42 additions & 4 deletions docs/plugins/swagger-faker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default defineConfig({

#### output.exportType

Define what needs to be exported, here you can also disable the export of barrel files
Define what needs to be exported, you can also disable the export of barrel files

::: info
Type: `'barrel' | false` <br/>
Expand Down Expand Up @@ -232,7 +232,7 @@ export default defineConfig({

### include

Array containing include parameters to include tags/operations/methods/paths.
An array containing include parameters to include tags/operations/methods/paths.

::: info type

Expand Down Expand Up @@ -285,7 +285,7 @@ export default defineConfig({

### exclude

Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
An array containing exclude parameters to exclude/skip tags/operations/methods/paths.

::: info type

Expand Down Expand Up @@ -338,7 +338,7 @@ export default defineConfig({

### override

Array containing override parameters to override `options` based on tags/operations/methods/paths.
An array containing override parameters to override `options` based on tags/operations/methods/paths.

::: info type

Expand Down Expand Up @@ -517,6 +517,44 @@ export default defineConfig({

:::

### seed

The use of Seed is intended to allow for consistent values in a test.

::: info
Type: `'number' | 'number[]'` <br/>

:::

::: code-group

```typescript
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerFaker from '@kubb/swagger-faker'
import createSwaggerTS from '@kubb/swagger-ts'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
seed: [222],
},
),
],
})
```

:::

## Depended

- [`@kubb/swagger`](/plugins/swagger/)
Expand Down
1 change: 1 addition & 0 deletions examples/msw-v2/kubb.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default defineConfig(async () => {
type: 'tag',
output: './mocks/{{tag}}Mocks',
},
seed: [220],
}],
['@kubb/swagger-msw', {
output: {
Expand Down
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createAddPetRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { faker } from '@faker-js/faker'
import type { AddPetRequest } from '../models/AddPetRequest'

export function createAddPetRequest(): NonNullable<AddPetRequest> {
faker.seed([220])
return {
'id': faker.number.float({}),
'name': faker.string.alpha(),
Expand Down
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { faker } from '@faker-js/faker'
import type { Address } from '../models/Address'

export function createAddress(): NonNullable<Address> {
faker.seed([220])
return { 'street': faker.string.alpha(), 'city': faker.string.alpha(), 'state': faker.string.alpha(), 'zip': faker.string.alpha() }
}
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createApiResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { faker } from '@faker-js/faker'
import type { ApiResponse } from '../models/ApiResponse'

export function createApiResponse(): NonNullable<ApiResponse> {
faker.seed([220])
return { 'code': faker.number.float({}), 'type': faker.string.alpha(), 'message': faker.string.alpha() }
}
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { faker } from '@faker-js/faker'
import type { Category } from '../models/Category'

export function createCategory(): NonNullable<Category> {
faker.seed([220])
return { 'id': faker.number.float({}), 'name': faker.string.alpha() }
}
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createCustomer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { faker } from '@faker-js/faker'
import type { Customer } from '../models/Customer'

export function createCustomer(): NonNullable<Customer> {
faker.seed([220])
return { 'id': faker.number.float({}), 'username': faker.string.alpha(), 'address': faker.helpers.arrayElements([createAddress()]) as any }
}
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { faker } from '@faker-js/faker'
import type { Order } from '../models/Order'

export function createOrder(): NonNullable<Order> {
faker.seed([220])
return {
'id': faker.number.float({}),
'petId': faker.number.float({}),
Expand Down
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createPet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { faker } from '@faker-js/faker'
import type { Pet } from '../models/Pet'

export function createPet(): NonNullable<Pet> {
faker.seed([220])
return {
'id': faker.number.float({}),
'name': faker.string.alpha(),
Expand Down
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createPetNotFound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { faker } from '@faker-js/faker'
import type { PetNotFound } from '../models/PetNotFound'

export function createPetNotFound(): NonNullable<PetNotFound> {
faker.seed([220])
return { 'code': faker.number.float({}), 'message': faker.string.alpha() }
}
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { faker } from '@faker-js/faker'
import type { Tag } from '../models/Tag'

export function createTag(): NonNullable<Tag> {
faker.seed([220])
return { 'id': faker.number.float({}), 'name': faker.string.alpha() }
}
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { faker } from '@faker-js/faker'
import type { User } from '../models/User'

export function createUser(): NonNullable<User> {
faker.seed([220])
return {
'id': faker.number.float({}),
'username': faker.string.alpha(),
Expand Down
1 change: 1 addition & 0 deletions examples/msw-v2/src/gen/mocks/createUserArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { faker } from '@faker-js/faker'
import type { UserArray } from '../models/UserArray'

export function createUserArray(): NonNullable<UserArray> {
faker.seed([220])
return faker.helpers.arrayElements([createUser()]) as any
}
3 changes: 3 additions & 0 deletions examples/msw-v2/src/gen/mocks/petMocks/createAddPet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import { createPet } from '../createPet'
import type { AddPet405, AddPetMutationRequest, AddPetMutationResponse } from '../../models/AddPet'

export function createAddPet405(): NonNullable<AddPet405> {
faker.seed([220])
return { 'code': faker.number.float({}), 'message': faker.string.alpha() }
}
/**
* @description Create a new pet in the store
*/

export function createAddPetMutationRequest(): NonNullable<AddPetMutationRequest> {
faker.seed([220])
return createAddPetRequest()
}
/**
* @description Successful operation
*/

export function createAddPetMutationResponse(): NonNullable<AddPetMutationResponse> {
faker.seed([220])
return createPet()
}
4 changes: 4 additions & 0 deletions examples/msw-v2/src/gen/mocks/petMocks/createDeletePet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import type { DeletePet400, DeletePetHeaderParams, DeletePetMutationResponse, De
*/

export function createDeletePet400(): NonNullable<DeletePet400> {
faker.seed([220])
return undefined
}

export function createDeletePetHeaderParams(): NonNullable<DeletePetHeaderParams> {
faker.seed([220])
return { 'api_key': faker.string.alpha() }
}

export function createDeletePetMutationResponse(): NonNullable<DeletePetMutationResponse> {
faker.seed([220])
return undefined
}

export function createDeletePetPathParams(): NonNullable<DeletePetPathParams> {
faker.seed([220])
return { 'petId': faker.number.float({}) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import type { FindPetsByStatus400, FindPetsByStatusQueryParams, FindPetsByStatus
*/

export function createFindPetsByStatus400(): NonNullable<FindPetsByStatus400> {
faker.seed([220])
return undefined
}

export function createFindPetsByStatusQueryParams(): NonNullable<FindPetsByStatusQueryParams> {
faker.seed([220])
return { 'status': faker.helpers.arrayElement<any>([`available`, `pending`, `sold`]) }
}
/**
* @description successful operation
*/

export function createFindPetsByStatusQueryResponse(): NonNullable<FindPetsByStatusQueryResponse> {
faker.seed([220])
return faker.helpers.arrayElements([createPet()]) as any
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import type { FindPetsByTags400, FindPetsByTagsQueryParams, FindPetsByTagsQueryR
*/

export function createFindPetsByTags400(): NonNullable<FindPetsByTags400> {
faker.seed([220])
return undefined
}

export function createFindPetsByTagsQueryParams(): NonNullable<FindPetsByTagsQueryParams> {
faker.seed([220])
return { 'tags': faker.helpers.arrayElements([faker.string.alpha()]) as any, 'page': faker.string.alpha(), 'pageSize': faker.string.alpha() }
}
/**
* @description successful operation
*/

export function createFindPetsByTagsQueryResponse(): NonNullable<FindPetsByTagsQueryResponse> {
faker.seed([220])
return faker.helpers.arrayElements([createPet()]) as any
}
4 changes: 4 additions & 0 deletions examples/msw-v2/src/gen/mocks/petMocks/createGetPetById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ import type { GetPetById400, GetPetById404, GetPetByIdPathParams, GetPetByIdQuer
*/

export function createGetPetById400(): NonNullable<GetPetById400> {
faker.seed([220])
return undefined
}
/**
* @description Pet not found
*/

export function createGetPetById404(): NonNullable<GetPetById404> {
faker.seed([220])
return undefined
}

export function createGetPetByIdPathParams(): NonNullable<GetPetByIdPathParams> {
faker.seed([220])
return { 'petId': faker.number.float({}) }
}
/**
* @description successful operation
*/

export function createGetPetByIdQueryResponse(): NonNullable<GetPetByIdQueryResponse> {
faker.seed([220])
return createPet()
}
6 changes: 6 additions & 0 deletions examples/msw-v2/src/gen/mocks/petMocks/createUpdatePet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { faker } from '@faker-js/faker'
import { createPet } from '../createPet'
import type { UpdatePet400, UpdatePet404, UpdatePet405, UpdatePetMutationRequest, UpdatePetMutationResponse } from '../../models/UpdatePet'

Expand All @@ -6,33 +7,38 @@ import type { UpdatePet400, UpdatePet404, UpdatePet405, UpdatePetMutationRequest
*/

export function createUpdatePet400(): NonNullable<UpdatePet400> {
faker.seed([220])
return undefined
}
/**
* @description Pet not found
*/

export function createUpdatePet404(): NonNullable<UpdatePet404> {
faker.seed([220])
return undefined
}
/**
* @description Validation exception
*/

export function createUpdatePet405(): NonNullable<UpdatePet405> {
faker.seed([220])
return undefined
}
/**
* @description Update an existent pet in the store
*/

export function createUpdatePetMutationRequest(): NonNullable<UpdatePetMutationRequest> {
faker.seed([220])
return createPet()
}
/**
* @description Successful operation
*/

export function createUpdatePetMutationResponse(): NonNullable<UpdatePetMutationResponse> {
faker.seed([220])
return createPet()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import type {
*/

export function createUpdatePetWithForm405(): NonNullable<UpdatePetWithForm405> {
faker.seed([220])
return undefined
}

export function createUpdatePetWithFormMutationResponse(): NonNullable<UpdatePetWithFormMutationResponse> {
faker.seed([220])
return undefined
}

export function createUpdatePetWithFormPathParams(): NonNullable<UpdatePetWithFormPathParams> {
faker.seed([220])
return { 'petId': faker.number.float({}) }
}

export function createUpdatePetWithFormQueryParams(): NonNullable<UpdatePetWithFormQueryParams> {
faker.seed([220])
return { 'name': faker.string.alpha(), 'status': faker.string.alpha() }
}
4 changes: 4 additions & 0 deletions examples/msw-v2/src/gen/mocks/petMocks/createUploadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ import { createApiResponse } from '../createApiResponse'
import type { UploadFileMutationRequest, UploadFileMutationResponse, UploadFilePathParams, UploadFileQueryParams } from '../../models/UploadFile'

export function createUploadFileMutationRequest(): NonNullable<UploadFileMutationRequest> {
faker.seed([220])
return faker.string.alpha()
}

export function createUploadFilePathParams(): NonNullable<UploadFilePathParams> {
faker.seed([220])
return { 'petId': faker.number.float({}) }
}

export function createUploadFileQueryParams(): NonNullable<UploadFileQueryParams> {
faker.seed([220])
return { 'additionalMetadata': faker.string.alpha() }
}
/**
* @description successful operation
*/

export function createUploadFileMutationResponse(): NonNullable<UploadFileMutationResponse> {
faker.seed([220])
return createApiResponse()
}
Loading

0 comments on commit ffd0439

Please sign in to comment.