Use of your own baseURL
Specify a baseURL in three ways: use a custom client, reference the serverIndex from your OpenAPI spec, or set it in the baseURL config option.
Use custom client
When defining your own client, set a baseURL that applies to every HTTP call.
typescript
import axios from 'axios'
import type { AxiosError, AxiosHeaders, AxiosRequestConfig, AxiosResponse } from 'axios'
declare const AXIOS_BASE: string
declare const AXIOS_HEADERS: string
/**
* Subset of AxiosRequestConfig
*/
export type RequestConfig<TData = unknown> = {
url?: string
method: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE'
params?: unknown
data?: TData
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'
signal?: AbortSignal
headers?: AxiosRequestConfig['headers']
}
/**
* Subset of AxiosResponse
*/
export type ResponseConfig<TData = unknown> = {
data: TData
status: number
statusText: string
headers?: AxiosResponse['headers']
}
export const axiosInstance = axios.create({
baseURL: 'https://localhost:8080/api/v1',
})
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 promise = axiosInstance.request<TVariables, ResponseConfig<TData>>({ ...config }).catch((e: AxiosError<TError>) => {
throw e
})
return promise
}typescript
import { } from 'kubb'
import { } from '@kubb/plugin-client'
import { } from '@kubb/adapter-oas'
import { } from '@kubb/plugin-ts'
export default ({
: {
: './petStore.yaml',
},
: {
: './src/gen',
},
: (),
: [
(),
({
: '../../client.ts',
}),
],
})Use serverIndex
Reuse the server URL from your Swagger/OpenAPI spec file by defining which index to use.
yaml
openapi: 3.0.3
info:
title: Swagger Example
description:
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.0
servers:
- url: http://petstore.swagger.io/api
- url: http://localhost:3000typescript
import { } from 'kubb'
import { } from '@kubb/adapter-oas'
import { } from '@kubb/plugin-client'
export default ({
: {
: './petStore.yaml',
},
: {
: './src/gen',
},
: (),
: [()],
})Use baseURL
Set the baseURL in your config.
typescript
import { } from 'kubb'
import { } from '@kubb/adapter-oas'
import { } from '@kubb/plugin-client'
import { } from '@kubb/plugin-react-query'
export default ({
: {
: './petStore.yaml',
},
: {
: './src/gen',
},
: (),
: [
({
: 'https://localhost:8080/api/v1',
}),
({
: {
: 'https://localhost:8080/api/v1',
},
}),
],
})