Skip to content

Commit

Permalink
Merge pull request #1536 from kubb-labs/fix/1226-use-client-in-genera…
Browse files Browse the repository at this point in the history
…ted-clients

Use client in generated clients
  • Loading branch information
stijnvanhulle authored Jan 24, 2025
2 parents eda182f + cf6681e commit d89efb7
Show file tree
Hide file tree
Showing 257 changed files with 2,297 additions and 1,222 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-eagles-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-client": patch
---

support custom client in options
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ title: Changelog

# Changelog

## 3.5.5
- [`plugin-client`](/plugins/plugin-client): support custom client in options
- [`plugin-faker`](/plugins/plugin-zod): `faker.number.string` with default min `Number.MIN_VALUE` and max set to `Number.MAX_VALUE`

## 3.5.4
- [`plugin-zod`](/plugins/plugin-zod): Support uniqueItems in Zod

Expand Down
3 changes: 1 addition & 2 deletions docs/knowledge-base/base-url.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ export const axiosInstance = axios.create({
baseURL: 'https://localhost:8080/api/v1' // [!code ++]
})

export const axiosClient = async <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
}

export default axiosClient
```
```typescript twoslash [kubb.config.ts]
import { defineConfig } from '@kubb/core'
Expand Down
5 changes: 1 addition & 4 deletions docs/knowledge-base/fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export type ResponseConfig<TData = unknown> = {
statusText: string
}

export const fetchClient = async <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),
Expand All @@ -89,9 +89,6 @@ export const fetchClient = async <TData, TError = unknown, TVariables = unknown>
statusText: response.statusText,
}
}

export default fetchClient

