Options#
Every option below is a key on pluginMsw({ ... }).
| Option | Type | Default | Description |
|---|---|---|---|
output | Output | { path: 'handlers', barrel: { type: 'named' } } | Where the generated files are written and exported |
group | Group | — | Split output into per-tag or per-path folders |
baseURL | string | — | Base URL prepended to every handler's request |
handlers | boolean | false | Emit a handlers.ts that re-exports every handler |
parser | 'data' | 'faker' | 'data' | Source of the response body each handler returns |
include | Array<Include> | — | Keep only operations that match |
exclude | Array<Exclude> | [] | Skip operations that match |
override | Array<Override> | [] | Apply different options per pattern |
resolver | ResolverPatch<ResolverMsw> | — | Customize generated names and file paths |
macros | Array<Macro> | — | Rewrite AST nodes before printing |
output#
Where the generated handler files are written and how they are exported.
output.path#
Folder where the plugin writes its files, resolved against the global output.path on defineConfig and defaulting to 'handlers'. To write everything to one file instead, set output.mode: 'file' and give path a file name with its extension, such as 'handlers.ts'.
output.mode#
How the plugin consolidates its generated code into files. 'file' writes everything into a single file, so output.path must include the extension. 'directory' writes one file per operation under output.path. Pair 'directory' with group to organize output into subdirectories. Leave it unset and Kubb reads output.path: a name with an extension means one file, anything else a directory.
Important
group works with the inferred directory mode, no mode needed. Set mode: 'directory' yourself only to override the inference, such as a directory name that carries a dot (path: 'clients.v2'). An explicit mode: 'file' still forbids group and stops the build with KUBB_INVALID_PLUGIN_OPTIONS, since a single file has nothing to group.
output.barrel#
output.banner#
output.footer#
group#
group.name#
Function that turns a group key into the subdirectory name. It defaults to ({ group }) => camelCase(group) for tag groups, while type: 'path' groups default to the raw first URL segment, uncased.
baseURL#
Base URL prepended to every handler's request. When omitted, no host is prepended and each handler matches the operation's relative path from the spec. Set it to point at a different environment than the spec.
handlers#
Emits a handlers.ts file that re-exports every generated handler in operation order. Spread it into your MSW setupServer(...handlers) or setupWorker(...handlers) call.
import { getPetHandler } from './getPetHandler'
import { addPetHandler } from './addPetHandler'
export const handlers = [getPetHandler(), addPetHandler()] as const
parser#
Source of the response body each handler returns. 'data' (the default) returns a typed payload from @kubb/plugin-ts that you fill in from tests, so the handler body is new Response(JSON.stringify(data), ...). 'faker' falls back to a value built by @kubb/plugin-faker when you pass no data. Register pluginFaker() in the plugins array, since the plugin depends on Faker only for this value.
export function getPetHandler(data?: GetPetQueryResponse | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Response | Promise<Response>)) {
return http.get('/pet/:petId', function handler(info) {
if (typeof data === 'function') return data(info)
return new Response(JSON.stringify(data || createGetPetQueryResponse(data)), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
})
}
include#
exclude#
override#
resolver#
Changes how the plugin names generated files and symbols. Pass a partial patch. Override only the members you want, and anything you omit keeps resolverMsw. See Override a resolver for the this context and how a patch layers over the default.
Tip
Inside a method this is the full resolver, so this.default.name(name) reuses the built-in casing.
type ResolverMswPatch = {
name?(name: string): string
file?: {
baseName?(params: { name: string; extname: string }): string
path?(params: { baseName: string; output: Output }): string
}
handler?: {
name?(node: OperationNode): string
listName?(): string
}
}