@kubb/swagger-client 🦙 ​
With the Swagger client plugin you can create Axios API calls.
Installation ​
bun add @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
bun add @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
pnpm add @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
pnpm add @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
npm install @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
npm install @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
yarn add @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
yarn add @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
Options ​
output ​
Output to save the clients.
INFO
Type: string
Default: 'clients'
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({ output: './axios' }),
],
})
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({ output: './axios' }),
],
})
groupBy ​
Group the clients 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 clients. {{tag}}
will be replaced by the current tagName.
Type: string
Example: clients/{{tag}}Controller
=> clients/PetController
Default: ${output}/{{tag}}Controller
exportAs ​
Name to be used for the export * as {{exportAs}} from './
Type: string
Default: '{{tag}}Service'
INFO
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(
{
output: './clients/axios',
groupBy: { type: 'tag', output: './clients/axios/{{tag}}Service' },
},
),
],
})
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(
{
output: './clients/axios',
groupBy: { type: 'tag', output: './clients/axios/{{tag}}Service' },
},
),
],
})
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 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(
{
clientImportPath: '../../client.ts',
},
),
],
})
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(
{
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>> {
...
}
INFO
Type: 'data' | 'full'
Default: 'data'
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(
{
dataReturnType: 'data',
},
),
],
})
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(
{
dataReturnType: 'data',
},
),
],
})
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(
{
dataReturnType: 'full',
},
),
],
})
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(
{
dataReturnType: 'full',
},
),
],
})
pathParamsType experimental ​
How to pass your pathParams.
'object'
will return the pathParams as an object. 'inline'
will return the pathParams as comma separated params.
type
export async function getPetById<TData>(
{ petId }: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>> {
...
}
export async function getPetById<TData>(
{ petId }: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>> {
...
}
export async function getPetById<TData>(
petId: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>> {
...
}
export async function getPetById<TData>(
petId: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>> {
...
}
INFO
Type: 'object' | 'inline'
Default: 'data'
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(
{
pathParamsType: 'object',
},
),
],
})
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(
{
pathParamsType: 'object',
},
),
],
})
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(
{
pathParamsType: 'inline',
},
),
],
})
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(
{
pathParamsType: 'inline',
},
),
],
})
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 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(
{
skipBy: [
{
type: 'tag',
pattern: 'store',
},
],
},
),
],
})
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(
{
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 client 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 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(
{
transformers: {
name: (name) => {
return `${name}Client`
},
},
},
),
],
})
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(
{
transformers: {
name: (name) => {
return `${name}Client`
},
},
},
),
],
})