Beta You're reading the docs for Kubb v5, which is currently in beta. View the stable v4 docs
Skip to content
Official v5.0.0-beta.84 MIT kubb >=5.0.0 node >=22

@kubb/plugin-swr

Generates typed SWR hooks from your OpenAPI spec, so data fetching stays in sync with the API.

swrreacthooksdata-fetchingcodegenopenapi
Downloads
33.5k / mo
Stars
3
Bundle size
322.5 kB
Updated
today

@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

shell
bun add -d @kubb/plugin-swr@beta
shell
pnpm add -D @kubb/plugin-swr@beta
shell
npm install --save-dev @kubb/plugin-swr@beta
shell
yarn add -D @kubb/plugin-swr@beta

Dependencies

This plugin needs these plugins in your config:

For runtime validation, set validator on the client plugin. The generated operations carry the validation, so the hooks get it for free.

Example

typescript
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'] },
    }),
  ],
})

See also