@kubb/plugin-react-query
@kubb/plugin-react-query turns each OpenAPI operation into a TanStack Query hook for React. Read operations become useFoo, with useFooSuspense and useFooInfinite variants. Write operations become useFoo mutations. Every hook is typed: query 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-react-query@betapnpm add -D @kubb/plugin-react-query@betanpm install --save-dev @kubb/plugin-react-query@betayarn add -D @kubb/plugin-react-query@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-react-query'
export default ({
: { : './petStore.yaml' },
: { : './src/gen' },
: [
(),
(),
({
: { : './hooks' },
: {
: 'tag',
: ({ }) => `${}Hooks`,
},
: 'fetch',
: { : ['POST', 'PUT', 'DELETE'] },
: {
: 'next_page',
: 0,
: 'pagination.next.cursor',
: ['pagination', 'prev', 'cursor'],
},
: {
: ['GET'],
: '@tanstack/react-query',
},
: {},
}),
],
})