@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.
Query hooks take the grouped request config ({ path, query, headers }, camelCase property names) as their first argument. Mutation hooks take only an options object, and you pass the grouped config through trigger(...) instead. 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.
For runtime validation, set validator on the client plugin. The generated operations carry the validation, so the hooks get it for free.
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', 'PATCH', 'DELETE'] },
}),
],
})