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

types.ts

538 lines17.4 KB

1/* istanbul ignore file */
2​
3import type {
4 DefaultError,
5 DefinedInfiniteQueryObserverResult,
6 DefinedQueryObserverResult,
7 InfiniteQueryObserverOptions,
8 InfiniteQueryObserverResult,
9 MutateFunction,
10 MutationObserverOptions,
11 MutationObserverResult,
12 OmitKeyof,
13 Override,
14 QueryKey,
15 QueryObserverOptions,
16 QueryObserverResult,
17} from '@tanstack/query-core'
18import type { Signal } from '@angular/core'
19import type { MapToSignals } from './utils/signal-proxy'
20import type {
21 InfiniteQueryResultFields,
22 MutationResultFields,
23 QueryResultFields,
24} from './utils/result-fields'
25​
26type SignalFunction<T extends () => any> = T & Signal<ReturnType<T>>
27​
28/**
29 * Options accepted by `injectQuery`, with cache data projected through an optional `select`.
30 * Angular owns result notifications and exposes errors through signals rather than error boundaries.
31 */
32export type CreateQueryOptions<
33 TQueryFnData = unknown,
34 TError = DefaultError,
35 TData = TQueryFnData,
36 TQueryKey extends QueryKey = QueryKey,
37> = OmitKeyof<
38 QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
39 'notifyOnChangeProps' | 'suspense' | 'throwOnError'
41​
42type CreateStatusBasedQueryResult<
43 TStatus extends QueryObserverResult['status'],
44 TData = unknown,
45 TError = DefaultError,
46> = Extract<QueryObserverResult<TData, TError>, { status: TStatus }>
47​
48type CreateStatusBasedDefinedQueryResult<
49 TStatus extends DefinedQueryObserverResult['status'],
50 TData = unknown,
51 TError = DefaultError,
52> = Extract<DefinedQueryObserverResult<TData, TError>, { status: TStatus }>
53​
54type CreateStatusBasedInfiniteQueryResult<
55 TStatus extends InfiniteQueryObserverResult['status'],
56 TData = unknown,
57 TError = DefaultError,
58> = Extract<InfiniteQueryObserverResult<TData, TError>, { status: TStatus }>
59​
60type CreateStatusBasedDefinedInfiniteQueryResult<
61 TStatus extends DefinedInfiniteQueryObserverResult['status'],
62 TData = unknown,
63 TError = DefaultError,
64> = Extract<
65 DefinedInfiniteQueryObserverResult<TData, TError>,
66 { status: TStatus }
68​
69/**
70 * Signal-backed guards that narrow a query result and its data and error fields by status.
71 */
72export interface BaseQueryNarrowing<TData = unknown, TError = DefaultError> {
73 /**
74 * Returns whether the result is successful and narrows its data and error signals.
75 */
76 isSuccess: SignalFunction<
77 () => this is CreateBaseQueryResult<
78 TData,
79 TError,
80 CreateStatusBasedQueryResult<'success', TData, TError>
81 >
82 >
83 /**
84 * Returns whether the result failed and narrows its data and error signals.
85 */
86 isError: SignalFunction<
87 () => this is CreateBaseQueryResult<
88 TData,
89 TError,
90 CreateStatusBasedQueryResult<'error', TData, TError>
91 >
92 >
93 /**
94 * Narrows the result to the pending state when that state is possible.
95 */
96 isPending: SignalFunction<
97 () => this is CreateBaseQueryResult<
98 TData,
99 TError,
100 CreateStatusBasedQueryResult<'pending', TData, TError>
101 >
102 >
104​
105/**
106 * Signal-backed guards for a query whose initial data excludes the pending state.
107 */
108export interface DefinedQueryNarrowing<TData = unknown, TError = DefaultError> {
109 /**
110 * Returns whether the result is successful and narrows its data and error signals.
111 */
112 isSuccess: SignalFunction<
113 () => this is DefinedCreateQueryResult<
114 TData,
115 TError,
116 CreateStatusBasedDefinedQueryResult<'success', TData, TError>
117 >
118 >
119 /**
120 * Returns whether the result failed and narrows its data and error signals.
121 */
122 isError: SignalFunction<
123 () => this is DefinedCreateQueryResult<
124 TData,
125 TError,
126 CreateStatusBasedDefinedQueryResult<'error', TData, TError>
127 >
128 >
129 /**
130 * Narrows the result to the pending state when that state is possible.
131 */
132 isPending: SignalFunction<() => this is never>
134​
135/**
136 * Signal-backed guards that narrow a paginated query result by its current status.
137 */
138export interface BaseInfiniteQueryNarrowing<
139 TData = unknown,
140 TError = DefaultError,
141> {
142 /**
143 * Returns whether the result is successful and narrows its data and error signals.
144 */
145 isSuccess: SignalFunction<
146 (
147 this: CreateInfiniteQueryResult<TData, TError>,
148 ) => this is CreateInfiniteQueryResult<
149 TData,
150 TError,
151 CreateStatusBasedInfiniteQueryResult<'success', TData, TError>
152 >
153 >
154 /**
155 * Returns whether the result failed and narrows its data and error signals.
156 */
157 isError: SignalFunction<
158 (
159 this: CreateInfiniteQueryResult<TData, TError>,
160 ) => this is CreateInfiniteQueryResult<
161 TData,
162 TError,
163 CreateStatusBasedInfiniteQueryResult<'error', TData, TError>
164 >
165 >
166 /**
167 * Narrows the result to the pending state when that state is possible.
168 */
169 isPending: SignalFunction<
170 (
171 this: CreateInfiniteQueryResult<TData, TError>,
172 ) => this is CreateInfiniteQueryResult<
173 TData,
174 TError,
175 CreateStatusBasedInfiniteQueryResult<'pending', TData, TError>
176 >
177 >
179​
180/**
181 * Signal-backed guards for a paginated query whose initial data excludes the pending state.
182 */
183export interface DefinedInfiniteQueryNarrowing<
184 TData = unknown,
185 TError = DefaultError,
186> {
187 /**
188 * Returns whether the result is successful and narrows its data and error signals.
189 */
190 isSuccess: SignalFunction<
191 (
192 this: DefinedCreateInfiniteQueryResult<TData, TError>,
193 ) => this is DefinedCreateInfiniteQueryResult<
194 TData,
195 TError,
196 CreateStatusBasedDefinedInfiniteQueryResult<'success', TData, TError>
197 >
198 >
199 /**
200 * Returns whether the result failed and narrows its data and error signals.
201 */
202 isError: SignalFunction<
203 (
204 this: DefinedCreateInfiniteQueryResult<TData, TError>,
205 ) => this is DefinedCreateInfiniteQueryResult<
206 TData,
207 TError,
208 CreateStatusBasedDefinedInfiniteQueryResult<'error', TData, TError>
209 >
210 >
211 /**
212 * Narrows the result to the pending state when that state is possible.
213 */
214 isPending: SignalFunction<
215 (this: DefinedCreateInfiniteQueryResult<TData, TError>) => this is never
216 >
218​
219/**
220 * Options accepted by `injectInfiniteQuery`, including page parameters and pagination callbacks.
221 */
222export interface CreateInfiniteQueryOptions<
223 TQueryFnData = unknown,
224 TError = DefaultError,
225 TData = TQueryFnData,
226 TQueryKey extends QueryKey = QueryKey,
227 TPageParam = unknown,
228> extends OmitKeyof<
229 InfiniteQueryObserverOptions<
230 TQueryFnData,
231 TError,
232 TData,
233 TQueryKey,
234 TPageParam
235 >,
236 'notifyOnChangeProps' | 'suspense' | 'throwOnError'
237> {}
238​
239/**
240 * The result of `injectQuery` when `initialData` isn't set β€” `data` may be `undefined` while the query is
241 * `pending`. Same shape as {@link QueryObserverResult} from `@tanstack/query-core`, but value fields (like
242 * `data`, `error`, `status`) are exposed as a `Signal` β€” read them with `query.data()`, not `query.data` β€”
243 * while function fields (like `refetch`) are called directly, unchanged. `isSuccess`/`isError`/`isPending`
244 * are {@link BaseQueryNarrowing} type-guard methods rather than plain booleans.
245 * `injectInfiniteQuery` returns {@link CreateInfiniteQueryResult} instead.
246 * @template TData - The type `data` ends up as after `select` runs.
247 * @template TError - The type of errors your `queryFn` may throw.
248 */
249export type CreateBaseQueryResult<
250 TData = unknown,
251 TError = DefaultError,
252 TState extends QueryObserverResult<TData, TError> = QueryObserverResult<
253 TData,
254 TError
255 >,
256> = BaseQueryNarrowing<TData, TError> &
257 MapToSignals<
258 OmitKeyof<TState, keyof BaseQueryNarrowing, 'safely'>,
259 QueryResultFields
260 >
261​
262/**
263 * The result of `injectQuery`. Same as {@link CreateBaseQueryResult}.
264 * @template TData - The type `data` ends up as after `select` runs.
265 * @template TError - The type of errors your `queryFn` may throw.
266 */
267export type CreateQueryResult<
268 TData = unknown,
269 TError = DefaultError,
270> = CreateBaseQueryResult<TData, TError>
271​
272/**
273 * The result of `injectQuery` when `initialData` is set β€” `data` is never `undefined`. Same shape as
274 * {@link DefinedQueryObserverResult} from `@tanstack/query-core`, but value fields are exposed as a
275 * `Signal` while function fields are called directly, unchanged.
276 * @template TData - The type `data` ends up as after `select` runs.
277 * @template TError - The type of errors your `queryFn` may throw.
278 */
279export type DefinedCreateQueryResult<
280 TData = unknown,
281 TError = DefaultError,
282 TState extends DefinedQueryObserverResult<TData, TError> =
283 DefinedQueryObserverResult<TData, TError>,
284> = DefinedQueryNarrowing<TData, TError> &
285 MapToSignals<
286 OmitKeyof<TState, keyof DefinedQueryNarrowing, 'safely'>,
287 QueryResultFields
288 >
289​
290/**
291 * The result of `injectInfiniteQuery` when `initialData` isn't set β€” `data` may be `undefined` while the
292 * query is `pending`. Same shape as {@link InfiniteQueryObserverResult} from `@tanstack/query-core`, but
293 * value fields are exposed as a `Signal` while function fields (like `fetchNextPage`) are called directly,
294 * unchanged.
295 * @template TData - The type `data` ends up as after `select` runs.
296 * @template TError - The type of errors your `queryFn` may throw.
297 */
298export type CreateInfiniteQueryResult<
299 TData = unknown,
300 TError = DefaultError,
301 TState extends InfiniteQueryObserverResult<TData, TError> =
302 InfiniteQueryObserverResult<TData, TError>,
303> = BaseInfiniteQueryNarrowing<TData, TError> &
304 MapToSignals<TState, InfiniteQueryResultFields>
305​
306/**
307 * The result of `injectInfiniteQuery` when `initialData` is set β€” `data` is never `undefined`. Same shape as
308 * {@link DefinedInfiniteQueryObserverResult} from `@tanstack/query-core`, but value fields are exposed as a
309 * `Signal` while function fields are called directly, unchanged.
310 * @template TData - The type `data` ends up as after `select` runs.
311 * @template TError - The type of errors your `queryFn` may throw.
312 */
313export type DefinedCreateInfiniteQueryResult<
314 TData = unknown,
315 TError = DefaultError,
316 TDefinedInfiniteQueryObserver extends DefinedInfiniteQueryObserverResult<
317 TData,
318 TError
319 > = DefinedInfiniteQueryObserverResult<TData, TError>,
320> = DefinedInfiniteQueryNarrowing<TData, TError> &
321 MapToSignals<TDefinedInfiniteQueryObserver, InfiniteQueryResultFields>
322​
323/**
324 * The options accepted by `injectMutation`. Same as {@link MutationObserverOptions} from
325 * `@tanstack/query-core`, minus the internal `_defaulted` flag.
326 * @template TData - The type your mutation function resolves to.
327 * @template TError - The type of errors your mutation function may throw.
328 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
329 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
330 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
331 */
332export interface CreateMutationOptions<
333 TData = unknown,
334 TError = DefaultError,
335 TVariables = void,
336 TOnMutateResult = unknown,
337> extends OmitKeyof<
338 MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
339 '_defaulted' | 'throwOnError'
340> {}
341​
342/**
343 * The type of `mutate`, as returned by `injectMutation`. Forwards the variables (and an optional per-call
344 * `onSuccess`/`onError`/`onSettled`) to the underlying `mutate` call. Fire-and-forget β€” errors are surfaced
345 * through the mutation result, not thrown.
346 * @template TData - The type your mutation function resolves to.
347 * @template TError - The type of errors your mutation function may throw.
348 * @template TVariables - The type of the variable passed to `mutate`.
349 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
350 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
351 */
352export type CreateMutateFunction<
353 TData = unknown,
354 TError = DefaultError,
355 TVariables = void,
356 TOnMutateResult = unknown,
357> = (
358 ...args: Parameters<
359 MutateFunction<TData, TError, TVariables, TOnMutateResult>
360 >
361) => void
362​
363/**
364 * The type of `mutateAsync`, as returned by `injectMutation`. Similar to {@link CreateMutateFunction}, but
365 * returns a promise which can be awaited.
366 * @template TData - The type your mutation function resolves to.
367 * @template TError - The type of errors your mutation function may throw.
368 * @template TVariables - The type of the variable passed to `mutateAsync`.
369 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
370 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
371 */
372export type CreateMutateAsyncFunction<
373 TData = unknown,
374 TError = DefaultError,
375 TVariables = void,
376 TOnMutateResult = unknown,
377> = MutateFunction<TData, TError, TVariables, TOnMutateResult>
378​
379/**
380 * The pre-`Signal` shape {@link CreateMutationResult} is built from β€” not what `injectMutation` actually
381 * returns. Same as {@link MutationObserverResult} from `@tanstack/query-core`, with `mutate` narrowed to the
382 * fire-and-forget {@link CreateMutateFunction} signature, plus the added `mutateAsync`.
383 * @template TData - The type your mutation function resolves to.
384 * @template TError - The type of errors your mutation function may throw.
385 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
386 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
387 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
388 */
389export type CreateBaseMutationResult<
390 TData = unknown,
391 TError = DefaultError,
392 TVariables = unknown,
393 TOnMutateResult = unknown,
394> = Override<
395 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
396 { mutate: CreateMutateFunction<TData, TError, TVariables, TOnMutateResult> }
397> & {
398 /**
399 * Similar to `mutate`, but returns a promise which can be awaited.
400 */
401 mutateAsync: CreateMutateAsyncFunction<
402 TData,
403 TError,
404 TVariables,
405 TOnMutateResult
406 >
408​
409type CreateStatusBasedMutationResult<
410 TStatus extends CreateBaseMutationResult['status'],
411 TData = unknown,
412 TError = DefaultError,
413 TVariables = unknown,
414 TOnMutateResult = unknown,
415> = Extract<
416 CreateBaseMutationResult<TData, TError, TVariables, TOnMutateResult>,
417 { status: TStatus }
419​
420/**
421 * Signal-backed guards that narrow mutation data, errors, and variables by the current status.
422 */
423export interface BaseMutationNarrowing<
424 TData = unknown,
425 TError = DefaultError,
426 TVariables = unknown,
427 TOnMutateResult = unknown,
428> {
429 /**
430 * Whether the mutation is in the `success` state. Calling it narrows the result to that state.
431 */
432 isSuccess: SignalFunction<
433 (
434 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
435 ) => this is CreateMutationResult<
436 TData,
437 TError,
438 TVariables,
439 TOnMutateResult,
440 CreateStatusBasedMutationResult<
441 'success',
442 TData,
443 TError,
444 TVariables,
445 TOnMutateResult
446 >
447 >
448 >
449 /**
450 * Whether the mutation is in the `error` state. Calling it narrows the result to that state.
451 */
452 isError: SignalFunction<
453 (
454 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
455 ) => this is CreateMutationResult<
456 TData,
457 TError,
458 TVariables,
459 TOnMutateResult,
460 CreateStatusBasedMutationResult<
461 'error',
462 TData,
463 TError,
464 TVariables,
465 TOnMutateResult
466 >
467 >
468 >
469 /**
470 * Whether the mutation is in the `pending` state. Calling it narrows the result to that state.
471 */
472 isPending: SignalFunction<
473 (
474 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
475 ) => this is CreateMutationResult<
476 TData,
477 TError,
478 TVariables,
479 TOnMutateResult,
480 CreateStatusBasedMutationResult<
481 'pending',
482 TData,
483 TError,
484 TVariables,
485 TOnMutateResult
486 >
487 >
488 >
489 /**
490 * Whether the mutation is in the `idle` state. Calling it narrows the result to that state.
491 */
492 isIdle: SignalFunction<
493 (
494 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
495 ) => this is CreateMutationResult<
496 TData,
497 TError,
498 TVariables,
499 TOnMutateResult,
500 CreateStatusBasedMutationResult<
501 'idle',
502 TData,
503 TError,
504 TVariables,
505 TOnMutateResult
506 >
507 >
508 >
510​
511/**
512 * The result of `injectMutation`. Based on {@link CreateBaseMutationResult}, but value fields are exposed as
513 * a `Signal` β€” read them with `mutation.data()`, not `mutation.data` β€” while function fields (`mutate`,
514 * `mutateAsync`, `reset`) are called directly, unchanged. `isSuccess`/`isError`/`isPending`/`isIdle` are
515 * {@link BaseMutationNarrowing} type-guard methods rather than plain booleans.
516 * @template TData - The type your mutation function resolves to.
517 * @template TError - The type of errors your mutation function may throw.
518 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
519 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
520 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
521 */
522export type CreateMutationResult<
523 TData = unknown,
524 TError = DefaultError,
525 TVariables = unknown,
526 TOnMutateResult = unknown,
527 TState = CreateStatusBasedMutationResult<
528 CreateBaseMutationResult['status'],
529 TData,
530 TError,
531 TVariables,
532 TOnMutateResult
533 >,
534> = BaseMutationNarrowing<TData, TError, TVariables, TOnMutateResult> &
535 MapToSignals<
536 OmitKeyof<TState, keyof BaseMutationNarrowing, 'safely'>,
537 MutationResultFields
538 >