@kubb/swagger-faker 🦙 ​
With the Swagger Faker plugin, you can use Faker to create mocks based on a Swagger file.
Installation ​
bun add @kubb/swagger-faker @kubb/swagger-ts @kubb/swagger
pnpm add @kubb/swagger-faker @kubb/swagger-ts @kubb/swagger
npm install @kubb/swagger-faker @kubb/swagger-ts @kubb/swagger
yarn add @kubb/swagger-faker @kubb/swagger-ts @kubb/swagger
Options ​
output ​
output.path ​
Relative path to save the Faker mocks. When output is a file it will save all models inside that file else it will create a file per schema item.
INFO
Type: string
Default: 'mocks'
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
output: {
path: './mocks',
},
},
),
],
})
output.exportAs ​
Name to be used for the export * as from './'
INFO
Type: string
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
output: {
exportAs: 'mocks',
},
},
),
],
})
output.extName ​
Add an extension to the generated imports and exports, default it will not use an extension
INFO
Type: string
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
output: {
extName: '.js',
},
},
),
],
})
output.exportType ​
Define what needs to be exported, you can also disable the export of barrel files
INFO
Type: 'barrel' | 'barrelNamed' | false
group ​
Group the Faker mocks based on the provided name.
group.type ​
Tag will group based on the operation tag inside the Swagger file.
Type: 'tag'
Required: true
group.output ​
Relative path to save the grouped Faker mocks. {{tag}}
will be replaced by the current tagName.
Type: string
Example: mocks/{{tag}}Controller
=> mocks/PetController
Default: ${output}/{{tag}}Controller
group.exportAs ​
Name to be used for the export * as from './
Type: string
Default: '{{tag}}Mocks'
INFO
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
output: {
path: './mocks',
},
group: {
type: 'tag',
output: './mocks/{{tag}}Mocks',
},
},
),
],
})
include ​
An array containing include parameters to include tags/operations/methods/paths.
type
export type Include = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
}
INFO
Type: Array<Include>
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
include: [
{
type: 'tag',
pattern: 'store',
},
],
},
),
],
})
exclude ​
An array containing exclude parameters to exclude/skip tags/operations/methods/paths.
type
export type Exclude = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
}
INFO
Type: Array<Exclude>
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
exclude: [
{
type: 'tag',
pattern: 'store',
},
],
},
),
],
})
override ​
An array containing override parameters to override options
based on tags/operations/methods/paths.
type
export type Override = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
options: PluginOptions
}
INFO
Type: Array<Override>
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
override: [
{
type: 'tag',
pattern: 'pet',
options: {
output: './custom',
},
},
],
},
),
],
})
transformers ​
transformers.name ​
Override the name of the faker data that is getting generated, this will also override the name of the file.
INFO
Type: (name: string, type?: "function" | "type" | "file" ) => string
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
output: {
path: './mocks',
},
transformers: {
name: (name) => {
return `${name}Mock`
},
},
},
),
],
})
dateType ​
Choose to use date
or datetime
as JavaScript Date
instead of string
.
type
faker.string.alpha
faker.date.anytime()
INFO
Type: 'string' | 'date'
Default: 'string'
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
dateType: 'string',
},
),
],
})
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
dateType: 'date',
},
),
],
})
unknownType ​
Which type to use when the Swagger/OpenAPI file is not providing more information.
type
any
unknown
INFO
Type: 'any' | 'unknown'
Default: 'any'
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
unknownType: 'any',
},
),
],
})
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
unknownType: 'unknown',
},
),
],
})
seed ​
The use of Seed is intended to allow for consistent values in a test.
INFO
Type: 'number' | 'number[]'
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerFaker } from '@kubb/swagger-faker'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerFaker(
{
seed: [222],
},
),
],
})