@tanstack/vue-query 5.107.0 → 5.107.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/devtools/devtools.d.cts +1 -1
- package/build/legacy/devtools/devtools.d.ts +1 -1
- package/build/legacy/index.d.cts +4 -4
- package/build/legacy/index.d.ts +4 -4
- package/build/legacy/{infiniteQueryOptions-lJ_zR-As.d.cts → infiniteQueryOptions-BSwO8rvy.d.cts} +41 -5
- package/build/legacy/infiniteQueryOptions-BSwO8rvy.d.cts.map +1 -0
- package/build/legacy/{infiniteQueryOptions-CN37iZvc.d.ts → infiniteQueryOptions-Bq8UQFvj.d.ts} +41 -5
- package/build/legacy/infiniteQueryOptions-Bq8UQFvj.d.ts.map +1 -0
- package/build/legacy/infiniteQueryOptions.d.cts +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +1 -1
- package/build/{modern/queryClient-BGn8uIej.d.cts → legacy/queryClient-BSSPWSvX.d.cts} +2 -2
- package/build/legacy/{queryClient-BGn8uIej.d.cts.map → queryClient-BSSPWSvX.d.cts.map} +1 -1
- package/build/{modern/queryClient-Ba4HSFnn.d.ts → legacy/queryClient-Bgw9hMbf.d.ts} +2 -2
- package/build/legacy/{queryClient-Ba4HSFnn.d.ts.map → queryClient-Bgw9hMbf.d.ts.map} +1 -1
- package/build/legacy/queryClient.d.cts +1 -1
- package/build/legacy/queryClient.d.ts +1 -1
- package/build/{modern/queryOptions-BuZEBLtg.d.cts → legacy/queryOptions-B-eHjgaY.d.cts} +12 -4
- package/build/legacy/queryOptions-B-eHjgaY.d.cts.map +1 -0
- package/build/{modern/queryOptions-dXiBZnGk.d.ts → legacy/queryOptions-D1-foH1W.d.ts} +12 -4
- package/build/legacy/queryOptions-D1-foH1W.d.ts.map +1 -0
- package/build/legacy/queryOptions.cjs.map +1 -1
- package/build/legacy/queryOptions.d.cts +2 -2
- package/build/legacy/queryOptions.d.ts +2 -2
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/useBaseQuery.cjs +11 -5
- package/build/legacy/useBaseQuery.cjs.map +1 -1
- package/build/legacy/useBaseQuery.d.cts +1 -1
- package/build/legacy/useBaseQuery.d.ts +1 -1
- package/build/legacy/useBaseQuery.js +11 -5
- package/build/legacy/useBaseQuery.js.map +1 -1
- package/build/legacy/useInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +1 -1
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useIsFetching.d.cts +1 -1
- package/build/legacy/useIsFetching.d.ts +1 -1
- package/build/legacy/useMutation.d.cts +1 -1
- package/build/legacy/useMutation.d.ts +1 -1
- package/build/legacy/useMutationState.d.cts +1 -1
- package/build/legacy/useMutationState.d.ts +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.cts +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.ts +1 -1
- package/build/legacy/usePrefetchQuery.d.cts +1 -1
- package/build/legacy/usePrefetchQuery.d.ts +1 -1
- package/build/legacy/useQueries.cjs +1 -1
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +2 -2
- package/build/legacy/useQueries.d.ts +2 -2
- package/build/legacy/useQueries.js +1 -1
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +25 -4
- package/build/legacy/useQuery.d.cts.map +1 -1
- package/build/legacy/useQuery.d.ts +25 -4
- package/build/legacy/useQuery.d.ts.map +1 -1
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useQueryClient.d.cts +1 -1
- package/build/legacy/useQueryClient.d.ts +1 -1
- package/build/legacy/vueQueryPlugin.d.cts +1 -1
- package/build/legacy/vueQueryPlugin.d.ts +1 -1
- package/build/modern/devtools/devtools.d.cts +1 -1
- package/build/modern/devtools/devtools.d.ts +1 -1
- package/build/modern/index.d.cts +4 -4
- package/build/modern/index.d.ts +4 -4
- package/build/modern/{infiniteQueryOptions-lJ_zR-As.d.cts → infiniteQueryOptions-BSwO8rvy.d.cts} +41 -5
- package/build/modern/infiniteQueryOptions-BSwO8rvy.d.cts.map +1 -0
- package/build/modern/{infiniteQueryOptions-CN37iZvc.d.ts → infiniteQueryOptions-Bq8UQFvj.d.ts} +41 -5
- package/build/modern/infiniteQueryOptions-Bq8UQFvj.d.ts.map +1 -0
- package/build/modern/infiniteQueryOptions.d.cts +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +1 -1
- package/build/{legacy/queryClient-BGn8uIej.d.cts → modern/queryClient-BSSPWSvX.d.cts} +2 -2
- package/build/modern/{queryClient-BGn8uIej.d.cts.map → queryClient-BSSPWSvX.d.cts.map} +1 -1
- package/build/{legacy/queryClient-Ba4HSFnn.d.ts → modern/queryClient-Bgw9hMbf.d.ts} +2 -2
- package/build/modern/{queryClient-Ba4HSFnn.d.ts.map → queryClient-Bgw9hMbf.d.ts.map} +1 -1
- package/build/modern/queryClient.d.cts +1 -1
- package/build/modern/queryClient.d.ts +1 -1
- package/build/{legacy/queryOptions-BuZEBLtg.d.cts → modern/queryOptions-B-eHjgaY.d.cts} +12 -4
- package/build/modern/queryOptions-B-eHjgaY.d.cts.map +1 -0
- package/build/{legacy/queryOptions-dXiBZnGk.d.ts → modern/queryOptions-D1-foH1W.d.ts} +12 -4
- package/build/modern/queryOptions-D1-foH1W.d.ts.map +1 -0
- package/build/modern/queryOptions.cjs.map +1 -1
- package/build/modern/queryOptions.d.cts +2 -2
- package/build/modern/queryOptions.d.ts +2 -2
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/useBaseQuery.cjs +11 -5
- package/build/modern/useBaseQuery.cjs.map +1 -1
- package/build/modern/useBaseQuery.d.cts +1 -1
- package/build/modern/useBaseQuery.d.ts +1 -1
- package/build/modern/useBaseQuery.js +11 -5
- package/build/modern/useBaseQuery.js.map +1 -1
- package/build/modern/useInfiniteQuery.cjs.map +1 -1
- package/build/modern/useInfiniteQuery.d.cts +1 -1
- package/build/modern/useInfiniteQuery.d.ts +1 -1
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useIsFetching.d.cts +1 -1
- package/build/modern/useIsFetching.d.ts +1 -1
- package/build/modern/useMutation.d.cts +1 -1
- package/build/modern/useMutation.d.ts +1 -1
- package/build/modern/useMutationState.d.cts +1 -1
- package/build/modern/useMutationState.d.ts +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.cts +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.ts +1 -1
- package/build/modern/usePrefetchQuery.d.cts +1 -1
- package/build/modern/usePrefetchQuery.d.ts +1 -1
- package/build/modern/useQueries.cjs +1 -1
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +2 -2
- package/build/modern/useQueries.d.ts +2 -2
- package/build/modern/useQueries.js +1 -1
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +25 -4
- package/build/modern/useQuery.d.cts.map +1 -1
- package/build/modern/useQuery.d.ts +25 -4
- package/build/modern/useQuery.d.ts.map +1 -1
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useQueryClient.d.cts +1 -1
- package/build/modern/useQueryClient.d.ts +1 -1
- package/build/modern/vueQueryPlugin.d.cts +1 -1
- package/build/modern/vueQueryPlugin.d.ts +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/queryOptions.ts +23 -9
- package/src/useBaseQuery.ts +68 -8
- package/src/useInfiniteQuery.ts +6 -8
- package/src/useQueries.ts +7 -2
- package/src/useQuery.ts +22 -1
- package/build/legacy/infiniteQueryOptions-CN37iZvc.d.ts.map +0 -1
- package/build/legacy/infiniteQueryOptions-lJ_zR-As.d.cts.map +0 -1
- package/build/legacy/queryOptions-BuZEBLtg.d.cts.map +0 -1
- package/build/legacy/queryOptions-dXiBZnGk.d.ts.map +0 -1
- package/build/modern/infiniteQueryOptions-CN37iZvc.d.ts.map +0 -1
- package/build/modern/infiniteQueryOptions-lJ_zR-As.d.cts.map +0 -1
- package/build/modern/queryOptions-BuZEBLtg.d.cts.map +0 -1
- package/build/modern/queryOptions-dXiBZnGk.d.ts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQueries.js","names":[],"sources":["../../src/useQueries.ts"],"sourcesContent":["import { QueriesObserver } from '@tanstack/query-core'\nimport {\n computed,\n getCurrentScope,\n onScopeDispose,\n readonly,\n shallowReadonly,\n shallowRef,\n unref,\n watch,\n} from 'vue-demi'\n\nimport { useQueryClient } from './useQueryClient'\nimport { cloneDeepUnref } from './utils'\nimport type { Ref } from 'vue-demi'\nimport type {\n DefaultError,\n DefinedQueryObserverResult,\n QueriesObserverOptions,\n QueryFunction,\n QueryKey,\n QueryObserverResult,\n ThrowOnError,\n} from '@tanstack/query-core'\nimport type { UseQueryOptions } from './queryOptions'\nimport type { QueryClient } from './queryClient'\nimport type { DeepUnwrapRef, MaybeRefDeep, ShallowOption } from './types'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function does not have a parameter\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\n// Widen the type of the symbol to enable type inference even if skipToken is not immutable.\ntype SkipTokenForUseQueries = symbol\n\ntype GetUseQueryOptionsForUseQueries<T> =\n // Part 1: if UseQueryOptions are already being sent through, then just return T\n T extends UseQueryOptions\n ? DeepUnwrapRef<T>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 3: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 4: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, infer TQueryKey>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? UseQueryOptionsForUseQueries<\n TQueryFnData,\n unknown extends TError ? DefaultError : TError,\n unknown extends TData ? TQueryFnData : TData,\n TQueryKey\n >\n : T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, infer TQueryKey>\n | SkipTokenForUseQueries\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? UseQueryOptionsForUseQueries<\n TQueryFnData,\n TError,\n TQueryFnData,\n TQueryKey\n >\n : // Fallback\n UseQueryOptionsForUseQueries\n\n// A defined initialData setting should return a DefinedQueryObserverResult rather than QueryObserverResult\ntype GetDefinedOrUndefinedQueryResult<T, TData, TError = unknown> = T extends {\n initialData?: infer TInitialData\n}\n ? unknown extends TInitialData\n ? QueryObserverResult<TData, TError>\n : TInitialData extends TData\n ? DefinedQueryObserverResult<TData, TError>\n : TInitialData extends () => infer TInitialDataResult\n ? unknown extends TInitialDataResult\n ? QueryObserverResult<TData, TError>\n : TInitialDataResult extends TData\n ? DefinedQueryObserverResult<TData, TError>\n : QueryObserverResult<TData, TError>\n : QueryObserverResult<TData, TError>\n : QueryObserverResult<TData, TError>\n\ntype GetUseQueryResult<T> =\n // Part 1: if using UseQueryOptions then the types are already set\n T extends UseQueryOptions<\n infer TQueryFnData,\n infer TError,\n infer TData,\n any,\n any\n >\n ? GetDefinedOrUndefinedQueryResult<\n T,\n undefined extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : // Part 2: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : // Part 3: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData>\n : // Part 4: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, any>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? GetDefinedOrUndefinedQueryResult<\n T,\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, any>\n | SkipTokenForUseQueries\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? GetDefinedOrUndefinedQueryResult<\n T,\n TQueryFnData,\n unknown extends TError ? DefaultError : TError\n >\n : // Fallback\n QueryObserverResult\n\n/**\n * UseQueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n */\nexport type UseQueriesOptions<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryOptionsForUseQueries>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryOptionsForUseQueries<Head>]\n : T extends [infer Head, ...infer Tails]\n ? UseQueriesOptions<\n [...Tails],\n [...TResults, GetUseQueryOptionsForUseQueries<Head>],\n [...TDepth, 1]\n >\n : ReadonlyArray<unknown> extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogeneous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends Array<\n UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >\n >\n ? Array<\n UseQueryOptionsForUseQueries<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >\n >\n : // Fallback\n Array<UseQueryOptionsForUseQueries>\n\n/**\n * UseQueriesResults reducer recursively maps type param to results\n */\nexport type UseQueriesResults<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<QueryObserverResult>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryResult<Head>]\n : T extends [infer Head, ...infer Tails]\n ? UseQueriesResults<\n [...Tails],\n [...TResults, GetUseQueryResult<Head>],\n [...TDepth, 1]\n >\n : { [K in keyof T]: GetUseQueryResult<T[K]> }\n\ntype UseQueriesOptionsArg<T extends Array<any>> = readonly [\n ...UseQueriesOptions<T>,\n]\n\n/**\n * The `useQueries` composable can be used to fetch a variable number of queries.\n *\n * The `queries` option accepts an array of query option objects mostly identical to `useQuery`'s. It may be a\n * plain array, a `ref`/reactive array (each entry tracked individually), or a getter function — pass a getter\n * if the array itself (its length, or which queries are in it) depends on other reactive state.\n *\n * Having the same query key more than once in the array of query objects may cause some data to be shared\n * between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired\n * structure.\n *\n * The `combine` option can be used to combine the results of the queries into a single value. The result will\n * be structurally shared to be as referentially stable as possible.\n *\n * `placeholderData` is supported here too, but unlike `useQuery`, it doesn't receive information from\n * previously rendered queries, because the number of queries can differ between renders.\n * @param options - The `queries` array to run, and the optional `combine` and `shallow` options.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns A `ref` to the combined result. Without `combine`, this is an array with all the query results, in\n * the same order as the input. When `combine` is provided, this is the value returned by `combine` instead.\n * @example\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQueries } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ ids: Array<number> }>()\n *\n * const postQueries = useQueries({\n * queries: () =>\n * props.ids.map((id) => ({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * staleTime: Infinity,\n * })),\n * })\n * </script>\n *\n * <template>\n * <ul>\n * <li v-for=\"(query, index) in postQueries\" :key=\"props.ids[index]\">\n * <span v-if=\"query.isPending\">Loading...</span>\n * <span v-else-if=\"query.isError\">Error: {{ query.error.message }}</span>\n * <span v-else>{{ query.data.title }}</span>\n * </li>\n * </ul>\n * </template>\n * ```\n * @example\n * Combining results into a single value:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQueries } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ ids: Array<number> }>()\n *\n * const combined = useQueries({\n * queries: () =>\n * props.ids.map((id) => ({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * })),\n * combine: (postQueries) => ({\n * data: postQueries.map((query) => query.data),\n * isPending: postQueries.some((query) => query.isPending),\n * isError: postQueries.some((query) => query.isError),\n * }),\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"combined.isPending\">Loading...</span>\n * <span v-else-if=\"combined.isError\">Error!</span>\n * <ul v-else>\n * <li v-for=\"post in combined.data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * </template>\n * ```\n * @example\n * Typing `select` via {@link queryOptions}. Note that spreading a `queryOptions` result and overriding\n * `select` inline still falls back to `unknown` — wrap the spread in `queryOptions` again so the override is\n * resolved before it reaches `useQueries`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { queryOptions, useQueries } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ id: number }>()\n *\n * const postOptions = (id: number) =>\n * queryOptions({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * })\n *\n * const [{ data: broken }] = useQueries({\n * queries: () => [\n * {\n * ...postOptions(props.id),\n * // ❌ `data` is `unknown` here\n * select: (data) => data.title,\n * },\n * ],\n * })\n *\n * const [{ data: fixed }] = useQueries({\n * queries: () => [\n * queryOptions({\n * ...postOptions(props.id),\n * // ✅ `data` is `Post`\n * select: (data) => data.title,\n * }),\n * ],\n * })\n * </script>\n *\n * <template>\n * <h1>{{ fixed }}</h1>\n * </template>\n * ```\n */\nexport function useQueries<\n T extends Array<any>,\n TCombinedResult = UseQueriesResults<T>,\n>(\n {\n queries,\n ...options\n }: ShallowOption & {\n queries:\n | (() => MaybeRefDeep<UseQueriesOptionsArg<T>>)\n | MaybeRefDeep<UseQueriesOptionsArg<T>>\n | MaybeRefDeep<\n readonly [\n ...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> },\n ]\n >\n combine?: (result: UseQueriesResults<T>) => TCombinedResult\n },\n queryClient?: QueryClient,\n): Readonly<Ref<TCombinedResult>> {\n if (process.env.NODE_ENV === 'development') {\n if (!getCurrentScope()) {\n console.warn(\n 'vue-query composable like \"useQuery()\" should only be used inside a \"setup()\" function or a running effect scope. They might otherwise lead to memory leaks.',\n )\n }\n }\n\n const client = queryClient || useQueryClient()\n\n const defaultedQueries = computed(() => {\n const resolvedQueries =\n typeof queries === 'function'\n ? (queries as () => MaybeRefDeep<UseQueriesOptionsArg<T>>)()\n : queries\n // Only unref the top level array.\n const queriesRaw = unref(resolvedQueries) as ReadonlyArray<any>\n\n // Unref the rest for each element in the top level array.\n return queriesRaw.map((queryOptions) => {\n const clonedOptions = cloneDeepUnref(queryOptions)\n\n if (typeof clonedOptions.enabled === 'function') {\n clonedOptions.enabled = queryOptions.enabled()\n }\n\n const defaulted = client.defaultQueryOptions(clonedOptions)\n defaulted._optimisticResults = client.isRestoring?.value\n ? 'isRestoring'\n : 'optimistic'\n\n return defaulted\n })\n })\n\n const observer = new QueriesObserver<TCombinedResult>(\n client,\n defaultedQueries.value,\n options as QueriesObserverOptions<TCombinedResult>,\n )\n\n const getOptimisticResult = () => {\n const [results, getCombinedResult] = observer.getOptimisticResult(\n defaultedQueries.value,\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n return getCombinedResult(\n results.map((result, index) => {\n return {\n ...result,\n refetch: async (...args: Array<any>) => {\n const [{ [index]: query }] = observer.getOptimisticResult(\n defaultedQueries.value,\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n return query!.refetch(...args)\n },\n }\n }),\n )\n }\n\n const state = shallowRef(getOptimisticResult())\n\n let unsubscribe = () => {\n // noop\n }\n\n if (client.isRestoring) {\n watch(\n client.isRestoring,\n (isRestoring) => {\n if (!isRestoring) {\n unsubscribe()\n unsubscribe = observer.subscribe(() => {\n state.value = getOptimisticResult()\n })\n\n state.value = getOptimisticResult()\n }\n },\n { immediate: true },\n )\n }\n\n watch(defaultedQueries, (queriesValue) => {\n observer.setQueries(\n queriesValue,\n options as QueriesObserverOptions<TCombinedResult>,\n )\n state.value = getOptimisticResult()\n })\n\n onScopeDispose(() => {\n unsubscribe()\n })\n\n return options.shallow\n ? shallowReadonly(state)\n : (readonly(state) as Readonly<Ref<TCombinedResult>>)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiWA,SAAgB,WAId,EACE,SACA,GAAG,WAYL,aACgC;CAChC,IAAI,QAAQ,IAAI,aAAa,eACvB;MAAA,CAAC,gBAAgB,GACnB,QAAQ,KACN,kKACF;CAAA;CAIJ,MAAM,SAAS,eAAe,eAAe;CAE7C,MAAM,mBAAmB,eAAe;EACtC,MAAM,kBACJ,OAAO,YAAY,aACd,QAAwD,IACzD;EAKN,OAHmB,MAAM,eAGT,CAAC,CAAC,KAAK,iBAAiB;GACtC,MAAM,gBAAgB,eAAe,YAAY;GAEjD,IAAI,OAAO,cAAc,YAAY,YACnC,cAAc,UAAU,aAAa,QAAQ;GAG/C,MAAM,YAAY,OAAO,oBAAoB,aAAa;GAC1D,UAAU,qBAAqB,OAAO,aAAa,QAC/C,gBACA;GAEJ,OAAO;EACT,CAAC;CACH,CAAC;CAED,MAAM,WAAW,IAAI,gBACnB,QACA,iBAAiB,OACjB,OACF;CAEA,MAAM,4BAA4B;EAChC,MAAM,CAAC,SAAS,qBAAqB,SAAS,oBAC5C,iBAAiB,OAChB,QAAoD,OACvD;EAEA,OAAO,kBACL,QAAQ,KAAK,QAAQ,UAAU;GAC7B,OAAO;IACL,GAAG;IACH,SAAS,OAAO,GAAG,SAAqB;KACtC,MAAM,CAAC,GAAG,QAAQ,WAAW,SAAS,oBACpC,iBAAiB,OAChB,QAAoD,OACvD;KAEA,OAAO,MAAO,QAAQ,GAAG,IAAI;IAC/B;GACF;EACF,CAAC,CACH;CACF;CAEA,MAAM,QAAQ,WAAW,oBAAoB,CAAC;CAE9C,IAAI,oBAAoB,CAExB;CAEA,IAAI,OAAO,aACT,MACE,OAAO,cACN,gBAAgB;EACf,IAAI,CAAC,aAAa;GAChB,YAAY;GACZ,cAAc,SAAS,gBAAgB;IACrC,MAAM,QAAQ,oBAAoB;GACpC,CAAC;GAED,MAAM,QAAQ,oBAAoB;EACpC;CACF,GACA,EAAE,WAAW,KAAK,CACpB;CAGF,MAAM,mBAAmB,iBAAiB;EACxC,SAAS,WACP,cACA,OACF;EACA,MAAM,QAAQ,oBAAoB;CACpC,CAAC;CAED,qBAAqB;EACnB,YAAY;CACd,CAAC;CAED,OAAO,QAAQ,UACX,gBAAgB,KAAK,IACpB,SAAS,KAAK;AACrB"}
|
|
1
|
+
{"version":3,"file":"useQueries.js","names":[],"sources":["../../src/useQueries.ts"],"sourcesContent":["import { QueriesObserver } from '@tanstack/query-core'\nimport {\n computed,\n getCurrentScope,\n onScopeDispose,\n readonly,\n shallowReadonly,\n shallowRef,\n unref,\n watch,\n} from 'vue-demi'\n\nimport { useQueryClient } from './useQueryClient'\nimport { cloneDeepUnref } from './utils'\nimport type { Ref } from 'vue-demi'\nimport type {\n DefaultError,\n DefinedQueryObserverResult,\n QueriesObserverOptions,\n QueryFunction,\n QueryKey,\n QueryObserverResult,\n ThrowOnError,\n} from '@tanstack/query-core'\nimport type { UseQueryOptions } from './queryOptions'\nimport type { QueryClient } from './queryClient'\nimport type { DeepUnwrapRef, MaybeRefDeep, ShallowOption } from './types'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function does not have a parameter\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\n// Widen the type of the symbol to enable type inference even if skipToken is not immutable.\ntype SkipTokenForUseQueries = symbol\n\ntype GetUseQueryOptionsForUseQueries<T> =\n // Part 1: if UseQueryOptions are already being sent through, then just return T\n T extends UseQueryOptions\n ? DeepUnwrapRef<T>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 3: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 4: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, infer TQueryKey>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? UseQueryOptionsForUseQueries<\n TQueryFnData,\n unknown extends TError ? DefaultError : TError,\n unknown extends TData ? TQueryFnData : TData,\n TQueryKey\n >\n : T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, infer TQueryKey>\n | SkipTokenForUseQueries\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? UseQueryOptionsForUseQueries<\n TQueryFnData,\n TError,\n TQueryFnData,\n TQueryKey\n >\n : // Fallback\n UseQueryOptionsForUseQueries\n\n// A defined initialData setting should return a DefinedQueryObserverResult rather than QueryObserverResult\ntype GetDefinedOrUndefinedQueryResult<T, TData, TError = unknown> = T extends {\n initialData?: infer TInitialData\n}\n ? unknown extends TInitialData\n ? QueryObserverResult<TData, TError>\n : TInitialData extends TData\n ? DefinedQueryObserverResult<TData, TError>\n : TInitialData extends () => infer TInitialDataResult\n ? unknown extends TInitialDataResult\n ? QueryObserverResult<TData, TError>\n : TInitialDataResult extends TData\n ? DefinedQueryObserverResult<TData, TError>\n : QueryObserverResult<TData, TError>\n : QueryObserverResult<TData, TError>\n : QueryObserverResult<TData, TError>\n\ntype GetUseQueryResult<T> =\n // Part 1: if using UseQueryOptions then the types are already set\n T extends UseQueryOptions<\n infer TQueryFnData,\n infer TError,\n infer TData,\n any,\n any\n >\n ? GetDefinedOrUndefinedQueryResult<\n T,\n undefined extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : // Part 2: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : // Part 3: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData>\n : // Part 4: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, any>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? GetDefinedOrUndefinedQueryResult<\n T,\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, any>\n | SkipTokenForUseQueries\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? GetDefinedOrUndefinedQueryResult<\n T,\n TQueryFnData,\n unknown extends TError ? DefaultError : TError\n >\n : // Fallback\n QueryObserverResult\n\n/**\n * UseQueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n */\nexport type UseQueriesOptions<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryOptionsForUseQueries>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryOptionsForUseQueries<Head>]\n : T extends [infer Head, ...infer Tails]\n ? UseQueriesOptions<\n [...Tails],\n [...TResults, GetUseQueryOptionsForUseQueries<Head>],\n [...TDepth, 1]\n >\n : ReadonlyArray<unknown> extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogeneous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends Array<\n UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >\n >\n ? Array<\n UseQueryOptionsForUseQueries<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >\n >\n : // Fallback\n Array<UseQueryOptionsForUseQueries>\n\n/**\n * UseQueriesResults reducer recursively maps type param to results\n */\nexport type UseQueriesResults<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<QueryObserverResult>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryResult<Head>]\n : T extends [infer Head, ...infer Tails]\n ? UseQueriesResults<\n [...Tails],\n [...TResults, GetUseQueryResult<Head>],\n [...TDepth, 1]\n >\n : { [K in keyof T]: GetUseQueryResult<T[K]> }\n\ntype UseQueriesOptionsArg<T extends Array<any>> = readonly [\n ...UseQueriesOptions<T>,\n]\n\n/**\n * The `useQueries` composable can be used to fetch a variable number of queries.\n *\n * The `queries` option accepts an array of query option objects mostly identical to `useQuery`'s. It may be a\n * plain array, a `ref`/reactive array (each entry tracked individually), or a getter function — pass a getter\n * if the array itself (its length, or which queries are in it) depends on other reactive state.\n *\n * Having the same query key more than once in the array of query objects may cause some data to be shared\n * between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired\n * structure.\n *\n * The `combine` option can be used to combine the results of the queries into a single value. The result will\n * be structurally shared to be as referentially stable as possible.\n *\n * `placeholderData` is supported here too, but unlike `useQuery`, it doesn't receive information from\n * previously rendered queries, because the number of queries can differ between renders.\n * @param options - The `queries` array to run, and the optional `combine` and `shallow` options.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns A `ref` to the combined result. Without `combine`, this is an array with all the query results, in\n * the same order as the input. When `combine` is provided, this is the value returned by `combine` instead.\n * @example\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQueries } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ ids: Array<number> }>()\n *\n * const postQueries = useQueries({\n * queries: () =>\n * props.ids.map((id) => ({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * staleTime: Infinity,\n * })),\n * })\n * </script>\n *\n * <template>\n * <ul>\n * <li v-for=\"(query, index) in postQueries\" :key=\"props.ids[index]\">\n * <span v-if=\"query.isPending\">Loading...</span>\n * <span v-else-if=\"query.isError\">Error: {{ query.error.message }}</span>\n * <span v-else>{{ query.data.title }}</span>\n * </li>\n * </ul>\n * </template>\n * ```\n * @example\n * Combining results into a single value:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQueries } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ ids: Array<number> }>()\n *\n * const combined = useQueries({\n * queries: () =>\n * props.ids.map((id) => ({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * })),\n * combine: (postQueries) => ({\n * data: postQueries.map((query) => query.data),\n * isPending: postQueries.some((query) => query.isPending),\n * isError: postQueries.some((query) => query.isError),\n * }),\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"combined.isPending\">Loading...</span>\n * <span v-else-if=\"combined.isError\">Error!</span>\n * <ul v-else>\n * <li v-for=\"post in combined.data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * </template>\n * ```\n * @example\n * Typing `select` via {@link queryOptions}. Note that spreading a `queryOptions` result and overriding\n * `select` inline still falls back to `unknown` — wrap the spread in `queryOptions` again so the override is\n * resolved before it reaches `useQueries`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { queryOptions, useQueries } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ id: number }>()\n *\n * const postOptions = (id: number) =>\n * queryOptions({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * })\n *\n * const [{ data: broken }] = useQueries({\n * queries: () => [\n * {\n * ...postOptions(props.id),\n * // ❌ `data` is `unknown` here\n * select: (data) => data.title,\n * },\n * ],\n * })\n *\n * const [{ data: fixed }] = useQueries({\n * queries: () => [\n * queryOptions({\n * ...postOptions(props.id),\n * // ✅ `data` is `Post`\n * select: (data) => data.title,\n * }),\n * ],\n * })\n * </script>\n *\n * <template>\n * <h1>{{ fixed }}</h1>\n * </template>\n * ```\n */\nexport function useQueries<\n T extends Array<any>,\n TCombinedResult = UseQueriesResults<T>,\n>(\n {\n queries,\n ...options\n }: ShallowOption & {\n queries:\n | (() => MaybeRefDeep<UseQueriesOptionsArg<T>>)\n | MaybeRefDeep<UseQueriesOptionsArg<T>>\n | MaybeRefDeep<\n readonly [\n ...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> },\n ]\n >\n combine?: (result: UseQueriesResults<T>) => TCombinedResult\n },\n queryClient?: QueryClient,\n): Readonly<Ref<TCombinedResult>> {\n if (process.env.NODE_ENV === 'development') {\n if (!getCurrentScope()) {\n console.warn(\n 'vue-query composable like \"useQuery()\" should only be used inside a \"setup()\" function or a running effect scope. They might otherwise lead to memory leaks.',\n )\n }\n }\n\n const client = queryClient || useQueryClient()\n\n const defaultedQueries = computed(() => {\n const resolvedQueries =\n typeof queries === 'function'\n ? (queries as () => MaybeRefDeep<UseQueriesOptionsArg<T>>)()\n : queries\n // Only unref the top level array.\n const queriesRaw = unref(resolvedQueries) as ReadonlyArray<any>\n\n // Unref the rest for each element in the top level array.\n return queriesRaw.map((queryOptions) => {\n const clonedOptions = cloneDeepUnref(queryOptions)\n\n // A zero-arg function is a Vue getter and must run here so its refs are tracked.\n // `(query) => boolean` is query-core's callback and is left for core to call.\n if (\n typeof clonedOptions.enabled === 'function' &&\n clonedOptions.enabled.length === 0\n ) {\n clonedOptions.enabled = clonedOptions.enabled()\n }\n\n const defaulted = client.defaultQueryOptions(clonedOptions)\n defaulted._optimisticResults = client.isRestoring?.value\n ? 'isRestoring'\n : 'optimistic'\n\n return defaulted\n })\n })\n\n const observer = new QueriesObserver<TCombinedResult>(\n client,\n defaultedQueries.value,\n options as QueriesObserverOptions<TCombinedResult>,\n )\n\n const getOptimisticResult = () => {\n const [results, getCombinedResult] = observer.getOptimisticResult(\n defaultedQueries.value,\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n return getCombinedResult(\n results.map((result, index) => {\n return {\n ...result,\n refetch: async (...args: Array<any>) => {\n const [{ [index]: query }] = observer.getOptimisticResult(\n defaultedQueries.value,\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n return query!.refetch(...args)\n },\n }\n }),\n )\n }\n\n const state = shallowRef(getOptimisticResult())\n\n let unsubscribe = () => {\n // noop\n }\n\n if (client.isRestoring) {\n watch(\n client.isRestoring,\n (isRestoring) => {\n if (!isRestoring) {\n unsubscribe()\n unsubscribe = observer.subscribe(() => {\n state.value = getOptimisticResult()\n })\n\n state.value = getOptimisticResult()\n }\n },\n { immediate: true },\n )\n }\n\n watch(defaultedQueries, (queriesValue) => {\n observer.setQueries(\n queriesValue,\n options as QueriesObserverOptions<TCombinedResult>,\n )\n state.value = getOptimisticResult()\n })\n\n onScopeDispose(() => {\n unsubscribe()\n })\n\n return options.shallow\n ? shallowReadonly(state)\n : (readonly(state) as Readonly<Ref<TCombinedResult>>)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiWA,SAAgB,WAId,EACE,SACA,GAAG,WAYL,aACgC;CAChC,IAAI,QAAQ,IAAI,aAAa,eACvB;MAAA,CAAC,gBAAgB,GACnB,QAAQ,KACN,kKACF;CAAA;CAIJ,MAAM,SAAS,eAAe,eAAe;CAE7C,MAAM,mBAAmB,eAAe;EACtC,MAAM,kBACJ,OAAO,YAAY,aACd,QAAwD,IACzD;EAKN,OAHmB,MAAM,eAGT,CAAC,CAAC,KAAK,iBAAiB;GACtC,MAAM,gBAAgB,eAAe,YAAY;GAIjD,IACE,OAAO,cAAc,YAAY,cACjC,cAAc,QAAQ,WAAW,GAEjC,cAAc,UAAU,cAAc,QAAQ;GAGhD,MAAM,YAAY,OAAO,oBAAoB,aAAa;GAC1D,UAAU,qBAAqB,OAAO,aAAa,QAC/C,gBACA;GAEJ,OAAO;EACT,CAAC;CACH,CAAC;CAED,MAAM,WAAW,IAAI,gBACnB,QACA,iBAAiB,OACjB,OACF;CAEA,MAAM,4BAA4B;EAChC,MAAM,CAAC,SAAS,qBAAqB,SAAS,oBAC5C,iBAAiB,OAChB,QAAoD,OACvD;EAEA,OAAO,kBACL,QAAQ,KAAK,QAAQ,UAAU;GAC7B,OAAO;IACL,GAAG;IACH,SAAS,OAAO,GAAG,SAAqB;KACtC,MAAM,CAAC,GAAG,QAAQ,WAAW,SAAS,oBACpC,iBAAiB,OAChB,QAAoD,OACvD;KAEA,OAAO,MAAO,QAAQ,GAAG,IAAI;IAC/B;GACF;EACF,CAAC,CACH;CACF;CAEA,MAAM,QAAQ,WAAW,oBAAoB,CAAC;CAE9C,IAAI,oBAAoB,CAExB;CAEA,IAAI,OAAO,aACT,MACE,OAAO,cACN,gBAAgB;EACf,IAAI,CAAC,aAAa;GAChB,YAAY;GACZ,cAAc,SAAS,gBAAgB;IACrC,MAAM,QAAQ,oBAAoB;GACpC,CAAC;GAED,MAAM,QAAQ,oBAAoB;EACpC;CACF,GACA,EAAE,WAAW,KAAK,CACpB;CAGF,MAAM,mBAAmB,iBAAiB;EACxC,SAAS,WACP,cACA,OACF;EACA,MAAM,QAAQ,oBAAoB;CACpC,CAAC;CAED,qBAAqB;EACnB,YAAY;CACd,CAAC;CAED,OAAO,QAAQ,UACX,gBAAgB,KAAK,IACpB,SAAS,KAAK;AACrB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.cjs","names":["useBaseQuery","QueryObserver"],"sources":["../../src/useQuery.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport { useBaseQuery } from './useBaseQuery'\nimport type {\n DefaultError,\n DefinedQueryObserverResult,\n QueryKey,\n} from '@tanstack/query-core'\nimport type { UseBaseQueryReturnType } from './useBaseQuery'\nimport type { MaybeRefOrGetter } from './types'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefinedInitialQueryOptions,\n UndefinedInitialQueryOptions,\n UseQueryOptions,\n} from './queryOptions'\n\n/**\n * The result of `useQuery` when `initialData` is omitted, may be `undefined`, or is a function that may\n * return `undefined` — `data` may be `undefined` while the query is `pending`. See\n * {@link UseBaseQueryReturnType}.\n */\nexport type UseQueryReturnType<TData, TError> = UseBaseQueryReturnType<\n TData,\n TError\n>\n\n/**\n * The result of `useQuery` when `initialData` is set — `data` is never `undefined` (unless a `select` changes\n * `TData` to include `undefined`).\n */\nexport type UseQueryDefinedReturnType<TData, TError> = UseBaseQueryReturnType<\n TData,\n TError,\n DefinedQueryObserverResult<TData, TError>\n>\n\n/**\n * This overload is selected when `initialData` is known to be defined, so the resulting `data` is never\n * `undefined` (unless a `select` changes `TData` to include `undefined`).\n *\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries. Other options are read once when passed as a plain value, and stay\n * reactive when passed as a `ref` or a `computed`.\n * @param options - The {@link DefinedInitialQueryOptions} to use — everything you can pass to `useQuery`,\n * with `initialData` known to be defined.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result, typed so that `data` is never `undefined` (unless a `select` changes\n * `TData` to include `undefined`). `status` never resolves to `pending` in this overload's type, since\n * `initialData` guarantees data upfront.\n * @example\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,\n * // so the list stays visible alongside the error.\n * const { data, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <ul>\n * <li v-for=\"post in data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryDefinedReturnType<TData, TError>\n\n/**\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries. Other options are read once when passed as a plain value, and stay\n * reactive when passed as a `ref` or a `computed`.\n * @param options - The {@link UndefinedInitialQueryOptions} to use — everything you can pass to `useQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if\n * the last fetch attempt failed, or `success` if the query has data to display.\n * @example\n * A query key built from a reactive `ref` — the query refetches whenever `postId` changes:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const postId = ref(1)\n * const { status, data, error } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId.value),\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"status === 'pending'\">Loading...</span>\n * <span v-else-if=\"status === 'error'\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data.title }}</h1>\n * </template>\n * ```\n * @example\n * `select` derives whatever `data` a component needs from the cached value, without changing what's\n * actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const { data, isPending, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * select: (posts) => posts.length,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isPending\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <span v-else>{{ data }} posts</span>\n * </template>\n * ```\n * @example\n * A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the\n * loading state doesn't show while the query is disabled:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number | undefined }>()\n *\n * const { data, isLoading, isError, error } = useQuery({\n * queryKey: ['post', props.postId],\n * queryFn: () => fetchPost(props.postId!),\n * enabled: () => props.postId != null,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"props.postId == null\">Select a post</span>\n * <span v-else-if=\"isLoading\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n * @example\n * Seeding a detail query from an already-cached list, to skip the loading state. `initialDataUpdatedAt` carries\n * over the list's own fetch time, so that if you set a `staleTime`, it's measured from when the list was\n * fetched rather than from now:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery, useQueryClient } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number }>()\n * const queryClient = useQueryClient()\n *\n * const { data, isError, error } = useQuery({\n * queryKey: ['post', props.postId],\n * queryFn: () => fetchPost(props.postId),\n * initialData: () =>\n * queryClient\n * .getQueryData<Array<Post>>(['posts'])\n * ?.find((post) => post.id === props.postId),\n * initialDataUpdatedAt: () =>\n * queryClient.getQueryState(['posts'])?.dataUpdatedAt,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n * @example\n * Paginated data, keeping the previous page's data visible while the next page loads:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { keepPreviousData, useQuery } from '@tanstack/vue-query'\n *\n * const page = ref(0)\n *\n * const { data, isPlaceholderData, isError, error } = useQuery({\n * queryKey: ['posts', page],\n * queryFn: () => fetchPosts(page.value),\n * placeholderData: keepPreviousData,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <template v-else>\n * <ul>\n * <li v-for=\"post in data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * <button :disabled=\"isPlaceholderData\" @click=\"page++\">Next Page</button>\n * </template>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryReturnType<TData, TError>\n\n/**\n * Fallback overload for options whose `initialData` presence isn't statically known — for example, a\n * `ref`/reactive object built up conditionally, rather than a plain object literal. Prefer one of the other\n * overloads when possible, since they infer whether `data` can be `undefined` from `initialData` directly.\n *\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries.\n *\n * When `options` itself is a reactive getter, the whole object is re-evaluated on every change to its\n * dependencies, so any option inside it — not just `queryKey` and `enabled` — can change over time.\n * @param options - A `ref`, plain value, or reactive getter resolving to the {@link UseQueryOptions} to use.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result, with `data` typed as possibly `undefined`.\n * @example\n * Passing a whole-options getter so `staleTime` reacts to a setting stored elsewhere, not just `queryKey`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ id: number; staleTime: number }>()\n *\n * const { data } = useQuery(() => ({\n * queryKey: ['post', props.id],\n * queryFn: () => fetchPost(props.id),\n * staleTime: props.staleTime,\n * }))\n * </script>\n *\n * <template>\n * <h1 v-if=\"data\">{{ data.title }}</h1>\n * </template>\n * ```\n * @example\n * `skipToken` disables the query in a type-safe way, without a non-null assertion on `props.postId` —\n * `queryFn` is only ever called when it's defined. The whole-options getter re-evaluates `queryFn` on every\n * change to `props.postId`. `queryFn` can also be a `computed`, but never a bare getter, since a function\n * there is the query function itself. `refetch` doesn't work while `queryFn` is `skipToken` — use\n * `enabled: false` instead if you need to trigger the query manually:\n * ```vue\n * <script setup lang=\"ts\">\n * import { skipToken, useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number | undefined }>()\n *\n * const { data, isLoading, isError, error } = useQuery(() => {\n * const postId = props.postId\n * return {\n * queryKey: ['post', postId],\n * queryFn: postId != null ? () => fetchPost(postId) : skipToken,\n * }\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"props.postId == null\">Select a post</span>\n * <span v-else-if=\"isLoading\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: MaybeRefOrGetter<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n >,\n queryClient?: QueryClient,\n): UseQueryReturnType<TData, TError>\n\nexport function useQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: MaybeRefOrGetter<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n >,\n queryClient?: QueryClient,\n):\n | UseQueryReturnType<TData, TError>\n | UseQueryDefinedReturnType<TData, TError> {\n return useBaseQuery(QueryObserver, options, queryClient)\n}\n"],"mappings":";;;;AAySA,SAAgB,SAMd,SAGA,aAG2C;CAC3C,OAAOA,qBAAAA,aAAaC,qBAAAA,eAAe,SAAS,WAAW;AACzD"}
|
|
1
|
+
{"version":3,"file":"useQuery.cjs","names":["useBaseQuery","QueryObserver"],"sources":["../../src/useQuery.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport { useBaseQuery } from './useBaseQuery'\nimport type {\n DefaultError,\n DefinedQueryObserverResult,\n QueryKey,\n} from '@tanstack/query-core'\nimport type { UseBaseQueryReturnType } from './useBaseQuery'\nimport type { MaybeRefOrGetter } from './types'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefinedInitialQueryOptions,\n UndefinedInitialQueryOptions,\n UseQueryOptions,\n} from './queryOptions'\n\n/**\n * The result of `useQuery` when `initialData` is omitted, may be `undefined`, or is a function that may\n * return `undefined` — `data` may be `undefined` while the query is `pending`. See\n * {@link UseBaseQueryReturnType}.\n */\nexport type UseQueryReturnType<TData, TError> = UseBaseQueryReturnType<\n TData,\n TError\n>\n\n/**\n * The result of `useQuery` when `initialData` is set — `data` is never `undefined` (unless a `select` changes\n * `TData` to include `undefined`).\n */\nexport type UseQueryDefinedReturnType<TData, TError> = UseBaseQueryReturnType<\n TData,\n TError,\n DefinedQueryObserverResult<TData, TError>\n>\n\n/**\n * This overload is selected when `initialData` is known to be defined, so the resulting `data` is never\n * `undefined` (unless a `select` changes `TData` to include `undefined`).\n *\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries. Other options are read once when passed as a plain value, and stay\n * reactive when passed as a `ref` or a `computed`.\n * @param options - The {@link DefinedInitialQueryOptions} to use — everything you can pass to `useQuery`,\n * with `initialData` known to be defined.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result, typed so that `data` is never `undefined` (unless a `select` changes\n * `TData` to include `undefined`). `status` never resolves to `pending` in this overload's type, since\n * `initialData` guarantees data upfront.\n * @example\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,\n * // so the list stays visible alongside the error.\n * const { data, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <ul>\n * <li v-for=\"post in data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryDefinedReturnType<TData, TError>\n\n/**\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries. Other options are read once when passed as a plain value, and stay\n * reactive when passed as a `ref` or a `computed`.\n * @param options - The {@link UndefinedInitialQueryOptions} to use — everything you can pass to `useQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if\n * the last fetch attempt failed, or `success` if the query has data to display. `state` is that result as one\n * reactive object — `state.data`, `state.isPending`, and the rest are plain values, and `state.status === 'success'`\n * narrows `state.data` to `TData`.\n * @example\n * A query key built from a reactive `ref` — the query refetches whenever `postId` changes:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const postId = ref(1)\n * const { status, data, error } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId.value),\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"status === 'pending'\">Loading...</span>\n * <span v-else-if=\"status === 'error'\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data.title }}</h1>\n * </template>\n * ```\n * @example\n * `select` derives whatever `data` a component needs from the cached value, without changing what's\n * actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const { data, isPending, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * select: (posts) => posts.length,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isPending\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <span v-else>{{ data }} posts</span>\n * </template>\n * ```\n * @example\n * A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the\n * loading state doesn't show while the query is disabled:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number | undefined }>()\n *\n * const { data, isLoading, isError, error } = useQuery({\n * queryKey: ['post', props.postId],\n * queryFn: () => fetchPost(props.postId!),\n * enabled: () => props.postId != null,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"props.postId == null\">Select a post</span>\n * <span v-else-if=\"isLoading\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n * @example\n * Seeding a detail query from an already-cached list, to skip the loading state. `initialDataUpdatedAt` carries\n * over the list's own fetch time, so that if you set a `staleTime`, it's measured from when the list was\n * fetched rather than from now:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery, useQueryClient } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number }>()\n * const queryClient = useQueryClient()\n *\n * const { data, isError, error } = useQuery({\n * queryKey: ['post', props.postId],\n * queryFn: () => fetchPost(props.postId),\n * initialData: () =>\n * queryClient\n * .getQueryData<Array<Post>>(['posts'])\n * ?.find((post) => post.id === props.postId),\n * initialDataUpdatedAt: () =>\n * queryClient.getQueryState(['posts'])?.dataUpdatedAt,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n * @example\n * Paginated data, keeping the previous page's data visible while the next page loads:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { keepPreviousData, useQuery } from '@tanstack/vue-query'\n *\n * const page = ref(0)\n *\n * const { data, isPlaceholderData, isError, error } = useQuery({\n * queryKey: ['posts', page],\n * queryFn: () => fetchPosts(page.value),\n * placeholderData: keepPreviousData,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <template v-else>\n * <ul>\n * <li v-for=\"post in data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * <button :disabled=\"isPlaceholderData\" @click=\"page++\">Next Page</button>\n * </template>\n * </template>\n * ```\n * @example\n * `state` keeps `data`, `error`, `status`, and `isPending` on one reactive object, so a `status` check\n * narrows `data` and you never read `.value`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const { state } = useQuery({\n * queryKey: ['post'],\n * queryFn: fetchPost,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"state.status === 'pending'\">Loading...</span>\n * <span v-else-if=\"state.status === 'error'\">Error: {{ state.error.message }}</span>\n * <h1 v-else>{{ state.data.title }}</h1>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryReturnType<TData, TError>\n\n/**\n * Fallback overload for options whose `initialData` presence isn't statically known — for example, a\n * `ref`/reactive object built up conditionally, rather than a plain object literal. Prefer one of the other\n * overloads when possible, since they infer whether `data` can be `undefined` from `initialData` directly.\n *\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries.\n *\n * When `options` itself is a reactive getter, the whole object is re-evaluated on every change to its\n * dependencies, so any option inside it — not just `queryKey` and `enabled` — can change over time.\n * @param options - A `ref`, plain value, or reactive getter resolving to the {@link UseQueryOptions} to use.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result, with `data` typed as possibly `undefined`.\n * @example\n * Passing a whole-options getter so `staleTime` reacts to a setting stored elsewhere, not just `queryKey`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ id: number; staleTime: number }>()\n *\n * const { data } = useQuery(() => ({\n * queryKey: ['post', props.id],\n * queryFn: () => fetchPost(props.id),\n * staleTime: props.staleTime,\n * }))\n * </script>\n *\n * <template>\n * <h1 v-if=\"data\">{{ data.title }}</h1>\n * </template>\n * ```\n * @example\n * `skipToken` disables the query in a type-safe way, without a non-null assertion on `props.postId` —\n * `queryFn` is only ever called when it's defined. The whole-options getter re-evaluates `queryFn` on every\n * change to `props.postId`. `queryFn` can also be a `computed`, but never a bare getter, since a function\n * there is the query function itself. `refetch` doesn't work while `queryFn` is `skipToken` — use\n * `enabled: false` instead if you need to trigger the query manually:\n * ```vue\n * <script setup lang=\"ts\">\n * import { skipToken, useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number | undefined }>()\n *\n * const { data, isLoading, isError, error } = useQuery(() => {\n * const postId = props.postId\n * return {\n * queryKey: ['post', postId],\n * queryFn: postId != null ? () => fetchPost(postId) : skipToken,\n * }\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"props.postId == null\">Select a post</span>\n * <span v-else-if=\"isLoading\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: MaybeRefOrGetter<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n >,\n queryClient?: QueryClient,\n): UseQueryReturnType<TData, TError>\n\nexport function useQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: MaybeRefOrGetter<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n >,\n queryClient?: QueryClient,\n):\n | UseQueryReturnType<TData, TError>\n | UseQueryDefinedReturnType<TData, TError> {\n return useBaseQuery(QueryObserver, options, queryClient)\n}\n"],"mappings":";;;;AA8TA,SAAgB,SAMd,SAGA,aAG2C;CAC3C,OAAOA,qBAAAA,aAAaC,qBAAAA,eAAe,SAAS,WAAW;AACzD"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { s as MaybeRefOrGetter } from "./types-u_Tu5Ybs.cjs";
|
|
2
|
-
import { i as UndefinedInitialQueryOptions, o as UseQueryOptions, t as DefinedInitialQueryOptions } from "./queryOptions-
|
|
3
|
-
import { t as QueryClient$1 } from "./queryClient-
|
|
4
|
-
import { c as UseBaseQueryReturnType } from "./infiniteQueryOptions-
|
|
2
|
+
import { i as UndefinedInitialQueryOptions, o as UseQueryOptions, t as DefinedInitialQueryOptions } from "./queryOptions-B-eHjgaY.cjs";
|
|
3
|
+
import { t as QueryClient$1 } from "./queryClient-BSSPWSvX.cjs";
|
|
4
|
+
import { c as UseBaseQueryReturnType } from "./infiniteQueryOptions-BSwO8rvy.cjs";
|
|
5
5
|
import { DefaultError, DefinedQueryObserverResult, QueryKey } from "@tanstack/query-core";
|
|
6
6
|
/**
|
|
7
7
|
* The result of `useQuery` when `initialData` is omitted, may be `undefined`, or is a function that may
|
|
@@ -61,7 +61,9 @@ export declare function useQuery<TQueryFnData = unknown, TError = DefaultError,
|
|
|
61
61
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
62
62
|
* will be used.
|
|
63
63
|
* @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
|
|
64
|
-
* the last fetch attempt failed, or `success` if the query has data to display.
|
|
64
|
+
* the last fetch attempt failed, or `success` if the query has data to display. `state` is that result as one
|
|
65
|
+
* reactive object — `state.data`, `state.isPending`, and the rest are plain values, and `state.status === 'success'`
|
|
66
|
+
* narrows `state.data` to `TData`.
|
|
65
67
|
* @example
|
|
66
68
|
* A query key built from a reactive `ref` — the query refetches whenever `postId` changes:
|
|
67
69
|
* ```vue
|
|
@@ -180,6 +182,25 @@ export declare function useQuery<TQueryFnData = unknown, TError = DefaultError,
|
|
|
180
182
|
* </template>
|
|
181
183
|
* </template>
|
|
182
184
|
* ```
|
|
185
|
+
* @example
|
|
186
|
+
* `state` keeps `data`, `error`, `status`, and `isPending` on one reactive object, so a `status` check
|
|
187
|
+
* narrows `data` and you never read `.value`:
|
|
188
|
+
* ```vue
|
|
189
|
+
* <script setup lang="ts">
|
|
190
|
+
* import { useQuery } from '@tanstack/vue-query'
|
|
191
|
+
*
|
|
192
|
+
* const { state } = useQuery({
|
|
193
|
+
* queryKey: ['post'],
|
|
194
|
+
* queryFn: fetchPost,
|
|
195
|
+
* })
|
|
196
|
+
* </script>
|
|
197
|
+
*
|
|
198
|
+
* <template>
|
|
199
|
+
* <span v-if="state.status === 'pending'">Loading...</span>
|
|
200
|
+
* <span v-else-if="state.status === 'error'">Error: {{ state.error.message }}</span>
|
|
201
|
+
* <h1 v-else>{{ state.data.title }}</h1>
|
|
202
|
+
* </template>
|
|
203
|
+
* ```
|
|
183
204
|
*/
|
|
184
205
|
export declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient$1): UseQueryReturnType<TData, TError>;
|
|
185
206
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.cts","names":["QueryClient"],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;YAqBY,mBAAmB,OAAO,UAAU,uBAC9C,OACA;;;;;YAOU,0BAA0B,OAAO,UAAU,uBACrD,OACA,QACA,2BAA2B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAwCpB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,2BAA2B,cAAc,QAAQ,OAAO,YACjE,cAAcA,gBACb,0BAA0B,OAAO
|
|
1
|
+
{"version":3,"file":"useQuery.d.cts","names":["QueryClient"],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;YAqBY,mBAAmB,OAAO,UAAU,uBAC9C,OACA;;;;;YAOU,0BAA0B,OAAO,UAAU,uBACrD,OACA,QACA,2BAA2B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAwCpB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,2BAA2B,cAAc,QAAQ,OAAO,YACjE,cAAcA,gBACb,0BAA0B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAwJpB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,6BAA6B,cAAc,QAAQ,OAAO,YACnE,cAAcA,gBACb,mBAAmB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAiEb,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,iBACP,gBAAgB,cAAc,QAAQ,OAAO,cAAc,aAE7D,cAAcA,gBACb,mBAAmB,OAAO"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { s as MaybeRefOrGetter } from "./types-u_Tu5Ybs.js";
|
|
2
|
-
import { i as UndefinedInitialQueryOptions, o as UseQueryOptions, t as DefinedInitialQueryOptions } from "./queryOptions-
|
|
3
|
-
import { t as QueryClient$1 } from "./queryClient-
|
|
4
|
-
import { c as UseBaseQueryReturnType } from "./infiniteQueryOptions-
|
|
2
|
+
import { i as UndefinedInitialQueryOptions, o as UseQueryOptions, t as DefinedInitialQueryOptions } from "./queryOptions-D1-foH1W.js";
|
|
3
|
+
import { t as QueryClient$1 } from "./queryClient-Bgw9hMbf.js";
|
|
4
|
+
import { c as UseBaseQueryReturnType } from "./infiniteQueryOptions-Bq8UQFvj.js";
|
|
5
5
|
import { DefaultError, DefinedQueryObserverResult, QueryKey } from "@tanstack/query-core";
|
|
6
6
|
/**
|
|
7
7
|
* The result of `useQuery` when `initialData` is omitted, may be `undefined`, or is a function that may
|
|
@@ -61,7 +61,9 @@ export declare function useQuery<TQueryFnData = unknown, TError = DefaultError,
|
|
|
61
61
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
62
62
|
* will be used.
|
|
63
63
|
* @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
|
|
64
|
-
* the last fetch attempt failed, or `success` if the query has data to display.
|
|
64
|
+
* the last fetch attempt failed, or `success` if the query has data to display. `state` is that result as one
|
|
65
|
+
* reactive object — `state.data`, `state.isPending`, and the rest are plain values, and `state.status === 'success'`
|
|
66
|
+
* narrows `state.data` to `TData`.
|
|
65
67
|
* @example
|
|
66
68
|
* A query key built from a reactive `ref` — the query refetches whenever `postId` changes:
|
|
67
69
|
* ```vue
|
|
@@ -180,6 +182,25 @@ export declare function useQuery<TQueryFnData = unknown, TError = DefaultError,
|
|
|
180
182
|
* </template>
|
|
181
183
|
* </template>
|
|
182
184
|
* ```
|
|
185
|
+
* @example
|
|
186
|
+
* `state` keeps `data`, `error`, `status`, and `isPending` on one reactive object, so a `status` check
|
|
187
|
+
* narrows `data` and you never read `.value`:
|
|
188
|
+
* ```vue
|
|
189
|
+
* <script setup lang="ts">
|
|
190
|
+
* import { useQuery } from '@tanstack/vue-query'
|
|
191
|
+
*
|
|
192
|
+
* const { state } = useQuery({
|
|
193
|
+
* queryKey: ['post'],
|
|
194
|
+
* queryFn: fetchPost,
|
|
195
|
+
* })
|
|
196
|
+
* </script>
|
|
197
|
+
*
|
|
198
|
+
* <template>
|
|
199
|
+
* <span v-if="state.status === 'pending'">Loading...</span>
|
|
200
|
+
* <span v-else-if="state.status === 'error'">Error: {{ state.error.message }}</span>
|
|
201
|
+
* <h1 v-else>{{ state.data.title }}</h1>
|
|
202
|
+
* </template>
|
|
203
|
+
* ```
|
|
183
204
|
*/
|
|
184
205
|
export declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient$1): UseQueryReturnType<TData, TError>;
|
|
185
206
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.ts","names":["QueryClient"],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;YAqBY,mBAAmB,OAAO,UAAU,uBAC9C,OACA;;;;;YAOU,0BAA0B,OAAO,UAAU,uBACrD,OACA,QACA,2BAA2B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAwCpB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,2BAA2B,cAAc,QAAQ,OAAO,YACjE,cAAcA,gBACb,0BAA0B,OAAO
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","names":["QueryClient"],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;YAqBY,mBAAmB,OAAO,UAAU,uBAC9C,OACA;;;;;YAOU,0BAA0B,OAAO,UAAU,uBACrD,OACA,QACA,2BAA2B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAwCpB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,2BAA2B,cAAc,QAAQ,OAAO,YACjE,cAAcA,gBACb,0BAA0B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAwJpB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,6BAA6B,cAAc,QAAQ,OAAO,YACnE,cAAcA,gBACb,mBAAmB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAiEb,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,iBACP,gBAAgB,cAAc,QAAQ,OAAO,cAAc,aAE7D,cAAcA,gBACb,mBAAmB,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.js","names":[],"sources":["../../src/useQuery.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport { useBaseQuery } from './useBaseQuery'\nimport type {\n DefaultError,\n DefinedQueryObserverResult,\n QueryKey,\n} from '@tanstack/query-core'\nimport type { UseBaseQueryReturnType } from './useBaseQuery'\nimport type { MaybeRefOrGetter } from './types'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefinedInitialQueryOptions,\n UndefinedInitialQueryOptions,\n UseQueryOptions,\n} from './queryOptions'\n\n/**\n * The result of `useQuery` when `initialData` is omitted, may be `undefined`, or is a function that may\n * return `undefined` — `data` may be `undefined` while the query is `pending`. See\n * {@link UseBaseQueryReturnType}.\n */\nexport type UseQueryReturnType<TData, TError> = UseBaseQueryReturnType<\n TData,\n TError\n>\n\n/**\n * The result of `useQuery` when `initialData` is set — `data` is never `undefined` (unless a `select` changes\n * `TData` to include `undefined`).\n */\nexport type UseQueryDefinedReturnType<TData, TError> = UseBaseQueryReturnType<\n TData,\n TError,\n DefinedQueryObserverResult<TData, TError>\n>\n\n/**\n * This overload is selected when `initialData` is known to be defined, so the resulting `data` is never\n * `undefined` (unless a `select` changes `TData` to include `undefined`).\n *\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries. Other options are read once when passed as a plain value, and stay\n * reactive when passed as a `ref` or a `computed`.\n * @param options - The {@link DefinedInitialQueryOptions} to use — everything you can pass to `useQuery`,\n * with `initialData` known to be defined.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result, typed so that `data` is never `undefined` (unless a `select` changes\n * `TData` to include `undefined`). `status` never resolves to `pending` in this overload's type, since\n * `initialData` guarantees data upfront.\n * @example\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,\n * // so the list stays visible alongside the error.\n * const { data, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <ul>\n * <li v-for=\"post in data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryDefinedReturnType<TData, TError>\n\n/**\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries. Other options are read once when passed as a plain value, and stay\n * reactive when passed as a `ref` or a `computed`.\n * @param options - The {@link UndefinedInitialQueryOptions} to use — everything you can pass to `useQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if\n * the last fetch attempt failed, or `success` if the query has data to display.\n * @example\n * A query key built from a reactive `ref` — the query refetches whenever `postId` changes:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const postId = ref(1)\n * const { status, data, error } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId.value),\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"status === 'pending'\">Loading...</span>\n * <span v-else-if=\"status === 'error'\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data.title }}</h1>\n * </template>\n * ```\n * @example\n * `select` derives whatever `data` a component needs from the cached value, without changing what's\n * actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const { data, isPending, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * select: (posts) => posts.length,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isPending\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <span v-else>{{ data }} posts</span>\n * </template>\n * ```\n * @example\n * A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the\n * loading state doesn't show while the query is disabled:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number | undefined }>()\n *\n * const { data, isLoading, isError, error } = useQuery({\n * queryKey: ['post', props.postId],\n * queryFn: () => fetchPost(props.postId!),\n * enabled: () => props.postId != null,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"props.postId == null\">Select a post</span>\n * <span v-else-if=\"isLoading\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n * @example\n * Seeding a detail query from an already-cached list, to skip the loading state. `initialDataUpdatedAt` carries\n * over the list's own fetch time, so that if you set a `staleTime`, it's measured from when the list was\n * fetched rather than from now:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery, useQueryClient } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number }>()\n * const queryClient = useQueryClient()\n *\n * const { data, isError, error } = useQuery({\n * queryKey: ['post', props.postId],\n * queryFn: () => fetchPost(props.postId),\n * initialData: () =>\n * queryClient\n * .getQueryData<Array<Post>>(['posts'])\n * ?.find((post) => post.id === props.postId),\n * initialDataUpdatedAt: () =>\n * queryClient.getQueryState(['posts'])?.dataUpdatedAt,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n * @example\n * Paginated data, keeping the previous page's data visible while the next page loads:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { keepPreviousData, useQuery } from '@tanstack/vue-query'\n *\n * const page = ref(0)\n *\n * const { data, isPlaceholderData, isError, error } = useQuery({\n * queryKey: ['posts', page],\n * queryFn: () => fetchPosts(page.value),\n * placeholderData: keepPreviousData,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <template v-else>\n * <ul>\n * <li v-for=\"post in data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * <button :disabled=\"isPlaceholderData\" @click=\"page++\">Next Page</button>\n * </template>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryReturnType<TData, TError>\n\n/**\n * Fallback overload for options whose `initialData` presence isn't statically known — for example, a\n * `ref`/reactive object built up conditionally, rather than a plain object literal. Prefer one of the other\n * overloads when possible, since they infer whether `data` can be `undefined` from `initialData` directly.\n *\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries.\n *\n * When `options` itself is a reactive getter, the whole object is re-evaluated on every change to its\n * dependencies, so any option inside it — not just `queryKey` and `enabled` — can change over time.\n * @param options - A `ref`, plain value, or reactive getter resolving to the {@link UseQueryOptions} to use.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result, with `data` typed as possibly `undefined`.\n * @example\n * Passing a whole-options getter so `staleTime` reacts to a setting stored elsewhere, not just `queryKey`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ id: number; staleTime: number }>()\n *\n * const { data } = useQuery(() => ({\n * queryKey: ['post', props.id],\n * queryFn: () => fetchPost(props.id),\n * staleTime: props.staleTime,\n * }))\n * </script>\n *\n * <template>\n * <h1 v-if=\"data\">{{ data.title }}</h1>\n * </template>\n * ```\n * @example\n * `skipToken` disables the query in a type-safe way, without a non-null assertion on `props.postId` —\n * `queryFn` is only ever called when it's defined. The whole-options getter re-evaluates `queryFn` on every\n * change to `props.postId`. `queryFn` can also be a `computed`, but never a bare getter, since a function\n * there is the query function itself. `refetch` doesn't work while `queryFn` is `skipToken` — use\n * `enabled: false` instead if you need to trigger the query manually:\n * ```vue\n * <script setup lang=\"ts\">\n * import { skipToken, useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number | undefined }>()\n *\n * const { data, isLoading, isError, error } = useQuery(() => {\n * const postId = props.postId\n * return {\n * queryKey: ['post', postId],\n * queryFn: postId != null ? () => fetchPost(postId) : skipToken,\n * }\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"props.postId == null\">Select a post</span>\n * <span v-else-if=\"isLoading\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: MaybeRefOrGetter<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n >,\n queryClient?: QueryClient,\n): UseQueryReturnType<TData, TError>\n\nexport function useQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: MaybeRefOrGetter<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n >,\n queryClient?: QueryClient,\n):\n | UseQueryReturnType<TData, TError>\n | UseQueryDefinedReturnType<TData, TError> {\n return useBaseQuery(QueryObserver, options, queryClient)\n}\n"],"mappings":";;;AAySA,SAAgB,SAMd,SAGA,aAG2C;CAC3C,OAAO,aAAa,eAAe,SAAS,WAAW;AACzD"}
|
|
1
|
+
{"version":3,"file":"useQuery.js","names":[],"sources":["../../src/useQuery.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport { useBaseQuery } from './useBaseQuery'\nimport type {\n DefaultError,\n DefinedQueryObserverResult,\n QueryKey,\n} from '@tanstack/query-core'\nimport type { UseBaseQueryReturnType } from './useBaseQuery'\nimport type { MaybeRefOrGetter } from './types'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefinedInitialQueryOptions,\n UndefinedInitialQueryOptions,\n UseQueryOptions,\n} from './queryOptions'\n\n/**\n * The result of `useQuery` when `initialData` is omitted, may be `undefined`, or is a function that may\n * return `undefined` — `data` may be `undefined` while the query is `pending`. See\n * {@link UseBaseQueryReturnType}.\n */\nexport type UseQueryReturnType<TData, TError> = UseBaseQueryReturnType<\n TData,\n TError\n>\n\n/**\n * The result of `useQuery` when `initialData` is set — `data` is never `undefined` (unless a `select` changes\n * `TData` to include `undefined`).\n */\nexport type UseQueryDefinedReturnType<TData, TError> = UseBaseQueryReturnType<\n TData,\n TError,\n DefinedQueryObserverResult<TData, TError>\n>\n\n/**\n * This overload is selected when `initialData` is known to be defined, so the resulting `data` is never\n * `undefined` (unless a `select` changes `TData` to include `undefined`).\n *\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries. Other options are read once when passed as a plain value, and stay\n * reactive when passed as a `ref` or a `computed`.\n * @param options - The {@link DefinedInitialQueryOptions} to use — everything you can pass to `useQuery`,\n * with `initialData` known to be defined.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result, typed so that `data` is never `undefined` (unless a `select` changes\n * `TData` to include `undefined`). `status` never resolves to `pending` in this overload's type, since\n * `initialData` guarantees data upfront.\n * @example\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,\n * // so the list stays visible alongside the error.\n * const { data, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <ul>\n * <li v-for=\"post in data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryDefinedReturnType<TData, TError>\n\n/**\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries. Other options are read once when passed as a plain value, and stay\n * reactive when passed as a `ref` or a `computed`.\n * @param options - The {@link UndefinedInitialQueryOptions} to use — everything you can pass to `useQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if\n * the last fetch attempt failed, or `success` if the query has data to display. `state` is that result as one\n * reactive object — `state.data`, `state.isPending`, and the rest are plain values, and `state.status === 'success'`\n * narrows `state.data` to `TData`.\n * @example\n * A query key built from a reactive `ref` — the query refetches whenever `postId` changes:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const postId = ref(1)\n * const { status, data, error } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId.value),\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"status === 'pending'\">Loading...</span>\n * <span v-else-if=\"status === 'error'\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data.title }}</h1>\n * </template>\n * ```\n * @example\n * `select` derives whatever `data` a component needs from the cached value, without changing what's\n * actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const { data, isPending, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * select: (posts) => posts.length,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isPending\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <span v-else>{{ data }} posts</span>\n * </template>\n * ```\n * @example\n * A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the\n * loading state doesn't show while the query is disabled:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number | undefined }>()\n *\n * const { data, isLoading, isError, error } = useQuery({\n * queryKey: ['post', props.postId],\n * queryFn: () => fetchPost(props.postId!),\n * enabled: () => props.postId != null,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"props.postId == null\">Select a post</span>\n * <span v-else-if=\"isLoading\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n * @example\n * Seeding a detail query from an already-cached list, to skip the loading state. `initialDataUpdatedAt` carries\n * over the list's own fetch time, so that if you set a `staleTime`, it's measured from when the list was\n * fetched rather than from now:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery, useQueryClient } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number }>()\n * const queryClient = useQueryClient()\n *\n * const { data, isError, error } = useQuery({\n * queryKey: ['post', props.postId],\n * queryFn: () => fetchPost(props.postId),\n * initialData: () =>\n * queryClient\n * .getQueryData<Array<Post>>(['posts'])\n * ?.find((post) => post.id === props.postId),\n * initialDataUpdatedAt: () =>\n * queryClient.getQueryState(['posts'])?.dataUpdatedAt,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n * @example\n * Paginated data, keeping the previous page's data visible while the next page loads:\n * ```vue\n * <script setup lang=\"ts\">\n * import { ref } from 'vue'\n * import { keepPreviousData, useQuery } from '@tanstack/vue-query'\n *\n * const page = ref(0)\n *\n * const { data, isPlaceholderData, isError, error } = useQuery({\n * queryKey: ['posts', page],\n * queryFn: () => fetchPosts(page.value),\n * placeholderData: keepPreviousData,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"isError\">Error: {{ error.message }}</span>\n * <template v-else>\n * <ul>\n * <li v-for=\"post in data\" :key=\"post.id\">{{ post.title }}</li>\n * </ul>\n * <button :disabled=\"isPlaceholderData\" @click=\"page++\">Next Page</button>\n * </template>\n * </template>\n * ```\n * @example\n * `state` keeps `data`, `error`, `status`, and `isPending` on one reactive object, so a `status` check\n * narrows `data` and you never read `.value`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const { state } = useQuery({\n * queryKey: ['post'],\n * queryFn: fetchPost,\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"state.status === 'pending'\">Loading...</span>\n * <span v-else-if=\"state.status === 'error'\">Error: {{ state.error.message }}</span>\n * <h1 v-else>{{ state.data.title }}</h1>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryReturnType<TData, TError>\n\n/**\n * Fallback overload for options whose `initialData` presence isn't statically known — for example, a\n * `ref`/reactive object built up conditionally, rather than a plain object literal. Prefer one of the other\n * overloads when possible, since they infer whether `data` can be `undefined` from `initialData` directly.\n *\n * `enabled` tracks reactive dependencies automatically as a `ref`, a plain value, or a reactive getter\n * (`() => ...`). `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and\n * reactive getters as individual entries.\n *\n * When `options` itself is a reactive getter, the whole object is re-evaluated on every change to its\n * dependencies, so any option inside it — not just `queryKey` and `enabled` — can change over time.\n * @param options - A `ref`, plain value, or reactive getter resolving to the {@link UseQueryOptions} to use.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`\n * will be used.\n * @returns The current query result, with `data` typed as possibly `undefined`.\n * @example\n * Passing a whole-options getter so `staleTime` reacts to a setting stored elsewhere, not just `queryKey`:\n * ```vue\n * <script setup lang=\"ts\">\n * import { useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ id: number; staleTime: number }>()\n *\n * const { data } = useQuery(() => ({\n * queryKey: ['post', props.id],\n * queryFn: () => fetchPost(props.id),\n * staleTime: props.staleTime,\n * }))\n * </script>\n *\n * <template>\n * <h1 v-if=\"data\">{{ data.title }}</h1>\n * </template>\n * ```\n * @example\n * `skipToken` disables the query in a type-safe way, without a non-null assertion on `props.postId` —\n * `queryFn` is only ever called when it's defined. The whole-options getter re-evaluates `queryFn` on every\n * change to `props.postId`. `queryFn` can also be a `computed`, but never a bare getter, since a function\n * there is the query function itself. `refetch` doesn't work while `queryFn` is `skipToken` — use\n * `enabled: false` instead if you need to trigger the query manually:\n * ```vue\n * <script setup lang=\"ts\">\n * import { skipToken, useQuery } from '@tanstack/vue-query'\n *\n * const props = defineProps<{ postId: number | undefined }>()\n *\n * const { data, isLoading, isError, error } = useQuery(() => {\n * const postId = props.postId\n * return {\n * queryKey: ['post', postId],\n * queryFn: postId != null ? () => fetchPost(postId) : skipToken,\n * }\n * })\n * </script>\n *\n * <template>\n * <span v-if=\"props.postId == null\">Select a post</span>\n * <span v-else-if=\"isLoading\">Loading...</span>\n * <span v-else-if=\"isError\">Error: {{ error.message }}</span>\n * <h1 v-else>{{ data?.title }}</h1>\n * </template>\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: MaybeRefOrGetter<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n >,\n queryClient?: QueryClient,\n): UseQueryReturnType<TData, TError>\n\nexport function useQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: MaybeRefOrGetter<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>\n >,\n queryClient?: QueryClient,\n):\n | UseQueryReturnType<TData, TError>\n | UseQueryDefinedReturnType<TData, TError> {\n return useBaseQuery(QueryObserver, options, queryClient)\n}\n"],"mappings":";;;AA8TA,SAAgB,SAMd,SAGA,aAG2C;CAC3C,OAAO,aAAa,eAAe,SAAS,WAAW;AACzD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as QueryClient } from "./queryClient-
|
|
1
|
+
import { t as QueryClient } from "./queryClient-BSSPWSvX.cjs";
|
|
2
2
|
/**
|
|
3
3
|
* Retrieves the `QueryClient` installed by `VueQueryPlugin`, via Vue's `inject`. Must be called inside
|
|
4
4
|
* `setup()` or another function that supports an injection context.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as QueryClient } from "./queryClient-
|
|
1
|
+
import { t as QueryClient } from "./queryClient-Bgw9hMbf.js";
|
|
2
2
|
/**
|
|
3
3
|
* Retrieves the `QueryClient` installed by `VueQueryPlugin`, via Vue's `inject`. Must be called inside
|
|
4
4
|
* `setup()` or another function that supports an injection context.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { u as QueryClientConfig } from "./types-u_Tu5Ybs.cjs";
|
|
2
|
-
import { t as QueryClient } from "./queryClient-
|
|
2
|
+
import { t as QueryClient } from "./queryClient-BSSPWSvX.cjs";
|
|
3
3
|
type ClientPersister = (client: QueryClient) => [() => void, Promise<void>];
|
|
4
4
|
interface CommonOptions {
|
|
5
5
|
/** Enables the legacy Vue Devtools v6 plugin panel. Prefer `@tanstack/vue-query-devtools` for Vue 3 apps. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { u as QueryClientConfig } from "./types-u_Tu5Ybs.js";
|
|
2
|
-
import { t as QueryClient } from "./queryClient-
|
|
2
|
+
import { t as QueryClient } from "./queryClient-Bgw9hMbf.js";
|
|
3
3
|
type ClientPersister = (client: QueryClient) => [() => void, Promise<void>];
|
|
4
4
|
interface CommonOptions {
|
|
5
5
|
/** Enables the legacy Vue Devtools v6 plugin panel. Prefer `@tanstack/vue-query-devtools` for Vue 3 apps. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/vue-query",
|
|
3
|
-
"version": "5.107.
|
|
3
|
+
"version": "5.107.1",
|
|
4
4
|
"description": "Hooks for managing, caching and syncing asynchronous and remote data in Vue",
|
|
5
5
|
"author": "Damian Osipiuk",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@tanstack/match-sorter-utils": "^9.1.2",
|
|
43
|
-
"@tanstack/query-core": "5.107.
|
|
43
|
+
"@tanstack/query-core": "5.107.1",
|
|
44
44
|
"@vue/devtools-api": "^6.6.3",
|
|
45
45
|
"vue-demi": "^0.14.10"
|
|
46
46
|
},
|
package/src/index.ts
CHANGED
package/src/queryOptions.ts
CHANGED
|
@@ -15,6 +15,21 @@ import type {
|
|
|
15
15
|
QueryObserverOptions,
|
|
16
16
|
} from '@tanstack/query-core'
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* `enabled` for Vue Query. A `ref`, `computed`, or plain boolean is reactive. A zero-argument getter
|
|
20
|
+
* (`() => ...`) is too: it is called while tracking dependencies. query-core's `(query) => boolean` is
|
|
21
|
+
* kept as that callback and is not tracked by Vue — a `ref` read inside it is current when core calls it,
|
|
22
|
+
* and a `ref` change does not by itself call it again.
|
|
23
|
+
*/
|
|
24
|
+
export type VueQueryEnabledOption<
|
|
25
|
+
TQueryFnData,
|
|
26
|
+
TError,
|
|
27
|
+
TQueryData,
|
|
28
|
+
TQueryKey extends QueryKey,
|
|
29
|
+
> =
|
|
30
|
+
| MaybeRefOrGetter<boolean | undefined>
|
|
31
|
+
| QueryBooleanOption<TQueryFnData, TError, TQueryData, TQueryKey>
|
|
32
|
+
|
|
18
33
|
/**
|
|
19
34
|
* The plain, unwrapped options that `queryOptions` hands back, and what `useQuery`, `useQueries`, and the
|
|
20
35
|
* `queryClient` methods see once `ref`s have been resolved. To pass options in, use
|
|
@@ -42,14 +57,12 @@ export type QueryOptions<
|
|
|
42
57
|
TQueryKey
|
|
43
58
|
>
|
|
44
59
|
]: Property extends 'enabled'
|
|
45
|
-
?
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
DeepUnwrapRef<TQueryKey>
|
|
52
|
-
>)
|
|
60
|
+
? VueQueryEnabledOption<
|
|
61
|
+
TQueryFnData,
|
|
62
|
+
TError,
|
|
63
|
+
TQueryData,
|
|
64
|
+
DeepUnwrapRef<TQueryKey>
|
|
65
|
+
>
|
|
53
66
|
: Property extends 'queryKey'
|
|
54
67
|
? MaybeRefOrGetter<TQueryKey>
|
|
55
68
|
: QueryObserverOptions<
|
|
@@ -66,7 +79,8 @@ type SkipTokenForUseQuery = symbol
|
|
|
66
79
|
|
|
67
80
|
/**
|
|
68
81
|
* The options accepted by `queryOptions`, `useQuery`, and the other query hooks. `enabled` tracks reactive
|
|
69
|
-
* dependencies
|
|
82
|
+
* dependencies as a `ref`, a plain boolean, or a zero-argument getter (`() => ...`). query-core's
|
|
83
|
+
* `(query) => boolean` callback is accepted too and is passed through untracked. `queryKey` reacts
|
|
70
84
|
* through a `ref` or a reactive getter for the array itself, or `ref`s and reactive getters as individual
|
|
71
85
|
* entries. Other options are read once when passed as a plain value, and stay reactive when passed as a `ref`
|
|
72
86
|
* or a `computed`. Only `enabled` and `queryKey` read a function as a getter, so `queryFn` reacts through a
|
package/src/useBaseQuery.ts
CHANGED
|
@@ -24,9 +24,17 @@ import type { UseQueryOptions } from './queryOptions'
|
|
|
24
24
|
import type { UseInfiniteQueryOptions } from './useInfiniteQuery'
|
|
25
25
|
import type { MaybeRefOrGetter } from './types'
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Readonly view of a union that stays a union. A single `Readonly<Union>` collapses the members, which
|
|
29
|
+
* drops the `status` / `isSuccess` / `isPending` discriminants.
|
|
30
|
+
*/
|
|
31
|
+
type ReadonlyQueryState<T> = T extends any
|
|
32
|
+
? { readonly [K in keyof T]: T[K] }
|
|
33
|
+
: never
|
|
34
|
+
|
|
27
35
|
/**
|
|
28
36
|
* The result of the query composables: every property of the observer result wrapped in a `Ref`, except
|
|
29
|
-
* the `refetch`, `fetchNextPage`, and `fetchPreviousPage` functions, plus `suspense`.
|
|
37
|
+
* the `refetch`, `fetchNextPage`, and `fetchPreviousPage` functions, plus `suspense` and `state`.
|
|
30
38
|
*/
|
|
31
39
|
export type UseBaseQueryReturnType<
|
|
32
40
|
TData,
|
|
@@ -49,6 +57,37 @@ export type UseBaseQueryReturnType<
|
|
|
49
57
|
* `true`, in which case it rejects.
|
|
50
58
|
*/
|
|
51
59
|
suspense: () => Promise<TResult>
|
|
60
|
+
/**
|
|
61
|
+
* The query result as one readonly reactive object. `data`, `error`, `status`, `isPending`, and the other
|
|
62
|
+
* fields are plain values — read `state.data`, not `state.data.value`.
|
|
63
|
+
*
|
|
64
|
+
* The object is a discriminated union, so a check on `status`, `isSuccess`, `isPending`, or `isError`
|
|
65
|
+
* narrows the other fields. When `status` is `'success'` (or `isSuccess` is `true`), `data` is `TData`.
|
|
66
|
+
* When `status` is `'pending'` (or `isPending` is `true`), `data` is `undefined` and `error` is `null`.
|
|
67
|
+
* @example
|
|
68
|
+
* ```vue
|
|
69
|
+
* <script setup lang="ts">
|
|
70
|
+
* import { useQuery } from '@tanstack/vue-query'
|
|
71
|
+
*
|
|
72
|
+
* const { state } = useQuery({
|
|
73
|
+
* queryKey: ['post'],
|
|
74
|
+
* queryFn: fetchPost,
|
|
75
|
+
* })
|
|
76
|
+
*
|
|
77
|
+
* if (state.status === 'success') {
|
|
78
|
+
* // `state.data` is `Post`, not `Post | undefined`
|
|
79
|
+
* console.log(state.data.title)
|
|
80
|
+
* }
|
|
81
|
+
* </script>
|
|
82
|
+
*
|
|
83
|
+
* <template>
|
|
84
|
+
* <span v-if="state.status === 'pending'">Loading...</span>
|
|
85
|
+
* <span v-else-if="state.status === 'error'">Error: {{ state.error.message }}</span>
|
|
86
|
+
* <h1 v-else>{{ state.data.title }}</h1>
|
|
87
|
+
* </template>
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
state: ReadonlyQueryState<TResult>
|
|
52
91
|
}
|
|
53
92
|
|
|
54
93
|
type UseQueryOptionsGeneric<
|
|
@@ -108,7 +147,12 @@ export function useBaseQuery<
|
|
|
108
147
|
}
|
|
109
148
|
const clonedOptions = cloneDeepUnref(resolvedOptions as any)
|
|
110
149
|
|
|
111
|
-
|
|
150
|
+
// A zero-arg function is a Vue getter and must run here so its refs are tracked.
|
|
151
|
+
// `(query) => boolean` is query-core's callback and is left for core to call.
|
|
152
|
+
if (
|
|
153
|
+
typeof clonedOptions.enabled === 'function' &&
|
|
154
|
+
clonedOptions.enabled.length === 0
|
|
155
|
+
) {
|
|
112
156
|
clonedOptions.enabled = clonedOptions.enabled()
|
|
113
157
|
}
|
|
114
158
|
|
|
@@ -137,6 +181,11 @@ export function useBaseQuery<
|
|
|
137
181
|
// noop
|
|
138
182
|
}
|
|
139
183
|
|
|
184
|
+
// Replaced below with the wrapper. Declared here so the subscriber can put it
|
|
185
|
+
// back after `updateState` copies the observer's own `refetch` onto `state`.
|
|
186
|
+
let refetch: (typeof state)['refetch'] = (...args) =>
|
|
187
|
+
observer.refetch(...args)
|
|
188
|
+
|
|
140
189
|
if (client.isRestoring) {
|
|
141
190
|
watch(
|
|
142
191
|
client.isRestoring,
|
|
@@ -145,6 +194,7 @@ export function useBaseQuery<
|
|
|
145
194
|
unsubscribe()
|
|
146
195
|
unsubscribe = observer.subscribe((result) => {
|
|
147
196
|
updateState(state, result)
|
|
197
|
+
state.refetch = refetch
|
|
148
198
|
})
|
|
149
199
|
}
|
|
150
200
|
},
|
|
@@ -155,6 +205,7 @@ export function useBaseQuery<
|
|
|
155
205
|
const updater = () => {
|
|
156
206
|
observer.setOptions(defaultedOptions.value)
|
|
157
207
|
updateState(state, observer.getCurrentResult())
|
|
208
|
+
state.refetch = refetch
|
|
158
209
|
}
|
|
159
210
|
|
|
160
211
|
watch(defaultedOptions, updater)
|
|
@@ -163,11 +214,13 @@ export function useBaseQuery<
|
|
|
163
214
|
unsubscribe()
|
|
164
215
|
})
|
|
165
216
|
|
|
166
|
-
// fix #5910
|
|
167
|
-
|
|
217
|
+
// fix #5910. Call `observer.refetch`: `state.refetch` is this wrapper, so
|
|
218
|
+
// calling it would recurse.
|
|
219
|
+
refetch = (...args) => {
|
|
168
220
|
updater()
|
|
169
|
-
return
|
|
221
|
+
return observer.refetch(...args)
|
|
170
222
|
}
|
|
223
|
+
state.refetch = refetch
|
|
171
224
|
|
|
172
225
|
const suspense = () => {
|
|
173
226
|
return new Promise<QueryObserverResult<TData, TError>>(
|
|
@@ -176,9 +229,15 @@ export function useBaseQuery<
|
|
|
176
229
|
// noop
|
|
177
230
|
}
|
|
178
231
|
const run = () => {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
232
|
+
// Apply the latest options before resolving `enabled`, so a query callback sees the current query.
|
|
233
|
+
// fix #6133
|
|
234
|
+
observer.setOptions(defaultedOptions.value)
|
|
235
|
+
const { enabled } = defaultedOptions.value
|
|
236
|
+
if (
|
|
237
|
+
(typeof enabled === 'function'
|
|
238
|
+
? enabled(observer.getCurrentQuery())
|
|
239
|
+
: enabled) !== false
|
|
240
|
+
) {
|
|
182
241
|
const optimisticResult = observer.getOptimisticResult(
|
|
183
242
|
defaultedOptions.value,
|
|
184
243
|
)
|
|
@@ -243,6 +302,7 @@ export function useBaseQuery<
|
|
|
243
302
|
|
|
244
303
|
object.suspense = suspense
|
|
245
304
|
object.refetch = refetch
|
|
305
|
+
object.state = readonlyState
|
|
246
306
|
|
|
247
307
|
return object as UseBaseQueryReturnType<TData, TError>
|
|
248
308
|
}
|
package/src/useInfiniteQuery.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
DefinedInitialDataInfiniteOptions,
|
|
5
5
|
UndefinedInitialDataInfiniteOptions,
|
|
6
6
|
} from './infiniteQueryOptions'
|
|
7
|
+
import type { VueQueryEnabledOption } from './queryOptions'
|
|
7
8
|
import type {
|
|
8
9
|
DefaultError,
|
|
9
10
|
DefinedInfiniteQueryObserverResult,
|
|
@@ -47,14 +48,11 @@ export type UseInfiniteQueryOptions<
|
|
|
47
48
|
TPageParam
|
|
48
49
|
>
|
|
49
50
|
]: Property extends 'enabled'
|
|
50
|
-
?
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
DeepUnwrapRef<TQueryKey>,
|
|
56
|
-
TPageParam
|
|
57
|
-
>[Property]
|
|
51
|
+
? VueQueryEnabledOption<
|
|
52
|
+
TQueryFnData,
|
|
53
|
+
TError,
|
|
54
|
+
InfiniteData<TQueryFnData, TPageParam>,
|
|
55
|
+
DeepUnwrapRef<TQueryKey>
|
|
58
56
|
>
|
|
59
57
|
: MaybeRefDeep<
|
|
60
58
|
InfiniteQueryObserverOptions<
|