Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow exporting custom client fetch function in react-query #1530

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/cool-apricots-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@kubb/plugin-svelte-query": patch
"@kubb/plugin-react-query": patch
"@kubb/plugin-solid-query": patch
"@kubb/plugin-vue-query": patch
"@kubb/plugin-client": patch
"@kubb/plugin-swr": patch
---

allow exporting custom client fetch function in react-query
9 changes: 9 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ title: Changelog

# Changelog

## 3.5.3
- [`plugin-client`](/plugins/plugin-client): allow exporting custom client fetch function and use generated fetch when `pluginClient` is available
- [`plugin-react-query`](/plugins/plugin-react-query/): allow exporting custom client fetch function and use generated fetch when `pluginClient` is available
- [`plugin-svelte-query`](/plugins/plugin-svelte-query/): allow exporting custom client fetch function and use generated fetch when `pluginClient` is available
- [`plugin-vue-query`](/plugins/plugin-vue-query/): allow exporting custom client fetch function and use generated fetch when `pluginClient` is available
- [`plugin-solid-query`](/plugins/plugin-solid-query/): allow exporting custom client fetch function and use generated fetch when `pluginClient` is available
- [`plugin-swr`](/plugins/plugin-swr/): allow exporting custom client fetch function and use generated fetch when `pluginClient` is available


