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