Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

query-options.ts

120 lines3.2 KB

1import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
2import type { CreateQueryOptions, NonUndefinedGuard } from './types'
3​
4/**
5 * @public
6 */
7export type UndefinedInitialDataOptions<
8 TQueryFnData = unknown,
9 TError = DefaultError,
10 TData = TQueryFnData,
11 TQueryKey extends QueryKey = QueryKey,
12> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
13 initialData?: undefined
15​
16/**
17 * @public
18 */
19export type DefinedInitialDataOptions<
20 TQueryFnData = unknown,
21 TError = DefaultError,
22 TData = TQueryFnData,
23 TQueryKey extends QueryKey = QueryKey,
24> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
25 initialData:
26 | NonUndefinedGuard<TQueryFnData>
27 | (() => NonUndefinedGuard<TQueryFnData>)
29​
30/**
31 * Allows to share and re-use query options in a type-safe way.
32 *
33 * The `queryKey` will be tagged with the type from `queryFn`.
34 *
35 * **Example**
36 *
37 * ```ts
38 * const { queryKey } = queryOptions({
39 * queryKey: ['key'],
40 * queryFn: () => Promise.resolve(5),
41 * // ^? Promise<number>
42 * })
43 *
44 * const queryClient = new QueryClient()
45 * const data = queryClient.getQueryData(queryKey)
46 * // ^? number | undefined
47 * ```
48 * @param options - The query options to tag with the type from `queryFn`.
49 * @returns The tagged query options.
50 * @public
51 */
52export function queryOptions<
53 TQueryFnData = unknown,
54 TError = DefaultError,
55 TData = TQueryFnData,
56 TQueryKey extends QueryKey = QueryKey,
57>(
58 options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
59): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & {
60 queryKey: DataTag<TQueryKey, TQueryFnData>
62​
63/**
64 * Allows to share and re-use query options in a type-safe way.
65 *
66 * The `queryKey` will be tagged with the type from `queryFn`.
67 *
68 * **Example**
69 *
70 * ```ts
71 * const { queryKey } = queryOptions({
72 * queryKey: ['key'],
73 * queryFn: () => Promise.resolve(5),
74 * // ^? Promise<number>
75 * })
76 *
77 * const queryClient = new QueryClient()
78 * const data = queryClient.getQueryData(queryKey)
79 * // ^? number | undefined
80 * ```
81 * @param options - The query options to tag with the type from `queryFn`.
82 * @returns The tagged query options.
83 * @public
84 */
85export function queryOptions<
86 TQueryFnData = unknown,
87 TError = DefaultError,
88 TData = TQueryFnData,
89 TQueryKey extends QueryKey = QueryKey,
90>(
91 options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
92): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & {
93 queryKey: DataTag<TQueryKey, TQueryFnData>
95​
96/**
97 * Allows to share and re-use query options in a type-safe way.
98 *
99 * The `queryKey` will be tagged with the type from `queryFn`.
100 *
101 * **Example**
102 *
103 * ```ts
104 * const { queryKey } = queryOptions({
105 * queryKey: ['key'],
106 * queryFn: () => Promise.resolve(5),
107 * // ^? Promise<number>
108 * })
109 *
110 * const queryClient = new QueryClient()
111 * const data = queryClient.getQueryData(queryKey)
112 * // ^? number | undefined
113 * ```
114 * @param options - The query options to tag with the type from `queryFn`.
115 * @returns The tagged query options.
116 * @public
117 */
118export function queryOptions(options: unknown) {
119 return options