Skip to content
MSW logo

MSW

Officialv6.1.0

@kubb/plugin-msw

kubb add plugin-msw

Options

Pass these options to pluginMsw(). Shared options link to Shared plugin options.

Options overview

OptionPurposeDefault
outputWhere files are written: path, mode, barrel, banner, footer.{ path: 'handlers', barrel: { type: 'named' } }
groupSplit output by tag or path: type, name.None
baseURLBase URL prepended to every handler's request.None
handlersEmit a handlers.ts that re-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.resolverMsw
macrosRewrite 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.

Typestring
Requiredfalse

handlers

Emits a handlers.ts file with every generated handler in operation order. It exports two things:

  • handlers: the collection with the configured baseURL. Spread it into setupServer(...handlers) or setupWorker(...handlers).
  • createHandlers(options?): builds the same collection with a baseURL you pass at runtime.
Typeboolean
Requiredfalse
Defaultfalse
gen/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'
Default value. Returns the data you pass to the handler, typed with the @kubb/plugin-ts types. The handler uses new HttpResponse(JSON.stringify(data), ...).
'faker'
Falls back to a value generated by @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.
'faker'
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.

TypeResolverPatch<ResolverMsw>
Requiredfalse
Partial override
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'
  }
}