Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 15 additions & 1 deletion packages/vue-query/src/__tests__/useInfiniteQuery.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, it } from 'vitest'
import { reactive } from 'vue-demi'
import { computed, reactive } from 'vue-demi'
import { useInfiniteQuery } from '../useInfiniteQuery'
import { simpleFetcher } from './test-utils'
import type { InfiniteData } from '@tanstack/query-core'
Expand Down Expand Up @@ -81,4 +81,18 @@ describe('Discriminated union return type', () => {
expectTypeOf(query.error).toEqualTypeOf<Error>()
}
})

it('should accept computed options', () => {
const options = computed(() => ({
queryKey: ['infiniteQuery'],
queryFn: simpleFetcher,
getNextPageParam: () => undefined,
initialPageParam: 0,
}))
const query = reactive(useInfiniteQuery(options))

if (query.isSuccess) {
expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
}
})
})
2 changes: 1 addition & 1 deletion packages/vue-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type UseQueryOptionsGeneric<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryFnData,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is a proper change. Why this is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! that's my fault, I'm already fix it

TQueryKey,
TPageParam
>
Expand Down
119 changes: 85 additions & 34 deletions packages/vue-query/src/useInfiniteQuery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { InfiniteQueryObserver } from '@tanstack/query-core'
import { useBaseQuery } from './useBaseQuery'
import type {
DefinedInitialDataInfiniteOptions,
UndefinedInitialDataInfiniteOptions,
} from './infiniteQueryOptions'
import type {
DefaultError,
InfiniteData,
Expand All @@ -11,7 +15,12 @@ import type {

import type { UseBaseQueryReturnType } from './useBaseQuery'

import type { DeepUnwrapRef, MaybeRefDeep, MaybeRefOrGetter } from './types'
import type {
DeepUnwrapRef,
MaybeRef,
MaybeRefDeep,
MaybeRefOrGetter,
} from './types'
import type { QueryClient } from './queryClient'

export type UseInfiniteQueryOptions<
Expand All @@ -21,44 +30,80 @@ export type UseInfiniteQueryOptions<
TQueryData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
> = {
[Property in keyof InfiniteQueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryKey,
TPageParam
>]: Property extends 'enabled'
? MaybeRefOrGetter<
InfiniteQueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
DeepUnwrapRef<TQueryKey>
>[Property]
>
: MaybeRefDeep<
InfiniteQueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
DeepUnwrapRef<TQueryKey>,
TPageParam
>[Property]
>
} & {
shallow?: boolean
}
> = MaybeRef<
{
[Property in keyof InfiniteQueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryKey,
TPageParam
>]: Property extends 'enabled'
? MaybeRefOrGetter<
InfiniteQueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
DeepUnwrapRef<TQueryKey>
>[Property]
>
: MaybeRefDeep<
InfiniteQueryObserverOptions<
TQueryFnData,
TError,
TData,
TQueryData,
DeepUnwrapRef<TQueryKey>,
TPageParam
>[Property]
>
} & {
shallow?: boolean
}
>

export type UseInfiniteQueryReturnType<TData, TError> = UseBaseQueryReturnType<
TData,
TError,
InfiniteQueryObserverResult<TData, TError>
>

export function useInfiniteQuery<
TQueryFnData,
TError = DefaultError,
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: DefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>,
queryClient?: QueryClient,
): UseInfiniteQueryReturnType<TData, TError>

export function useInfiniteQuery<
TQueryFnData,
TError = DefaultError,
TData = InfiniteData<TQueryFnData>,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: UndefinedInitialDataInfiniteOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>,
queryClient?: QueryClient,
): UseInfiniteQueryReturnType<TData, TError>

export function useInfiniteQuery<
TQueryFnData,
TError = DefaultError,
Expand All @@ -75,10 +120,16 @@ export function useInfiniteQuery<
TPageParam
>,
queryClient?: QueryClient,
): UseInfiniteQueryReturnType<TData, TError> {
): UseInfiniteQueryReturnType<TData, TError>

export function useInfiniteQuery(
options: UseInfiniteQueryOptions,
queryClient?: QueryClient,
) {
return useBaseQuery(
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
InfiniteQueryObserver as typeof QueryObserver,
options,
queryClient,
) as UseInfiniteQueryReturnType<TData, TError>
)
}
Loading