## 3.5.2
- [`plugin-faker`](/plugins/plugin-faker): `faker.number.float` with default min `Number.MIN_VALUE` and max set to `Number.MAX_VALUE`.
- [`plugin-oas`](/plugins/plugin-oas): remove duplicated keys when using `allOf` and applying required on fields
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced/configs/kubb.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ export default defineConfig(() => {
group: { type: 'tag' },
client: {
importPath: '../../../../swr-client.ts',
dataReturnType: 'data',
dataReturnType: 'full',
baseURL: 'https://petstore3.swagger.io/api/v3',
},
paramsType: 'object',
parser: 'zod',
pathParamsType: 'object',
transformers: {
name(name, type) {
return `${name}SWR`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseConfig, ResponseErrorConfig } from '../../../../tanstack-query-client'
import type { AddPetMutationRequest, AddPetMutationResponse, AddPet405 } from '../../../models/ts/petController/AddPet.ts'
import type { UseMutationOptions } from '@tanstack/react-query'
import { addPetMutationResponseSchema } from '../../../zod/petController/addPetSchema.ts'
import { addPet } from '../../axios/petService/addPet.ts'
import { useMutation } from '@tanstack/react-query'

export const addPetMutationKey = () => [{ url: '/pet' }] as const

export type AddPetMutationKey = ReturnType<typeof addPetMutationKey>

/**
* @description Add a new pet to the store
* @summary Add a new pet to the store
* {@link /pet}
*/
async function addPet({ data }: { data: AddPetMutationRequest }, config: Partial<RequestConfig<AddPetMutationRequest>> = {}) {
const res = await client<AddPetMutationResponse, ResponseErrorConfig<AddPet405>, AddPetMutationRequest>({ method: 'POST', url: '/pet', data, ...config })
return { ...res, data: addPetMutationResponseSchema.parse(res.data) }
}

/**
* @description Add a new pet to the store
* @summary Add a new pet to the store
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseConfig, ResponseErrorConfig } from '../../../../tanstack-query-client'
import type { DeletePetMutationResponse, DeletePetPathParams, DeletePetHeaderParams, DeletePet400 } from '../../../models/ts/petController/DeletePet.ts'
import type { UseMutationOptions } from '@tanstack/react-query'
import { deletePetMutationResponseSchema } from '../../../zod/petController/deletePetSchema.ts'
import { deletePet } from '../../axios/petService/deletePet.ts'
import { useMutation } from '@tanstack/react-query'

export const deletePetMutationKey = () => [{ url: '/pet/{petId}' }] as const

export type DeletePetMutationKey = ReturnType<typeof deletePetMutationKey>

/**
* @description delete a pet
* @summary Deletes a pet
* {@link /pet/:petId}
*/
async function deletePet({ petId, headers }: { petId: DeletePetPathParams['petId']; headers?: DeletePetHeaderParams }, config: Partial<RequestConfig> = {}) {
const res = await client<DeletePetMutationResponse, ResponseErrorConfig<DeletePet400>, unknown>({
method: 'DELETE',
url: `/pet/${petId}`,
headers: { ...headers, ...config.headers },
...config,
})
return { ...res, data: deletePetMutationResponseSchema.parse(res.data) }
}

/**
* @description delete a pet
* @summary Deletes a pet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseErrorConfig, ResponseConfig } from '../../../../tanstack-query-client'
import type { QueryKey, QueryObserverOptions, UseQueryResult } from '../../../../tanstack-query-hook'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusPathParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts'
import { queryOptions, useQuery } from '../../../../tanstack-query-hook'
import { findPetsByStatusQueryResponseSchema } from '../../../zod/petController/findPetsByStatusSchema.ts'
import { findPetsByStatus } from '../../axios/petService/findPetsByStatus.ts'

export const findPetsByStatusQueryKey = ({ step_id }: { step_id: FindPetsByStatusPathParams['step_id'] }) =>
[{ url: '/pet/findByStatus/:step_id', params: { step_id: step_id } }] as const

export type FindPetsByStatusQueryKey = ReturnType<typeof findPetsByStatusQueryKey>

/**
* @description Multiple status values can be provided with comma separated strings
* @summary Finds Pets by status
* {@link /pet/findByStatus/:step_id}
*/
async function findPetsByStatus({ step_id }: { step_id: FindPetsByStatusPathParams['step_id'] }, config: Partial<RequestConfig> = {}) {
const res = await client<FindPetsByStatusQueryResponse, ResponseErrorConfig<FindPetsByStatus400>, unknown>({
method: 'GET',
url: `/pet/findByStatus/${step_id}`,
...config,
})
return { ...res, data: findPetsByStatusQueryResponseSchema.parse(res.data) }
}

export function findPetsByStatusQueryOptions({ step_id }: { step_id: FindPetsByStatusPathParams['step_id'] }, config: Partial<RequestConfig> = {}) {
const queryKey = findPetsByStatusQueryKey({ step_id })
return queryOptions<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseErrorConfig, ResponseConfig } from '../../../../tanstack-query-client'
import type { QueryKey, QueryObserverOptions, UseQueryResult } from '../../../../tanstack-query-hook'
import type {
Expand All @@ -8,31 +7,12 @@ import type {
FindPetsByTags400,
} from '../../../models/ts/petController/FindPetsByTags.ts'
import { queryOptions, useQuery } from '../../../../tanstack-query-hook'
import { findPetsByTagsQueryResponseSchema } from '../../../zod/petController/findPetsByTagsSchema.ts'
import { findPetsByTags } from '../../axios/petService/findPetsByTags.ts'

export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: '/pet/findByTags' }, ...(params ? [params] : [])] as const

export type FindPetsByTagsQueryKey = ReturnType<typeof findPetsByTagsQueryKey>

/**
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* @summary Finds Pets by tags
* {@link /pet/findByTags}
*/
async function findPetsByTags(
{ headers, params }: { headers: FindPetsByTagsHeaderParams; params?: FindPetsByTagsQueryParams },
config: Partial<RequestConfig> = {},
) {
const res = await client<FindPetsByTagsQueryResponse, ResponseErrorConfig<FindPetsByTags400>, unknown>({
method: 'GET',
url: '/pet/findByTags',
params,
headers: { ...headers, ...config.headers },
...config,
})
return { ...res, data: findPetsByTagsQueryResponseSchema.parse(res.data) }
}

export function findPetsByTagsQueryOptions(
{ headers, params }: { headers: FindPetsByTagsHeaderParams; params?: FindPetsByTagsQueryParams },
config: Partial<RequestConfig> = {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseErrorConfig, ResponseConfig } from '../../../../tanstack-query-client'
import type { InfiniteData, QueryKey, InfiniteQueryObserverOptions, UseInfiniteQueryResult } from '../../../../tanstack-query-hook'
import type {
Expand All @@ -8,31 +7,12 @@ import type {
FindPetsByTags400,
} from '../../../models/ts/petController/FindPetsByTags.ts'
import { infiniteQueryOptions, useInfiniteQuery } from '../../../../tanstack-query-hook'
import { findPetsByTagsQueryResponseSchema } from '../../../zod/petController/findPetsByTagsSchema.ts'
import { findPetsByTags } from '../../axios/petService/findPetsByTags.ts'

export const findPetsByTagsInfiniteQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: '/pet/findByTags' }, ...(params ? [params] : [])] as const

export type FindPetsByTagsInfiniteQueryKey = ReturnType<typeof findPetsByTagsInfiniteQueryKey>

/**
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* @summary Finds Pets by tags
* {@link /pet/findByTags}
*/
async function findPetsByTags(
{ headers, params }: { headers: FindPetsByTagsHeaderParams; params?: FindPetsByTagsQueryParams },
config: Partial<RequestConfig> = {},
) {
const res = await client<FindPetsByTagsQueryResponse, ResponseErrorConfig<FindPetsByTags400>, unknown>({
method: 'GET',
url: '/pet/findByTags',
params,
headers: { ...headers, ...config.headers },
...config,
})
return { ...res, data: findPetsByTagsQueryResponseSchema.parse(res.data) }
}

export function findPetsByTagsInfiniteQueryOptions(
{ headers, params }: { headers: FindPetsByTagsHeaderParams; params?: FindPetsByTagsQueryParams },
config: Partial<RequestConfig> = {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseErrorConfig, ResponseConfig } from '../../../../tanstack-query-client'
import type { QueryKey, QueryObserverOptions, UseQueryResult } from '../../../../tanstack-query-hook'
import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from '../../../models/ts/petController/GetPetById.ts'
import { queryOptions, useQuery } from '../../../../tanstack-query-hook'
import { getPetByIdQueryResponseSchema } from '../../../zod/petController/getPetByIdSchema.ts'
import { getPetById } from '../../axios/petService/getPetById.ts'

export const getPetByIdQueryKey = ({ petId }: { petId: GetPetByIdPathParams['petId'] }) => [{ url: '/pet/:petId', params: { petId: petId } }] as const

export type GetPetByIdQueryKey = ReturnType<typeof getPetByIdQueryKey>

/**
* @description Returns a single pet
* @summary Find pet by ID
* {@link /pet/:petId}
*/
async function getPetById({ petId }: { petId: GetPetByIdPathParams['petId'] }, config: Partial<RequestConfig> = {}) {
const res = await client<GetPetByIdQueryResponse, ResponseErrorConfig<GetPetById400 | GetPetById404>, unknown>({
method: 'GET',
url: `/pet/${petId}`,
...config,
})
return { ...res, data: getPetByIdQueryResponseSchema.parse(res.data) }
}

export function getPetByIdQueryOptions({ petId }: { petId: GetPetByIdPathParams['petId'] }, config: Partial<RequestConfig> = {}) {
const queryKey = getPetByIdQueryKey({ petId })
return queryOptions<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseConfig, ResponseErrorConfig } from '../../../../tanstack-query-client'
import type {
UpdatePetMutationRequest,
Expand All @@ -8,28 +7,13 @@ import type {
UpdatePet405,
} from '../../../models/ts/petController/UpdatePet.ts'
import type { UseMutationOptions } from '@tanstack/react-query'
import { updatePetMutationResponseSchema } from '../../../zod/petController/updatePetSchema.ts'
import { updatePet } from '../../axios/petService/updatePet.ts'
import { useMutation } from '@tanstack/react-query'

export const updatePetMutationKey = () => [{ url: '/pet' }] as const

export type UpdatePetMutationKey = ReturnType<typeof updatePetMutationKey>

/**
* @description Update an existing pet by Id
* @summary Update an existing pet
* {@link /pet}
*/
async function updatePet({ data }: { data: UpdatePetMutationRequest }, config: Partial<RequestConfig<UpdatePetMutationRequest>> = {}) {
const res = await client<UpdatePetMutationResponse, ResponseErrorConfig<UpdatePet400 | UpdatePet404 | UpdatePet405>, UpdatePetMutationRequest>({
method: 'PUT',
url: '/pet',
data,
...config,
})
return { ...res, data: updatePetMutationResponseSchema.parse(res.data) }
}

/**
* @description Update an existing pet by Id
* @summary Update an existing pet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseConfig, ResponseErrorConfig } from '../../../../tanstack-query-client'
import type {
UpdatePetWithFormMutationResponse,
Expand All @@ -7,30 +6,13 @@ import type {
UpdatePetWithForm405,
} from '../../../models/ts/petController/UpdatePetWithForm.ts'
import type { UseMutationOptions } from '@tanstack/react-query'
import { updatePetWithFormMutationResponseSchema } from '../../../zod/petController/updatePetWithFormSchema.ts'
import { updatePetWithForm } from '../../axios/petService/updatePetWithForm.ts'
import { useMutation } from '@tanstack/react-query'

export const updatePetWithFormMutationKey = () => [{ url: '/pet/{petId}' }] as const

export type UpdatePetWithFormMutationKey = ReturnType<typeof updatePetWithFormMutationKey>

/**
* @summary Updates a pet in the store with form data
* {@link /pet/:petId}
*/
async function updatePetWithForm(
{ petId, params }: { petId: UpdatePetWithFormPathParams['petId']; params?: UpdatePetWithFormQueryParams },
config: Partial<RequestConfig> = {},
) {
const res = await client<UpdatePetWithFormMutationResponse, ResponseErrorConfig<UpdatePetWithForm405>, unknown>({
method: 'POST',
url: `/pet/${petId}`,
params,
...config,
})
return { ...res, data: updatePetWithFormMutationResponseSchema.parse(res.data) }
}

/**
* @summary Updates a pet in the store with form data
* {@link /pet/:petId}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseConfig, ResponseErrorConfig } from '../../../../tanstack-query-client'
import type {
UploadFileMutationRequest,
Expand All @@ -7,32 +6,13 @@ import type {
UploadFileQueryParams,
} from '../../../models/ts/petController/UploadFile.ts'
import type { UseMutationOptions } from '@tanstack/react-query'
import { uploadFileMutationResponseSchema } from '../../../zod/petController/uploadFileSchema.ts'
import { uploadFile } from '../../axios/petService/uploadFile.ts'
import { useMutation } from '@tanstack/react-query'

export const uploadFileMutationKey = () => [{ url: '/pet/{petId}/uploadImage' }] as const

export type UploadFileMutationKey = ReturnType<typeof uploadFileMutationKey>

/**
* @summary uploads an image
* {@link /pet/:petId/uploadImage}
*/
async function uploadFile(
{ petId, data, params }: { petId: UploadFilePathParams['petId']; data?: UploadFileMutationRequest; params?: UploadFileQueryParams },
config: Partial<RequestConfig<UploadFileMutationRequest>> = {},
) {
const res = await client<UploadFileMutationResponse, ResponseErrorConfig<Error>, UploadFileMutationRequest>({
method: 'POST',
url: `/pet/${petId}/uploadImage`,
params,
data,
headers: { 'Content-Type': 'application/octet-stream', ...config.headers },
...config,
})
return { ...res, data: uploadFileMutationResponseSchema.parse(res.data) }
}

/**
* @summary uploads an image
* {@link /pet/:petId/uploadImage}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import client from '../../../../tanstack-query-client'
import type { RequestConfig, ResponseConfig, ResponseErrorConfig } from '../../../../tanstack-query-client'
import type {
CreatePetsMutationRequest,
Expand All @@ -8,37 +7,13 @@ import type {
CreatePetsHeaderParams,
} from '../../../models/ts/petsController/CreatePets.ts'
import type { UseMutationOptions } from '@tanstack/react-query'
import { createPetsMutationResponseSchema } from '../../../zod/petsController/createPetsSchema.ts'
import { createPets } from '../../axios/petsService/createPets.ts'
import { useMutation } from '@tanstack/react-query'

export const createPetsMutationKey = () => [{ url: '/pets/{uuid}' }] as const

export type CreatePetsMutationKey = ReturnType<typeof createPetsMutationKey>

/**
* @summary Create a pet
* {@link /pets/:uuid}
*/
async function createPets(
{
uuid,
data,
headers,
params,
}: { uuid: CreatePetsPathParams['uuid']; data: CreatePetsMutationRequest; headers: CreatePetsHeaderParams; params?: CreatePetsQueryParams },
config: Partial<RequestConfig<CreatePetsMutationRequest>> = {},
) {
const res = await client<CreatePetsMutationResponse, ResponseErrorConfig<Error>, CreatePetsMutationRequest>({
method: 'POST',
url: `/pets/${uuid}`,
params,
data,
headers: { ...headers, ...config.headers },
...config,
})
return { ...res, data: createPetsMutationResponseSchema.parse(res.data) }
}

/**
* @summary Create a pet
* {@link /pets/:uuid}
Expand Down
Loading
Loading