@kubb/swagger-swr 🦙 ​
With the Swagger SWR plugin you can create SWR hooks based on an operation in the Swagger file.
Installation ​
bun add @kubb/swagger-swr @kubb/swagger-ts @kubb/swagger
bun add @kubb/swagger-swr @kubb/swagger-ts @kubb/swagger
pnpm add @kubb/swagger-swr @kubb/swagger-ts @kubb/swagger
pnpm add @kubb/swagger-swr @kubb/swagger-ts @kubb/swagger
npm install @kubb/swagger-swr @kubb/swagger-ts @kubb/swagger
npm install @kubb/swagger-swr @kubb/swagger-ts @kubb/swagger
yarn add @kubb/swagger-swr @kubb/swagger-ts @kubb/swagger
yarn add @kubb/swagger-swr @kubb/swagger-ts @kubb/swagger
Options ​
output ​
Output to save the SWR hooks.
INFO
Type: string
Default: 'hooks'
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
output: './hooks',
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
output: './hooks',
},
),
],
})
groupBy ​
Group the SWR hooks based on the provided name.
type ​
Tag will group based on the operation tag inside the Swagger file.
Type: 'tag'
Required: true
output ​
Relative path to save the grouped SWR hooks. {{tag}}
will be replaced by the current tagName.
Type: string
Example: hooks/{{tag}}Controller
=> hooks/PetController
Default: '${output}/{{tag}}Controller'
exportAs ​
Name to be used for the export * as {{exportAs}} from './
Type: string
Default: '{{tag}}SWRHooks'
INFO
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerFaker from '@kubb/swagger-faker'
import createSwaggerMsw from '@kubb/swagger-msw'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
output: './hooks',
groupBy: { type: 'tag', output: './hooks/{{tag}}Controller' },
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerFaker from '@kubb/swagger-faker'
import createSwaggerMsw from '@kubb/swagger-msw'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
output: './hooks',
groupBy: { type: 'tag', output: './hooks/{{tag}}Controller' },
},
),
],
})
client deprecated ​
Path to the client that will be used to do the API calls.
Relative to the root
INFO
Type: string
Default: '@kubb/swagger-client/client'
Deprecated. Use clientImportPath
instead. It will be skipped if clientImportPath
is provided.
clientImportPath ​
Path to the client import path that will be used to do the API calls.
It will be used as import client from '${clientImportPath}'
.
It allow both relative and absolute path. the path will be applied as is, so relative path shoule be based on the file being generated.
INFO
Type: string
Default: '@kubb/swagger-client/client'
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
clientImportPath: '../../client.ts',
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
clientImportPath: '../../client.ts',
},
),
],
})
dataReturnType experimental ​
ReturnType that needs to be used when calling client().
'data'
will return ResponseConfig[data]. 'full'
will return ResponseConfig.
type
export async function getPetById<TData>(
petId: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>["data"]> {
...
}
export async function getPetById<TData>(
petId: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>["data"]> {
...
}
export async function getPetById<TData>(
petId: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>> {
...
}
export async function getPetById<TData>(
petId: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>> {
...
}
Type: 'data' | 'full'
Default: 'data'
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
dataReturnType: 'data',
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
dataReturnType: 'data',
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
dataReturnType: 'full',
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
dataReturnType: 'full',
},
),
],
})
skipBy ​
Array containing skipBy paramaters to exclude/skip tags/operations/methods/paths.
type
export type SkipBy = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
}
export type SkipBy = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
}
INFO
Type: Array<SkipBy>
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
skipBy: [
{
type: 'tag',
pattern: 'store',
},
],
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
skipBy: [
{
type: 'tag',
pattern: 'store',
},
],
},
),
],
})
overrideBy ​
Array containing overrideBy paramaters to override options
based on tags/operations/methods/paths.
type
export type OverrideBy = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
options: PluginOptions
}
export type OverrideBy = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
options: PluginOptions
}
INFO
Type: Array<OverrideBy>
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerClient from '@kubb/swagger-client'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerClient(
{
overrideBy: [
{
type: 'tag',
pattern: 'pet',
options: {
output: './custom',
},
},
],
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerClient from '@kubb/swagger-client'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerClient(
{
overrideBy: [
{
type: 'tag',
pattern: 'pet',
options: {
output: './custom',
},
},
],
},
),
],
})
transformers ​
name ​
Override the name of the hook that is getting generated, this will also override the name of the file.
INFO
Type: (name: string) => string
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
output: './hooks',
transformers: {
name: (name) => {
return `${name}Hook`
},
},
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerSwr from '@kubb/swagger-swr'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerSwr(
{
output: './hooks',
transformers: {
name: (name) => {
return `${name}Hook`
},
},
},
),
],
})