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

types.ts

349 lines12.5 KB

1/* istanbul ignore file */
2​
3import type {
4 DefaultError,
5 DefinedInfiniteQueryObserverResult,
6 DefinedQueryObserverResult,
7 InfiniteQueryObserverResult,
8 MutateFunction,
9 MutationObserverOptions,
10 MutationObserverResult,
11 OmitKeyof,
12 Override,
13 QueryKey,
14 QueryObserverResult,
15} from '@tanstack/query-core'
16import type {
17 InfiniteQueryObserverOptions,
18 QueryObserverOptions,
19} from './QueryClient'
20import type { Accessor } from 'solid-js'
21​
22/**
23 * The options accepted by `useQuery`. Extends {@link QueryObserverOptions} from `@tanstack/query-core` with
24 * the `solid-query`-specific `deferStream` and `suspense` options.
25 *
26 * @template TQueryFnData - The type your `queryFn` resolves to.
27 * @template TError - The type of errors your `queryFn` may throw.
28 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
29 * `select` is used.
30 * @template TQueryData - The type of the data actually held in the query cache β€” the input to `select` and
31 * `placeholderData`. Defaults to, and is usually the same as, `TQueryFnData`.
32 * @template TQueryKey - The type of your `queryKey`.
33 */
34export interface UseBaseQueryOptions<
35 TQueryFnData = unknown,
36 TError = DefaultError,
37 TData = TQueryFnData,
38 TQueryData = TQueryFnData,
39 TQueryKey extends QueryKey = QueryKey,
40> extends OmitKeyof<
41 QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
42 'suspense'
43> {
44 /**
45 * Only applicable while rendering queries on the server with streaming.
46 * Set `deferStream` to `true` to wait for the query to resolve on the server before flushing the stream.
47 * This can be useful to avoid sending a loading state to the client before the query has resolved.
48 *
49 * @defaultValue false
50 */
51 deferStream?: boolean
52 /**
53 * @deprecated The `suspense` option has been deprecated in v5 and will be removed in the next major version.
54 * The `data` property on useQuery is a SolidJS resource and will automatically suspend when the data is loading.
55 * Setting `suspense` to `false` will be a no-op.
56 */
57 suspense?: boolean
59​
60/**
61 * The options accepted by `useQuery` and `queryOptions`.
62 *
63 * @template TQueryFnData - The type your `queryFn` resolves to.
64 * @template TError - The type of errors your `queryFn` may throw.
65 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
66 * `select` is used.
67 * @template TQueryKey - The type of your `queryKey`.
68 */
69export interface QueryOptions<
70 TQueryFnData = unknown,
71 TError = DefaultError,
72 TData = TQueryFnData,
73 TQueryKey extends QueryKey = QueryKey,
74> extends UseBaseQueryOptions<
75 TQueryFnData,
76 TError,
77 TData,
78 TQueryFnData,
79 TQueryKey
80> {}
81​
82/**
83 * The accessor `useQuery` expects as its first argument β€” Solid re-evaluates it reactively, so `queryKey` and
84 * other options can depend on signals.
85 *
86 * @template TQueryFnData - The type your `queryFn` resolves to.
87 * @template TError - The type of errors your `queryFn` may throw.
88 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
89 * `select` is used.
90 * @template TQueryKey - The type of your `queryKey`.
91 */
92export type UseQueryOptions<
93 TQueryFnData = unknown,
94 TError = DefaultError,
95 TData = TQueryFnData,
96 TQueryKey extends QueryKey = QueryKey,
97> = Accessor<QueryOptions<TQueryFnData, TError, TData, TQueryKey>>
98​
99/* --- Create Query and Create Base Query Types --- */
100​
101/**
102 * The object `useQuery` returns when `initialData` isn't set β€” `data`/`error` may still be `undefined`/`null`
103 * while the query is `pending`. Re-exports {@link QueryObserverResult} from `@tanstack/query-core`.
104 * `useInfiniteQuery` returns {@link UseInfiniteQueryResult} instead.
105 *
106 * @template TData - The type `data` ends up as, after `select` runs (if set).
107 * @template TError - The type of errors this query may hold.
108 */
109export type UseBaseQueryResult<
110 TData = unknown,
111 TError = DefaultError,
112> = QueryObserverResult<TData, TError>
113​
114/**
115 * The object `useQuery` returns β€” `data`/`error` may still be `undefined`/`null` while the query is
116 * `pending`.
117 *
118 * @template TData - The type `data` ends up as, after `select` runs (if set).
119 * @template TError - The type of errors this query may hold.
120 */
121export type UseQueryResult<
122 TData = unknown,
123 TError = DefaultError,
124> = UseBaseQueryResult<TData, TError>
125​
126/**
127 * The object `useQuery` returns when `initialData` guarantees `data` is never `undefined`.
128 *
129 * @template TData - The type `data` ends up as, after `select` runs (if set).
130 * @template TError - The type of errors this query may hold.
131 */
132export type DefinedUseBaseQueryResult<
133 TData = unknown,
134 TError = DefaultError,
135> = DefinedQueryObserverResult<TData, TError>
136​
137/**
138 * The object `useQuery` returns when `initialData` guarantees `data` is never `undefined`.
139 *
140 * @template TData - The type `data` ends up as, after `select` runs (if set).
141 * @template TError - The type of errors this query may hold.
142 */
143export type DefinedUseQueryResult<
144 TData = unknown,
145 TError = DefaultError,
146> = DefinedUseBaseQueryResult<TData, TError>
147​
148/* --- Create Infinite Queries Types --- */
149/**
150 * The options accepted by `useInfiniteQuery`.
151 *
152 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
153 * @template TError - The type of errors your `queryFn` may throw.
154 * @template TData - The type `data` ends up as after `select` runs.
155 * @template TQueryKey - The type of your `queryKey`.
156 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
157 */
158export interface InfiniteQueryOptions<
159 TQueryFnData = unknown,
160 TError = DefaultError,
161 TData = TQueryFnData,
162 TQueryKey extends QueryKey = QueryKey,
163 TPageParam = unknown,
164> extends OmitKeyof<
165 InfiniteQueryObserverOptions<
166 TQueryFnData,
167 TError,
168 TData,
169 TQueryKey,
170 TPageParam
171 >,
172 'queryKey' | 'suspense'
173> {
174 /**
175 * The query key to use for this query. Required here, unlike on the options this type extends.
176 *
177 * The query key will be hashed into a stable hash. See [Query Keys](https://tanstack.com/query/latest/docs/framework/solid/guides/query-keys)
178 * for more information.
179 *
180 * The query will automatically update when this key changes (as long as `enabled` is not set to `false`).
181 */
182 queryKey: TQueryKey
183 /**
184 * Only applicable while rendering queries on the server with streaming.
185 * Set `deferStream` to `true` to wait for the query to resolve on the server before flushing the stream.
186 * This can be useful to avoid sending a loading state to the client before the query has resolved.
187 *
188 * @defaultValue false
189 */
190 deferStream?: boolean
191 /**
192 * @deprecated The `suspense` option has been deprecated in v5 and will be removed in the next major version.
193 * The `data` property on useInfiniteQuery is a SolidJS resource and will automatically suspend when the data is loading.
194 * Setting `suspense` to `false` will be a no-op.
195 */
196 suspense?: boolean
198​
199/**
200 * The accessor `useInfiniteQuery` expects as its first argument β€” Solid re-evaluates it reactively, so
201 * `queryKey` and other options can depend on signals.
202 *
203 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
204 * @template TError - The type of errors your `queryFn` may throw.
205 * @template TData - The type `data` ends up as after `select` runs.
206 * @template TQueryKey - The type of your `queryKey`.
207 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
208 */
209export type UseInfiniteQueryOptions<
210 TQueryFnData = unknown,
211 TError = DefaultError,
212 TData = TQueryFnData,
213 TQueryKey extends QueryKey = QueryKey,
214 TPageParam = unknown,
215> = Accessor<
216 InfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>
218​
219/**
220 * The object `useInfiniteQuery` returns β€” `data`/`error` may still be `undefined`/`null` while the query is
221 * `pending`.
222 *
223 * @template TData - The type `data` ends up as, after `select` runs (if set).
224 * @template TError - The type of errors this query may hold.
225 */
226export type UseInfiniteQueryResult<
227 TData = unknown,
228 TError = DefaultError,
229> = InfiniteQueryObserverResult<TData, TError>
230​
231/**
232 * The object `useInfiniteQuery` returns when `initialData` guarantees `data` is never `undefined`.
233 *
234 * @template TData - The type `data` ends up as, after `select` runs (if set).
235 * @template TError - The type of errors this query may hold.
236 */
237export type DefinedUseInfiniteQueryResult<
238 TData = unknown,
239 TError = DefaultError,
240> = DefinedInfiniteQueryObserverResult<TData, TError>
241​
242/* --- Create Mutation Types --- */
243/**
244 * The options accepted by `useMutation` and `mutationOptions`.
245 *
246 * @template TData - The type your `mutationFn` resolves to.
247 * @template TError - The type of errors your `mutationFn` may throw.
248 * @template TVariables - The type of the variables your `mutationFn` accepts.
249 * @template TOnMutateResult - The type returned by `onMutate`, passed on to `onSuccess`/`onError`/`onSettled`.
250 */
251export interface MutationOptions<
252 TData = unknown,
253 TError = DefaultError,
254 TVariables = void,
255 TOnMutateResult = unknown,
256> extends OmitKeyof<
257 MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
258 '_defaulted'
259> {}
260​
261/**
262 * The accessor `useMutation` expects as its first argument β€” Solid re-evaluates it reactively, so callbacks
263 * and other options can depend on signals.
264 *
265 * @template TData - The type your `mutationFn` resolves to.
266 * @template TError - The type of errors your `mutationFn` may throw.
267 * @template TVariables - The type of the variables your `mutationFn` accepts.
268 * @template TOnMutateResult - The type returned by `onMutate`, passed on to `onSuccess`/`onError`/`onSettled`.
269 */
270export type UseMutationOptions<
271 TData = unknown,
272 TError = DefaultError,
273 TVariables = void,
274 TOnMutateResult = unknown,
275> = Accessor<MutationOptions<TData, TError, TVariables, TOnMutateResult>>
276​
277export type UseMutateFunction<
278 TData = unknown,
279 TError = DefaultError,
280 TVariables = void,
281 TOnMutateResult = unknown,
282> = (
283 ...args: Parameters<
284 MutateFunction<TData, TError, TVariables, TOnMutateResult>
285 >
286) => void
287​
288/**
289 * The type of `mutateAsync`, as returned by `useMutation`. Similar to {@link UseMutateFunction}, but returns a
290 * promise which can be awaited.
291 *
292 * @template TData - The type your `mutationFn` resolves to.
293 * @template TError - The type of errors your `mutationFn` may throw.
294 * @template TVariables - The type of the variable passed to `mutateAsync`.
295 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
296 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
297 */
298export type UseMutateAsyncFunction<
299 TData = unknown,
300 TError = DefaultError,
301 TVariables = void,
302 TOnMutateResult = unknown,
303> = MutateFunction<TData, TError, TVariables, TOnMutateResult>
304​
305/**
306 * The result of `useMutation`. Same as {@link MutationObserverResult} from `@tanstack/query-core`, with
307 * `mutate` narrowed to the fire-and-forget {@link UseMutateFunction} signature, plus the added `mutateAsync`.
308 *
309 * @template TData - The type your `mutationFn` resolves to.
310 * @template TError - The type of errors your `mutationFn` may throw.
311 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
312 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
313 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
314 */
315export type UseBaseMutationResult<
316 TData = unknown,
317 TError = DefaultError,
318 TVariables = unknown,
319 TOnMutateResult = unknown,
320> = Override<
321 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
322 { mutate: UseMutateFunction<TData, TError, TVariables, TOnMutateResult> }
323> & {
324 /**
325 * Similar to `mutate`, but returns a promise which can be awaited.
326 */
327 mutateAsync: UseMutateAsyncFunction<
328 TData,
329 TError,
330 TVariables,
331 TOnMutateResult
332 >
334​
335/**
336 * The result of `useMutation`. Same as {@link UseBaseMutationResult}.
337 *
338 * @template TData - The type your `mutationFn` resolves to.
339 * @template TError - The type of errors your `mutationFn` may throw.
340 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
341 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
342 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
343 */
344export type UseMutationResult<
345 TData = unknown,
346 TError = DefaultError,
347 TVariables = unknown,
348 TOnMutateResult = unknown,
349> = UseBaseMutationResult<TData, TError, TVariables, TOnMutateResult>