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: Use of enabled based on optional params #1268

Merged
merged 1 commit into from
Sep 25, 2024
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
8 changes: 8 additions & 0 deletions .changeset/popular-donkeys-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@kubb/plugin-svelte-query": minor
"@kubb/plugin-react-query": minor
"@kubb/plugin-solid-query": minor
"@kubb/plugin-vue-query": minor
---

Use of `enabled` based on optional params
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ title: Changelog

# Changelog

## 3.0.0-alpha.30
- [`plugin-react-query`](/plugins/plugin-react-query/): Use of `enabled` based on optional params
- [`plugin-svelte-query`](/plugins/plugin-svelte-query/): Use of `enabled` based on optional params
- [`plugin-vue-query`](/plugins/plugin-vue-query/): Use of `enabled` based on optional params
- [`plugin-solid-query`](/plugins/plugin-solid-query/): Use of `enabled` based on optional params

## 3.0.0-alpha.29
- [`plugin-react-query`](/plugins/plugin-react-query/): Support for cancellation of queries with the help of `signal`
- [`plugin-svelte-query`](/plugins/plugin-svelte-query/): Support for cancellation of queries with the help of `signal`
Expand Down
6 changes: 5 additions & 1 deletion examples/advanced/configs/kubb.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export default defineConfig(() => {
initialPageParam: 0,
},
mutation: {
key(key) {
return key
},
importPath: '@tanstack/react-query',
methods: ['post', 'put', 'delete'],
},
Expand All @@ -82,14 +85,15 @@ export default defineConfig(() => {
],
group: { type: 'tag' },
client: {
dataReturnType: 'full',
importPath: '../../../../tanstack-query-client.ts',
},
query: {
importPath: '../../../../tanstack-query-hook.ts',
},
infinite: false,
suspense: false,
dataReturnType: 'full',

parser: 'zod',
}),
pluginSwr({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig } from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
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'
Expand All @@ -22,7 +22,7 @@ async function addPet(data: AddPetMutationRequest, config: Partial<RequestConfig
data,
...config,
})
return addPetMutationResponseSchema.parse(res.data)
return { ...res, data: addPetMutationResponseSchema.parse(res.data) }
}

/**
Expand All @@ -33,7 +33,7 @@ async function addPet(data: AddPetMutationRequest, config: Partial<RequestConfig
export function useAddPet(
options: {
mutation?: UseMutationOptions<
AddPetMutationResponse,
ResponseConfig<AddPetMutationResponse>,
AddPet405,
{
data: AddPetMutationRequest
Expand All @@ -45,7 +45,7 @@ export function useAddPet(
const { mutation: mutationOptions, client: config = {} } = options ?? {}
const mutationKey = mutationOptions?.mutationKey ?? addPetMutationKey()
return useMutation<
AddPetMutationResponse,
ResponseConfig<AddPetMutationResponse>,
AddPet405,
{
data: AddPetMutationRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig } from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
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'
Expand All @@ -22,7 +22,7 @@ async function deletePet(petId: DeletePetPathParams['petId'], headers?: DeletePe
headers: { ...headers, ...config.headers },
...config,
})
return deletePetMutationResponseSchema.parse(res.data)
return { ...res, data: deletePetMutationResponseSchema.parse(res.data) }
}

/**
Expand All @@ -33,7 +33,7 @@ async function deletePet(petId: DeletePetPathParams['petId'], headers?: DeletePe
export function useDeletePet(
options: {
mutation?: UseMutationOptions<
DeletePetMutationResponse,
ResponseConfig<DeletePetMutationResponse>,
DeletePet400,
{
petId: DeletePetPathParams['petId']
Expand All @@ -46,7 +46,7 @@ export function useDeletePet(
const { mutation: mutationOptions, client: config = {} } = options ?? {}
const mutationKey = mutationOptions?.mutationKey ?? deletePetMutationKey()
return useMutation<
DeletePetMutationResponse,
ResponseConfig<DeletePetMutationResponse>,
DeletePet400,
{
petId: DeletePetPathParams['petId']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig } from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
import type { QueryKey, QueryObserverOptions, UseQueryResult } from '../../../../tanstack-query-hook.ts'
import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts'
import { useQuery, queryOptions } from '../../../../tanstack-query-hook.ts'
Expand All @@ -22,7 +22,7 @@ async function findPetsByStatus(params?: FindPetsByStatusQueryParams, config: Pa
params,
...config,
})
return findPetsByStatusQueryResponseSchema.parse(res.data)
return { ...res, data: findPetsByStatusQueryResponseSchema.parse(res.data) }
}

export function findPetsByStatusQueryOptions(params?: FindPetsByStatusQueryParams, config: Partial<RequestConfig> = {}) {
Expand All @@ -42,13 +42,13 @@ export function findPetsByStatusQueryOptions(params?: FindPetsByStatusQueryParam
* @link /pet/findByStatus
*/
export function useFindPetsByStatus<
TData = FindPetsByStatusQueryResponse,
TQueryData = FindPetsByStatusQueryResponse,
TData = ResponseConfig<FindPetsByStatusQueryResponse>,
TQueryData = ResponseConfig<FindPetsByStatusQueryResponse>,
TQueryKey extends QueryKey = FindPetsByStatusQueryKey,
>(
params?: FindPetsByStatusQueryParams,
options: {
query?: Partial<QueryObserverOptions<FindPetsByStatusQueryResponse, FindPetsByStatus400, TData, TQueryData, TQueryKey>>
query?: Partial<QueryObserverOptions<ResponseConfig<FindPetsByStatusQueryResponse>, FindPetsByStatus400, TData, TQueryData, TQueryKey>>
client?: Partial<RequestConfig>
} = {},
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig } from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
import type { QueryKey, QueryObserverOptions, UseQueryResult } from '../../../../tanstack-query-hook.ts'
import type {
FindPetsByTagsQueryResponse,
Expand Down Expand Up @@ -28,7 +28,7 @@ async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: Find
headers: { ...headers, ...config.headers },
...config,
})
return findPetsByTagsQueryResponseSchema.parse(res.data)
return { ...res, data: findPetsByTagsQueryResponseSchema.parse(res.data) }
}

