Skip to content

Options

Configuration options for @kubb/plugin-swr, passed to pluginSwr({ ... }). Every field is optional.

Option Type Default Description
output Output { path: 'hooks' } Where the generated hooks are written and exported
group Group Split output into per-tag or per-path folders
client 'fetch' | 'axios' Which registered client plugin the hooks call
query Partial<Query> | false { methods: ['GET'], importPath: 'swr' } Configure the useSWR hooks, or turn them off
queryKey Transformer built-in Build the SWR key for each query hook
mutation Partial<Mutation> | false { methods: ['POST', 'PUT', 'PATCH', 'DELETE'], importPath: 'swr/mutation' } Configure the useSWRMutation hooks, or turn them off
mutationKey Transformer built-in Build the SWR key for each mutation hook
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<ResolverSwr> Customize generated names and file paths
macros Array<Macro> Rewrite AST nodes before printing

output

Where the generated .ts files are written and how they are exported. Defaults to { path: 'hooks', barrel: { type: 'named' } }.

output.path

Folder where the plugin writes its files (string, default 'hooks'), resolved against the global output.path on defineConfig. To write everything to one file instead, set output.mode: 'file' and give path a file name with its extension, such as 'hooks.ts'.

output.mode

How the plugin consolidates generated code ('file' | 'directory', default 'file'). The default writes everything into a single file whose path must include the extension. 'directory' writes one file per operation under output.path and can be grouped into subdirectories.

IMPORTANT

group requires mode: 'directory'. Pairing group with mode: 'file' (or leaving mode unset) fails the build with KUBB_INVALID_PLUGIN_OPTIONS.

output.barrel

Toggle the export style and depth to see the generated barrels.

  • src/gen/
  • models/
  • Pet.ts
  • User.ts
  • clients/
  • pet/
  • getPetById.ts
  • store/
  • getInventory.ts
src/gen/index.ts

Controls how the generated index.ts (barrel) re-exports the output. Accepts { type: 'named' } or { type: 'all' }, optionally with nested: true (for example { type: 'named', nested: true }) to write an index.ts in every subdirectory, or false to skip the barrel entirely. Each generator plugin defaults output.barrel to { type: 'named' }; the root output.barrel on defineConfig defaults to false.

output.banner

Text added to the top of every generated file, such as a license header or @ts-nocheck directive. Pass a string, or a function (meta: BannerMeta) => string that receives the document info (title, description, version, baseURL) and per-file context (filePath, baseName, isBarrel, isAggregation), so a directive can skip barrel files.

Text added to the bottom of every generated file (string or (meta: BannerMeta) => string), like banner but for closing comments. Pair banner: '/* eslint-disable */' with footer: '/* eslint-enable */' to scope a lint disable to the generated file.

group

Switch the mode to see where these operations land on disk.

clients/pet/
  • getPetById
  • addPet
clients/store/
  • getInventory
clients/order/
  • placeOrder
  • getOrderById
clients/user/
  • loginUser
group: { type: "tag" } splits the output by the operation tag, so placeOrder follows its order tag.

Splits generated files into subfolders by the operation's tag or URL path, each under {output.path}/{groupName}/. Without group, every file lands directly in output.path. It applies only to output.mode: 'directory'.

IMPORTANT

Combining group with output.mode: 'file' stops the build with a KUBB_INVALID_PLUGIN_OPTIONS error.

group.type

Property used to assign each operation to a group ('tag' | 'path'), required whenever group is set. An operation with no tag goes in the default group.

  • 'tag' uses the operation's first tag.
  • 'path' uses the first URL segment, such as pet for /pet/{petId}.

group.name

Function that turns a group key into a folder or identifier name, used as the subdirectory name and as a suffix when naming aggregate files. Defaults to ({ group }) => camelCase(group), except type: 'path' groups use the first URL segment as-is.

client

Selects which registered client plugin the generated hooks call ('fetch' | 'axios'). 'fetch' calls the @kubb/plugin-fetch functions and 'axios' the @kubb/plugin-axios functions, both through one grouped options object. A client plugin must be registered. When only one is registered it is auto-detected, so client is only needed to disambiguate several.

