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

types.ts

436 lines16.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 './signal-proxy'
20​
21/**
22 * The options shared across `angular-query-experimental`'s query functions. Extends
23 * {@link QueryObserverOptions} from `@tanstack/query-core` as-is β€” unlike `react-query`,
24 * `angular-query-experimental` has no extra framework-specific option here.
25 * @template TQueryFnData - The type your `queryFn` resolves to.
26 * @template TError - The type of errors your `queryFn` may throw.
27 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
28 * `select` is used.
29 * @template TQueryData - The type of the data actually held in the query cache β€” the input to `select` and
30 * `placeholderData`. Defaults to, and is usually the same as, `TQueryFnData`.
31 * @template TQueryKey - The type of your `queryKey`.
32 */
33export interface CreateBaseQueryOptions<
34 TQueryFnData = unknown,
35 TError = DefaultError,
36 TData = TQueryFnData,
37 TQueryData = TQueryFnData,
38 TQueryKey extends QueryKey = QueryKey,
39> extends QueryObserverOptions<
40 TQueryFnData,
41 TError,
42 TData,
43 TQueryData,
44 TQueryKey
45> {}
46​
47/**
48 * The options accepted by `injectQuery`. Same as {@link CreateBaseQueryOptions}, minus `suspense` β€” which
49 * `angular-query-experimental` doesn't support, unlike `react-query`.
50 * @template TQueryFnData - The type your `queryFn` resolves to.
51 * @template TError - The type of errors your `queryFn` may throw.
52 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
53 * `select` is used.
54 * @template TQueryKey - The type of your `queryKey`.
55 */
56export interface CreateQueryOptions<
57 TQueryFnData = unknown,
58 TError = DefaultError,
59 TData = TQueryFnData,
60 TQueryKey extends QueryKey = QueryKey,
61> extends OmitKeyof<
62 CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
63 'suspense'
64> {}
65​
66type CreateStatusBasedQueryResult<
67 TStatus extends QueryObserverResult['status'],
68 TData = unknown,
69 TError = DefaultError,
70> = Extract<QueryObserverResult<TData, TError>, { status: TStatus }>
71​
72/**
73 * The `isSuccess`/`isError`/`isPending` methods on a query result. Unlike `react-query`'s derived booleans,
74 * these are type-guard methods you call β€” `if (query.isSuccess())` β€” so that `query.data` narrows away
75 * `undefined` inside the branch, the same way `status` narrowing works on the plain object `react-query`
76 * returns.
77 * @template TData - The type `data` ends up as after `select` runs.
78 * @template TError - The type of errors your `queryFn` may throw.
79 */
80export interface BaseQueryNarrowing<TData = unknown, TError = DefaultError> {
81 /**
82 * Returns `true` if the query is in the `success` state, narrowing the result to that state.
83 */
84 isSuccess: (
85 this: CreateBaseQueryResult<TData, TError>,
86 ) => this is CreateBaseQueryResult<
87 TData,
88 TError,
89 CreateStatusBasedQueryResult<'success', TData, TError>
90 >
91 /**
92 * Returns `true` if the query is in the `error` state, narrowing the result to that state.
93 */
94 isError: (
95 this: CreateBaseQueryResult<TData, TError>,
96 ) => this is CreateBaseQueryResult<
97 TData,
98 TError,
99 CreateStatusBasedQueryResult<'error', TData, TError>
100 >
101 /**
102 * Returns `true` if the query is in the `pending` state, narrowing the result to that state.
103 */
104 isPending: (
105 this: CreateBaseQueryResult<TData, TError>,
106 ) => this is CreateBaseQueryResult<
107 TData,
108 TError,
109 CreateStatusBasedQueryResult<'pending', TData, TError>
110 >
112​
113/**
114 * The options accepted by `injectInfiniteQuery`. Same as {@link CreateBaseQueryOptions}, minus `suspense` β€”
115 * which `angular-query-experimental` doesn't support, unlike `react-query` β€” extends
116 * {@link InfiniteQueryObserverOptions} from `@tanstack/query-core` for the infinite-query-specific options
117 * (`getNextPageParam`, `initialPageParam`, etc.).
118 * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
119 * @template TError - The type of errors your `queryFn` may throw.
120 * @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` here, though
121 * `injectInfiniteQuery` itself defaults it to `InfiniteData<TQueryFnData>` β€” the shape `data` actually has
122 * when no `select` is used.
123 * @template TQueryKey - The type of your `queryKey`.
124 * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
125 */
126export interface CreateInfiniteQueryOptions<
127 TQueryFnData = unknown,
128 TError = DefaultError,
129 TData = TQueryFnData,
130 TQueryKey extends QueryKey = QueryKey,
131 TPageParam = unknown,
132> extends OmitKeyof<
133 InfiniteQueryObserverOptions<
134 TQueryFnData,
135 TError,
136 TData,
137 TQueryKey,
138 TPageParam
139 >,
140 'suspense'
141> {}
142​
143/**
144 * The result of `injectQuery` when `initialData` isn't set β€” `data` may be `undefined` while the query is
145 * `pending`. Same shape as {@link QueryObserverResult} from `@tanstack/query-core`, but value fields (like
146 * `data`, `error`, `status`) are exposed as a `Signal` β€” read them with `query.data()`, not `query.data` β€”
147 * while function fields (like `refetch`) are called directly, unchanged. `isSuccess`/`isError`/`isPending`
148 * are {@link BaseQueryNarrowing} type-guard methods rather than plain booleans.
149 * `injectInfiniteQuery` returns {@link CreateInfiniteQueryResult} instead.
150 * @template TData - The type `data` ends up as after `select` runs.
151 * @template TError - The type of errors your `queryFn` may throw.
152 */
153export type CreateBaseQueryResult<
154 TData = unknown,
155 TError = DefaultError,
156 TState = QueryObserverResult<TData, TError>,
157> = BaseQueryNarrowing<TData, TError> &
158 MapToSignals<OmitKeyof<TState, keyof BaseQueryNarrowing, 'safely'>>
159​
160/**
161 * The result of `injectQuery`. Same as {@link CreateBaseQueryResult}.
162 * @template TData - The type `data` ends up as after `select` runs.
163 * @template TError - The type of errors your `queryFn` may throw.
164 */
165export type CreateQueryResult<
166 TData = unknown,
167 TError = DefaultError,
168> = CreateBaseQueryResult<TData, TError>
169​
170/**
171 * The result of `injectQuery` when `initialData` is set β€” `data` is never `undefined`. Same shape as
172 * {@link DefinedQueryObserverResult} from `@tanstack/query-core`, but value fields are exposed as a
173 * `Signal` while function fields are called directly, unchanged.
174 * @template TData - The type `data` ends up as after `select` runs.
175 * @template TError - The type of errors your `queryFn` may throw.
176 */
177export type DefinedCreateQueryResult<
178 TData = unknown,
179 TError = DefaultError,
180 TState = DefinedQueryObserverResult<TData, TError>,
181> = BaseQueryNarrowing<TData, TError> &
182 MapToSignals<OmitKeyof<TState, keyof BaseQueryNarrowing, 'safely'>>
183​
184/**
185 * The result of `injectInfiniteQuery` when `initialData` isn't set β€” `data` may be `undefined` while the
186 * query is `pending`. Same shape as {@link InfiniteQueryObserverResult} from `@tanstack/query-core`, but
187 * value fields are exposed as a `Signal` while function fields (like `fetchNextPage`) are called directly,
188 * unchanged.
189 * @template TData - The type `data` ends up as after `select` runs.
190 * @template TError - The type of errors your `queryFn` may throw.
191 */
192export type CreateInfiniteQueryResult<
193 TData = unknown,
194 TError = DefaultError,
195> = BaseQueryNarrowing<TData, TError> &
196 MapToSignals<InfiniteQueryObserverResult<TData, TError>>
197​
198/**
199 * The result of `injectInfiniteQuery` when `initialData` is set β€” `data` is never `undefined`. Same shape as
200 * {@link DefinedInfiniteQueryObserverResult} from `@tanstack/query-core`, but value fields are exposed as a
201 * `Signal` while function fields are called directly, unchanged.
202 * @template TData - The type `data` ends up as after `select` runs.
203 * @template TError - The type of errors your `queryFn` may throw.
204 */
205export type DefinedCreateInfiniteQueryResult<
206 TData = unknown,
207 TError = DefaultError,
208 TDefinedInfiniteQueryObserver = DefinedInfiniteQueryObserverResult<
209 TData,
210 TError
211 >,
212> = MapToSignals<TDefinedInfiniteQueryObserver>
213​
214/**
215 * The options accepted by `injectMutation`. Same as {@link MutationObserverOptions} from
216 * `@tanstack/query-core`, minus the internal `_defaulted` flag.
217 * @template TData - The type your mutation function resolves to.
218 * @template TError - The type of errors your mutation function may throw.
219 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
220 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
221 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
222 */
223export interface CreateMutationOptions<
224 TData = unknown,
225 TError = DefaultError,
226 TVariables = void,
227 TOnMutateResult = unknown,
228> extends OmitKeyof<
229 MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
230 '_defaulted'
231> {}
232​
233/**
234 * The type of `mutate`, as returned by `injectMutation`. Forwards the variables (and an optional per-call
235 * `onSuccess`/`onError`/`onSettled`) to the underlying `mutate` call. Fire-and-forget β€” errors are surfaced
236 * through the mutation result instead of being thrown by `mutate`, unless `throwOnError` makes
237 * `injectMutation` rethrow them after emitting them on `ngZone.onError`.
238 * @template TData - The type your mutation function resolves to.
239 * @template TError - The type of errors your mutation function may throw.
240 * @template TVariables - The type of the variable passed to `mutate`.
241 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
242 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
243 */
244export type CreateMutateFunction<
245 TData = unknown,
246 TError = DefaultError,
247 TVariables = void,
248 TOnMutateResult = unknown,
249> = (
250 ...args: Parameters<
251 MutateFunction<TData, TError, TVariables, TOnMutateResult>
252 >
253) => void
254​
255/**
256 * The type of `mutateAsync`, as returned by `injectMutation`. Similar to {@link CreateMutateFunction}, but
257 * returns a promise which can be awaited.
258 * @template TData - The type your mutation function resolves to.
259 * @template TError - The type of errors your mutation function may throw.
260 * @template TVariables - The type of the variable passed to `mutateAsync`.
261 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
262 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
263 */
264export type CreateMutateAsyncFunction<
265 TData = unknown,
266 TError = DefaultError,
267 TVariables = void,
268 TOnMutateResult = unknown,
269> = MutateFunction<TData, TError, TVariables, TOnMutateResult>
270​
271/**
272 * The pre-`Signal` shape {@link CreateMutationResult} is built from β€” not what `injectMutation` actually
273 * returns. Same as {@link MutationObserverResult} from `@tanstack/query-core`, with `mutate` narrowed to the
274 * fire-and-forget {@link CreateMutateFunction} signature, plus the added `mutateAsync`.
275 * @template TData - The type your mutation function resolves to.
276 * @template TError - The type of errors your mutation function may throw.
277 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
278 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
279 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
280 */
281export type CreateBaseMutationResult<
282 TData = unknown,
283 TError = DefaultError,
284 TVariables = unknown,
285 TOnMutateResult = unknown,
286> = Override<
287 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
288 { mutate: CreateMutateFunction<TData, TError, TVariables, TOnMutateResult> }
289> & {
290 /**
291 * Similar to `mutate`, but returns a promise which can be awaited.
292 */
293 mutateAsync: CreateMutateAsyncFunction<
294 TData,
295 TError,
296 TVariables,
297 TOnMutateResult
298 >
300​
301type CreateStatusBasedMutationResult<
302 TStatus extends CreateBaseMutationResult['status'],
303 TData = unknown,
304 TError = DefaultError,
305 TVariables = unknown,
306 TOnMutateResult = unknown,
307> = Extract<
308 CreateBaseMutationResult<TData, TError, TVariables, TOnMutateResult>,
309 { status: TStatus }
311​
312type SignalFunction<T extends () => any> = T & Signal<ReturnType<T>>
313​
314/**
315 * The `isSuccess`/`isError`/`isPending`/`isIdle` methods on a mutation result. Each is both a `Signal`
316 * (its current boolean value is read reactively without calling it) and a type-guard function you can
317 * call β€” `if (mutation.isSuccess())` β€” so that `mutation.data` narrows away `undefined` inside the branch.
318 * @template TData - The type your mutation function resolves to.
319 * @template TError - The type of errors your mutation function may throw.
320 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
321 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
322 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
323 */
324export interface BaseMutationNarrowing<
325 TData = unknown,
326 TError = DefaultError,
327 TVariables = unknown,
328 TOnMutateResult = unknown,
329> {
330 /**
331 * Whether the mutation is in the `success` state. Calling it narrows the result to that state.
332 */
333 isSuccess: SignalFunction<
334 (
335 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
336 ) => this is CreateMutationResult<
337 TData,
338 TError,
339 TVariables,
340 TOnMutateResult,
341 CreateStatusBasedMutationResult<
342 'success',
343 TData,
344 TError,
345 TVariables,
346 TOnMutateResult
347 >
348 >
349 >
350 /**
351 * Whether the mutation is in the `error` state. Calling it narrows the result to that state.
352 */
353 isError: SignalFunction<
354 (
355 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
356 ) => this is CreateMutationResult<
357 TData,
358 TError,
359 TVariables,
360 TOnMutateResult,
361 CreateStatusBasedMutationResult<
362 'error',
363 TData,
364 TError,
365 TVariables,
366 TOnMutateResult
367 >
368 >
369 >
370 /**
371 * Whether the mutation is in the `pending` state. Calling it narrows the result to that state.
372 */
373 isPending: SignalFunction<
374 (
375 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
376 ) => this is CreateMutationResult<
377 TData,
378 TError,
379 TVariables,
380 TOnMutateResult,
381 CreateStatusBasedMutationResult<
382 'pending',
383 TData,
384 TError,
385 TVariables,
386 TOnMutateResult
387 >
388 >
389 >
390 /**
391 * Whether the mutation is in the `idle` state. Calling it narrows the result to that state.
392 */
393 isIdle: SignalFunction<
394 (
395 this: CreateMutationResult<TData, TError, TVariables, TOnMutateResult>,
396 ) => this is CreateMutationResult<
397 TData,
398 TError,
399 TVariables,
400 TOnMutateResult,
401 CreateStatusBasedMutationResult<
402 'idle',
403 TData,
404 TError,
405 TVariables,
406 TOnMutateResult
407 >
408 >
409 >
411​
412/**
413 * The result of `injectMutation`. Based on {@link CreateBaseMutationResult}, but value fields are exposed as
414 * a `Signal` β€” read them with `mutation.data()`, not `mutation.data` β€” while function fields (`mutate`,
415 * `mutateAsync`, `reset`) are called directly, unchanged. `isSuccess`/`isError`/`isPending`/`isIdle` are
416 * {@link BaseMutationNarrowing} type-guard methods rather than plain booleans.
417 * @template TData - The type your mutation function resolves to.
418 * @template TError - The type of errors your mutation function may throw.
419 * @template TVariables - The type of the variable passed to `mutate`/`mutateAsync`.
420 * @template TOnMutateResult - The type returned by `onMutate`, passed to `onSuccess`/`onError`/`onSettled` as
421 * their `onMutateResult` parameter β€” useful for optimistic-update rollback data.
422 */
423export type CreateMutationResult<
424 TData = unknown,
425 TError = DefaultError,
426 TVariables = unknown,
427 TOnMutateResult = unknown,
428 TState = CreateStatusBasedMutationResult<
429 CreateBaseMutationResult['status'],
430 TData,
431 TError,
432 TVariables,
433 TOnMutateResult
434 >,
435> = BaseMutationNarrowing<TData, TError, TVariables, TOnMutateResult> &
436 MapToSignals<OmitKeyof<TState, keyof BaseMutationNarrowing, 'safely'>>