Skip to content

@kubb/swagger-client 🦙 ​

With the Swagger client plugin you can create Axios API calls.

Installation ​

shell
bun add @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
shell
pnpm add @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
shell
npm install @kubb/swagger-client @kubb/swagger-ts @kubb/swagger
shell
yarn add @kubb/swagger-client @kubb/swagger-ts @kubb/swagger

Options ​

output ​

output.path ​

Output to save the clients.

INFO

Type: string
Default: 'clients'

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  output: {
    path: './axios',
  },
})

output.exportAs ​

Name to be used for the export * as from './'

INFO

Type: string

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  output: {
    path: './axios',
    exportAs: 'clients',
  },
})

output.extName ​

Add an extension to the generated imports and exports, default it will not use an extension

INFO

Type: string

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  output: {
    path: './axios',
    extName: '.js',
  },
})

output.exportType ​

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

INFO

Type: 'barrel' | 'barrelNamed' | false

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  output: {
    path: './client',
    exportType: 'barrel',
  },
})

group ​

Group the clients based on the provided name.

group.type ​

Tag will group based on the operation tag inside the Swagger file.

Type: 'tag'
Required: true

group.output ​

TIP

When defining a custom output path, you should also update output.path to contain the same root path.

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

group.exportAs ​

Name to be used for the export * as {{exportAs}} from './

Type: string
Default: '{{tag}}Service'

INFO

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  output: {
    path: './clients/axios'
  },
  group: { type: 'tag', output: './clients/axios/{{tag}}Service' },
})

client ​

client.importPath ​

Path to the client import path that will be used to do the API calls.
It will be used as import client from '${client.importPath}'.
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'

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  client: {
    importPath: '../../client.ts',
  },
})

dataReturnType ​

ReturnType that needs to be used when calling client().

'data' will return ResponseConfig[data].
'full' will return ResponseConfig.

TYPE

typescript
export async function getPetById<TData>(
  petId: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>["data"]> {
  ...
}
typescript
export async function getPetById<TData>(
  petId: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>> {
  ...
}

INFO

Type: 'data' | 'full'
Default: 'data'

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  dataReturnType: 'data'
})

pathParamsType ​

How to pass your pathParams.

'object' will return the pathParams as an object.
'inline' will return the pathParams as comma separated params.

TYPE

typescript
export async function getPetById<TData>(
  { petId }: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>> {
  ...
}
typescript
export async function getPetById<TData>(
  petId: GetPetByIdPathParams,
): Promise<ResponseConfig<TData>> {
  ...
}

INFO

Type: 'object' | 'inline'
Default: 'data'

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  pathParamsType: 'object',
})

include ​

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

TYPE

typescript
export type Include = {
  type: 'tag' | 'operationId' | 'path' | 'method'
  pattern: string | RegExp
}

INFO

Type: Array<Include>

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  include: [
    {
      type: 'tag',
      pattern: 'store',
    },
  ],
})

exclude ​

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

TYPE

typescript
export type Exclude = {
  type: 'tag' | 'operationId' | 'path' | 'method'
  pattern: string | RegExp
}

INFO

Type: Array<Exclude>

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  exclude: [
    {
      type: 'tag',
      pattern: 'store',
    },
  ],
})

override ​

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

TYPE

typescript
export type Override = {
  type: 'tag' | 'operationId' | 'path' | 'method'
  pattern: string | RegExp
  options: PluginOptions
}

INFO

Type: Array<Override>

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  override: [
    {
      type: 'tag',
      pattern: 'pet',
      options: {
        dataReturnType: "full"
      },
    },
  ],
})

transformers ​

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, type?: "function" | "type" | "file" ) => string

typescript
import { pluginClient } from '@kubb/swagger-client'

const plugin = pluginClient({
  transformers: {
    name: (name) => {
      return `${name}Client`
    },
  },
})

templates ​

Make it possible to override one of the templates.

TIP

See templates for more information about creating templates.
Set false to disable a template.

TYPE

typescript
import type { Client, Operations } from '@kubb/swagger-client/components'

export type Templates = {
  operations?: typeof Operations.templates | false
  client?: typeof Client.templates | false
}

INFO

Type: Templates

tsx
import { pluginClient } from '@kubb/swagger-client'
import { Parser, File, Function } from '@kubb/react'
import { Client } from '@kubb/swagger-client/components'
import React from 'react'

export const templates = {
  ...Client.templates,
  default: function ({ name, generics, returnType, params, JSDoc, client }: React.ComponentProps<typeof Client.templates.default>) {
    const clientParams = [client.path.template, client.withData ? 'data' : undefined, 'options'].filter(Boolean).join(', ')

    return (
      <>
        <File.Import name="axios" path="axios" />
        <Function name={name} async export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
          {`return axios.${client.method}(${clientParams})`}
        </Function>
    </>
  )
  },
} as const

const plugin = pluginClient({
  templates: {
    client: templates,
  },
})

Example ​

typescript
import { defineConfig } from '@kubb/core'
import { pluginOas } from '@kubb/plugin-oas'
import { pluginTs } from '@kubb/swagger-ts'
import { pluginClient } from '@kubb/swagger-client'

export default defineConfig({
  input: {
    path: './petStore.yaml',
  },
  output: {
    path: './src/gen',
  },
  plugins: [
    pluginOas(),
    pluginTs(),
    pluginClient({
      output: {
        path: './clients/axios',
      },
      group: {
        type: 'tag',
        output: './clients/axios/{{tag}}Service',
      },
      exclude: [
        {
          type: 'tag',
          pattern: 'store',
        },
      ],
      pathParamsType: "object",
      dataReturnType: 'full',
    }),
  ],
})

Released under the MIT License.