Movatterモバイル変換


[0]ホーム

URL:


Skip to content

@kubb/plugin-swr

With the SWR plugin you can createSWR hooks based on an operation.

Installation

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

Options

output

Specify the export location for the files and define the behavior of the output.

output.path

Path to the output folder or file that will contain the generated code.

TIP

ifoutput.path is a file,group cannot be used.

Type:string
Required:true
Default:'hooks'

output.barrelType

Define what needs to be exported, here you can also disable the export of barrel files.

TIP

Using propagate will prevent a plugin from creating a barrel file, but it will still propagate, allowingoutput.barrelType to export the specific function or type.

Type:'all' | 'named' | 'propagate' | false
Required:false
Default:'named'
typescript
export * from "./gen/petService.ts"
typescript
export { PetService } from "./gen/petService.ts"
typescript
typescript

output.banner

Add a banner text in the beginning of every file.

Type:string | (oas: Oas) => string
Required:false

output.footer

Add a footer text at the end of every file.

Type:string | (oas: Oas) => string
Required:false

contentType

Define which contentType should be used. By default, the first JSON valid mediaType will be used

Type:'application/json' | (string & {})
Required:false

group

Grouping makes it possible to combine files in a folder based on specifictype.

Imagine you have the following setup:

typescript
group: {  type: 'tag',  name({ group }){    return `${group}Controller`  }}

That will create a structure like this:

.├── src/│   └── petController/│   │   ├── addPet.ts│   │   └── getPet.ts│   └── storeController/│       ├── createStore.ts│       └── getStoreById.ts├── petStore.yaml├── kubb.config.ts└── package.json

group.type

Define a type where to group the files on.

Type:'tag'
Required:true
  • 'tag': Use of operation.getTags().at(0)?.name

group.name

Return the name of a group based on the group name, this will be used for the file and name generation.

Type:(context: GroupContext) => string
Required:false
Default:(ctx) => '${ctx.group}Controller'

client

client.importPath

Path to the client import path that will be used to do the API calls.
It will be used asimport client from '${client.importPath}'.
It allows both relative and absolute path but be aware that we will not change the path.

TIP

Use of default exports asexport const client = ()=>{}

Type:string
Required:false
Default:'@kubb/plugin-client/clients/axios'

client.dataReturnType

ReturnType that will be used when calling the client.

Type:'data' | 'full'
Required:false
Default:'data'
  • 'data' will return ResponseConfig[data].
  • 'full' will return ResponseConfig.
typescript
export async function getPetById<TData>(  petId: GetPetByIdPathParams,): Promise<ResponseConfig<TData>["data"]> {  ...}
typescript
export async function getPetById<TData>(  petId: GetPetByIdPathParams,): Promise<ResponseConfig<TData>> {  ...}

client.baseURL

Allows you to set a custom base url for all generated calls.

Type:string
Required:false

paramsType

How to pass your params. Here you can switch between object-style parameters and inline parameters.

Type:'object' | 'inline'
Required:false
Default:'inline'

TIP

WhenparamsType is set to'object',pathParams will also be set to'object'.

  • 'object' will return the params and pathParams as an object.
  • 'inline' will return the params as comma separated params.
typescript
export async function deletePet(  {    petId,    headers,  }: {    petId: DeletePetPathParams['petId']    headers?: DeletePetHeaderParams  },  config: Partial<RequestConfig> = {},) {...}
typescript
export async function deletePet(  petId: DeletePetPathParams['petId'],  headers?: DeletePetHeaderParams,  config: Partial<RequestConfig> = {}){  ...}

paramsCasing

How to style your params, by default no casing is applied.

Type:'camelcase'
Required:false
Default:``
  • 'camelcase' will use camelcase for the params names
typescript
export async function deletePet(  petId: DeletePetPathParams['pet_id'],  headers?: DeletePetHeaderParams,  config: Partial<RequestConfig> = {}){...}
typescript
export async function deletePet(  pet_id: DeletePetPathParams['pet_id'],  headers?: DeletePetHeaderParams,  config: Partial<RequestConfig> = {}){ ...}

pathParamsType

How to pass your pathParams.

Type:'object' | 'inline'
Required:false
Default:'inline'
  • 'object' will return the pathParams as an object.
  • 'inline' will return the pathParams as comma separated params.
typescript
export async function getPetById (  { petId }: GetPetByIdPathParams,) {  ...}
typescript
export async function getPetById(  petId: GetPetByIdPathParams,) {  ...}

parser

Which parser should be used before returning the data.

Type:'client' | 'zod'
Required:false
Default:'client'
  • 'zod' will use@kubb/plugin-zod to parse the data.
  • 'client' will return without parsing the data.

queryKey

Customize the queryKey.

WARNING

When using a string you need to useJSON.stringify.

Type:(props: { operation: Operation; schemas: OperationSchemas }) => unknown[]
Required:false

query

query.methods

Type:Array<HttpMethod>
Required:false

query.importPath

Type:string
Required:false
Default:'swr'

mutation

mutation.methods

Type:Array<HttpMethod>
Required:false

mutation.importPath

Type:string
Required:false
Default:'swr/mutation'

mutationKey

Customize the mutationKey.

WARNING

When using a string you need to useJSON.stringify.

Type:(props: { operation: Operation; schemas: OperationSchemas }) => unknown[]
Required:false

include

Array containing include parameters to include tags/operations/methods/paths.

