Options
Pass these options to pluginSwr(), all of them optional. Shared options link to Shared plugin options, which documents their behavior once.
Options overview
| Option | Purpose | Default |
|---|---|---|
output | Where the generated hooks are written and exported. | { path: 'hooks', barrel: { type: 'named' } } |
↳ output.path | Choose the output folder or file. | 'hooks' |
↳ output.mode | Write a single file or a directory of files. | Inferred from output.path |
↳ output.barrel | Configure barrel exports. | { type: 'named' } |
↳ output.banner | Add content before generated code. | None |
↳ output.footer | Add content after generated code. | None |
group | Split output into per-tag or per-path folders. | None |
↳ group.type | Group operations by tag or URL path. | Required with group |
↳ group.name | Customize output group names. | camelCased tag or raw path segment |
client | Which registered client plugin the hooks call. | Auto-detected |
query | Configure the useSWR hooks, or turn them off. | { methods: ['GET'], importPath: 'swr' } |
↳ query.methods | Choose which HTTP methods generate queries. | ['GET'] |
↳ query.importPath | Set the module used for query imports. | 'swr' |
queryKey | Build the SWR key for each query hook. | Built-in queryKeyTransformer |
mutation | Configure the useSWRMutation hooks, or turn them off. | { methods: ['POST', 'PUT', 'PATCH', 'DELETE'], importPath: 'swr/mutation' } |
↳ mutation.methods | Choose which HTTP methods generate mutations. | ['POST', 'PUT', 'PATCH', 'DELETE'] |
↳ mutation.importPath | Set the module used for mutation imports. | 'swr/mutation' |
mutationKey | Build the SWR key for each mutation hook. | Built-in mutationKeyTransformer |
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. | resolverSwr |
macros | Rewrite 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' |
| Required | false |
@kubb/plugin-fetch.@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.
| Type | Partial<Query> | false |
| Required | false |
| 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.
| Type | Array<string> |
| Required | false |
| 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.
| Type | string |
| Required | false |
| 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. String values are inlined into generated code verbatim, so wrap any literal string in JSON.stringify(...).
| Type | (props: { node, casing, variant }) => Array<unknown> |
| Required | false |
| Default | Built-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.
| Type | Partial<Mutation> | false |
| Required | false |
| 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.
| Type | Array<string> |
| Required | false |
| 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.
| Type | string |
| Required | false |
| 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. String values are inlined verbatim, so wrap any literal string in JSON.stringify(...).
| Type | (props: { node, casing, variant }) => Array<unknown> |
| Required | false |
| Default | Built-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.
| Type | ResolverPatch<ResolverSwr> |
| Required | false |
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'
}
}