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