diff --git a/.changeset/famous-crews-pay.md b/.changeset/famous-crews-pay.md new file mode 100644 index 000000000..b0b36e310 --- /dev/null +++ b/.changeset/famous-crews-pay.md @@ -0,0 +1,5 @@ +--- +"@kubb/swagger-tanstack-query": minor +--- + +Add `cursorParam` to use infinite with a cursor. diff --git a/docs/plugins/swagger-tanstack-query/index.md b/docs/plugins/swagger-tanstack-query/index.md index d71abced0..531c385ab 100644 --- a/docs/plugins/swagger-tanstack-query/index.md +++ b/docs/plugins/swagger-tanstack-query/index.md @@ -421,7 +421,11 @@ type Infinite = { */ queryParam: string /** - * For v5 + * Which field of the data will be used, set it to undefined when no cursor is known. + */ + cursorParam: string | undefined + /** + * The initial value, the value of the first page. * @default `0` */ initialPageParam: number diff --git a/examples/react-query-v5/kubb.config.js b/examples/react-query-v5/kubb.config.js index ea51266bf..442fd4a4e 100644 --- a/examples/react-query-v5/kubb.config.js +++ b/examples/react-query-v5/kubb.config.js @@ -35,8 +35,18 @@ export default defineConfig({ path: './hooks', }, framework: 'react', - infinite: {}, suspense: {}, + override: [{ + type: 'operationId', + pattern: 'findPetsByTags', + options: { + infinite: { + queryParam: 'pageSize', + initialPageParam: 0, + cursorParam: undefined, + }, + }, + }], }), ], }) diff --git a/examples/react-query-v5/src/gen/hooks/useFindPetsByStatusHook.ts b/examples/react-query-v5/src/gen/hooks/useFindPetsByStatusHook.ts index 7af2f3dd6..f3ccdbb60 100644 --- a/examples/react-query-v5/src/gen/hooks/useFindPetsByStatusHook.ts +++ b/examples/react-query-v5/src/gen/hooks/useFindPetsByStatusHook.ts @@ -1,15 +1,7 @@ import client from '@kubb/swagger-client/client' -import { useQuery, queryOptions, useInfiniteQuery, infiniteQueryOptions, useSuspenseQuery } from '@tanstack/react-query' +import { useQuery, queryOptions, useSuspenseQuery } from '@tanstack/react-query' import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../models/FindPetsByStatus' -import type { - QueryObserverOptions, - UseQueryResult, - QueryKey, - InfiniteQueryObserverOptions, - UseInfiniteQueryResult, - UseSuspenseQueryOptions, - UseSuspenseQueryResult, -} from '@tanstack/react-query' +import type { QueryObserverOptions, UseQueryResult, QueryKey, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query' type FindPetsByStatusClient = typeof client type FindPetsByStatus = { @@ -59,7 +51,7 @@ export function useFindPetsByStatusHook< const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? findPetsByStatusQueryKey(params) const query = useQuery({ - ...findPetsByStatusQueryOptions(params, clientOptions), + ...findPetsByStatusQueryOptions(params, clientOptions) as QueryObserverOptions, queryKey, ...queryOptions as unknown as QueryObserverOptions, }) as UseQueryResult & { @@ -68,56 +60,6 @@ export function useFindPetsByStatusHook< query.queryKey = queryKey as TQueryKey return query } -export const findPetsByStatusInfiniteQueryKey = (params?: FindPetsByStatus['queryParams']) => - [{ url: '/pet/findByStatus' }, ...(params ? [params] : [])] as const -export type FindPetsByStatusInfiniteQueryKey = ReturnType -export function findPetsByStatusInfiniteQueryOptions(params?: FindPetsByStatus['queryParams'], options: FindPetsByStatus['client']['parameters'] = {}) { - const queryKey = findPetsByStatusInfiniteQueryKey(params) - return infiniteQueryOptions({ - queryKey, - queryFn: async ({ pageParam }) => { - const res = await client({ - method: 'get', - url: `/pet/findByStatus`, - ...options, - params: { - ...params, - ['id']: pageParam, - ...(options.params || {}), - }, - }) - return res.data - }, - initialPageParam: 0, - getNextPageParam: (lastPage) => lastPage['id'], - }) -} -/** - * @description Multiple status values can be provided with comma separated strings - * @summary Finds Pets by status - * @link /pet/findByStatus */ -export function useFindPetsByStatusHookInfinite< - TData = FindPetsByStatus['response'], - TQueryData = FindPetsByStatus['response'], - TQueryKey extends QueryKey = FindPetsByStatusInfiniteQueryKey, ->(params?: FindPetsByStatus['queryParams'], options: { - query?: InfiniteQueryObserverOptions - client?: FindPetsByStatus['client']['parameters'] -} = {}): UseInfiniteQueryResult & { - queryKey: TQueryKey -} { - const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const queryKey = queryOptions?.queryKey ?? findPetsByStatusInfiniteQueryKey(params) - const query = useInfiniteQuery({ - ...findPetsByStatusInfiniteQueryOptions(params, clientOptions), - queryKey, - ...queryOptions as unknown as InfiniteQueryObserverOptions, - }) as UseInfiniteQueryResult & { - queryKey: TQueryKey - } - query.queryKey = queryKey as TQueryKey - return query -} export const findPetsByStatusSuspenseQueryKey = (params?: FindPetsByStatus['queryParams']) => [{ url: '/pet/findByStatus' }, ...(params ? [params] : [])] as const export type FindPetsByStatusSuspenseQueryKey = ReturnType @@ -152,7 +94,7 @@ export function useFindPetsByStatusHookSuspense & { diff --git a/examples/react-query-v5/src/gen/hooks/useFindPetsByTagsHook.ts b/examples/react-query-v5/src/gen/hooks/useFindPetsByTagsHook.ts index 1c8a4a8cd..46f44b4b6 100644 --- a/examples/react-query-v5/src/gen/hooks/useFindPetsByTagsHook.ts +++ b/examples/react-query-v5/src/gen/hooks/useFindPetsByTagsHook.ts @@ -59,7 +59,7 @@ export function useFindPetsByTagsHook< const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params) const query = useQuery({ - ...findPetsByTagsQueryOptions(params, clientOptions), + ...findPetsByTagsQueryOptions(params, clientOptions) as QueryObserverOptions, queryKey, ...queryOptions as unknown as QueryObserverOptions, }) as UseQueryResult & { @@ -81,14 +81,15 @@ export function findPetsByTagsInfiniteQueryOptions(params?: FindPetsByTags['quer ...options, params: { ...params, - ['id']: pageParam, + ['pageSize']: pageParam, ...(options.params || {}), }, }) return res.data }, initialPageParam: 0, - getNextPageParam: (lastPage) => lastPage['id'], + getNextPageParam: (lastPage, allPages, lastPageParam) => lastPage.length === 0 ? undefined : lastPageParam + 1, + getPreviousPageParam: (firstPage, allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1, }) } /** @@ -108,7 +109,7 @@ export function useFindPetsByTagsHookInfinite< const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? findPetsByTagsInfiniteQueryKey(params) const query = useInfiniteQuery({ - ...findPetsByTagsInfiniteQueryOptions(params, clientOptions), + ...findPetsByTagsInfiniteQueryOptions(params, clientOptions) as InfiniteQueryObserverOptions, queryKey, ...queryOptions as unknown as InfiniteQueryObserverOptions, }) as UseInfiniteQueryResult & { @@ -150,7 +151,7 @@ export function useFindPetsByTagsHookSuspense & { diff --git a/examples/react-query-v5/src/gen/hooks/useGetInventoryHook.ts b/examples/react-query-v5/src/gen/hooks/useGetInventoryHook.ts index 145ca8102..d77dbc885 100644 --- a/examples/react-query-v5/src/gen/hooks/useGetInventoryHook.ts +++ b/examples/react-query-v5/src/gen/hooks/useGetInventoryHook.ts @@ -1,15 +1,7 @@ import client from '@kubb/swagger-client/client' -import { useQuery, queryOptions, useInfiniteQuery, infiniteQueryOptions, useSuspenseQuery } from '@tanstack/react-query' +import { useQuery, queryOptions, useSuspenseQuery } from '@tanstack/react-query' import type { GetInventoryQueryResponse } from '../models/GetInventory' -import type { - QueryObserverOptions, - UseQueryResult, - QueryKey, - InfiniteQueryObserverOptions, - UseInfiniteQueryResult, - UseSuspenseQueryOptions, - UseSuspenseQueryResult, -} from '@tanstack/react-query' +import type { QueryObserverOptions, UseQueryResult, QueryKey, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query' type GetInventoryClient = typeof client type GetInventory = { @@ -56,7 +48,7 @@ export function useGetInventoryHook & { @@ -65,50 +57,6 @@ export function useGetInventoryHook [{ url: '/store/inventory' }] as const -export type GetInventoryInfiniteQueryKey = ReturnType -export function getInventoryInfiniteQueryOptions(options: GetInventory['client']['parameters'] = {}) { - const queryKey = getInventoryInfiniteQueryKey() - return infiniteQueryOptions({ - queryKey, - queryFn: async ({ pageParam }) => { - const res = await client({ - method: 'get', - url: `/store/inventory`, - ...options, - }) - return res.data - }, - initialPageParam: 0, - getNextPageParam: (lastPage) => lastPage['id'], - }) -} -/** - * @description Returns a map of status codes to quantities - * @summary Returns pet inventories by status - * @link /store/inventory */ -export function useGetInventoryHookInfinite< - TData = GetInventory['response'], - TQueryData = GetInventory['response'], - TQueryKey extends QueryKey = GetInventoryInfiniteQueryKey, ->(options: { - query?: InfiniteQueryObserverOptions - client?: GetInventory['client']['parameters'] -} = {}): UseInfiniteQueryResult & { - queryKey: TQueryKey -} { - const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const queryKey = queryOptions?.queryKey ?? getInventoryInfiniteQueryKey() - const query = useInfiniteQuery({ - ...getInventoryInfiniteQueryOptions(clientOptions), - queryKey, - ...queryOptions as unknown as InfiniteQueryObserverOptions, - }) as UseInfiniteQueryResult & { - queryKey: TQueryKey - } - query.queryKey = queryKey as TQueryKey - return query -} export const getInventorySuspenseQueryKey = () => [{ url: '/store/inventory' }] as const export type GetInventorySuspenseQueryKey = ReturnType export function getInventorySuspenseQueryOptions(options: GetInventory['client']['parameters'] = {}) { @@ -138,7 +86,7 @@ export function useGetInventoryHookSuspense & { diff --git a/examples/react-query-v5/src/gen/hooks/useGetOrderByIdHook.ts b/examples/react-query-v5/src/gen/hooks/useGetOrderByIdHook.ts index d50810676..6e4ba453d 100644 --- a/examples/react-query-v5/src/gen/hooks/useGetOrderByIdHook.ts +++ b/examples/react-query-v5/src/gen/hooks/useGetOrderByIdHook.ts @@ -1,15 +1,7 @@ import client from '@kubb/swagger-client/client' -import { useQuery, queryOptions, useInfiniteQuery, infiniteQueryOptions, useSuspenseQuery } from '@tanstack/react-query' +import { useQuery, queryOptions, useSuspenseQuery } from '@tanstack/react-query' import type { GetOrderByIdQueryResponse, GetOrderByIdPathParams, GetOrderById400, GetOrderById404 } from '../models/GetOrderById' -import type { - QueryObserverOptions, - UseQueryResult, - QueryKey, - InfiniteQueryObserverOptions, - UseInfiniteQueryResult, - UseSuspenseQueryOptions, - UseSuspenseQueryResult, -} from '@tanstack/react-query' +import type { QueryObserverOptions, UseQueryResult, QueryKey, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query' type GetOrderByIdClient = typeof client type GetOrderById = { @@ -57,7 +49,7 @@ export function useGetOrderByIdHook & { @@ -66,51 +58,6 @@ export function useGetOrderByIdHook - [{ url: '/store/order/:orderId', params: { orderId: orderId } }] as const -export type GetOrderByIdInfiniteQueryKey = ReturnType -export function getOrderByIdInfiniteQueryOptions(orderId: GetOrderByIdPathParams['orderId'], options: GetOrderById['client']['parameters'] = {}) { - const queryKey = getOrderByIdInfiniteQueryKey(orderId) - return infiniteQueryOptions({ - queryKey, - queryFn: async ({ pageParam }) => { - const res = await client({ - method: 'get', - url: `/store/order/${orderId}`, - ...options, - }) - return res.data - }, - initialPageParam: 0, - getNextPageParam: (lastPage) => lastPage['id'], - }) -} -/** - * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. - * @summary Find purchase order by ID - * @link /store/order/:orderId */ -export function useGetOrderByIdHookInfinite< - TData = GetOrderById['response'], - TQueryData = GetOrderById['response'], - TQueryKey extends QueryKey = GetOrderByIdInfiniteQueryKey, ->(orderId: GetOrderByIdPathParams['orderId'], options: { - query?: InfiniteQueryObserverOptions - client?: GetOrderById['client']['parameters'] -} = {}): UseInfiniteQueryResult & { - queryKey: TQueryKey -} { - const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const queryKey = queryOptions?.queryKey ?? getOrderByIdInfiniteQueryKey(orderId) - const query = useInfiniteQuery({ - ...getOrderByIdInfiniteQueryOptions(orderId, clientOptions), - queryKey, - ...queryOptions as unknown as InfiniteQueryObserverOptions, - }) as UseInfiniteQueryResult & { - queryKey: TQueryKey - } - query.queryKey = queryKey as TQueryKey - return query -} export const getOrderByIdSuspenseQueryKey = (orderId: GetOrderByIdPathParams['orderId']) => [{ url: '/store/order/:orderId', params: { orderId: orderId } }] as const export type GetOrderByIdSuspenseQueryKey = ReturnType @@ -144,7 +91,7 @@ export function useGetOrderByIdHookSuspense & { diff --git a/examples/react-query-v5/src/gen/hooks/useGetPetByIdHook.ts b/examples/react-query-v5/src/gen/hooks/useGetPetByIdHook.ts index 3bf69437f..1bea1aae8 100644 --- a/examples/react-query-v5/src/gen/hooks/useGetPetByIdHook.ts +++ b/examples/react-query-v5/src/gen/hooks/useGetPetByIdHook.ts @@ -1,15 +1,7 @@ import client from '@kubb/swagger-client/client' -import { useQuery, queryOptions, useInfiniteQuery, infiniteQueryOptions, useSuspenseQuery } from '@tanstack/react-query' +import { useQuery, queryOptions, useSuspenseQuery } from '@tanstack/react-query' import type { GetPetByIdQueryResponse, GetPetByIdPathParams, GetPetById400, GetPetById404 } from '../models/GetPetById' -import type { - QueryObserverOptions, - UseQueryResult, - QueryKey, - InfiniteQueryObserverOptions, - UseInfiniteQueryResult, - UseSuspenseQueryOptions, - UseSuspenseQueryResult, -} from '@tanstack/react-query' +import type { QueryObserverOptions, UseQueryResult, QueryKey, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query' type GetPetByIdClient = typeof client type GetPetById = { @@ -57,7 +49,7 @@ export function useGetPetByIdHook & { @@ -66,50 +58,6 @@ export function useGetPetByIdHook [{ url: '/pet/:petId', params: { petId: petId } }] as const -export type GetPetByIdInfiniteQueryKey = ReturnType -export function getPetByIdInfiniteQueryOptions(petId: GetPetByIdPathParams['petId'], options: GetPetById['client']['parameters'] = {}) { - const queryKey = getPetByIdInfiniteQueryKey(petId) - return infiniteQueryOptions({ - queryKey, - queryFn: async ({ pageParam }) => { - const res = await client({ - method: 'get', - url: `/pet/${petId}`, - ...options, - }) - return res.data - }, - initialPageParam: 0, - getNextPageParam: (lastPage) => lastPage['id'], - }) -} -/** - * @description Returns a single pet - * @summary Find pet by ID - * @link /pet/:petId */ -export function useGetPetByIdHookInfinite< - TData = GetPetById['response'], - TQueryData = GetPetById['response'], - TQueryKey extends QueryKey = GetPetByIdInfiniteQueryKey, ->(petId: GetPetByIdPathParams['petId'], options: { - query?: InfiniteQueryObserverOptions - client?: GetPetById['client']['parameters'] -} = {}): UseInfiniteQueryResult & { - queryKey: TQueryKey -} { - const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const queryKey = queryOptions?.queryKey ?? getPetByIdInfiniteQueryKey(petId) - const query = useInfiniteQuery({ - ...getPetByIdInfiniteQueryOptions(petId, clientOptions), - queryKey, - ...queryOptions as unknown as InfiniteQueryObserverOptions, - }) as UseInfiniteQueryResult & { - queryKey: TQueryKey - } - query.queryKey = queryKey as TQueryKey - return query -} export const getPetByIdSuspenseQueryKey = (petId: GetPetByIdPathParams['petId']) => [{ url: '/pet/:petId', params: { petId: petId } }] as const export type GetPetByIdSuspenseQueryKey = ReturnType export function getPetByIdSuspenseQueryOptions(petId: GetPetByIdPathParams['petId'], options: GetPetById['client']['parameters'] = {}) { @@ -142,7 +90,7 @@ export function useGetPetByIdHookSuspense & { diff --git a/examples/react-query-v5/src/gen/hooks/useGetUserByNameHook.ts b/examples/react-query-v5/src/gen/hooks/useGetUserByNameHook.ts index 8bee90ccd..647fd6070 100644 --- a/examples/react-query-v5/src/gen/hooks/useGetUserByNameHook.ts +++ b/examples/react-query-v5/src/gen/hooks/useGetUserByNameHook.ts @@ -1,15 +1,7 @@ import client from '@kubb/swagger-client/client' -import { useQuery, queryOptions, useInfiniteQuery, infiniteQueryOptions, useSuspenseQuery } from '@tanstack/react-query' +import { useQuery, queryOptions, useSuspenseQuery } from '@tanstack/react-query' import type { GetUserByNameQueryResponse, GetUserByNamePathParams, GetUserByName400, GetUserByName404 } from '../models/GetUserByName' -import type { - QueryObserverOptions, - UseQueryResult, - QueryKey, - InfiniteQueryObserverOptions, - UseInfiniteQueryResult, - UseSuspenseQueryOptions, - UseSuspenseQueryResult, -} from '@tanstack/react-query' +import type { QueryObserverOptions, UseQueryResult, QueryKey, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query' type GetUserByNameClient = typeof client type GetUserByName = { @@ -57,7 +49,7 @@ export function useGetUserByNameHook< const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? getUserByNameQueryKey(username) const query = useQuery({ - ...getUserByNameQueryOptions(username, clientOptions), + ...getUserByNameQueryOptions(username, clientOptions) as QueryObserverOptions, queryKey, ...queryOptions as unknown as QueryObserverOptions, }) as UseQueryResult & { @@ -66,50 +58,6 @@ export function useGetUserByNameHook< query.queryKey = queryKey as TQueryKey return query } -export const getUserByNameInfiniteQueryKey = (username: GetUserByNamePathParams['username']) => - [{ url: '/user/:username', params: { username: username } }] as const -export type GetUserByNameInfiniteQueryKey = ReturnType -export function getUserByNameInfiniteQueryOptions(username: GetUserByNamePathParams['username'], options: GetUserByName['client']['parameters'] = {}) { - const queryKey = getUserByNameInfiniteQueryKey(username) - return infiniteQueryOptions({ - queryKey, - queryFn: async ({ pageParam }) => { - const res = await client({ - method: 'get', - url: `/user/${username}`, - ...options, - }) - return res.data - }, - initialPageParam: 0, - getNextPageParam: (lastPage) => lastPage['id'], - }) -} -/** - * @summary Get user by user name - * @link /user/:username */ -export function useGetUserByNameHookInfinite< - TData = GetUserByName['response'], - TQueryData = GetUserByName['response'], - TQueryKey extends QueryKey = GetUserByNameInfiniteQueryKey, ->(username: GetUserByNamePathParams['username'], options: { - query?: InfiniteQueryObserverOptions - client?: GetUserByName['client']['parameters'] -} = {}): UseInfiniteQueryResult & { - queryKey: TQueryKey -} { - const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const queryKey = queryOptions?.queryKey ?? getUserByNameInfiniteQueryKey(username) - const query = useInfiniteQuery({ - ...getUserByNameInfiniteQueryOptions(username, clientOptions), - queryKey, - ...queryOptions as unknown as InfiniteQueryObserverOptions, - }) as UseInfiniteQueryResult & { - queryKey: TQueryKey - } - query.queryKey = queryKey as TQueryKey - return query -} export const getUserByNameSuspenseQueryKey = (username: GetUserByNamePathParams['username']) => [{ url: '/user/:username', params: { username: username } }] as const export type GetUserByNameSuspenseQueryKey = ReturnType @@ -142,7 +90,7 @@ export function useGetUserByNameHookSuspense & { diff --git a/examples/react-query-v5/src/gen/hooks/useLoginUserHook.ts b/examples/react-query-v5/src/gen/hooks/useLoginUserHook.ts index eaec986ea..2d4901f9f 100644 --- a/examples/react-query-v5/src/gen/hooks/useLoginUserHook.ts +++ b/examples/react-query-v5/src/gen/hooks/useLoginUserHook.ts @@ -1,15 +1,7 @@ import client from '@kubb/swagger-client/client' -import { useQuery, queryOptions, useInfiniteQuery, infiniteQueryOptions, useSuspenseQuery } from '@tanstack/react-query' +import { useQuery, queryOptions, useSuspenseQuery } from '@tanstack/react-query' import type { LoginUserQueryResponse, LoginUserQueryParams, LoginUser400 } from '../models/LoginUser' -import type { - QueryObserverOptions, - UseQueryResult, - QueryKey, - InfiniteQueryObserverOptions, - UseInfiniteQueryResult, - UseSuspenseQueryOptions, - UseSuspenseQueryResult, -} from '@tanstack/react-query' +import type { QueryObserverOptions, UseQueryResult, QueryKey, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query' type LoginUserClient = typeof client type LoginUser = { @@ -57,7 +49,7 @@ export function useLoginUserHook & { @@ -66,54 +58,6 @@ export function useLoginUserHook [{ url: '/user/login' }, ...(params ? [params] : [])] as const -export type LoginUserInfiniteQueryKey = ReturnType -export function loginUserInfiniteQueryOptions(params?: LoginUser['queryParams'], options: LoginUser['client']['parameters'] = {}) { - const queryKey = loginUserInfiniteQueryKey(params) - return infiniteQueryOptions({ - queryKey, - queryFn: async ({ pageParam }) => { - const res = await client({ - method: 'get', - url: `/user/login`, - ...options, - params: { - ...params, - ['id']: pageParam, - ...(options.params || {}), - }, - }) - return res.data - }, - initialPageParam: 0, - getNextPageParam: (lastPage) => lastPage['id'], - }) -} -/** - * @summary Logs user into the system - * @link /user/login */ -export function useLoginUserHookInfinite< - TData = LoginUser['response'], - TQueryData = LoginUser['response'], - TQueryKey extends QueryKey = LoginUserInfiniteQueryKey, ->(params?: LoginUser['queryParams'], options: { - query?: InfiniteQueryObserverOptions - client?: LoginUser['client']['parameters'] -} = {}): UseInfiniteQueryResult & { - queryKey: TQueryKey -} { - const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const queryKey = queryOptions?.queryKey ?? loginUserInfiniteQueryKey(params) - const query = useInfiniteQuery({ - ...loginUserInfiniteQueryOptions(params, clientOptions), - queryKey, - ...queryOptions as unknown as InfiniteQueryObserverOptions, - }) as UseInfiniteQueryResult & { - queryKey: TQueryKey - } - query.queryKey = queryKey as TQueryKey - return query -} export const loginUserSuspenseQueryKey = (params?: LoginUser['queryParams']) => [{ url: '/user/login' }, ...(params ? [params] : [])] as const export type LoginUserSuspenseQueryKey = ReturnType export function loginUserSuspenseQueryOptions(params?: LoginUser['queryParams'], options: LoginUser['client']['parameters'] = {}) { @@ -146,7 +90,7 @@ export function useLoginUserHookSuspense & { diff --git a/examples/react-query-v5/src/gen/hooks/useLogoutUserHook.ts b/examples/react-query-v5/src/gen/hooks/useLogoutUserHook.ts index 4a5528de3..c6c011ba8 100644 --- a/examples/react-query-v5/src/gen/hooks/useLogoutUserHook.ts +++ b/examples/react-query-v5/src/gen/hooks/useLogoutUserHook.ts @@ -1,15 +1,7 @@ import client from '@kubb/swagger-client/client' -import { useQuery, queryOptions, useInfiniteQuery, infiniteQueryOptions, useSuspenseQuery } from '@tanstack/react-query' +import { useQuery, queryOptions, useSuspenseQuery } from '@tanstack/react-query' import type { LogoutUserQueryResponse, LogoutUserError } from '../models/LogoutUser' -import type { - QueryObserverOptions, - UseQueryResult, - QueryKey, - InfiniteQueryObserverOptions, - UseInfiniteQueryResult, - UseSuspenseQueryOptions, - UseSuspenseQueryResult, -} from '@tanstack/react-query' +import type { QueryObserverOptions, UseQueryResult, QueryKey, UseSuspenseQueryOptions, UseSuspenseQueryResult } from '@tanstack/react-query' type LogoutUserClient = typeof client type LogoutUser = { @@ -55,7 +47,7 @@ export function useLogoutUserHook & { @@ -64,49 +56,6 @@ export function useLogoutUserHook [{ url: '/user/logout' }] as const -export type LogoutUserInfiniteQueryKey = ReturnType -export function logoutUserInfiniteQueryOptions(options: LogoutUser['client']['parameters'] = {}) { - const queryKey = logoutUserInfiniteQueryKey() - return infiniteQueryOptions({ - queryKey, - queryFn: async ({ pageParam }) => { - const res = await client({ - method: 'get', - url: `/user/logout`, - ...options, - }) - return res.data - }, - initialPageParam: 0, - getNextPageParam: (lastPage) => lastPage['id'], - }) -} -/** - * @summary Logs out current logged in user session - * @link /user/logout */ -export function useLogoutUserHookInfinite< - TData = LogoutUser['response'], - TQueryData = LogoutUser['response'], - TQueryKey extends QueryKey = LogoutUserInfiniteQueryKey, ->(options: { - query?: InfiniteQueryObserverOptions - client?: LogoutUser['client']['parameters'] -} = {}): UseInfiniteQueryResult & { - queryKey: TQueryKey -} { - const { query: queryOptions, client: clientOptions = {} } = options ?? {} - const queryKey = queryOptions?.queryKey ?? logoutUserInfiniteQueryKey() - const query = useInfiniteQuery({ - ...logoutUserInfiniteQueryOptions(clientOptions), - queryKey, - ...queryOptions as unknown as InfiniteQueryObserverOptions, - }) as UseInfiniteQueryResult & { - queryKey: TQueryKey - } - query.queryKey = queryKey as TQueryKey - return query -} export const logoutUserSuspenseQueryKey = () => [{ url: '/user/logout' }] as const export type LogoutUserSuspenseQueryKey = ReturnType export function logoutUserSuspenseQueryOptions(options: LogoutUser['client']['parameters'] = {}) { @@ -135,7 +84,7 @@ export function useLogoutUserHookSuspense & { diff --git a/examples/react-query-v5/tsconfig.json b/examples/react-query-v5/tsconfig.json index c1f835ec2..ebabe27ca 100644 --- a/examples/react-query-v5/tsconfig.json +++ b/examples/react-query-v5/tsconfig.json @@ -15,7 +15,9 @@ "esModuleInterop": true, "allowJs": true, "allowImportingTsExtensions": true, - "noEmit": true + "noEmit": true, + "strict": true, + "noUncheckedIndexedAccess": true }, "include": [ "./src/**/*" diff --git a/examples/vue-query-v5/src/gen/hooks/useFindPetsByStatus.ts b/examples/vue-query-v5/src/gen/hooks/useFindPetsByStatus.ts index 71956b6dc..6fdb684b3 100644 --- a/examples/vue-query-v5/src/gen/hooks/useFindPetsByStatus.ts +++ b/examples/vue-query-v5/src/gen/hooks/useFindPetsByStatus.ts @@ -58,7 +58,7 @@ export function useFindPetsByStatus< const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? findPetsByStatusQueryKey(refParams) const query = useQuery({ - ...findPetsByStatusQueryOptions(refParams, clientOptions), + ...(findPetsByStatusQueryOptions(refParams, clientOptions) as QueryObserverOptions), queryKey, ...(queryOptions as unknown as QueryObserverOptions), }) as UseQueryReturnType & { diff --git a/examples/vue-query-v5/src/gen/hooks/useFindPetsByTags.ts b/examples/vue-query-v5/src/gen/hooks/useFindPetsByTags.ts index 71f2c7447..c8107b035 100644 --- a/examples/vue-query-v5/src/gen/hooks/useFindPetsByTags.ts +++ b/examples/vue-query-v5/src/gen/hooks/useFindPetsByTags.ts @@ -57,7 +57,7 @@ export function useFindPetsByTags< const { query: queryOptions, client: clientOptions = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(refParams) const query = useQuery({ - ...findPetsByTagsQueryOptions(refParams, clientOptions), + ...(findPetsByTagsQueryOptions(refParams, clientOptions) as QueryObserverOptions), queryKey, ...(queryOptions as unknown as QueryObserverOptions), }) as UseQueryReturnType & { diff --git a/examples/vue-query-v5/src/gen/hooks/useGetInventory.ts b/examples/vue-query-v5/src/gen/hooks/useGetInventory.ts index 495b89f7d..b26b77b22 100644 --- a/examples/vue-query-v5/src/gen/hooks/useGetInventory.ts +++ b/examples/vue-query-v5/src/gen/hooks/useGetInventory.ts @@ -48,7 +48,7 @@ export function useGetInventory & { diff --git a/examples/vue-query-v5/src/gen/hooks/useGetOrderById.ts b/examples/vue-query-v5/src/gen/hooks/useGetOrderById.ts index 21d3245fb..f00d3be87 100644 --- a/examples/vue-query-v5/src/gen/hooks/useGetOrderById.ts +++ b/examples/vue-query-v5/src/gen/hooks/useGetOrderById.ts @@ -53,7 +53,7 @@ export function useGetOrderById & { diff --git a/examples/vue-query-v5/src/gen/hooks/useGetPetById.ts b/examples/vue-query-v5/src/gen/hooks/useGetPetById.ts index d84dfd8ea..b5b5cac4f 100644 --- a/examples/vue-query-v5/src/gen/hooks/useGetPetById.ts +++ b/examples/vue-query-v5/src/gen/hooks/useGetPetById.ts @@ -52,7 +52,7 @@ export function useGetPetById & { diff --git a/examples/vue-query-v5/src/gen/hooks/useGetUserByName.ts b/examples/vue-query-v5/src/gen/hooks/useGetUserByName.ts index 6019e4607..578a76711 100644 --- a/examples/vue-query-v5/src/gen/hooks/useGetUserByName.ts +++ b/examples/vue-query-v5/src/gen/hooks/useGetUserByName.ts @@ -52,7 +52,7 @@ export function useGetUserByName & { diff --git a/examples/vue-query-v5/src/gen/hooks/useLoginUser.ts b/examples/vue-query-v5/src/gen/hooks/useLoginUser.ts index baf941e32..294c35600 100644 --- a/examples/vue-query-v5/src/gen/hooks/useLoginUser.ts +++ b/examples/vue-query-v5/src/gen/hooks/useLoginUser.ts @@ -52,7 +52,7 @@ export function useLoginUser & { diff --git a/examples/vue-query-v5/src/gen/hooks/useLogoutUser.ts b/examples/vue-query-v5/src/gen/hooks/useLogoutUser.ts index c4691e593..2683abe4e 100644 --- a/examples/vue-query-v5/src/gen/hooks/useLogoutUser.ts +++ b/examples/vue-query-v5/src/gen/hooks/useLogoutUser.ts @@ -47,7 +47,7 @@ export function useLogoutUser & { diff --git a/packages/swagger-tanstack-query/src/components/Query.tsx b/packages/swagger-tanstack-query/src/components/Query.tsx index f3b2cff48..ca9e47dbb 100644 --- a/packages/swagger-tanstack-query/src/components/Query.tsx +++ b/packages/swagger-tanstack-query/src/components/Query.tsx @@ -68,7 +68,7 @@ function Template({ const queryKey = queryOptions?.queryKey ?? ${hook.queryKey} const query = ${hook.name}({ - ...${hook.queryOptions}, + ...${hook.queryOptions} as ${infinite ? 'InfiniteQueryObserverOptions' : 'QueryObserverOptions'}, queryKey, ...queryOptions as unknown as ${infinite ? 'InfiniteQueryObserverOptions' : 'QueryObserverOptions'} }) as ${resolvedReturnType} diff --git a/packages/swagger-tanstack-query/src/components/QueryOptions.tsx b/packages/swagger-tanstack-query/src/components/QueryOptions.tsx index 701e16a1e..b8431b9ec 100644 --- a/packages/swagger-tanstack-query/src/components/QueryOptions.tsx +++ b/packages/swagger-tanstack-query/src/components/QueryOptions.tsx @@ -80,7 +80,18 @@ function Template({ const queryOptions = [ isV5 && !!infinite ? `initialPageParam: ${infinite.initialPageParam}` : undefined, - isV5 && !!infinite ? `getNextPageParam: (lastPage) => lastPage['${infinite.queryParam}']` : undefined, + isV5 && !!infinite && !!infinite.cursorParam + ? `getNextPageParam: (lastPage) => lastPage['${infinite.cursorParam}']` + : undefined, + isV5 && !!infinite && !!infinite.cursorParam + ? `getPreviousPageParam: (firstPage) => firstPage['${infinite.cursorParam}']` + : undefined, + isV5 && !!infinite && !infinite.cursorParam + ? `getNextPageParam: (lastPage, allPages, lastPageParam) => lastPage.length === 0 ? undefined : lastPageParam + 1` + : undefined, + isV5 && !!infinite && !infinite.cursorParam + ? `getPreviousPageParam: (firstPage, allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1` + : undefined, ].filter(Boolean) const resolvedClientOptions = `${transformers.createIndent(4)}${clientOptions.join(`,\n${transformers.createIndent(4)}`)}` diff --git a/packages/swagger-tanstack-query/src/plugin.ts b/packages/swagger-tanstack-query/src/plugin.ts index 4d0420578..9413d6624 100644 --- a/packages/swagger-tanstack-query/src/plugin.ts +++ b/packages/swagger-tanstack-query/src/plugin.ts @@ -46,6 +46,7 @@ export const definePlugin = createPlugin((options) => { ? { queryParam: 'id', initialPageParam: 0, + cursorParam: undefined, ...infinite, } : undefined, diff --git a/packages/swagger-tanstack-query/src/types.ts b/packages/swagger-tanstack-query/src/types.ts index f6384f516..ca0c32d53 100644 --- a/packages/swagger-tanstack-query/src/types.ts +++ b/packages/swagger-tanstack-query/src/types.ts @@ -23,7 +23,11 @@ export type Infinite = { */ queryParam: string /** - * For v5 + * Which field of the data will be used, set it to undefined when no cursor is known. + */ + cursorParam?: string | undefined + /** + * The initial value, the value of the first page. * @default `0` */ initialPageParam: number