```

## View the generated code
Expand Down
6 changes: 3 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"serve": "vitepress serve"
},
"dependencies": {
"@shikijs/vitepress-twoslash": "^2.0.3",
"@shikijs/vitepress-twoslash": "^2.1.0",
"mermaid": "^11.4.1",
"sitemap": "^8.0.0",
"vitepress": "^1.6.1",
"vitepress": "^1.6.3",
"vitepress-plugin-group-icons": "^1.3.5",
"vue": "^3.5.13"
},
Expand All @@ -44,7 +44,7 @@
"@kubb/plugin-zod": "workspace:*",
"@kubb/react": "workspace:*",
"@mermaid-js/mermaid-cli": "^11.4.2",
"@types/node": "^20.17.14",
"@types/node": "^20.17.16",
"@types/react": "catalog:",
"cross-env": "^7.0.3",
"react": "catalog:",
Expand Down
2 changes: 1 addition & 1 deletion docs/plugins/plugin-client/importPath.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ It will be used as `import client from '${client.importPath}'`.<br/>
It allows both relative and absolute path but be aware that we will not change the path.

> [!TIP]
> Use of default exports as `export default client = ()=>{}`
> Use of default exports as `export const client = ()=>{}`
| | |
|----------:|:-------------------------------|
Expand Down
4 changes: 1 addition & 3 deletions e2e/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ export const axiosInstance = axios.create({
headers: typeof AXIOS_HEADERS !== 'undefined' ? (JSON.parse(AXIOS_HEADERS) as AxiosHeaders) : undefined,
})

export const axiosClient = async <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
}

export default axiosClient
4 changes: 2 additions & 2 deletions examples/advanced/src/axios-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export const axiosInstance = axios.create({
headers: typeof AXIOS_HEADERS !== 'undefined' ? (JSON.parse(AXIOS_HEADERS) as AxiosHeaders) : undefined,
})

export const axiosClient = async <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
}

export default axiosClient
export default client
4 changes: 2 additions & 2 deletions examples/advanced/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export const axiosInstance = axios.create({
headers: typeof AXIOS_HEADERS !== 'undefined' ? (JSON.parse(AXIOS_HEADERS) as AxiosHeaders) : undefined,
})

export const axiosClient = async <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
}

export default axiosClient
export default client
11 changes: 8 additions & 3 deletions examples/advanced/src/gen/clients/axios/petService/addPet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ export function getAddPetUrl() {
* @summary Add a new pet to the store
* {@link /pet}
*/
export async function addPet({ data }: { data: AddPetMutationRequest }, config: Partial<RequestConfig<AddPetMutationRequest>> = {}) {
const res = await client<AddPetMutationResponse, ResponseErrorConfig<AddPet405>, AddPetMutationRequest>({
export async function addPet(
{ data }: { data: AddPetMutationRequest },
config: Partial<RequestConfig<AddPetMutationRequest>> & { client?: typeof client } = {},
) {
const { client: request = client, ...requestConfig } = config

const res = await request<AddPetMutationResponse, ResponseErrorConfig<AddPet405>, AddPetMutationRequest>({
method: 'POST',
url: getAddPetUrl().toString(),
data,
...config,
...requestConfig,
})
return res
}
10 changes: 6 additions & 4 deletions examples/advanced/src/gen/clients/axios/petService/deletePet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export function getDeletePetUrl({ petId }: { petId: DeletePetPathParams['petId']
*/
export async function deletePet(
{ petId, headers }: { petId: DeletePetPathParams['petId']; headers?: DeletePetHeaderParams },
config: Partial<RequestConfig> = {},
config: Partial<RequestConfig> & { client?: typeof client } = {},
) {
const res = await client<DeletePetMutationResponse, ResponseErrorConfig<DeletePet400>, unknown>({
const { client: request = client, ...requestConfig } = config

const res = await request<DeletePetMutationResponse, ResponseErrorConfig<DeletePet400>, unknown>({
method: 'DELETE',
url: getDeletePetUrl({ petId }).toString(),
headers: { ...headers, ...config.headers },
...config,
headers: { ...headers, ...requestConfig.headers },
...requestConfig,
})
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ export function getFindPetsByStatusUrl({ step_id }: { step_id: FindPetsByStatusP
* @summary Finds Pets by status
* {@link /pet/findByStatus/:step_id}
*/
export async function findPetsByStatus({ step_id }: { step_id: FindPetsByStatusPathParams['step_id'] }, config: Partial<RequestConfig> = {}) {
const res = await client<FindPetsByStatusQueryResponse, ResponseErrorConfig<FindPetsByStatus400>, unknown>({
export async function findPetsByStatus(
{ step_id }: { step_id: FindPetsByStatusPathParams['step_id'] },
config: Partial<RequestConfig> & { client?: typeof client } = {},
) {
const { client: request = client, ...requestConfig } = config

const res = await request<FindPetsByStatusQueryResponse, ResponseErrorConfig<FindPetsByStatus400>, unknown>({
method: 'GET',
url: getFindPetsByStatusUrl({ step_id }).toString(),
...config,
...requestConfig,
})
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ export function getFindPetsByTagsUrl() {
*/
export async function findPetsByTags(
{ headers, params }: { headers: FindPetsByTagsHeaderParams; params?: FindPetsByTagsQueryParams },
config: Partial<RequestConfig> = {},
config: Partial<RequestConfig> & { client?: typeof client } = {},
) {
const res = await client<FindPetsByTagsQueryResponse, ResponseErrorConfig<FindPetsByTags400>, unknown>({
const { client: request = client, ...requestConfig } = config

const res = await request<FindPetsByTagsQueryResponse, ResponseErrorConfig<FindPetsByTags400>, unknown>({
method: 'GET',
url: getFindPetsByTagsUrl().toString(),
params,
headers: { ...headers, ...config.headers },
...config,
headers: { ...headers, ...requestConfig.headers },
...requestConfig,
})
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ export function getGetPetByIdUrl({ petId }: { petId: GetPetByIdPathParams['petId
* @summary Find pet by ID
* {@link /pet/:petId}
*/
export async function getPetById({ petId }: { petId: GetPetByIdPathParams['petId'] }, config: Partial<RequestConfig> = {}) {
const res = await client<GetPetByIdQueryResponse, ResponseErrorConfig<GetPetById400 | GetPetById404>, unknown>({
export async function getPetById({ petId }: { petId: GetPetByIdPathParams['petId'] }, config: Partial<RequestConfig> & { client?: typeof client } = {}) {
const { client: request = client, ...requestConfig } = config

const res = await request<GetPetByIdQueryResponse, ResponseErrorConfig<GetPetById400 | GetPetById404>, unknown>({
method: 'GET',
url: getGetPetByIdUrl({ petId }).toString(),
...config,
...requestConfig,
})
return res
}
11 changes: 8 additions & 3 deletions examples/advanced/src/gen/clients/axios/petService/updatePet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ export function getUpdatePetUrl() {
* @summary Update an existing pet
* {@link /pet}
*/
export async function updatePet({ data }: { data: UpdatePetMutationRequest }, config: Partial<RequestConfig<UpdatePetMutationRequest>> = {}) {
const res = await client<UpdatePetMutationResponse, ResponseErrorConfig<UpdatePet400 | UpdatePet404 | UpdatePet405>, UpdatePetMutationRequest>({
export async function updatePet(
{ data }: { data: UpdatePetMutationRequest },
config: Partial<RequestConfig<UpdatePetMutationRequest>> & { client?: typeof client } = {},
) {
const { client: request = client, ...requestConfig } = config

const res = await request<UpdatePetMutationResponse, ResponseErrorConfig<UpdatePet400 | UpdatePet404 | UpdatePet405>, UpdatePetMutationRequest>({
method: 'PUT',
url: getUpdatePetUrl().toString(),
data,
...config,
...requestConfig,
})
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export function getUpdatePetWithFormUrl({ petId }: { petId: UpdatePetWithFormPat
*/
export async function updatePetWithForm(
{ petId, params }: { petId: UpdatePetWithFormPathParams['petId']; params?: UpdatePetWithFormQueryParams },
config: Partial<RequestConfig> = {},
config: Partial<RequestConfig> & { client?: typeof client } = {},
) {
const res = await client<UpdatePetWithFormMutationResponse, ResponseErrorConfig<UpdatePetWithForm405>, unknown>({
const { client: request = client, ...requestConfig } = config

const res = await request<UpdatePetWithFormMutationResponse, ResponseErrorConfig<UpdatePetWithForm405>, unknown>({
method: 'POST',
url: getUpdatePetWithFormUrl({ petId }).toString(),
params,
...config,
...requestConfig,
})
return res
}
10 changes: 6 additions & 4 deletions examples/advanced/src/gen/clients/axios/petService/uploadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ export function getUploadFileUrl({ petId }: { petId: UploadFilePathParams['petId
*/
export async function uploadFile(
{ petId, data, params }: { petId: UploadFilePathParams['petId']; data?: UploadFileMutationRequest; params?: UploadFileQueryParams },
config: Partial<RequestConfig<UploadFileMutationRequest>> = {},
config: Partial<RequestConfig<UploadFileMutationRequest>> & { client?: typeof client } = {},
) {
const res = await client<UploadFileMutationResponse, ResponseErrorConfig<Error>, UploadFileMutationRequest>({
const { client: request = client, ...requestConfig } = config

const res = await request<UploadFileMutationResponse, ResponseErrorConfig<Error>, UploadFileMutationRequest>({
method: 'POST',
url: getUploadFileUrl({ petId }).toString(),
params,
data,
headers: { 'Content-Type': 'application/octet-stream', ...config.headers },
...config,
headers: { 'Content-Type': 'application/octet-stream', ...requestConfig.headers },
...requestConfig,
})
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ export async function createPets(
headers,
params,
}: { uuid: CreatePetsPathParams['uuid']; data: CreatePetsMutationRequest; headers: CreatePetsHeaderParams; params?: CreatePetsQueryParams },
config: Partial<RequestConfig<CreatePetsMutationRequest>> = {},
config: Partial<RequestConfig<CreatePetsMutationRequest>> & { client?: typeof client } = {},
) {
const res = await client<CreatePetsMutationResponse, ResponseErrorConfig<Error>, CreatePetsMutationRequest>({
const { client: request = client, ...requestConfig } = config

const res = await request<CreatePetsMutationResponse, ResponseErrorConfig<Error>, CreatePetsMutationRequest>({
method: 'POST',
url: getCreatePetsUrl({ uuid }).toString(),
params,
data,
headers: { ...headers, ...config.headers },
...config,
headers: { ...headers, ...requestConfig.headers },
...requestConfig,
})
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ export function getCreateUserUrl() {
* @summary Create user
* {@link /user}
*/
export async function createUser({ data }: { data?: CreateUserMutationRequest }, config: Partial<RequestConfig<CreateUserMutationRequest>> = {}) {
const res = await client<CreateUserMutationResponse, ResponseErrorConfig<Error>, CreateUserMutationRequest>({
export async function createUser(
{ data }: { data?: CreateUserMutationRequest },
config: Partial<RequestConfig<CreateUserMutationRequest>> & { client?: typeof client } = {},
) {
const { client: request = client, ...requestConfig } = config

const res = await request<CreateUserMutationResponse, ResponseErrorConfig<Error>, CreateUserMutationRequest>({
method: 'POST',
url: getCreateUserUrl().toString(),
data,
...config,
...requestConfig,
})
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export function getCreateUsersWithListInputUrl() {
*/
export async function createUsersWithListInput(
{ data }: { data?: CreateUsersWithListInputMutationRequest },
config: Partial<RequestConfig<CreateUsersWithListInputMutationRequest>> = {},
config: Partial<RequestConfig<CreateUsersWithListInputMutationRequest>> & { client?: typeof client } = {},
) {
const res = await client<CreateUsersWithListInputMutationResponse, ResponseErrorConfig<Error>, CreateUsersWithListInputMutationRequest>({
const { client: request = client, ...requestConfig } = config

const res = await request<CreateUsersWithListInputMutationResponse, ResponseErrorConfig<Error>, CreateUsersWithListInputMutationRequest>({
method: 'POST',
url: getCreateUsersWithListInputUrl().toString(),
data,
...config,
...requestConfig,
})
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ export function getDeleteUserUrl({ username }: { username: DeleteUserPathParams[
* @summary Delete user
* {@link /user/:username}
*/
export async function deleteUser({ username }: { username: DeleteUserPathParams['username'] }, config: Partial<RequestConfig> = {}) {
const res = await client<DeleteUserMutationResponse, ResponseErrorConfig<DeleteUser400 | DeleteUser404>, unknown>({
export async function deleteUser(
{ username }: { username: DeleteUserPathParams['username'] },
config: Partial<RequestConfig> & { client?: typeof client } = {},
) {
const { client: request = client, ...requestConfig } = config

const res = await request<DeleteUserMutationResponse, ResponseErrorConfig<DeleteUser400 | DeleteUser404>, unknown>({
method: 'DELETE',
url: getDeleteUserUrl({ username }).toString(),
...config,
...requestConfig,
})
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ export function getGetUserByNameUrl({ username }: { username: GetUserByNamePathP
* @summary Get user by user name
* {@link /user/:username}
*/
export async function getUserByName({ username }: { username: GetUserByNamePathParams['username'] }, config: Partial<RequestConfig> = {}) {
const res = await client<GetUserByNameQueryResponse, ResponseErrorConfig<GetUserByName400 | GetUserByName404>, unknown>({
export async function getUserByName(
{ username }: { username: GetUserByNamePathParams['username'] },
config: Partial<RequestConfig> & { client?: typeof client } = {},
) {
const { client: request = client, ...requestConfig } = config

const res = await request<GetUserByNameQueryResponse, ResponseErrorConfig<GetUserByName400 | GetUserByName404>, unknown>({
method: 'GET',
url: getGetUserByNameUrl({ username }).toString(),
...config,
...requestConfig,
})
return res
}
Loading

0 comments on commit d89efb7

Please sign in to comment.