export function findPetsByTagsQueryOptions(headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, config: Partial<RequestConfig> = {}) {
Expand All @@ -48,14 +48,14 @@ export function findPetsByTagsQueryOptions(headers: FindPetsByTagsHeaderParams,
* @link /pet/findByTags
*/
export function useFindPetsByTags<
TData = FindPetsByTagsQueryResponse,
TQueryData = FindPetsByTagsQueryResponse,
TData = ResponseConfig<FindPetsByTagsQueryResponse>,
TQueryData = ResponseConfig<FindPetsByTagsQueryResponse>,
TQueryKey extends QueryKey = FindPetsByTagsQueryKey,
>(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
options: {
query?: Partial<QueryObserverOptions<FindPetsByTagsQueryResponse, FindPetsByTags400, TData, TQueryData, TQueryKey>>
query?: Partial<QueryObserverOptions<ResponseConfig<FindPetsByTagsQueryResponse>, FindPetsByTags400, TData, TQueryData, TQueryKey>>
client?: Partial<RequestConfig>
} = {},
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig } from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
import type { QueryKey, InfiniteQueryObserverOptions, UseInfiniteQueryResult } from '../../../../tanstack-query-hook.ts'
import type {
FindPetsByTagsQueryResponse,
Expand Down Expand Up @@ -28,7 +28,7 @@ async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: Find
headers: { ...headers, ...config.headers },
...config,
})
return findPetsByTagsQueryResponseSchema.parse(res.data)
return { ...res, data: findPetsByTagsQueryResponseSchema.parse(res.data) }
}

export function findPetsByTagsInfiniteQueryOptions(
Expand All @@ -39,14 +39,15 @@ export function findPetsByTagsInfiniteQueryOptions(
const queryKey = findPetsByTagsInfiniteQueryKey(params)
return infiniteQueryOptions({
queryKey,
queryFn: async ({ pageParam }) => {
queryFn: async ({ signal, pageParam }) => {
config.signal = signal
if (params) {
params['pageSize'] = pageParam as unknown as FindPetsByTagsQueryParams['pageSize']
}
return findPetsByTags(headers, params, config)
},
initialPageParam: 0,
getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1),
getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1),
getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => (firstPageParam <= 1 ? undefined : firstPageParam - 1),
})
}
Expand All @@ -57,14 +58,14 @@ export function findPetsByTagsInfiniteQueryOptions(
* @link /pet/findByTags
*/
export function useFindPetsByTagsInfinite<
TData = FindPetsByTagsQueryResponse,
TQueryData = FindPetsByTagsQueryResponse,
TData = ResponseConfig<FindPetsByTagsQueryResponse>,
TQueryData = ResponseConfig<FindPetsByTagsQueryResponse>,
TQueryKey extends QueryKey = FindPetsByTagsInfiniteQueryKey,
>(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
options: {
query?: Partial<InfiniteQueryObserverOptions<FindPetsByTagsQueryResponse, FindPetsByTags400, TData, TQueryData, TQueryKey>>
query?: Partial<InfiniteQueryObserverOptions<ResponseConfig<FindPetsByTagsQueryResponse>, FindPetsByTags400, TData, TQueryData, TQueryKey>>
client?: Partial<RequestConfig>
} = {},
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig } from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
import type { QueryKey, QueryObserverOptions, UseQueryResult } from '../../../../tanstack-query-hook.ts'
import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from '../../../models/ts/petController/GetPetById.ts'
import { useQuery, queryOptions } from '../../../../tanstack-query-hook.ts'
Expand All @@ -21,12 +21,13 @@ async function getPetById(petId: GetPetByIdPathParams['petId'], config: Partial<
baseURL: 'https://petstore3.swagger.io/api/v3',
...config,
})
return getPetByIdQueryResponseSchema.parse(res.data)
return { ...res, data: getPetByIdQueryResponseSchema.parse(res.data) }
}