query

Configures the generated useSWR hooks. Pass an object to change the HTTP methods or import path, or false to skip query hook generation. Defaults to { methods: ['GET'], importPath: 'swr' }.

query.methods

HTTP methods treated as queries (Array<string>, default ['GET']). An operation whose method is in this list generates a useSWR hook instead of a mutation.

query.importPath

Module that useSWR is imported from (string, default 'swr'). The plugin emits import useSWR from '${importPath}'. Relative and absolute paths are used as written, with relative paths resolved against the generated file.

queryKey

Builds the SWR key for each query hook. The callback receives the operation node and the active casing and returns the key array, and the built-in transformer is used when unset. String values are inlined into generated code verbatim, so wrap any literal string in JSON.stringify(...).

mutation

Configures the generated useSWRMutation hooks. Pass an object to change the HTTP methods or import path, or false to skip mutation hook generation. Defaults to { methods: ['POST', 'PUT', 'PATCH', 'DELETE'], importPath: 'swr/mutation' }.

mutation.methods

HTTP methods treated as mutations (Array<string>, default ['POST', 'PUT', 'PATCH', 'DELETE']). An operation whose method is in this list generates a useSWRMutation hook instead of a query.

mutation.importPath

Module that useSWRMutation is imported from (string, default 'swr/mutation'). The plugin emits import useSWRMutation from '${importPath}'. Relative and absolute paths are used as written, with relative paths resolved against the generated file.

mutationKey

Builds the SWR key for each mutation hook. Like queryKey, the callback receives the operation node and the active casing and returns the key array, and the built-in transformer is used when unset. String values are inlined verbatim, so wrap any literal string in JSON.stringify(...).

include

Generates only the operations and schemas that match at least one entry, and skips the rest. Each entry filters by tag, operationId, path, method, contentType, or schemaName, with a pattern that can be a string or a RegExp, both matched as a regular expression against the value. A string pattern is compiled with new RegExp(pattern), so it is not an exact match: pattern: 'pet' also matches 'petType' or 'superpet'.

Type definition
typescript
export type Include = {
  type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType' | 'schemaName'
  pattern: string | RegExp
}

exclude

Skips any operation or schema that matches at least one entry, the opposite of include. Entries use the same type and pattern fields as include, and when both options match an item, exclude wins.

override

Applies different plugin options to operations that match a pattern. Each entry takes the same type and pattern as include, plus an options object that accepts any plugin option except override, so rules cannot nest. The first matching entry merges onto the plugin defaults, and later entries do not stack.

Type definition
typescript
export type Override = {
  type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType' | 'schemaName'
  pattern: string | RegExp
  options: Omit<Partial<Options>, '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 resolverSwr. 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.

Partial override
typescript
type ResolverSwrPatch = {
  name?(name: string): string
  file?: {
    baseName?(params: { name: string; extname: string }): string
    path?(params: { baseName: string; output: Output }): string
  }
  query?: {
    name?(node: OperationNode): string         // → 'useGetPetById'
    optionsName?(node: OperationNode): string  // → 'getPetByIdQueryOptions'
    keyName?(node: OperationNode): string       // → 'getPetByIdQueryKey'
    keyTypeName?(node: OperationNode): string   // → 'GetPetByIdQueryKey'
    clientName?(node: OperationNode): string    // → 'getPetById'
  }
  mutation?: {
    name?(node: OperationNode): string          // → 'useUpdatePet'
    keyName?(node: OperationNode): string        // → 'updatePetMutationKey'
    keyTypeName?(node: OperationNode): string    // → 'UpdatePetMutationKey'
    argTypeName?(node: OperationNode): string    // → 'UpdatePetMutationArg'
  }
}

macros

Rewrites AST nodes before they are printed, without forking the generator. Each macro callback (such as schema or operation) receives the node and a context object, and returns a replacement or undefined to leave it as is. Omitted callbacks keep their defaults, and macros run in order, so a later one sees the output of an earlier one.