MSW
Officialv6.2.6@kubb/plugin-msw
Options
Pass these options to pluginMsw(). Shared options link to Shared plugin options, and this page gives only their default here.
Options overview
| Option | Purpose | Default |
|---|---|---|
output | Where files are written. | { path: 'handlers', barrel: { type: 'named' } } |
group | Split output by tag or path. | None |
baseURL | Base URL prepended to every handler's path. | None |
handlers | Write a handlers.ts that exports every handler. | false |
parser | Source of the response body each handler returns. | 'data' |
include | Keep only operations that match. | None |
exclude | Skip operations that match. | [] |
override | Apply different options per pattern. | [] |
resolver | Customize generated names and file paths. See resolver members. | resolverMsw |
macros | Rewrite AST nodes before printing. | [] |
Option details
baseURL
Base URL prepended to the path each handler matches. Without it, a handler matches the operation's relative path. Leave off the trailing slash, because operation paths start with /.
| Type | string |
| Required | false |
| Value | Handler matches |
|---|---|
| None (default) | /pets/:petId |
'https://petstore.dev' | https://petstore.dev/pets/:petId |
'${process.env.API_URL}' | The value of API_URL when the handler is created |
To set the base URL at runtime, pass { baseURL } as a handler's second argument or to createHandlers.
handlers
Writes a handlers.ts with every handler in operation order.
| Type | boolean |
| Required | false |
| Default | false |
| Export | What it is |
|---|---|
handlers | Every handler with the configured baseURL, for setupServer(...handlers) or setupWorker(...handlers). |
createHandlers(options?) | Builds the same list with a baseURL passed at runtime. |
import { getPetHandler } from './getPetHandler'
import { addPetHandler } from './addPetHandler'
export function createHandlers(options?: { baseURL?: string }) {
return [getPetHandler(undefined, options), addPetHandler(undefined, options)] as const
}
export const handlers = createHandlers()parser
Source of the response body each handler returns.
| Type | 'data' | 'faker' |
| Required | false |
| Default | 'data' |
@kubb/plugin-ts types. Without data, the handler sends the first example of the success response, when the spec has one. Pass null to send a null body.@kubb/plugin-faker value when data is undefined or null and the success response has a schema. Other falsy values, such as 0, are sent as the body. Register pluginFaker() next to this plugin.return getPetHandlerResponse200(data)return getPetHandlerResponse200(data ?? createGetPetResponse())Each handler file also exports one response helper per status code, such as getPetHandlerResponse404(data). Return a helper from the callback to mock that status:
getPetHandler(() => getPetHandlerResponse404())Resolver members
Members you leave out of a resolver patch keep resolverMsw. Besides the shared name, file, and imports members of resolver, resolverMsw has these:
type ResolverMswPatch = {
handler?: {
name?(node: OperationNode): string // → 'getPetHandler'
listName?(): string // → 'handlers'
}
}