export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], config: Partial<RequestConfig> = {}) {
const queryKey = getPetByIdQueryKey(petId)
return queryOptions({
enabled: !!petId,
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
Expand All @@ -40,10 +41,14 @@ export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], con
* @summary Find pet by ID
* @link /pet/:petId
*/
export function useGetPetById<TData = GetPetByIdQueryResponse, TQueryData = GetPetByIdQueryResponse, TQueryKey extends QueryKey = GetPetByIdQueryKey>(
export function useGetPetById<
TData = ResponseConfig<GetPetByIdQueryResponse>,
TQueryData = ResponseConfig<GetPetByIdQueryResponse>,
TQueryKey extends QueryKey = GetPetByIdQueryKey,
>(
petId: GetPetByIdPathParams['petId'],
options: {
query?: Partial<QueryObserverOptions<GetPetByIdQueryResponse, GetPetById400 | GetPetById404, TData, TQueryData, TQueryKey>>
query?: Partial<QueryObserverOptions<ResponseConfig<GetPetByIdQueryResponse>, GetPetById400 | GetPetById404, TData, TQueryData, TQueryKey>>
client?: Partial<RequestConfig>
} = {},
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig } from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
import type {
UpdatePetMutationRequest,
UpdatePetMutationResponse,
Expand Down Expand Up @@ -28,7 +28,7 @@ async function updatePet(data: UpdatePetMutationRequest, config: Partial<Request
data,
...config,
})
return updatePetMutationResponseSchema.parse(res.data)
return { ...res, data: updatePetMutationResponseSchema.parse(res.data) }
}

/**
Expand All @@ -39,7 +39,7 @@ async function updatePet(data: UpdatePetMutationRequest, config: Partial<Request
export function useUpdatePet(
options: {
mutation?: UseMutationOptions<
UpdatePetMutationResponse,
ResponseConfig<UpdatePetMutationResponse>,
UpdatePet400 | UpdatePet404 | UpdatePet405,
{
data: UpdatePetMutationRequest
Expand All @@ -51,7 +51,7 @@ export function useUpdatePet(
const { mutation: mutationOptions, client: config = {} } = options ?? {}
const mutationKey = mutationOptions?.mutationKey ?? updatePetMutationKey()
return useMutation<
UpdatePetMutationResponse,
ResponseConfig<UpdatePetMutationResponse>,
UpdatePet400 | UpdatePet404 | UpdatePet405,
{
data: UpdatePetMutationRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig } from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
import type {
UpdatePetWithFormMutationResponse,
UpdatePetWithFormPathParams,
Expand All @@ -26,7 +26,7 @@ async function updatePetWithForm(petId: UpdatePetWithFormPathParams['petId'], pa
params,
...config,
})
return updatePetWithFormMutationResponseSchema.parse(res.data)
return { ...res, data: updatePetWithFormMutationResponseSchema.parse(res.data) }
}

/**
Expand All @@ -36,7 +36,7 @@ async function updatePetWithForm(petId: UpdatePetWithFormPathParams['petId'], pa
export function useUpdatePetWithForm(
options: {
mutation?: UseMutationOptions<
UpdatePetWithFormMutationResponse,
ResponseConfig<UpdatePetWithFormMutationResponse>,
UpdatePetWithForm405,
{
petId: UpdatePetWithFormPathParams['petId']
Expand All @@ -49,7 +49,7 @@ export function useUpdatePetWithForm(
const { mutation: mutationOptions, client: config = {} } = options ?? {}
const mutationKey = mutationOptions?.mutationKey ?? updatePetWithFormMutationKey()
return useMutation<
UpdatePetWithFormMutationResponse,
ResponseConfig<UpdatePetWithFormMutationResponse>,
UpdatePetWithForm405,
{
petId: UpdatePetWithFormPathParams['petId']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '../../../../tanstack-query-client.ts'
import type { RequestConfig } from '../../../../tanstack-query-client.ts'
import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts'
import type {
UploadFileMutationRequest,
UploadFileMutationResponse,
Expand Down Expand Up @@ -33,7 +33,7 @@ async function uploadFile(
headers: { 'Content-Type': 'application/octet-stream', ...config.headers },
...config,
})
return uploadFileMutationResponseSchema.parse(res.data)
return { ...res, data: uploadFileMutationResponseSchema.parse(res.data) }
}

/**
Expand All @@ -43,7 +43,7 @@ async function uploadFile(
export function useUploadFile(
options: {
mutation?: UseMutationOptions<
UploadFileMutationResponse,
ResponseConfig<UploadFileMutationResponse>,
Error,
{
petId: UploadFilePathParams['petId']
Expand All @@ -57,7 +57,7 @@ export function useUploadFile(
const { mutation: mutationOptions, client: config = {} } = options ?? {}
const mutationKey = mutationOptions?.mutationKey ?? uploadFileMutationKey()
return useMutation<
UploadFileMutationResponse,
ResponseConfig<UploadFileMutationResponse>,
Error,
{
petId: UploadFilePathParams['petId']
Expand Down
Loading
Loading