Use of Fetch @kubb/plugin-client
By default, @kubb/plugin-client uses the Axios client from @kubb/plugin-client/templates/axios, which is based on the Axios instance interface.
In some cases, you may want a custom client. For example, use Fetch or Ky.
Create kubb.config.ts
Set importPath to a relative path, import alias, or library (default: @kubb/plugin-client/templates/axios).
typescript
import { defineConfig } from '@kubb/core'
import { pluginClient } from '@kubb/plugin-client'
import { pluginOas } from '@kubb/plugin-oas'
import { pluginTs } from '@kubb/plugin-ts'
export default defineConfig(() => {
return {
root: '.',
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
clean: true,
},
plugins: [
pluginOas({
validate: false,
generators: [],
}),
pluginTs({
output: { path: 'models.ts' },
}),
pluginClient({
output: {
path: '.',
},
importPath: '../client.ts',
}),
],
}
})Add client.ts
Every POST, PUT, GET, PATCH, and DELETE request uses the importPath and invokes the default export with a configuration shaped by RequestConfig, modeled after the AxiosRequest interface/config.
IMPORTANT
The client must return an object in the shape of ResponseConfig, even if you change dataReturnType with dataReturnType: 'data'.
typescript
export type RequestConfig<TData = unknown> = {
url?: string
method: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE'
params?: object
data?: TData | FormData
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'
signal?: AbortSignal
headers?: HeadersInit
}
export type ResponseConfig<TData = unknown> = {
data: TData
status: number
statusText: string
}
export type Client = <TData, _TError = unknown, TVariables = unknown>(config: RequestConfig<TVariables>) => Promise<ResponseConfig<TData>>
export const client = async <TData, TError = unknown, TVariables = unknown>(config: RequestConfig<TVariables>): Promise<ResponseConfig<TData>> => {
const response = await fetch('https://example.org/post', {
method: config.method.toUpperCase(),
body: JSON.stringify(config.data),
signal: config.signal,
headers: config.headers,
})
const data = await response.json()
return {
data,
status: response.status,
statusText: response.statusText,
}
}View generated code
typescript
import client from '../client.ts'
import type { ResponseConfig } from '../client.ts'
import type { GetPetByIdQueryResponse, GetPetByIdPathParams } from './models.ts'
/**
* @description Returns a single pet
* @summary Find pet by ID
* @link /pet/:petId
*/
export async function getPetById(
petId: GetPetByIdPathParams['petId'],
options: Partial<Parameters<typeof client>[0]> = {},
): Promise<ResponseConfig<GetPetByIdQueryResponse>['data']> {
const res = await client<GetPetByIdQueryResponse>({ method: 'get', url: `/pet/${petId}`, ...options })
return res.data
}