@kubb/plugin-swr
@kubb/plugin-swr turns each OpenAPI operation into an SWR hook. Read operations become useSWR hooks. Write operations become useSWRMutation hooks. Every hook is typed: keys, input variables, response data, and error shape all come from the spec.
The hooks call an HTTP client, so a client plugin must be registered. Add @kubb/plugin-ts for the types and either @kubb/plugin-axios or @kubb/plugin-fetch for the client. Generation errors out when no client plugin is present.
Each hook takes its parameters as a single grouped options object shaped as { body, path, query, headers }, with camelCase property names. The request still sends the original parameter names from the spec, and Kubb writes that mapping for you.
Installation
bun add -d @kubb/plugin-swr@betapnpm add -D @kubb/plugin-swr@betanpm install --save-dev @kubb/plugin-swr@betayarn add -D @kubb/plugin-swr@betaDependencies
This plugin needs these plugins in your config:
@kubb/plugin-tsfor the types.- A client plugin,
@kubb/plugin-axiosor@kubb/plugin-fetch, for the HTTP layer. The hooks call its functions, so generation errors out when no client plugin is registered.
Set validator to 'zod' and the plugin also depends on @kubb/plugin-zod, which then has to be in the plugins list.
Example
import { } from 'kubb'
import { } from '@kubb/plugin-ts'
import { } from '@kubb/plugin-fetch'
import { } from '@kubb/plugin-swr'
export default ({
: { : './petStore.yaml' },
: { : './src/gen' },
: [
(),
(),
({
: { : './hooks' },
: { : 'tag', : ({ }) => `${}Hooks` },
: 'fetch',
: { : ['get'], : 'swr' },
: { : ['post', 'put', 'delete'] },
}),
],
})