Type:Array<Include>
Required:false
Include
typescript
export type Include = {  type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'  pattern: string | RegExp}

exclude

Array containing exclude parameters to exclude/skip tags/operations/methods/paths.

Type:Array<Exclude>
Required:false
Exclude
typescript
export type Exclude = {  type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'  pattern: string | RegExp}

override

Array containing override parameters to overrideoptions based on tags/operations/methods/paths.

Type:Array<Override>
Required:false
Override
typescript
export type Override = {  type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'  pattern: string | RegExp  options: PluginOptions}

generators

SeeGenerators for more information on how to use generators.

Type:Array<Generator<PluginSwr>>
Required:false

transformers

transformers.name

Customize the names based on the type that is provided by the plugin.

Type:(name: string, type?: ResolveType) => string
Required:false
typescript
type ResolveType = 'file' | 'function' | 'type' | 'const'

Example

typescript
import {
defineConfig
} from '@kubb/core'
import {
pluginOas
} from '@kubb/plugin-oas'
import {
pluginSwr
} from '@kubb/plugin-swr'
import {
pluginTs
} from '@kubb/plugin-ts'
import {
pluginZod
} from '@kubb/plugin-zod'
export default
defineConfig
({
input
: {
path
: './petStore.yaml',
},
output
: {
path
: './src/gen',
},
plugins
: [
pluginOas
(),
pluginTs
(),
pluginZod
(),
pluginSwr
({
output
: {
path
: './hooks',
},
group
: {
type
: 'tag',
name
: ({
group
}) => `${
group
}Hooks`,
},
client
: {
dataReturnType
: 'full',
},
parser
: 'zod',
}), ],})

Links

Contributors

Changelog

24d9e13-fix: support customcontentType per plugin
d2b9643-feat: support banner with context for Oas
4eb9c46-docs: client.baseUrl
ed08de3-feat: Switch between the use of type or interface when creating types
bebd382-chore: correct naming
713d202-docs: update docs for paramsCasing
a12aa73-Fix:barrelType: 'propagate' to make sure the core can still generate barrel files, even if the plugin will not have barrel files (#1404)
51a9e3a-Update index.md (#1380)
e246cc0-docs: cleanup docs(spelling + old knowledge base pages being removed)
a8d645c-Feat/disable query (#1365)
8b7d53c-feat: disable useQuery
622073d-feat: use ofbaseURL to override the default baseURL in every call
4bacdd6-chore: correct paramsType docs
4db1509-Feat/1195 object style params (#1358)
09934fb-chore: update docs
e55fb33-chore: update docs
7f30045-docs: better type/default/grouping
bf9700d-docs: api page
6436c1e-docs: update docs with one main docs page
dafd183-docs: resolve twoslash
49407d7-chore: update docs
2fbc18a-v3 prep (#1118)
da3cb26-Ts-plugin with custom mapper(experimental) (#1075)
a4cead3-Plugin oas (#993)
bd5bfc4-Docs/cleanup (#988)
4b52765-Chore/rename imports (#986)
0b8b68d-chore: examples update + CLI fixes
f3c5b7e-docs: update links, formatting and code examples (#740)
c22433e-Feat/barrel named (#713)
72a1efb-fix(docs): fix typo parameters (#728)
605878d-chore: prepare v2 (#711)
955f8ed-Fix/combine files (#707)
9607c78-fix: disable a specific template with passingfalse
f516ea1-chore: cleanup
210d58f-feat:exportType to disable the creation of barrel files
8314cb8-docs: plugin system
8044907-feat: path.extName to set.ts or.js to the barrel files (#689)
0c894ca-Chore/swagger infer (#686)
5cb9b11-docs: correct type for transformers.name
32baea8-feat: add templates option to make it possible to override the generation
7d6e892-Feat exclude and include (#624)
74f562b-fix: deprecate client and replace by clientImportPath#621 (#622)
4dc5292-docs: update dark style
b21ca29-chore: updating formatting
fb42958-docs: better docs with examples
9087387-docs: update docs with examples
2024426-use v-pre blocks to escape curly braces
0690e47-docs: escape curly braces
15c0cd3-fix: docs about wrong Badge usage, wrong import line example
9e5b124-feat:dataReturnType for @kubb/swagger-tanstack-query, @kubb/swagger-client and @kubb/swagger-swr
a1bb145-docs: readme update + typo's
f405183-feat: use of a tranformer function to override the name of the hook/type/client
8a0ee7c-chore: move src to root
8977443-docs:bun support
346a3b7-docs: use of vitepress
44be77b-feat: ✨ skipBy to excludetags,names,paths, ... out of the generation
89c0d08-feat:client.ts with the use ofprocess.env override to set options foraxios
214ff25-feat:groupBy.exportAs to make it possible go group all import withexport * as groupBy.exportAs from "./x"
c687864-feat: groupby as object withtype andoutput which can be customised with a handlebar like syntax
29b396f-feat: enumType to use anEnum or use* as const
d67dc01-chore: update docs
679524b-chore: update docs
fb091d9-feat: swr plugin
1f08081-docs: update links
2339010-docs: update docs
2a058da-feat: @kubb/swagger-client package
c011a12-feat: add client option for react-query package to override default axios instance(if needed)
8933734-feat: groupby for react-query with as first option: tag
e9c27a4-feat: use of swagger-ts name instead of swagger-typescript
9fc7df7-fix: fileManager.getSource public + cleanup
121b4f3-Initial commit

[8]ページ先頭

©2009-2025 Movatter.jp