Skip to content

Commit

Permalink
chore(zod): example for mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Nov 24, 2023
1 parent 9a27d71 commit 004e104
Show file tree
Hide file tree
Showing 48 changed files with 131 additions and 507 deletions.
51 changes: 37 additions & 14 deletions examples/faker/kubb.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { defineConfig } from '@kubb/core'

import createSwagger from '@kubb/swagger'
import createSwaggerFaker from '@kubb/swagger-faker'
import createSwaggerTS from '@kubb/swagger-ts'

export default defineConfig(async () => {
await setTimeout(() => {
// wait for 1s, async behaviour
Expand All @@ -15,23 +19,42 @@ export default defineConfig(async () => {
clean: true,
},
hooks: {
done: ['prettier --write "**/*.{ts,tsx}"', 'eslint --fix ./src/gen'],
// done: ['prettier --write "**/*.{ts,tsx}"', 'eslint --fix ./src/gen'],
},
plugins: [
['@kubb/swagger', {
output: false,
}],
['@kubb/swagger-ts', {
output: 'models',
}],
['@kubb/swagger-faker', {
output: './mocks',
group: { type: 'tag', output: './mocks/{{tag}}Mocks' },
}],
// ['@kubb/swagger-faker', {
// output: './mocks.ts',
createSwagger({ output: false }),
createSwaggerTS({ output: 'models' }),
// createSwaggerFaker({
// output: './mocks',
// group: { type: 'tag', output: './mocks/{{tag}}Mocks' },
// }],
// }),
createSwaggerFaker({
output: './customMocks',
transformers: {
schema: (_schema, baseName) => {
/* override a property with name 'name'
name:
type: string
example: doggie
*/
if (baseName === 'name') {
// see mapper where we map `productionName` to `faker.commerce.productName`
return [{ keyword: 'productName' }]
}
return undefined
},
},
mapper: {
'productName': 'faker.commerce.productName',
},
include: [
{
type: 'operationId',
pattern: 'updatePet',
},
],
exclude: [],
}),
],
}
})
12 changes: 12 additions & 0 deletions examples/faker/src/gen/customMocks/createAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { faker } from '@faker-js/faker'
import type { Address } from '../models/Address'

export function createAddress(): NonNullable<Address> {
return {
'street': faker.string.alpha(),
'city': faker.string.alpha(),
'state': faker.string.alpha(),
'zip': faker.string.alpha(),
'identifier': faker.helpers.arrayElements([faker.number.float({}), faker.string.alpha(), faker.helpers.arrayElement<any>([`NW`, `NE`, `SW`, `SE`])]) as any,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { faker } from '@faker-js/faker'
import type { ApiResponse } from '../models/ApiResponse'

export function createApiResponse(): NonNullable<ApiResponse> {
return { code: faker.number.float({}), type: faker.string.alpha(), message: faker.string.alpha() }
return { 'code': faker.number.float({}), 'type': faker.string.alpha(), 'message': faker.string.alpha() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { faker } from '@faker-js/faker'
import type { Category } from '../models/Category'

export function createCategory(): NonNullable<Category> {
return { id: faker.number.float({}), name: faker.string.alpha() }
return { 'id': faker.number.float({}), 'name': faker.commerce.productName() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { faker } from '@faker-js/faker'
import type { Customer } from '../models/Customer'

export function createCustomer(): NonNullable<Customer> {
return { id: faker.number.float({}), username: faker.string.alpha(), address: faker.helpers.arrayElements([createAddress()]) as any }
return { 'id': faker.number.float({}), 'username': faker.string.alpha(), 'address': faker.helpers.arrayElements([createAddress()]) as any }
}
13 changes: 13 additions & 0 deletions examples/faker/src/gen/customMocks/createOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { faker } from '@faker-js/faker'
import type { Order } from '../models/Order'

export function createOrder(): NonNullable<Order> {
return {
'id': faker.number.float({}),
'petId': faker.number.float({}),
'quantity': faker.number.float({}),
'shipDate': faker.string.alpha(),
'status': faker.helpers.arrayElement<any>([`placed`, `approved`, `delivered`]),
'complete': faker.datatype.boolean(),
}
}
15 changes: 15 additions & 0 deletions examples/faker/src/gen/customMocks/createPet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createCategory } from './createCategory'
import { createTag } from './createTag'
import { faker } from '@faker-js/faker'
import type { Pet } from '../models/Pet'

export function createPet(): NonNullable<Pet> {
return {
'id': faker.number.float({}),
'name': faker.commerce.productName(),
'category': createCategory(),
'photoUrls': faker.helpers.arrayElements([faker.string.alpha()]) as any,
'tags': faker.helpers.arrayElements([createTag()]) as any,
'status': faker.helpers.arrayElement<any>([`available`, `pending`, `sold`]),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { faker } from '@faker-js/faker'
import type { Tag } from '../models/Tag'

export function createTag(): NonNullable<Tag> {
return { id: faker.number.float({}), name: faker.string.alpha() }
return { 'id': faker.number.float({}), 'name': faker.commerce.productName() }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createPet } from '../createPet'
import type { UpdatePet400, UpdatePet404, UpdatePet405, UpdatePetMutationRequest, UpdatePetMutationResponse } from '../../models/UpdatePet'
import { createPet } from './createPet'
import type { UpdatePet400, UpdatePet404, UpdatePet405, UpdatePetMutationRequest, UpdatePetMutationResponse } from '../models/UpdatePet'

/**
* @description Invalid ID supplied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
UpdatePetWithFormMutationResponse,
UpdatePetWithFormPathParams,
UpdatePetWithFormQueryParams,
} from '../../models/UpdatePetWithForm'
} from '../models/UpdatePetWithForm'

/**
* @description Invalid input
Expand All @@ -19,9 +19,9 @@ export function createUpdatePetWithFormMutationResponse(): NonNullable<UpdatePet
}

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

export function createUpdatePetWithFormQueryParams(): NonNullable<UpdatePetWithFormQueryParams> {
return { name: faker.string.alpha(), status: faker.string.alpha() }
return { 'name': faker.commerce.productName(), 'status': faker.string.alpha() }
}
15 changes: 15 additions & 0 deletions examples/faker/src/gen/customMocks/createUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { faker } from '@faker-js/faker'
import type { User } from '../models/User'

export function createUser(): NonNullable<User> {
return {
'id': faker.number.float({}),
'username': faker.string.alpha(),
'firstName': faker.person.firstName(),
'lastName': faker.person.lastName(),
'email': faker.internet.email(),
'password': faker.internet.password(),
'phone': faker.phone.number(),
'userStatus': faker.number.float({}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ export * from './createCustomer'
export * from './createOrder'
export * from './createPet'
export * from './createTag'
export * from './createUpdatePet'
export * from './createUpdatePetWithForm'
export * from './createUser'
export * from './createUserArray'
export * as petMocks from './petMocks/index'
export * as storeMocks from './storeMocks/index'
export * as userMocks from './userMocks/index'
2 changes: 1 addition & 1 deletion examples/faker/src/gen/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './mocks/index'
export * from './customMocks/index'
export * from './models/index'
12 changes: 0 additions & 12 deletions examples/faker/src/gen/mocks/createAddress.ts

This file was deleted.

13 changes: 0 additions & 13 deletions examples/faker/src/gen/mocks/createOrder.ts

This file was deleted.

15 changes: 0 additions & 15 deletions examples/faker/src/gen/mocks/createPet.ts

This file was deleted.

15 changes: 0 additions & 15 deletions examples/faker/src/gen/mocks/createUser.ts

This file was deleted.

24 changes: 0 additions & 24 deletions examples/faker/src/gen/mocks/petMocks/createAddPet.ts

This file was deleted.

22 changes: 0 additions & 22 deletions examples/faker/src/gen/mocks/petMocks/createDeletePet.ts

This file was deleted.

22 changes: 0 additions & 22 deletions examples/faker/src/gen/mocks/petMocks/createFindPetsByStatus.ts

This file was deleted.

22 changes: 0 additions & 22 deletions examples/faker/src/gen/mocks/petMocks/createFindPetsByTags.ts

This file was deleted.

29 changes: 0 additions & 29 deletions examples/faker/src/gen/mocks/petMocks/createGetPetById.ts

This file was deleted.

Loading

0 comments on commit 004e104

Please sign in to comment.