Skip to content

@kubb/plugin-faker

With the Faker plugin, you can use Faker to create mocks.

Installation

shell
bun add @kubb/plugin-faker
shell
pnpm add @kubb/plugin-faker
shell
npm install @kubb/plugin-faker
shell
yarn add @kubb/plugin-faker

Options

output

Specify the export location for the files and define the behavior of the output.

output.path

Path to the output folder or file that will contain the generated code.

TIP

if output.path is a file, group cannot be used.

Type:string
Required:true
Default:'mocks'

output.barrelType

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

TIP

Using propagate will prevent a plugin from creating a barrel file, but it will still propagate, allowing output.barrelType to export the specific function or type.

Type:'all' | 'named' | 'propagate' | false
Required:false
Default:'named'
typescript
export * from "./gen/petService.ts"
typescript
export { PetService } from "./gen/petService.ts"
typescript
typescript

output.banner

Add a banner text in the beginning of every file.

Type:string
Required:false

Add a footer text at the end of every file.

Type:string
Required:false

group

Grouping makes it possible to combine files in a folder based on specific type.

Imagine you have the following setup:

typescript
group: {
  type: 'tag',
  name({ group }){
    return `${group}Controller`
  }
}

That will create a structure like this:

.
├── src/
│   └── petController/
│   │   ├── addPet.ts
│   │   └── getPet.ts
│   └── storeController/
│       ├── createStore.ts
│       └── getStoreById.ts
├── petStore.yaml
├── kubb.config.ts
└── package.json

group.type

Define a type where to group the files on.

Type:'tag'
Required:true
  • 'tag': Use of operation.getTags().at(0)?.name

group.name

Return the name of a group based on the group name, this will be used for the file and name generation.

Type:(context: GroupContext) => string
Required:false
Default:(ctx) => '${ctx.group}Controller'

dateType

Choose to use date or datetime as JavaScript Date instead of string.

Type:'string' | 'date'
Required:false
Default:'string'
typescript
faker.string.alpha()
typescript
faker.date.anytime()

dateParser

Which parser should be used when dateType is set to 'string'.

Type:'faker' | 'dayjs' | 'moment' | string
Required:false
Default:'faker'

TIP

You can use any other library. For example, when you want to use moment you can pass moment and Kubb will add the import for moment: import moment from 'moment'. This only works when the package is using default exports like Dayjs and Moment.

typescript
// schema with format set to 'date'
faker.date.anytime().toString()

// schema with format set to 'time'
faker.date.anytime().toString()
typescript
// schema with format set to 'date'
dayjs(faker.date.anytime()).format("YYYY-MM-DD")

// schema with format set to 'time'
dayjs(faker.date.anytime()).format("HH:mm:ss")
typescript
// schema with format set to 'date'
moment(faker.date.anytime()).format("YYYY-MM-DD")

// schema with format set to 'time'
moment(faker.date.anytime()).format("HH:mm:ss")

mapper

Type:Record<string, string>
Required:false

unknownType

Which type to use when the Swagger/OpenAPI file is not providing more information.

Type:'any' | 'unknown'
Required:false
Default:'any'

regexGenerator

Choose which generator to use when using Regexp.

Type:'faker' | 'randexp'
Required:false
Default:'faker'
typescript
faker.helpers.fromRegExp(new RegExp(/test/))
typescript
new RandExp(/test/).gen()

seed

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

Type:`number
Required:false

include

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

Type:Array<Include>
Required:false
Include
typescript
export type Include = {
  type: 'tag' | 'operationId' | 'path' | 'method'
  pattern: string | RegExp
}

exclude

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

Type:Array<Exclude>
Required:false
Exclude
typescript
export type Exclude = {
  type: 'tag' | 'operationId' | 'path' | 'method'
  pattern: string | RegExp
}

override

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

Type:Array<Override>
Required:false
Override
typescript
export type Override = {
  type: 'tag' | 'operationId' | 'path' | 'method'
  pattern: string | RegExp
  options: PluginOptions
}

generators

See Generators for more information on how to use generators.

Type:Array<Generator<PluginFaker>>
Required:false

transformers

transformers.name

Customize the names based on the type that is provided by the plugin.

Type:(name: string, type?: ResolveType) => string
Required:false
typescript
type ResolveType = 'file' | 'function' | 'type' | 'const'

Example

typescript
import { 
defineConfig
} from '@kubb/core'
import {
pluginOas
} from '@kubb/plugin-oas'
import {
pluginFaker
} from '@kubb/plugin-faker'
import {
pluginTs
} from '@kubb/plugin-ts'
export default
defineConfig
({
input
: {
path
: './petStore.yaml',
},
output
: {
path
: './src/gen',
},
plugins
: [
pluginOas
(),
pluginTs
(),
pluginFaker
({
output
: {
path
: './mocks',
barrelType
: 'named',
banner
: '/* eslint-disable no-alert, no-console */',
footer
: ''
},
group
: {
type
: 'tag',
name
: ({
group
}) => `${
group
}Service`,
},
dateType
: 'date',
unknownType
: 'unknown',
seed
: [100],
}), ], })

Released under the MIT License.