MSW
Officialv6.1.0@kubb/plugin-msw
Options
Pass these options to pluginMsw(). Shared options link to Shared plugin options.
Options overview
| Option | Purpose | Default |
|---|---|---|
output | Where files are written: path, mode, barrel, banner, footer. | { path: 'handlers', barrel: { type: 'named' } } |
group | Split output by tag or path: type, name. | None |
baseURL | Base URL prepended to every handler's request. | None |
handlers | Emit a handlers.ts that re-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. | resolverMsw |
macros | Rewrite AST nodes before printing. | [] |
Option details
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. Leave off the trailing slash, because operation paths already start with /.
A value with a template expression, such as '${process.env.API_URL}', is evaluated when the handler is created. To set the base URL at runtime instead, pass { baseURL } to a handler or to createHandlers.
| Type | string |
| Required | false |
handlers
Emits a handlers.ts file with every generated handler in operation order. It exports two things:
handlers: the collection with the configuredbaseURL. Spread it intosetupServer(...handlers)orsetupWorker(...handlers).createHandlers(options?): builds the same collection with abaseURLyou pass at runtime.
| Type | boolean |
| Required | false |
| Default | false |
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. The handler uses new HttpResponse(JSON.stringify(data), ...).@kubb/plugin-faker when data is undefined or null. Other falsy values, such as 0, false, or '', are sent as the response body. Register pluginFaker() alongside this plugin.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 HttpResponse(JSON.stringify(data ?? createGetPetQueryResponse(data)), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
})
}resolver
Overrides generated file and symbol names. Omitted members keep resolverMsw. The shared members (name, file, imports) and the this context are described under resolver.
| Type | ResolverPatch<ResolverMsw> |
| Required | false |
type ResolverMswPatch = {
name?(name: string): string
file?: {
baseName?(params: { name: string; extname: string }): string
path?(params: { baseName: string; output: Output }): string
}
imports?(options: ResolveImportsOptions): Array<ImportNode>
handler?: {
name?(node: OperationNode): string // → 'getPetHandler'
listName?(): string // → 'handlers'
}
}