Skip to content
MSW logo

MSW

Officialv6.2.6

@kubb/plugin-msw

kubb add 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

OptionPurposeDefault
outputWhere files are written.{ path: 'handlers', barrel: { type: 'named' } }
groupSplit output by tag or path.None
baseURLBase URL prepended to every handler's path.None
handlersWrite a handlers.ts that exports every handler.false
parserSource of the response body each handler returns.'data'
includeKeep only operations that match.None
excludeSkip operations that match.[]
overrideApply different options per pattern.[]
resolverCustomize generated names and file paths. See resolver members.resolverMsw
macrosRewrite 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 /.

Typestring
Requiredfalse
ValueHandler 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.

Typeboolean
Requiredfalse
Defaultfalse
ExportWhat it is
handlersEvery handler with the configured baseURL, for setupServer(...handlers) or setupWorker(...handlers).
createHandlers(options?)Builds the same list with a baseURL passed at runtime.
src/gen/handlers/handlers.ts
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'
Requiredfalse
Default'data'
'data'
Returns the data you pass, typed with the @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.
'faker'
Falls back to a @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.

See Examples from the spec.

return getPetHandlerResponse200(data)

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:

test-server.ts
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:

Partial override
type ResolverMswPatch = {
  handler?: {
    name?(node: OperationNode): string  // → 'getPetHandler'
    listName?(): string                 // → 'handlers'
  }
}