From 8106b7b0695d53b3e768615406ba2b2b2f5da1a3 Mon Sep 17 00:00:00 2001 From: Stijn Van Hulle Date: Wed, 25 Sep 2024 21:01:25 +0200 Subject: [PATCH] fix: Use of `enabled` based on optional params --- .changeset/popular-donkeys-speak.md | 8 ++++++++ docs/changelog.md | 6 ++++++ examples/advanced/configs/kubb.config.ts | 6 +++++- .../clients/hooks/petController/useAddPet.ts | 8 ++++---- .../hooks/petController/useDeletePet.ts | 8 ++++---- .../hooks/petController/useFindPetsByStatus.ts | 10 +++++----- .../hooks/petController/useFindPetsByTags.ts | 10 +++++----- .../petController/useFindPetsByTagsInfinite.ts | 15 ++++++++------- .../hooks/petController/useGetPetById.ts | 13 +++++++++---- .../hooks/petController/useUpdatePet.ts | 8 ++++---- .../petController/useUpdatePetWithForm.ts | 8 ++++---- .../hooks/petController/useUploadFile.ts | 8 ++++---- .../hooks/petsController/useCreatePets.ts | 8 ++++---- .../hooks/userController/useCreateUser.ts | 8 ++++---- .../useCreateUsersWithListInput.ts | 8 ++++---- .../hooks/userController/useDeleteUser.ts | 8 ++++---- .../hooks/userController/useGetUserByName.ts | 11 ++++++----- .../hooks/userController/useLoginUser.ts | 12 ++++++++---- .../hooks/userController/useLogoutUser.ts | 12 ++++++++---- .../hooks/userController/useUpdateUser.ts | 8 ++++---- .../gen/hooks/useFindPetsByTagsInfiniteHook.ts | 3 ++- .../src/gen/hooks/useGetOrderByIdHook.ts | 1 + .../gen/hooks/useGetOrderByIdSuspenseHook.ts | 1 + .../src/gen/hooks/useGetPetByIdHook.ts | 1 + .../src/gen/hooks/useGetPetByIdSuspenseHook.ts | 1 + .../src/gen/hooks/useGetUserByNameHook.ts | 1 + .../gen/hooks/useGetUserByNameSuspenseHook.ts | 1 + .../src/gen/hooks/useUpdatePetWithFormHook.ts | 1 + .../hooks/useUpdatePetWithFormSuspenseHook.ts | 1 + examples/simple-single/src/gen/hooks.ts | 6 ++++++ .../src/gen/hooks/createGetOrderById.ts | 1 + .../src/gen/hooks/createGetPetById.ts | 1 + .../src/gen/hooks/createGetUserByName.ts | 1 + .../src/gen/hooks/createUpdatePetWithForm.ts | 1 + .../src/gen/hooks/createGetOrderById.ts | 1 + .../src/gen/hooks/createGetPetById.ts | 1 + .../src/gen/hooks/createGetUserByName.ts | 1 + .../src/gen/hooks/createUpdatePetWithForm.ts | 1 + .../vue-query/src/gen/hooks/useGetOrderById.ts | 1 + .../vue-query/src/gen/hooks/useGetPetById.ts | 1 + .../src/gen/hooks/useGetUserByName.ts | 1 + .../src/components/InfiniteQueryOptions.tsx | 11 ++++++++++- .../src/components/QueryOptions.tsx | 8 ++++++++ .../__snapshots__/findInfiniteByTags.ts | 3 ++- .../__snapshots__/findInfiniteByTagsCursor.ts | 3 ++- .../generators/__snapshots__/postAsQuery.ts | 1 + .../src/components/QueryOptions.tsx | 8 ++++++++ .../generators/__snapshots__/postAsQuery.ts | 1 + .../src/components/QueryOptions.tsx | 8 ++++++++ .../generators/__snapshots__/postAsQuery.ts | 1 + .../src/components/InfiniteQueryOptions.tsx | 11 ++++++++++- .../src/components/QueryOptions.tsx | 8 ++++++++ .../__snapshots__/findInfiniteByTags.ts | 3 ++- .../__snapshots__/findInfiniteByTagsCursor.ts | 3 ++- .../generators/__snapshots__/postAsQuery.ts | 1 + packages/react/src/utils/getFunctionParams.ts | 18 ++++++++++++++++-- 56 files changed, 215 insertions(+), 84 deletions(-) create mode 100644 .changeset/popular-donkeys-speak.md diff --git a/.changeset/popular-donkeys-speak.md b/.changeset/popular-donkeys-speak.md new file mode 100644 index 000000000..f44858ab4 --- /dev/null +++ b/.changeset/popular-donkeys-speak.md @@ -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 diff --git a/docs/changelog.md b/docs/changelog.md index 3c2351b77..1618c8f88 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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` diff --git a/examples/advanced/configs/kubb.config.ts b/examples/advanced/configs/kubb.config.ts index 538d10ebc..651c136ee 100644 --- a/examples/advanced/configs/kubb.config.ts +++ b/examples/advanced/configs/kubb.config.ts @@ -74,6 +74,9 @@ export default defineConfig(() => { initialPageParam: 0, }, mutation: { + key(key) { + return key + }, importPath: '@tanstack/react-query', methods: ['post', 'put', 'delete'], }, @@ -82,6 +85,7 @@ export default defineConfig(() => { ], group: { type: 'tag' }, client: { + dataReturnType: 'full', importPath: '../../../../tanstack-query-client.ts', }, query: { @@ -89,7 +93,7 @@ export default defineConfig(() => { }, infinite: false, suspense: false, - dataReturnType: 'full', + parser: 'zod', }), pluginSwr({ diff --git a/examples/advanced/src/gen/clients/hooks/petController/useAddPet.ts b/examples/advanced/src/gen/clients/hooks/petController/useAddPet.ts index 01365e3b2..5904a2a0b 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useAddPet.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useAddPet.ts @@ -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' @@ -22,7 +22,7 @@ async function addPet(data: AddPetMutationRequest, config: Partial, AddPet405, { data: AddPetMutationRequest @@ -45,7 +45,7 @@ export function useAddPet( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? addPetMutationKey() return useMutation< - AddPetMutationResponse, + ResponseConfig, AddPet405, { data: AddPetMutationRequest diff --git a/examples/advanced/src/gen/clients/hooks/petController/useDeletePet.ts b/examples/advanced/src/gen/clients/hooks/petController/useDeletePet.ts index fbc5b0ab8..557cb687f 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useDeletePet.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useDeletePet.ts @@ -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' @@ -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) } } /** @@ -33,7 +33,7 @@ async function deletePet(petId: DeletePetPathParams['petId'], headers?: DeletePe export function useDeletePet( options: { mutation?: UseMutationOptions< - DeletePetMutationResponse, + ResponseConfig, DeletePet400, { petId: DeletePetPathParams['petId'] @@ -46,7 +46,7 @@ export function useDeletePet( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? deletePetMutationKey() return useMutation< - DeletePetMutationResponse, + ResponseConfig, DeletePet400, { petId: DeletePetPathParams['petId'] diff --git a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts index 383b32595..3133afabb 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts @@ -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' @@ -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 = {}) { @@ -42,13 +42,13 @@ export function findPetsByStatusQueryOptions(params?: FindPetsByStatusQueryParam * @link /pet/findByStatus */ export function useFindPetsByStatus< - TData = FindPetsByStatusQueryResponse, - TQueryData = FindPetsByStatusQueryResponse, + TData = ResponseConfig, + TQueryData = ResponseConfig, TQueryKey extends QueryKey = FindPetsByStatusQueryKey, >( params?: FindPetsByStatusQueryParams, options: { - query?: Partial> + query?: Partial, FindPetsByStatus400, TData, TQueryData, TQueryKey>> client?: Partial } = {}, ) { diff --git a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTags.ts b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTags.ts index 2e742cbb6..8354ce793 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTags.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTags.ts @@ -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, @@ -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 = {}) { @@ -48,14 +48,14 @@ export function findPetsByTagsQueryOptions(headers: FindPetsByTagsHeaderParams, * @link /pet/findByTags */ export function useFindPetsByTags< - TData = FindPetsByTagsQueryResponse, - TQueryData = FindPetsByTagsQueryResponse, + TData = ResponseConfig, + TQueryData = ResponseConfig, TQueryKey extends QueryKey = FindPetsByTagsQueryKey, >( headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, options: { - query?: Partial> + query?: Partial, FindPetsByTags400, TData, TQueryData, TQueryKey>> client?: Partial } = {}, ) { diff --git a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTagsInfinite.ts b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTagsInfinite.ts index 2531e993d..cda1545a8 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTagsInfinite.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByTagsInfinite.ts @@ -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, @@ -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( @@ -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), }) } @@ -57,14 +58,14 @@ export function findPetsByTagsInfiniteQueryOptions( * @link /pet/findByTags */ export function useFindPetsByTagsInfinite< - TData = FindPetsByTagsQueryResponse, - TQueryData = FindPetsByTagsQueryResponse, + TData = ResponseConfig, + TQueryData = ResponseConfig, TQueryKey extends QueryKey = FindPetsByTagsInfiniteQueryKey, >( headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, options: { - query?: Partial> + query?: Partial, FindPetsByTags400, TData, TQueryData, TQueryKey>> client?: Partial } = {}, ) { diff --git a/examples/advanced/src/gen/clients/hooks/petController/useGetPetById.ts b/examples/advanced/src/gen/clients/hooks/petController/useGetPetById.ts index 7f6f1e31c..92387a4ca 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useGetPetById.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useGetPetById.ts @@ -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' @@ -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 = {}) { const queryKey = getPetByIdQueryKey(petId) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal @@ -40,10 +41,14 @@ export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], con * @summary Find pet by ID * @link /pet/:petId */ -export function useGetPetById( +export function useGetPetById< + TData = ResponseConfig, + TQueryData = ResponseConfig, + TQueryKey extends QueryKey = GetPetByIdQueryKey, +>( petId: GetPetByIdPathParams['petId'], options: { - query?: Partial> + query?: Partial, GetPetById400 | GetPetById404, TData, TQueryData, TQueryKey>> client?: Partial } = {}, ) { diff --git a/examples/advanced/src/gen/clients/hooks/petController/useUpdatePet.ts b/examples/advanced/src/gen/clients/hooks/petController/useUpdatePet.ts index 0d1077932..d0b990b7b 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useUpdatePet.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useUpdatePet.ts @@ -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, @@ -28,7 +28,7 @@ async function updatePet(data: UpdatePetMutationRequest, config: Partial, UpdatePet400 | UpdatePet404 | UpdatePet405, { data: UpdatePetMutationRequest @@ -51,7 +51,7 @@ export function useUpdatePet( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? updatePetMutationKey() return useMutation< - UpdatePetMutationResponse, + ResponseConfig, UpdatePet400 | UpdatePet404 | UpdatePet405, { data: UpdatePetMutationRequest diff --git a/examples/advanced/src/gen/clients/hooks/petController/useUpdatePetWithForm.ts b/examples/advanced/src/gen/clients/hooks/petController/useUpdatePetWithForm.ts index 23f2518e6..cbd934590 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useUpdatePetWithForm.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useUpdatePetWithForm.ts @@ -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, @@ -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) } } /** @@ -36,7 +36,7 @@ async function updatePetWithForm(petId: UpdatePetWithFormPathParams['petId'], pa export function useUpdatePetWithForm( options: { mutation?: UseMutationOptions< - UpdatePetWithFormMutationResponse, + ResponseConfig, UpdatePetWithForm405, { petId: UpdatePetWithFormPathParams['petId'] @@ -49,7 +49,7 @@ export function useUpdatePetWithForm( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? updatePetWithFormMutationKey() return useMutation< - UpdatePetWithFormMutationResponse, + ResponseConfig, UpdatePetWithForm405, { petId: UpdatePetWithFormPathParams['petId'] diff --git a/examples/advanced/src/gen/clients/hooks/petController/useUploadFile.ts b/examples/advanced/src/gen/clients/hooks/petController/useUploadFile.ts index 717aa1058..064867081 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useUploadFile.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useUploadFile.ts @@ -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, @@ -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) } } /** @@ -43,7 +43,7 @@ async function uploadFile( export function useUploadFile( options: { mutation?: UseMutationOptions< - UploadFileMutationResponse, + ResponseConfig, Error, { petId: UploadFilePathParams['petId'] @@ -57,7 +57,7 @@ export function useUploadFile( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? uploadFileMutationKey() return useMutation< - UploadFileMutationResponse, + ResponseConfig, Error, { petId: UploadFilePathParams['petId'] diff --git a/examples/advanced/src/gen/clients/hooks/petsController/useCreatePets.ts b/examples/advanced/src/gen/clients/hooks/petsController/useCreatePets.ts index 3ecb51558..69ba9509b 100644 --- a/examples/advanced/src/gen/clients/hooks/petsController/useCreatePets.ts +++ b/examples/advanced/src/gen/clients/hooks/petsController/useCreatePets.ts @@ -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 { CreatePetsMutationRequest, CreatePetsMutationResponse, @@ -35,7 +35,7 @@ async function createPets( headers: { ...headers, ...config.headers }, ...config, }) - return createPetsMutationResponseSchema.parse(res.data) + return { ...res, data: createPetsMutationResponseSchema.parse(res.data) } } /** @@ -45,7 +45,7 @@ async function createPets( export function useCreatePets( options: { mutation?: UseMutationOptions< - CreatePetsMutationResponse, + ResponseConfig, Error, { uuid: CreatePetsPathParams['uuid'] @@ -60,7 +60,7 @@ export function useCreatePets( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? createPetsMutationKey() return useMutation< - CreatePetsMutationResponse, + ResponseConfig, Error, { uuid: CreatePetsPathParams['uuid'] diff --git a/examples/advanced/src/gen/clients/hooks/userController/useCreateUser.ts b/examples/advanced/src/gen/clients/hooks/userController/useCreateUser.ts index 47edc056b..fd9d57081 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useCreateUser.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useCreateUser.ts @@ -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 { CreateUserMutationRequest, CreateUserMutationResponse } from '../../../models/ts/userController/CreateUser.ts' import type { UseMutationOptions } from '@tanstack/react-query' import { createUserMutationResponseSchema } from '../../../zod/userController/createUserSchema.ts' @@ -22,7 +22,7 @@ async function createUser(data?: CreateUserMutationRequest, config: Partial, Error, { data?: CreateUserMutationRequest @@ -45,7 +45,7 @@ export function useCreateUser( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? createUserMutationKey() return useMutation< - CreateUserMutationResponse, + ResponseConfig, Error, { data?: CreateUserMutationRequest diff --git a/examples/advanced/src/gen/clients/hooks/userController/useCreateUsersWithListInput.ts b/examples/advanced/src/gen/clients/hooks/userController/useCreateUsersWithListInput.ts index 9ad1e4298..8e32b144e 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useCreateUsersWithListInput.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useCreateUsersWithListInput.ts @@ -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 { CreateUsersWithListInputMutationRequest, CreateUsersWithListInputMutationResponse, @@ -28,7 +28,7 @@ async function createUsersWithListInput( data, ...config, }) - return createUsersWithListInputMutationResponseSchema.parse(res.data) + return { ...res, data: createUsersWithListInputMutationResponseSchema.parse(res.data) } } /** @@ -39,7 +39,7 @@ async function createUsersWithListInput( export function useCreateUsersWithListInput( options: { mutation?: UseMutationOptions< - CreateUsersWithListInputMutationResponse, + ResponseConfig, Error, { data?: CreateUsersWithListInputMutationRequest @@ -51,7 +51,7 @@ export function useCreateUsersWithListInput( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? createUsersWithListInputMutationKey() return useMutation< - CreateUsersWithListInputMutationResponse, + ResponseConfig, Error, { data?: CreateUsersWithListInputMutationRequest diff --git a/examples/advanced/src/gen/clients/hooks/userController/useDeleteUser.ts b/examples/advanced/src/gen/clients/hooks/userController/useDeleteUser.ts index e5c765ff1..8b9b38386 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useDeleteUser.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useDeleteUser.ts @@ -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 { DeleteUserMutationResponse, DeleteUserPathParams, DeleteUser400, DeleteUser404 } from '../../../models/ts/userController/DeleteUser.ts' import type { UseMutationOptions } from '@tanstack/react-query' import { deleteUserMutationResponseSchema } from '../../../zod/userController/deleteUserSchema.ts' @@ -21,7 +21,7 @@ async function deleteUser(username: DeleteUserPathParams['username'], config: Pa baseURL: 'https://petstore3.swagger.io/api/v3', ...config, }) - return deleteUserMutationResponseSchema.parse(res.data) + return { ...res, data: deleteUserMutationResponseSchema.parse(res.data) } } /** @@ -32,7 +32,7 @@ async function deleteUser(username: DeleteUserPathParams['username'], config: Pa export function useDeleteUser( options: { mutation?: UseMutationOptions< - DeleteUserMutationResponse, + ResponseConfig, DeleteUser400 | DeleteUser404, { username: DeleteUserPathParams['username'] @@ -44,7 +44,7 @@ export function useDeleteUser( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? deleteUserMutationKey() return useMutation< - DeleteUserMutationResponse, + ResponseConfig, DeleteUser400 | DeleteUser404, { username: DeleteUserPathParams['username'] diff --git a/examples/advanced/src/gen/clients/hooks/userController/useGetUserByName.ts b/examples/advanced/src/gen/clients/hooks/userController/useGetUserByName.ts index 76d1cf251..c0315b2ac 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useGetUserByName.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useGetUserByName.ts @@ -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 { GetUserByNameQueryResponse, @@ -25,12 +25,13 @@ async function getUserByName(username: GetUserByNamePathParams['username'], conf baseURL: 'https://petstore3.swagger.io/api/v3', ...config, }) - return getUserByNameQueryResponseSchema.parse(res.data) + return { ...res, data: getUserByNameQueryResponseSchema.parse(res.data) } } export function getUserByNameQueryOptions(username: GetUserByNamePathParams['username'], config: Partial = {}) { const queryKey = getUserByNameQueryKey(username) return queryOptions({ + enabled: !!username, queryKey, queryFn: async ({ signal }) => { config.signal = signal @@ -44,13 +45,13 @@ export function getUserByNameQueryOptions(username: GetUserByNamePathParams['use * @link /user/:username */ export function useGetUserByName< - TData = GetUserByNameQueryResponse, - TQueryData = GetUserByNameQueryResponse, + TData = ResponseConfig, + TQueryData = ResponseConfig, TQueryKey extends QueryKey = GetUserByNameQueryKey, >( username: GetUserByNamePathParams['username'], options: { - query?: Partial> + query?: Partial, GetUserByName400 | GetUserByName404, TData, TQueryData, TQueryKey>> client?: Partial } = {}, ) { diff --git a/examples/advanced/src/gen/clients/hooks/userController/useLoginUser.ts b/examples/advanced/src/gen/clients/hooks/userController/useLoginUser.ts index cda17555f..cb381a86c 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useLoginUser.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useLoginUser.ts @@ -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 { LoginUserQueryResponse, LoginUserQueryParams, LoginUser400 } from '../../../models/ts/userController/LoginUser.ts' import { useQuery, queryOptions } from '../../../../tanstack-query-hook.ts' @@ -21,7 +21,7 @@ async function loginUser(params?: LoginUserQueryParams, config: Partial = {}) { @@ -39,10 +39,14 @@ export function loginUserQueryOptions(params?: LoginUserQueryParams, config: Par * @summary Logs user into the system * @link /user/login */ -export function useLoginUser( +export function useLoginUser< + TData = ResponseConfig, + TQueryData = ResponseConfig, + TQueryKey extends QueryKey = LoginUserQueryKey, +>( params?: LoginUserQueryParams, options: { - query?: Partial> + query?: Partial, LoginUser400, TData, TQueryData, TQueryKey>> client?: Partial } = {}, ) { diff --git a/examples/advanced/src/gen/clients/hooks/userController/useLogoutUser.ts b/examples/advanced/src/gen/clients/hooks/userController/useLogoutUser.ts index 5b2fd3e3c..06b10ad1c 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useLogoutUser.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useLogoutUser.ts @@ -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 { LogoutUserQueryResponse } from '../../../models/ts/userController/LogoutUser.ts' import { useQuery, queryOptions } from '../../../../tanstack-query-hook.ts' @@ -20,7 +20,7 @@ async function logoutUser(config: Partial = {}) { baseURL: 'https://petstore3.swagger.io/api/v3', ...config, }) - return logoutUserQueryResponseSchema.parse(res.data) + return { ...res, data: logoutUserQueryResponseSchema.parse(res.data) } } export function logoutUserQueryOptions(config: Partial = {}) { @@ -38,9 +38,13 @@ export function logoutUserQueryOptions(config: Partial = {}) { * @summary Logs out current logged in user session * @link /user/logout */ -export function useLogoutUser( +export function useLogoutUser< + TData = ResponseConfig, + TQueryData = ResponseConfig, + TQueryKey extends QueryKey = LogoutUserQueryKey, +>( options: { - query?: Partial> + query?: Partial, Error, TData, TQueryData, TQueryKey>> client?: Partial } = {}, ) { diff --git a/examples/advanced/src/gen/clients/hooks/userController/useUpdateUser.ts b/examples/advanced/src/gen/clients/hooks/userController/useUpdateUser.ts index 538d8d571..4f79da1ca 100644 --- a/examples/advanced/src/gen/clients/hooks/userController/useUpdateUser.ts +++ b/examples/advanced/src/gen/clients/hooks/userController/useUpdateUser.ts @@ -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 { UpdateUserMutationRequest, UpdateUserMutationResponse, UpdateUserPathParams } from '../../../models/ts/userController/UpdateUser.ts' import type { UseMutationOptions } from '@tanstack/react-query' import { updateUserMutationResponseSchema } from '../../../zod/userController/updateUserSchema.ts' @@ -26,7 +26,7 @@ async function updateUser( data, ...config, }) - return updateUserMutationResponseSchema.parse(res.data) + return { ...res, data: updateUserMutationResponseSchema.parse(res.data) } } /** @@ -37,7 +37,7 @@ async function updateUser( export function useUpdateUser( options: { mutation?: UseMutationOptions< - UpdateUserMutationResponse, + ResponseConfig, Error, { username: UpdateUserPathParams['username'] @@ -50,7 +50,7 @@ export function useUpdateUser( const { mutation: mutationOptions, client: config = {} } = options ?? {} const mutationKey = mutationOptions?.mutationKey ?? updateUserMutationKey() return useMutation< - UpdateUserMutationResponse, + ResponseConfig, Error, { username: UpdateUserPathParams['username'] diff --git a/examples/react-query/src/gen/hooks/useFindPetsByTagsInfiniteHook.ts b/examples/react-query/src/gen/hooks/useFindPetsByTagsInfiniteHook.ts index 06d06ea61..8dbc77845 100644 --- a/examples/react-query/src/gen/hooks/useFindPetsByTagsInfiniteHook.ts +++ b/examples/react-query/src/gen/hooks/useFindPetsByTagsInfiniteHook.ts @@ -28,7 +28,8 @@ export function findPetsByTagsInfiniteQueryOptions(params?: FindPetsByTagsQueryP 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'] } diff --git a/examples/react-query/src/gen/hooks/useGetOrderByIdHook.ts b/examples/react-query/src/gen/hooks/useGetOrderByIdHook.ts index 348d9506c..5b9228cac 100644 --- a/examples/react-query/src/gen/hooks/useGetOrderByIdHook.ts +++ b/examples/react-query/src/gen/hooks/useGetOrderByIdHook.ts @@ -44,6 +44,7 @@ export function getOrderByIdQueryOptions( ) { const queryKey = getOrderByIdQueryKey({ orderId }) return queryOptions({ + enabled: !!orderId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/react-query/src/gen/hooks/useGetOrderByIdSuspenseHook.ts b/examples/react-query/src/gen/hooks/useGetOrderByIdSuspenseHook.ts index 7874c456e..d33d74f2e 100644 --- a/examples/react-query/src/gen/hooks/useGetOrderByIdSuspenseHook.ts +++ b/examples/react-query/src/gen/hooks/useGetOrderByIdSuspenseHook.ts @@ -44,6 +44,7 @@ export function getOrderByIdSuspenseQueryOptions( ) { const queryKey = getOrderByIdSuspenseQueryKey({ orderId }) return queryOptions({ + enabled: !!orderId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/react-query/src/gen/hooks/useGetPetByIdHook.ts b/examples/react-query/src/gen/hooks/useGetPetByIdHook.ts index 3d7b2e1dc..304cff560 100644 --- a/examples/react-query/src/gen/hooks/useGetPetByIdHook.ts +++ b/examples/react-query/src/gen/hooks/useGetPetByIdHook.ts @@ -44,6 +44,7 @@ export function getPetByIdQueryOptions( ) { const queryKey = getPetByIdQueryKey({ petId }) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/react-query/src/gen/hooks/useGetPetByIdSuspenseHook.ts b/examples/react-query/src/gen/hooks/useGetPetByIdSuspenseHook.ts index 8856e0b5c..05a37c287 100644 --- a/examples/react-query/src/gen/hooks/useGetPetByIdSuspenseHook.ts +++ b/examples/react-query/src/gen/hooks/useGetPetByIdSuspenseHook.ts @@ -44,6 +44,7 @@ export function getPetByIdSuspenseQueryOptions( ) { const queryKey = getPetByIdSuspenseQueryKey({ petId }) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/react-query/src/gen/hooks/useGetUserByNameHook.ts b/examples/react-query/src/gen/hooks/useGetUserByNameHook.ts index fc1e78b7e..a8bee3b36 100644 --- a/examples/react-query/src/gen/hooks/useGetUserByNameHook.ts +++ b/examples/react-query/src/gen/hooks/useGetUserByNameHook.ts @@ -43,6 +43,7 @@ export function getUserByNameQueryOptions( ) { const queryKey = getUserByNameQueryKey({ username }) return queryOptions({ + enabled: !!username, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/react-query/src/gen/hooks/useGetUserByNameSuspenseHook.ts b/examples/react-query/src/gen/hooks/useGetUserByNameSuspenseHook.ts index 46b9ae5d2..74cd95dbc 100644 --- a/examples/react-query/src/gen/hooks/useGetUserByNameSuspenseHook.ts +++ b/examples/react-query/src/gen/hooks/useGetUserByNameSuspenseHook.ts @@ -43,6 +43,7 @@ export function getUserByNameSuspenseQueryOptions( ) { const queryKey = getUserByNameSuspenseQueryKey({ username }) return queryOptions({ + enabled: !!username, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/react-query/src/gen/hooks/useUpdatePetWithFormHook.ts b/examples/react-query/src/gen/hooks/useUpdatePetWithFormHook.ts index 9dffe1b72..93ec814b3 100644 --- a/examples/react-query/src/gen/hooks/useUpdatePetWithFormHook.ts +++ b/examples/react-query/src/gen/hooks/useUpdatePetWithFormHook.ts @@ -36,6 +36,7 @@ export function updatePetWithFormQueryOptions( ) { const queryKey = updatePetWithFormQueryKey(petId, params) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/react-query/src/gen/hooks/useUpdatePetWithFormSuspenseHook.ts b/examples/react-query/src/gen/hooks/useUpdatePetWithFormSuspenseHook.ts index b6da0377f..32328d65b 100644 --- a/examples/react-query/src/gen/hooks/useUpdatePetWithFormSuspenseHook.ts +++ b/examples/react-query/src/gen/hooks/useUpdatePetWithFormSuspenseHook.ts @@ -36,6 +36,7 @@ export function updatePetWithFormSuspenseQueryOptions( ) { const queryKey = updatePetWithFormSuspenseQueryKey(petId, params) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/simple-single/src/gen/hooks.ts b/examples/simple-single/src/gen/hooks.ts index c827e0a34..1a3878464 100644 --- a/examples/simple-single/src/gen/hooks.ts +++ b/examples/simple-single/src/gen/hooks.ts @@ -407,6 +407,7 @@ async function getPetById(petId: GetPetByIdPathParams['petId'], config: Partial< export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], config: Partial = {}) { const queryKey = getPetByIdQueryKey(petId) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal @@ -447,6 +448,7 @@ export type GetPetByIdSuspenseQueryKey = ReturnType = {}) { const queryKey = getPetByIdSuspenseQueryKey(petId) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal @@ -884,6 +886,7 @@ async function getOrderById(orderId: GetOrderByIdPathParams['orderId'], config: export function getOrderByIdQueryOptions(orderId: GetOrderByIdPathParams['orderId'], config: Partial = {}) { const queryKey = getOrderByIdQueryKey(orderId) return queryOptions({ + enabled: !!orderId, queryKey, queryFn: async ({ signal }) => { config.signal = signal @@ -925,6 +928,7 @@ export type GetOrderByIdSuspenseQueryKey = ReturnType = {}) { const queryKey = getOrderByIdSuspenseQueryKey(orderId) return queryOptions({ + enabled: !!orderId, queryKey, queryFn: async ({ signal }) => { config.signal = signal @@ -1338,6 +1342,7 @@ async function getUserByName(username: GetUserByNamePathParams['username'], conf export function getUserByNameQueryOptions(username: GetUserByNamePathParams['username'], config: Partial = {}) { const queryKey = getUserByNameQueryKey(username) return queryOptions({ + enabled: !!username, queryKey, queryFn: async ({ signal }) => { config.signal = signal @@ -1382,6 +1387,7 @@ export type GetUserByNameSuspenseQueryKey = ReturnType = {}) { const queryKey = getUserByNameSuspenseQueryKey(username) return queryOptions({ + enabled: !!username, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/solid-query/src/gen/hooks/createGetOrderById.ts b/examples/solid-query/src/gen/hooks/createGetOrderById.ts index 231652b9d..72f216a21 100644 --- a/examples/solid-query/src/gen/hooks/createGetOrderById.ts +++ b/examples/solid-query/src/gen/hooks/createGetOrderById.ts @@ -26,6 +26,7 @@ async function getOrderById(orderId: GetOrderByIdPathParams['orderId'], config: export function getOrderByIdQueryOptions(orderId: GetOrderByIdPathParams['orderId'], config: Partial = {}) { const queryKey = getOrderByIdQueryKey(orderId) return queryOptions({ + enabled: !!orderId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/solid-query/src/gen/hooks/createGetPetById.ts b/examples/solid-query/src/gen/hooks/createGetPetById.ts index e2e93be29..8c25fc854 100644 --- a/examples/solid-query/src/gen/hooks/createGetPetById.ts +++ b/examples/solid-query/src/gen/hooks/createGetPetById.ts @@ -26,6 +26,7 @@ async function getPetById(petId: GetPetByIdPathParams['petId'], config: Partial< export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], config: Partial = {}) { const queryKey = getPetByIdQueryKey(petId) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/solid-query/src/gen/hooks/createGetUserByName.ts b/examples/solid-query/src/gen/hooks/createGetUserByName.ts index 87cdd65f0..3d45112ec 100644 --- a/examples/solid-query/src/gen/hooks/createGetUserByName.ts +++ b/examples/solid-query/src/gen/hooks/createGetUserByName.ts @@ -25,6 +25,7 @@ async function getUserByName(username: GetUserByNamePathParams['username'], conf export function getUserByNameQueryOptions(username: GetUserByNamePathParams['username'], config: Partial = {}) { const queryKey = getUserByNameQueryKey(username) return queryOptions({ + enabled: !!username, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/solid-query/src/gen/hooks/createUpdatePetWithForm.ts b/examples/solid-query/src/gen/hooks/createUpdatePetWithForm.ts index 99cf3f7f7..0632a3bd2 100644 --- a/examples/solid-query/src/gen/hooks/createUpdatePetWithForm.ts +++ b/examples/solid-query/src/gen/hooks/createUpdatePetWithForm.ts @@ -36,6 +36,7 @@ export function updatePetWithFormQueryOptions( ) { const queryKey = updatePetWithFormQueryKey(petId, params) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/svelte-query/src/gen/hooks/createGetOrderById.ts b/examples/svelte-query/src/gen/hooks/createGetOrderById.ts index 4dc37b530..5850c5690 100644 --- a/examples/svelte-query/src/gen/hooks/createGetOrderById.ts +++ b/examples/svelte-query/src/gen/hooks/createGetOrderById.ts @@ -26,6 +26,7 @@ async function getOrderById(orderId: GetOrderByIdPathParams['orderId'], config: export function getOrderByIdQueryOptions(orderId: GetOrderByIdPathParams['orderId'], config: Partial = {}) { const queryKey = getOrderByIdQueryKey(orderId) return queryOptions({ + enabled: !!orderId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/svelte-query/src/gen/hooks/createGetPetById.ts b/examples/svelte-query/src/gen/hooks/createGetPetById.ts index 94322011c..4791690a5 100644 --- a/examples/svelte-query/src/gen/hooks/createGetPetById.ts +++ b/examples/svelte-query/src/gen/hooks/createGetPetById.ts @@ -26,6 +26,7 @@ async function getPetById(petId: GetPetByIdPathParams['petId'], config: Partial< export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['petId'], config: Partial = {}) { const queryKey = getPetByIdQueryKey(petId) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/svelte-query/src/gen/hooks/createGetUserByName.ts b/examples/svelte-query/src/gen/hooks/createGetUserByName.ts index b0729f806..ca3f67bbc 100644 --- a/examples/svelte-query/src/gen/hooks/createGetUserByName.ts +++ b/examples/svelte-query/src/gen/hooks/createGetUserByName.ts @@ -25,6 +25,7 @@ async function getUserByName(username: GetUserByNamePathParams['username'], conf export function getUserByNameQueryOptions(username: GetUserByNamePathParams['username'], config: Partial = {}) { const queryKey = getUserByNameQueryKey(username) return queryOptions({ + enabled: !!username, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/svelte-query/src/gen/hooks/createUpdatePetWithForm.ts b/examples/svelte-query/src/gen/hooks/createUpdatePetWithForm.ts index 0b5dcf0f6..01b8112c7 100644 --- a/examples/svelte-query/src/gen/hooks/createUpdatePetWithForm.ts +++ b/examples/svelte-query/src/gen/hooks/createUpdatePetWithForm.ts @@ -36,6 +36,7 @@ export function updatePetWithFormQueryOptions( ) { const queryKey = updatePetWithFormQueryKey(petId, params) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/vue-query/src/gen/hooks/useGetOrderById.ts b/examples/vue-query/src/gen/hooks/useGetOrderById.ts index fa9e90990..c2764be45 100644 --- a/examples/vue-query/src/gen/hooks/useGetOrderById.ts +++ b/examples/vue-query/src/gen/hooks/useGetOrderById.ts @@ -29,6 +29,7 @@ async function getOrderById(orderId: GetOrderByIdPathParams['orderId'], config: export function getOrderByIdQueryOptions(orderId: MaybeRef, config: Partial = {}) { const queryKey = getOrderByIdQueryKey(orderId) return queryOptions({ + enabled: !!orderId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/vue-query/src/gen/hooks/useGetPetById.ts b/examples/vue-query/src/gen/hooks/useGetPetById.ts index 2ab806aff..a067328d5 100644 --- a/examples/vue-query/src/gen/hooks/useGetPetById.ts +++ b/examples/vue-query/src/gen/hooks/useGetPetById.ts @@ -28,6 +28,7 @@ async function getPetById(petId: GetPetByIdPathParams['petId'], config: Partial< export function getPetByIdQueryOptions(petId: MaybeRef, config: Partial = {}) { const queryKey = getPetByIdQueryKey(petId) return queryOptions({ + enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/examples/vue-query/src/gen/hooks/useGetUserByName.ts b/examples/vue-query/src/gen/hooks/useGetUserByName.ts index 3b8766991..df1db6e22 100644 --- a/examples/vue-query/src/gen/hooks/useGetUserByName.ts +++ b/examples/vue-query/src/gen/hooks/useGetUserByName.ts @@ -28,6 +28,7 @@ async function getUserByName(username: GetUserByNamePathParams['username'], conf export function getUserByNameQueryOptions(username: MaybeRef, config: Partial = {}) { const queryKey = getUserByNameQueryKey(username) return queryOptions({ + enabled: !!username, queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/packages/plugin-react-query/src/components/InfiniteQueryOptions.tsx b/packages/plugin-react-query/src/components/InfiniteQueryOptions.tsx index 98715b4c3..c389b78f8 100644 --- a/packages/plugin-react-query/src/components/InfiniteQueryOptions.tsx +++ b/packages/plugin-react-query/src/components/InfiniteQueryOptions.tsx @@ -99,14 +99,23 @@ export function InfiniteQueryOptions({ }` : '' + const enabled = Object.entries(queryKeyParams.flatParams) + .map(([key, item]) => (item && !item.optional ? key : undefined)) + .filter(Boolean) + .join('&& ') + + const enabledText = enabled ? `enabled: !!(${enabled})` : '' + return ( {` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return infiniteQueryOptions({ + ${enabledText} queryKey, - queryFn: async ({ pageParam }) => { + queryFn: async ({ signal, pageParam }) => { + config.signal = signal ${infiniteOverrideParams} return ${clientName}(${clientParams.toCall()}) }, diff --git a/packages/plugin-react-query/src/components/QueryOptions.tsx b/packages/plugin-react-query/src/components/QueryOptions.tsx index d762c0442..9b72b17a0 100644 --- a/packages/plugin-react-query/src/components/QueryOptions.tsx +++ b/packages/plugin-react-query/src/components/QueryOptions.tsx @@ -64,12 +64,20 @@ export function QueryOptions({ name, clientName, typeSchemas, pathParamsType, qu typeSchemas, }) + const enabled = Object.entries(queryKeyParams.flatParams) + .map(([key, item]) => (item && !item.optional ? key : undefined)) + .filter(Boolean) + .join('&& ') + + const enabledText = enabled ? `enabled: !!(${enabled})` : '' + return ( {` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return queryOptions({ + ${enabledText} queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findInfiniteByTags.ts b/packages/plugin-react-query/src/generators/__snapshots__/findInfiniteByTags.ts index b1ddcb35b..1ee1c8847 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findInfiniteByTags.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findInfiniteByTags.ts @@ -21,7 +21,8 @@ async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: Find 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"]; } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findInfiniteByTagsCursor.ts b/packages/plugin-react-query/src/generators/__snapshots__/findInfiniteByTagsCursor.ts index e5f075076..32d80cad0 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findInfiniteByTagsCursor.ts @@ -21,7 +21,8 @@ async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: Find 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"]; } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/postAsQuery.ts b/packages/plugin-react-query/src/generators/__snapshots__/postAsQuery.ts index aed24c0f6..ce883fde5 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/postAsQuery.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/postAsQuery.ts @@ -19,6 +19,7 @@ async function updatePetWithForm(petId: UpdatePetWithFormPathParams["petId"], da export function updatePetWithFormQueryOptions(petId: UpdatePetWithFormPathParams["petId"], data?: UpdatePetWithFormMutationRequest, params?: UpdatePetWithFormQueryParams, config: Partial> = {}) { const queryKey = updatePetWithFormQueryKey(petId, data, params); return queryOptions({ + enabled: !!(petId), queryKey, queryFn: async ({ signal }) => { config.signal = signal; diff --git a/packages/plugin-solid-query/src/components/QueryOptions.tsx b/packages/plugin-solid-query/src/components/QueryOptions.tsx index 77010b599..cba6d41fb 100644 --- a/packages/plugin-solid-query/src/components/QueryOptions.tsx +++ b/packages/plugin-solid-query/src/components/QueryOptions.tsx @@ -64,12 +64,20 @@ export function QueryOptions({ name, clientName, typeSchemas, pathParamsType, qu typeSchemas, }) + const enabled = Object.entries(queryKeyParams.flatParams) + .map(([key, item]) => (item && !item.optional ? key : undefined)) + .filter(Boolean) + .join('&& ') + + const enabledText = enabled ? `enabled: !!(${enabled})` : '' + return ( {` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return queryOptions({ + ${enabledText} queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/packages/plugin-solid-query/src/generators/__snapshots__/postAsQuery.ts b/packages/plugin-solid-query/src/generators/__snapshots__/postAsQuery.ts index 884fee7bf..ab85dfcd6 100644 --- a/packages/plugin-solid-query/src/generators/__snapshots__/postAsQuery.ts +++ b/packages/plugin-solid-query/src/generators/__snapshots__/postAsQuery.ts @@ -19,6 +19,7 @@ async function updatePetWithForm(petId: UpdatePetWithFormPathParams["petId"], da export function updatePetWithFormQueryOptions(petId: UpdatePetWithFormPathParams["petId"], data?: UpdatePetWithFormMutationRequest, params?: UpdatePetWithFormQueryParams, config: Partial> = {}) { const queryKey = updatePetWithFormQueryKey(petId, data, params); return queryOptions({ + enabled: !!(petId), queryKey, queryFn: async ({ signal }) => { config.signal = signal; diff --git a/packages/plugin-svelte-query/src/components/QueryOptions.tsx b/packages/plugin-svelte-query/src/components/QueryOptions.tsx index 85ea23200..bf7394733 100644 --- a/packages/plugin-svelte-query/src/components/QueryOptions.tsx +++ b/packages/plugin-svelte-query/src/components/QueryOptions.tsx @@ -64,12 +64,20 @@ export function QueryOptions({ name, clientName, typeSchemas, pathParamsType, qu typeSchemas, }) + const enabled = Object.entries(queryKeyParams.flatParams) + .map(([key, item]) => (item && !item.optional ? key : undefined)) + .filter(Boolean) + .join('&& ') + + const enabledText = enabled ? `enabled: !!(${enabled})` : '' + return ( {` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return queryOptions({ + ${enabledText} queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/packages/plugin-svelte-query/src/generators/__snapshots__/postAsQuery.ts b/packages/plugin-svelte-query/src/generators/__snapshots__/postAsQuery.ts index 1bb0af68d..eb1a83e1e 100644 --- a/packages/plugin-svelte-query/src/generators/__snapshots__/postAsQuery.ts +++ b/packages/plugin-svelte-query/src/generators/__snapshots__/postAsQuery.ts @@ -19,6 +19,7 @@ async function updatePetWithForm(petId: UpdatePetWithFormPathParams["petId"], da export function updatePetWithFormQueryOptions(petId: UpdatePetWithFormPathParams["petId"], data?: UpdatePetWithFormMutationRequest, params?: UpdatePetWithFormQueryParams, config: Partial> = {}) { const queryKey = updatePetWithFormQueryKey(petId, data, params); return queryOptions({ + enabled: !!(petId), queryKey, queryFn: async ({ signal }) => { config.signal = signal; diff --git a/packages/plugin-vue-query/src/components/InfiniteQueryOptions.tsx b/packages/plugin-vue-query/src/components/InfiniteQueryOptions.tsx index 60f7973a2..e46d4dce0 100644 --- a/packages/plugin-vue-query/src/components/InfiniteQueryOptions.tsx +++ b/packages/plugin-vue-query/src/components/InfiniteQueryOptions.tsx @@ -107,14 +107,23 @@ export function InfiniteQueryOptions({ }` : '' + const enabled = Object.entries(queryKeyParams.flatParams) + .map(([key, item]) => (item && !item.optional ? key : undefined)) + .filter(Boolean) + .join('&& ') + + const enabledText = enabled ? `enabled: !!(${enabled})` : '' + return ( {` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return infiniteQueryOptions({ + ${enabledText} queryKey, - queryFn: async ({ pageParam }) => { + queryFn: async ({ signal, pageParam }) => { + config.signal = signal ${infiniteOverrideParams} return ${clientName}(${clientParams.toCall()}) }, diff --git a/packages/plugin-vue-query/src/components/QueryOptions.tsx b/packages/plugin-vue-query/src/components/QueryOptions.tsx index 1fb6a055a..011d0cba0 100644 --- a/packages/plugin-vue-query/src/components/QueryOptions.tsx +++ b/packages/plugin-vue-query/src/components/QueryOptions.tsx @@ -72,12 +72,20 @@ export function QueryOptions({ name, clientName, typeSchemas, pathParamsType, qu typeSchemas, }) + const enabled = Object.entries(queryKeyParams.flatParams) + .map(([key, item]) => (item && !item.optional ? key : undefined)) + .filter(Boolean) + .join('&& ') + + const enabledText = enabled ? `enabled: !!(${enabled})` : '' + return ( {` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return queryOptions({ + ${enabledText} queryKey, queryFn: async ({ signal }) => { config.signal = signal diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTags.ts b/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTags.ts index 5f0bff6c5..4283ea9a8 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTags.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTags.ts @@ -22,7 +22,8 @@ async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: Find 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"]; } diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTagsCursor.ts b/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTagsCursor.ts index 6a416cdc9..3ef4910b8 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTagsCursor.ts @@ -22,7 +22,8 @@ async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: Find 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"]; } diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/postAsQuery.ts b/packages/plugin-vue-query/src/generators/__snapshots__/postAsQuery.ts index 5e719fae4..299410992 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/postAsQuery.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/postAsQuery.ts @@ -21,6 +21,7 @@ async function updatePetWithForm(petId: UpdatePetWithFormPathParams["petId"], da export function updatePetWithFormQueryOptions(petId: MaybeRef, data?: MaybeRef, params?: MaybeRef, config: Partial> = {}) { const queryKey = updatePetWithFormQueryKey(petId, data, params); return queryOptions({ + enabled: !!(petId), queryKey, queryFn: async ({ signal }) => { config.signal = signal; diff --git a/packages/react/src/utils/getFunctionParams.ts b/packages/react/src/utils/getFunctionParams.ts index 7f6d928c3..2ebb1c4da 100644 --- a/packages/react/src/utils/getFunctionParams.ts +++ b/packages/react/src/utils/getFunctionParams.ts @@ -155,7 +155,7 @@ export function getFunctionParams(params: Params, options: Options): string { export function createFunctionParams(params: Params): Params { return params } - +//TODO use of string as `$name: $type` to create templates for functions instead of call/constructor export class FunctionParams { #params: Params @@ -166,10 +166,24 @@ export class FunctionParams { this.#params = params } - get params() { + get params(): Params { return this.#params } + get flatParams(): Params { + const flatter = (acc: Params, [key, item]: [key: string, item?: Param]): Params => { + if (item?.children) { + return Object.entries(item.children).reduce(flatter, acc) + } + if (item) { + acc[key] = item + } + + return acc + } + return Object.entries(this.#params).reduce(flatter, {} as Params) + } + toCall({ transformName, transformType }: Pick = {}): string { return getFunctionParams(this.#params, { type: 'call', transformName, transformType }) }