Skip to content
SWR

SWR

Officialv5.2.4

@kubb/plugin-swr

kubb add plugin-swr

Options

Pass these options to pluginSwr(), all of them optional. Shared options link to Shared plugin options for details.

Options overview

OptionPurposeDefault
outputWhere files are written: path, mode, barrel, banner, footer.{ path: 'hooks', barrel: { type: 'named' } }
groupSplit output by tag or path: type, name.None
clientWhich registered client plugin the hooks call.Auto-detected
queryConfigure the useSWR hooks, or turn them off.{ methods: ['GET'], importPath: 'swr' }
queryKeyBuild the SWR key for each query hook.Built-in queryKeyTransformer
mutationConfigure the useSWRMutation hooks, or turn them off.{ methods: ['POST', 'PUT', 'PATCH', 'DELETE'], importPath: 'swr/mutation' }
mutationKeyBuild the SWR key for each mutation hook.Built-in mutationKeyTransformer
includeKeep only operations that match.None
excludeSkip operations that match.[]
overrideApply different options per pattern.[]
resolverCustomize generated names and file paths.resolverSwr
macrosRewrite AST nodes before printing.[]

Option details

client

Selects which registered client plugin the hooks call. A single registered client is auto-detected. Set client when several are registered.

Type'fetch' | 'axios'
Requiredfalse
'fetch'
Calls the operations generated by @kubb/plugin-fetch.
'axios'
Calls the operations generated by @kubb/plugin-axios.

query

Configures the generated useSWR hooks. Pass an object to change the HTTP methods or import path, or false to skip query hook generation.

TypePartial<Query> | false
Requiredfalse
Default{ methods: ['GET'], importPath: 'swr' }

query.methods

HTTP methods treated as queries. An operation whose method is in this list generates a useSWR hook instead of a mutation.

TypeArray<string>
Requiredfalse
Default['GET']

query.importPath

Module that useSWR is imported from, emitted as import useSWR from '${importPath}'. Relative and absolute paths are used as written, with relative paths resolved against the generated file.

Typestring
Requiredfalse
Default'swr'

queryKey

Builds the SWR key for each query hook. The callback receives the operation node, the active casing and variant: 'query' and returns the key array. Strings are copied into the generated code as written, so wrap a string literal in JSON.stringify(...).

Type(props: { node, casing, variant }) => Array<unknown>
Requiredfalse
DefaultBuilt-in queryKeyTransformer
queryKey: ({ node, variant }) => [JSON.stringify({ variant, operationId: node.operationId })]

mutation

Configures the generated useSWRMutation hooks. Pass an object to change the HTTP methods or import path, or false to skip mutation hook generation.

TypePartial<Mutation> | false
Requiredfalse
Default{ methods: ['POST', 'PUT', 'PATCH', 'DELETE'], importPath: 'swr/mutation' }

mutation.methods

HTTP methods treated as mutations. An operation whose method is in this list generates a useSWRMutation hook instead of a query.

TypeArray<string>
Requiredfalse
Default['POST', 'PUT', 'PATCH', 'DELETE']

mutation.importPath

Module that useSWRMutation is imported from, emitted as import useSWRMutation from '${importPath}'. Relative and absolute paths are used as written, with relative paths resolved against the generated file.

Typestring
Requiredfalse
Default'swr/mutation'

mutationKey

Builds the SWR key for each mutation hook. Like queryKey, the callback receives the operation node, the active casing and variant: 'mutation' and returns the key array. Strings are copied into the generated code as written, so wrap a string literal in JSON.stringify(...).

Type(props: { node, casing, variant }) => Array<unknown>
Requiredfalse
DefaultBuilt-in mutationKeyTransformer

resolver

Overrides generated file and symbol names. Omitted members keep resolverSwr. The shared members (name, file, imports) and the this context are described under resolver.

TypeResolverPatch<ResolverSwr>
Requiredfalse
Partial override
type ResolverSwrPatch = {
  name?(name: string): string
  file?: {
    baseName?(params: { name: string; extname: string }): string
    path?(params: { baseName: string; output: Output }): string
  }
  imports?(options: ResolveImportsOptions): Array<ImportNode>
  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'
  }
}