Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: cursorParam #743

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-crews-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-tanstack-query": minor
---

Add `cursorParam` to use infinite with a cursor.
6 changes: 5 additions & 1 deletion docs/plugins/swagger-tanstack-query/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion examples/react-query-v5/kubb.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}],
}),
],
})
66 changes: 4 additions & 62 deletions examples/react-query-v5/src/gen/hooks/useFindPetsByStatusHook.ts
Original file line number Diff line number Diff line change
@@ -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<FindPetsByStatusQueryResponse, FindPetsByStatus400, never>
type FindPetsByStatus = {
Expand Down Expand Up @@ -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<TData, FindPetsByStatus['error']> & {
Expand All @@ -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<typeof findPetsByStatusInfiniteQueryKey>
export function findPetsByStatusInfiniteQueryOptions(params?: FindPetsByStatus['queryParams'], options: FindPetsByStatus['client']['parameters'] = {}) {
const queryKey = findPetsByStatusInfiniteQueryKey(params)
return infiniteQueryOptions({
queryKey,
queryFn: async ({ pageParam }) => {
const res = await client<FindPetsByStatus['data'], FindPetsByStatus['error']>({
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<FindPetsByStatus['data'], FindPetsByStatus['error'], TData, TQueryData, TQueryKey>
client?: FindPetsByStatus['client']['parameters']
} = {}): UseInfiniteQueryResult<TData, FindPetsByStatus['error']> & {
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<TData, FindPetsByStatus['error']> & {
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<typeof findPetsByStatusSuspenseQueryKey>
Expand Down Expand Up @@ -152,7 +94,7 @@ export function useFindPetsByStatusHookSuspense<TData = FindPetsByStatus['respon
const { query: queryOptions, client: clientOptions = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? findPetsByStatusSuspenseQueryKey(params)
const query = useSuspenseQuery({
...findPetsByStatusSuspenseQueryOptions(params, clientOptions),
...findPetsByStatusSuspenseQueryOptions(params, clientOptions) as QueryObserverOptions,
queryKey,
...queryOptions as unknown as QueryObserverOptions,
}) as UseSuspenseQueryResult<TData, FindPetsByStatus['error']> & {
Expand Down
11 changes: 6 additions & 5 deletions examples/react-query-v5/src/gen/hooks/useFindPetsByTagsHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TData, FindPetsByTags['error']> & {
Expand All @@ -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,
})
}
/**
Expand All @@ -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<TData, FindPetsByTags['error']> & {
Expand Down Expand Up @@ -150,7 +151,7 @@ export function useFindPetsByTagsHookSuspense<TData = FindPetsByTags['response']
const { query: queryOptions, client: clientOptions = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? findPetsByTagsSuspenseQueryKey(params)
const query = useSuspenseQuery({
...findPetsByTagsSuspenseQueryOptions(params, clientOptions),
...findPetsByTagsSuspenseQueryOptions(params, clientOptions) as QueryObserverOptions,
queryKey,
...queryOptions as unknown as QueryObserverOptions,
}) as UseSuspenseQueryResult<TData, FindPetsByTags['error']> & {
Expand Down
60 changes: 4 additions & 56 deletions examples/react-query-v5/src/gen/hooks/useGetInventoryHook.ts
Original file line number Diff line number Diff line change
@@ -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<GetInventoryQueryResponse, never, never>
type GetInventory = {
Expand Down Expand Up @@ -56,7 +48,7 @@ export function useGetInventoryHook<TData = GetInventory['response'], TQueryData
const { query: queryOptions, client: clientOptions = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? getInventoryQueryKey()
const query = useQuery({
...getInventoryQueryOptions(clientOptions),
...getInventoryQueryOptions(clientOptions) as QueryObserverOptions,
queryKey,
...queryOptions as unknown as QueryObserverOptions,
}) as UseQueryResult<TData, GetInventory['error']> & {
Expand All @@ -65,50 +57,6 @@ export function useGetInventoryHook<TData = GetInventory['response'], TQueryData
query.queryKey = queryKey as TQueryKey
return query
}
export const getInventoryInfiniteQueryKey = () => [{ url: '/store/inventory' }] as const
export type GetInventoryInfiniteQueryKey = ReturnType<typeof getInventoryInfiniteQueryKey>
export function getInventoryInfiniteQueryOptions(options: GetInventory['client']['parameters'] = {}) {
const queryKey = getInventoryInfiniteQueryKey()
return infiniteQueryOptions({
queryKey,
queryFn: async ({ pageParam }) => {
const res = await client<GetInventory['data'], GetInventory['error']>({
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<GetInventory['data'], GetInventory['error'], TData, TQueryData, TQueryKey>
client?: GetInventory['client']['parameters']
} = {}): UseInfiniteQueryResult<TData, GetInventory['error']> & {
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<TData, GetInventory['error']> & {
queryKey: TQueryKey
}
query.queryKey = queryKey as TQueryKey
return query
}
export const getInventorySuspenseQueryKey = () => [{ url: '/store/inventory' }] as const
export type GetInventorySuspenseQueryKey = ReturnType<typeof getInventorySuspenseQueryKey>
export function getInventorySuspenseQueryOptions(options: GetInventory['client']['parameters'] = {}) {
Expand Down Expand Up @@ -138,7 +86,7 @@ export function useGetInventoryHookSuspense<TData = GetInventory['response'], TQ
const { query: queryOptions, client: clientOptions = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? getInventorySuspenseQueryKey()
const query = useSuspenseQuery({
...getInventorySuspenseQueryOptions(clientOptions),
...getInventorySuspenseQueryOptions(clientOptions) as QueryObserverOptions,
queryKey,
...queryOptions as unknown as QueryObserverOptions,
}) as UseSuspenseQueryResult<TData, GetInventory['error']> & {
Expand Down
61 changes: 4 additions & 57 deletions examples/react-query-v5/src/gen/hooks/useGetOrderByIdHook.ts
Original file line number Diff line number Diff line change
@@ -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<GetOrderByIdQueryResponse, GetOrderById400 | GetOrderById404, never>
type GetOrderById = {
Expand Down Expand Up @@ -57,7 +49,7 @@ export function useGetOrderByIdHook<TData = GetOrderById['response'], TQueryData
const { query: queryOptions, client: clientOptions = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? getOrderByIdQueryKey(orderId)
const query = useQuery({
...getOrderByIdQueryOptions(orderId, clientOptions),
...getOrderByIdQueryOptions(orderId, clientOptions) as QueryObserverOptions,
queryKey,
...queryOptions as unknown as QueryObserverOptions,
}) as UseQueryResult<TData, GetOrderById['error']> & {
Expand All @@ -66,51 +58,6 @@ export function useGetOrderByIdHook<TData = GetOrderById['response'], TQueryData
query.queryKey = queryKey as TQueryKey
return query
}
export const getOrderByIdInfiniteQueryKey = (orderId: GetOrderByIdPathParams['orderId']) =>
[{ url: '/store/order/:orderId', params: { orderId: orderId } }] as const
export type GetOrderByIdInfiniteQueryKey = ReturnType<typeof getOrderByIdInfiniteQueryKey>
export function getOrderByIdInfiniteQueryOptions(orderId: GetOrderByIdPathParams['orderId'], options: GetOrderById['client']['parameters'] = {}) {
const queryKey = getOrderByIdInfiniteQueryKey(orderId)
return infiniteQueryOptions({
queryKey,
queryFn: async ({ pageParam }) => {
const res = await client<GetOrderById['data'], GetOrderById['error']>({
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<GetOrderById['data'], GetOrderById['error'], TData, TQueryData, TQueryKey>
client?: GetOrderById['client']['parameters']
} = {}): UseInfiniteQueryResult<TData, GetOrderById['error']> & {
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<TData, GetOrderById['error']> & {
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<typeof getOrderByIdSuspenseQueryKey>
Expand Down Expand Up @@ -144,7 +91,7 @@ export function useGetOrderByIdHookSuspense<TData = GetOrderById['response'], TQ
const { query: queryOptions, client: clientOptions = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? getOrderByIdSuspenseQueryKey(orderId)
const query = useSuspenseQuery({
...getOrderByIdSuspenseQueryOptions(orderId, clientOptions),
...getOrderByIdSuspenseQueryOptions(orderId, clientOptions) as QueryObserverOptions,
queryKey,
...queryOptions as unknown as QueryObserverOptions,
}) as UseSuspenseQueryResult<TData, GetOrderById['error']> & {
Expand Down
Loading
Loading