Migration: @kubb/plugin-faker#
Part of the v4 → v5 migration guide. See the full option reference in @kubb/plugin-faker.
dateType, integerType, unknownType, and emptySchemaType moved to adapterOas. See Migration: @kubb/adapter-oas. resolver.name replaces transformers.name (see Override a resolver), and macros replace transformers.schema. The generators option is gone.
Removed: paramsCasing#
Parameter property names in the generated path, query, and header mocks come straight from the OpenAPI document and match the plugin-ts *Path, *Query, and *Headers types exactly, so paramsCasing had nothing left to configure.
pluginFaker({ paramsCasing: 'camelcase' })
Removed: mapper#
The mapper option mapped a property name to a raw Faker expression. v5 removes it, matching the removal on plugin-ts and plugin-zod. Rewrite the property's schema with a macro instead. A printer override changes how a schema type renders.
The generated mock is typed against the @kubb/plugin-ts output, so pick values the property's type allows. The v4 mapper bypassed that check with a raw expression.
import { ast } from 'kubb/kit'
pluginFaker({
- mapper: {
- status: `faker.helpers.arrayElement<any>(['available', 'pending'])`,
- },
+ macros: [
+ {
+ name: 'pet-status-values',
+ schema(node) {
+ if (node.name === 'Pet' && 'properties' in node) {
+ return {
+ ...node,
+ properties: node.properties.map((property) =>
+ property.name === 'status'
+ ? { ...property, schema: ast.factory.createSchema({ type: 'enum', primitive: 'string', enumValues: ['available', 'pending'] }) }
+ : property,
+ ),
+ }
+ }
+ return node
+ },
+ },
+ ],
})
Generated output#
Generic return type and intermediate variable#
The create prefix stays, so createPet is still createPet, but the factory now takes a generic TData and lifts the fake values into a defaultFakeData variable before the spread.
- export function createPet(data?: Partial<Pet>): Pet {
- return {
- ...{
- id: faker.number.int(),
- ...
- },
- ...(data || {}),
- }
- }
+ export function createPet<TData extends Partial<Pet> = object>(data?: TData) {
+ const defaultFakeData = {
+ id: faker.number.int(),
+ ...
+ }
+ return {
+ ...defaultFakeData,
+ ...(data || {}),
+ } as Omit<typeof defaultFakeData, keyof TData> & TData
+ }
The inferred return type keeps the fields you pass in data exactly as typed and fills the rest from defaultFakeData, so an override like createPet({ id: 1 }) reads back id as the literal you set rather than the wider schema type.