@tanstack/angular-query-experimental 5.34.2 → 5.35.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/build/rollup.d.ts +591 -0
  2. package/package.json +10 -6
  3. package/build/create-base-query.d.ts +0 -6
  4. package/build/index.d.ts +0 -14
  5. package/build/infinite-query-options.d.ts +0 -34
  6. package/build/inject-infinite-query.d.ts +0 -31
  7. package/build/inject-is-fetching.d.ts +0 -13
  8. package/build/inject-is-mutating.d.ts +0 -12
  9. package/build/inject-mutation-state.d.ts +0 -21
  10. package/build/inject-mutation.d.ts +0 -13
  11. package/build/inject-queries.d.ts +0 -76
  12. package/build/inject-query-client.d.ts +0 -30
  13. package/build/inject-query.d.ts +0 -106
  14. package/build/providers.d.ts +0 -42
  15. package/build/query-options.d.ts +0 -66
  16. package/build/signal-proxy.d.ts +0 -11
  17. package/build/types.d.ts +0 -95
  18. package/build/util/assert-injector/assert-injector.d.ts +0 -54
  19. package/build/util/create-injection-token/create-injection-token.d.ts +0 -52
  20. package/build/util/index.d.ts +0 -2
  21. package/build/util/lazy-init/lazy-init.d.ts +0 -1
  22. package/build/util/lazy-signal-initializer/lazy-signal-initializer.d.ts +0 -4
  23. package/src/__tests__/inject-infinite-query.test.ts +0 -64
  24. package/src/__tests__/inject-is-fetching.test.ts +0 -35
  25. package/src/__tests__/inject-is-mutating.test.ts +0 -39
  26. package/src/__tests__/inject-mutation-state.test-d.ts +0 -22
  27. package/src/__tests__/inject-mutation-state.test.ts +0 -175
  28. package/src/__tests__/inject-mutation.test-d.ts +0 -71
  29. package/src/__tests__/inject-mutation.test.ts +0 -458
  30. package/src/__tests__/inject-query.test-d.ts +0 -59
  31. package/src/__tests__/inject-query.test.ts +0 -349
  32. package/src/__tests__/query-options.test-d.ts +0 -127
  33. package/src/__tests__/signal-proxy.test.ts +0 -27
  34. package/src/__tests__/test-utils.ts +0 -131
  35. package/src/__tests__/util/lazy-init/lazy-init.test.ts +0 -126
  36. package/src/__tests__/util/lazy-signal-initializer/lazy-signal-initializer.test.ts +0 -130
  37. package/src/create-base-query.ts +0 -116
  38. package/src/index.ts +0 -24
  39. package/src/infinite-query-options.ts +0 -118
  40. package/src/inject-infinite-query.ts +0 -125
  41. package/src/inject-is-fetching.ts +0 -49
  42. package/src/inject-is-mutating.ts +0 -48
  43. package/src/inject-mutation-state.ts +0 -107
  44. package/src/inject-mutation.ts +0 -118
  45. package/src/inject-queries.ts +0 -265
  46. package/src/inject-query-client.ts +0 -25
  47. package/src/inject-query.ts +0 -200
  48. package/src/providers.ts +0 -65
  49. package/src/query-options.ts +0 -122
  50. package/src/signal-proxy.ts +0 -46
  51. package/src/test-setup.ts +0 -12
  52. package/src/types.ts +0 -311
  53. package/src/util/assert-injector/assert-injector.test.ts +0 -74
  54. package/src/util/assert-injector/assert-injector.ts +0 -81
  55. package/src/util/create-injection-token/create-injection-token.test.ts +0 -32
  56. package/src/util/create-injection-token/create-injection-token.ts +0 -185
  57. package/src/util/index.ts +0 -13
  58. package/src/util/lazy-init/lazy-init.ts +0 -34
  59. package/src/util/lazy-signal-initializer/lazy-signal-initializer.ts +0 -28
@@ -1,48 +0,0 @@
1
- import { DestroyRef, NgZone, inject, signal } from '@angular/core'
2
- import { type MutationFilters, notifyManager } from '@tanstack/query-core'
3
- import { assertInjector } from './util/assert-injector/assert-injector'
4
- import { injectQueryClient } from './inject-query-client'
5
- import type { Injector, Signal } from '@angular/core'
6
-
7
- /**
8
- * Injects a signal that tracks the number of mutations that your application is fetching.
9
- *
10
- * Can be used for app-wide loading indicators
11
- * @param filters - The filters to apply to the query.
12
- * @param injector - The Angular injector to use.
13
- * @returns signal with number of fetching mutations.
14
- * @public
15
- */
16
- export function injectIsMutating(
17
- filters?: MutationFilters,
18
- injector?: Injector,
19
- ): Signal<number> {
20
- return assertInjector(injectIsMutating, injector, () => {
21
- const queryClient = injectQueryClient()
22
- const destroyRef = inject(DestroyRef)
23
- const ngZone = inject(NgZone)
24
-
25
- const cache = queryClient.getMutationCache()
26
- // isMutating is the prev value initialized on mount *
27
- let isMutating = queryClient.isMutating(filters)
28
-
29
- const result = signal(isMutating)
30
-
31
- const unsubscribe = cache.subscribe(
32
- notifyManager.batchCalls(() => {
33
- const newIsMutating = queryClient.isMutating(filters)
34
- if (isMutating !== newIsMutating) {
35
- // * and update with each change
36
- isMutating = newIsMutating
37
- ngZone.run(() => {
38
- result.set(isMutating)
39
- })
40
- }
41
- }),
42
- )
43
-
44
- destroyRef.onDestroy(unsubscribe)
45
-
46
- return result
47
- })
48
- }
@@ -1,107 +0,0 @@
1
- import {
2
- DestroyRef,
3
- NgZone,
4
- effect,
5
- inject,
6
- signal,
7
- untracked,
8
- } from '@angular/core'
9
- import {
10
- type DefaultError,
11
- type Mutation,
12
- type MutationCache,
13
- type MutationFilters,
14
- type MutationState,
15
- notifyManager,
16
- replaceEqualDeep,
17
- } from '@tanstack/query-core'
18
- import { assertInjector } from './util/assert-injector/assert-injector'
19
- import { injectQueryClient } from './inject-query-client'
20
- import { lazySignalInitializer } from './util/lazy-signal-initializer/lazy-signal-initializer'
21
- import type { Injector, Signal } from '@angular/core'
22
-
23
- type MutationStateOptions<TResult = MutationState> = {
24
- filters?: MutationFilters
25
- select?: (
26
- mutation: Mutation<unknown, DefaultError, unknown, unknown>,
27
- ) => TResult
28
- }
29
-
30
- function getResult<TResult = MutationState>(
31
- mutationCache: MutationCache,
32
- options: MutationStateOptions<TResult>,
33
- ): Array<TResult> {
34
- return mutationCache
35
- .findAll(options.filters)
36
- .map(
37
- (mutation): TResult =>
38
- (options.select
39
- ? options.select(
40
- mutation as Mutation<unknown, DefaultError, unknown, unknown>,
41
- )
42
- : mutation.state) as TResult,
43
- )
44
- }
45
-
46
- /**
47
- * @public
48
- */
49
- export interface InjectMutationStateOptions {
50
- injector?: Injector
51
- }
52
-
53
- /**
54
- * Injects a signal that tracks the state of all mutations.
55
- * @param mutationStateOptionsFn - A function that returns mutation state options.
56
- * @param options - The Angular injector to use.
57
- * @returns The signal that tracks the state of all mutations.
58
- * @public
59
- */
60
- export function injectMutationState<TResult = MutationState>(
61
- mutationStateOptionsFn: () => MutationStateOptions<TResult> = () => ({}),
62
- options?: InjectMutationStateOptions,
63
- ): Signal<Array<TResult>> {
64
- return assertInjector(injectMutationState, options?.injector, () => {
65
- const destroyRef = inject(DestroyRef)
66
- const queryClient = injectQueryClient()
67
- const ngZone = inject(NgZone)
68
-
69
- const mutationCache = queryClient.getMutationCache()
70
-
71
- return lazySignalInitializer((injector) => {
72
- const result = signal<Array<TResult>>(
73
- getResult(mutationCache, mutationStateOptionsFn()),
74
- )
75
-
76
- effect(
77
- () => {
78
- const mutationStateOptions = mutationStateOptionsFn()
79
- untracked(() => {
80
- // Setting the signal from an effect because it's both 'computed' from options()
81
- // and needs to be set imperatively in the mutationCache listener.
82
- result.set(getResult(mutationCache, mutationStateOptions))
83
- })
84
- },
85
- { injector },
86
- )
87
-
88
- const unsubscribe = mutationCache.subscribe(
89
- notifyManager.batchCalls(() => {
90
- const nextResult = replaceEqualDeep(
91
- result(),
92
- getResult(mutationCache, mutationStateOptionsFn()),
93
- )
94
- if (result() !== nextResult) {
95
- ngZone.run(() => {
96
- result.set(nextResult)
97
- })
98
- }
99
- }),
100
- )
101
-
102
- destroyRef.onDestroy(unsubscribe)
103
-
104
- return result
105
- })
106
- })
107
- }
@@ -1,118 +0,0 @@
1
- import {
2
- DestroyRef,
3
- Injector,
4
- NgZone,
5
- computed,
6
- effect,
7
- inject,
8
- runInInjectionContext,
9
- signal,
10
- } from '@angular/core'
11
- import { MutationObserver, notifyManager } from '@tanstack/query-core'
12
- import { assertInjector } from './util/assert-injector/assert-injector'
13
- import { signalProxy } from './signal-proxy'
14
- import { injectQueryClient } from './inject-query-client'
15
- import { noop, shouldThrowError } from './util'
16
-
17
- import { lazyInit } from './util/lazy-init/lazy-init'
18
- import type {
19
- DefaultError,
20
- MutationObserverResult,
21
- QueryClient,
22
- } from '@tanstack/query-core'
23
- import type {
24
- CreateMutateFunction,
25
- CreateMutationOptions,
26
- CreateMutationResult,
27
- } from './types'
28
-
29
- /**
30
- * Injects a mutation: an imperative function that can be invoked which typically performs server side effects.
31
- *
32
- * Unlike queries, mutations are not run automatically.
33
- * @param optionsFn - A function that returns mutation options.
34
- * @param injector - The Angular injector to use.
35
- * @returns The mutation.
36
- * @public
37
- */
38
- export function injectMutation<
39
- TData = unknown,
40
- TError = DefaultError,
41
- TVariables = void,
42
- TContext = unknown,
43
- >(
44
- optionsFn: (
45
- client: QueryClient,
46
- ) => CreateMutationOptions<TData, TError, TVariables, TContext>,
47
- injector?: Injector,
48
- ): CreateMutationResult<TData, TError, TVariables, TContext> {
49
- return assertInjector(injectMutation, injector, () => {
50
- const queryClient = injectQueryClient()
51
- const currentInjector = inject(Injector)
52
- const destroyRef = inject(DestroyRef)
53
- const ngZone = inject(NgZone)
54
-
55
- return lazyInit(() =>
56
- runInInjectionContext(currentInjector, () => {
57
- const observer = new MutationObserver<
58
- TData,
59
- TError,
60
- TVariables,
61
- TContext
62
- >(queryClient, optionsFn(queryClient))
63
- const mutate: CreateMutateFunction<
64
- TData,
65
- TError,
66
- TVariables,
67
- TContext
68
- > = (variables, mutateOptions) => {
69
- observer.mutate(variables, mutateOptions).catch(noop)
70
- }
71
-
72
- effect(() => {
73
- observer.setOptions(optionsFn(queryClient))
74
- })
75
-
76
- const result = signal(observer.getCurrentResult())
77
-
78
- const unsubscribe = observer.subscribe(
79
- notifyManager.batchCalls(
80
- (
81
- state: MutationObserverResult<
82
- TData,
83
- TError,
84
- TVariables,
85
- TContext
86
- >,
87
- ) => {
88
- ngZone.run(() => {
89
- if (
90
- state.isError &&
91
- shouldThrowError(observer.options.throwOnError, [state.error])
92
- ) {
93
- throw state.error
94
- }
95
- result.set(state)
96
- })
97
- },
98
- ),
99
- )
100
-
101
- destroyRef.onDestroy(unsubscribe)
102
-
103
- const resultSignal = computed(() => ({
104
- ...result(),
105
- mutate,
106
- mutateAsync: result().mutate,
107
- }))
108
-
109
- return signalProxy(resultSignal) as unknown as CreateMutationResult<
110
- TData,
111
- TError,
112
- TVariables,
113
- TContext
114
- >
115
- }),
116
- )
117
- })
118
- }
@@ -1,265 +0,0 @@
1
- import { QueriesObserver, notifyManager } from '@tanstack/query-core'
2
- import { DestroyRef, computed, effect, inject, signal } from '@angular/core'
3
- import { assertInjector } from './util/assert-injector/assert-injector'
4
- import { injectQueryClient } from './inject-query-client'
5
- import type { Injector, Signal } from '@angular/core'
6
- import type {
7
- DefaultError,
8
- OmitKeyof,
9
- QueriesObserverOptions,
10
- QueriesPlaceholderDataFunction,
11
- QueryFunction,
12
- QueryKey,
13
- QueryObserverOptions,
14
- QueryObserverResult,
15
- SkipToken,
16
- ThrowOnError,
17
- } from '@tanstack/query-core'
18
-
19
- // This defines the `CreateQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
20
- // `placeholderData` function does not have a parameter
21
- type QueryObserverOptionsForCreateQueries<
22
- TQueryFnData = unknown,
23
- TError = DefaultError,
24
- TData = TQueryFnData,
25
- TQueryKey extends QueryKey = QueryKey,
26
- > = OmitKeyof<
27
- QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
28
- 'placeholderData'
29
- > & {
30
- placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>
31
- }
32
-
33
- // Avoid TS depth-limit error in case of large array literal
34
- type MAXIMUM_DEPTH = 20
35
-
36
- type GetOptions<T> =
37
- // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }
38
- T extends {
39
- queryFnData: infer TQueryFnData
40
- error?: infer TError
41
- data: infer TData
42
- }
43
- ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
44
- : T extends { queryFnData: infer TQueryFnData; error?: infer TError }
45
- ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
46
- : T extends { data: infer TData; error?: infer TError }
47
- ? QueryObserverOptionsForCreateQueries<unknown, TError, TData>
48
- : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]
49
- T extends [infer TQueryFnData, infer TError, infer TData]
50
- ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
51
- : T extends [infer TQueryFnData, infer TError]
52
- ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
53
- : T extends [infer TQueryFnData]
54
- ? QueryObserverOptionsForCreateQueries<TQueryFnData>
55
- : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided
56
- T extends {
57
- queryFn?:
58
- | QueryFunction<infer TQueryFnData, infer TQueryKey>
59
- | SkipToken
60
- select: (data: any) => infer TData
61
- throwOnError?: ThrowOnError<any, infer TError, any, any>
62
- }
63
- ? QueryObserverOptionsForCreateQueries<
64
- TQueryFnData,
65
- TError,
66
- TData,
67
- TQueryKey
68
- >
69
- : T extends {
70
- queryFn?:
71
- | QueryFunction<infer TQueryFnData, infer TQueryKey>
72
- | SkipToken
73
- throwOnError?: ThrowOnError<any, infer TError, any, any>
74
- }
75
- ? QueryObserverOptionsForCreateQueries<
76
- TQueryFnData,
77
- TError,
78
- TQueryFnData,
79
- TQueryKey
80
- >
81
- : // Fallback
82
- QueryObserverOptionsForCreateQueries
83
-
84
- type GetResults<T> =
85
- // Part 1: responsible for mapping explicit type parameter to function result, if object
86
- T extends { queryFnData: any; error?: infer TError; data: infer TData }
87
- ? QueryObserverResult<TData, TError>
88
- : T extends { queryFnData: infer TQueryFnData; error?: infer TError }
89
- ? QueryObserverResult<TQueryFnData, TError>
90
- : T extends { data: infer TData; error?: infer TError }
91
- ? QueryObserverResult<TData, TError>
92
- : // Part 2: responsible for mapping explicit type parameter to function result, if tuple
93
- T extends [any, infer TError, infer TData]
94
- ? QueryObserverResult<TData, TError>
95
- : T extends [infer TQueryFnData, infer TError]
96
- ? QueryObserverResult<TQueryFnData, TError>
97
- : T extends [infer TQueryFnData]
98
- ? QueryObserverResult<TQueryFnData>
99
- : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided
100
- T extends {
101
- queryFn?: QueryFunction<unknown, any>
102
- select: (data: any) => infer TData
103
- throwOnError?: ThrowOnError<any, infer TError, any, any>
104
- }
105
- ? QueryObserverResult<
106
- TData,
107
- unknown extends TError ? DefaultError : TError
108
- >
109
- : T extends {
110
- queryFn?: QueryFunction<infer TQueryFnData, any>
111
- throwOnError?: ThrowOnError<any, infer TError, any, any>
112
- }
113
- ? QueryObserverResult<
114
- TQueryFnData,
115
- unknown extends TError ? DefaultError : TError
116
- >
117
- : // Fallback
118
- QueryObserverResult
119
-
120
- /**
121
- * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
122
- * @public
123
- */
124
- export type QueriesOptions<
125
- T extends Array<any>,
126
- TResult extends Array<any> = [],
127
- TDepth extends ReadonlyArray<number> = [],
128
- > = TDepth['length'] extends MAXIMUM_DEPTH
129
- ? Array<QueryObserverOptionsForCreateQueries>
130
- : T extends []
131
- ? []
132
- : T extends [infer Head]
133
- ? [...TResult, GetOptions<Head>]
134
- : T extends [infer Head, ...infer Tail]
135
- ? QueriesOptions<
136
- [...Tail],
137
- [...TResult, GetOptions<Head>],
138
- [...TDepth, 1]
139
- >
140
- : ReadonlyArray<unknown> extends T
141
- ? T
142
- : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!
143
- // use this to infer the param types in the case of Array.map() argument
144
- T extends Array<
145
- QueryObserverOptionsForCreateQueries<
146
- infer TQueryFnData,
147
- infer TError,
148
- infer TData,
149
- infer TQueryKey
150
- >
151
- >
152
- ? Array<
153
- QueryObserverOptionsForCreateQueries<
154
- TQueryFnData,
155
- TError,
156
- TData,
157
- TQueryKey
158
- >
159
- >
160
- : // Fallback
161
- Array<QueryObserverOptionsForCreateQueries>
162
-
163
- /**
164
- * QueriesResults reducer recursively maps type param to results
165
- * @public
166
- */
167
- export type QueriesResults<
168
- T extends Array<any>,
169
- TResult extends Array<any> = [],
170
- TDepth extends ReadonlyArray<number> = [],
171
- > = TDepth['length'] extends MAXIMUM_DEPTH
172
- ? Array<QueryObserverResult>
173
- : T extends []
174
- ? []
175
- : T extends [infer Head]
176
- ? [...TResult, GetResults<Head>]
177
- : T extends [infer Head, ...infer Tail]
178
- ? QueriesResults<
179
- [...Tail],
180
- [...TResult, GetResults<Head>],
181
- [...TDepth, 1]
182
- >
183
- : T extends Array<
184
- QueryObserverOptionsForCreateQueries<
185
- infer TQueryFnData,
186
- infer TError,
187
- infer TData,
188
- any
189
- >
190
- >
191
- ? // Dynamic-size (homogenous) CreateQueryOptions array: map directly to array of results
192
- Array<
193
- QueryObserverResult<
194
- unknown extends TData ? TQueryFnData : TData,
195
- unknown extends TError ? DefaultError : TError
196
- >
197
- >
198
- : // Fallback
199
- Array<QueryObserverResult>
200
-
201
- /**
202
- * @public
203
- */
204
- export function injectQueries<
205
- T extends Array<any>,
206
- TCombinedResult = QueriesResults<T>,
207
- >(
208
- {
209
- queries,
210
- ...options
211
- }: {
212
- queries: Signal<[...QueriesOptions<T>]>
213
- combine?: (result: QueriesResults<T>) => TCombinedResult
214
- },
215
- injector?: Injector,
216
- ): Signal<TCombinedResult> {
217
- return assertInjector(injectQueries, injector, () => {
218
- const queryClient = injectQueryClient()
219
- const destroyRef = inject(DestroyRef)
220
-
221
- const defaultedQueries = computed(() => {
222
- return queries().map((opts) => {
223
- const defaultedOptions = queryClient.defaultQueryOptions(opts)
224
- // Make sure the results are already in fetching state before subscribing or updating options
225
- defaultedOptions._optimisticResults = 'optimistic'
226
-
227
- return defaultedOptions as QueryObserverOptions<
228
- unknown,
229
- Error,
230
- unknown,
231
- unknown,
232
- QueryKey
233
- >
234
- })
235
- })
236
-
237
- const observer = new QueriesObserver<TCombinedResult>(
238
- queryClient,
239
- defaultedQueries(),
240
- options as QueriesObserverOptions<TCombinedResult>,
241
- )
242
-
243
- // Do not notify on updates because of changes in the options because
244
- // these changes should already be reflected in the optimistic result.
245
- effect(() => {
246
- observer.setQueries(
247
- defaultedQueries(),
248
- options as QueriesObserverOptions<TCombinedResult>,
249
- { listeners: false },
250
- )
251
- })
252
-
253
- const [, getCombinedResult] = observer.getOptimisticResult(
254
- defaultedQueries(),
255
- (options as QueriesObserverOptions<TCombinedResult>).combine,
256
- )
257
-
258
- const result = signal(getCombinedResult() as any)
259
-
260
- const unsubscribe = observer.subscribe(notifyManager.batchCalls(result.set))
261
- destroyRef.onDestroy(unsubscribe)
262
-
263
- return result
264
- })
265
- }
@@ -1,25 +0,0 @@
1
- import { createNoopInjectionToken } from './util/create-injection-token/create-injection-token'
2
- import type { QueryClient } from '@tanstack/query-core'
3
-
4
- const tokens = createNoopInjectionToken<QueryClient>('QueryClientToken')
5
-
6
- /**
7
- * Injects the `QueryClient` instance into the component or service.
8
- *
9
- * **Example**
10
- * ```ts
11
- * const queryClient = injectQueryClient();
12
- * ```
13
- * @returns The `QueryClient` instance.
14
- * @public
15
- */
16
- export const injectQueryClient = tokens[0]
17
-
18
- /**
19
- * Usually {@link provideAngularQuery} is used once to set up TanStack Query and the
20
- * {@link https://tanstack.com/query/latest/docs/reference/QueryClient|QueryClient}
21
- * for the entire application. You can use `provideQueryClient` to provide a
22
- * different `QueryClient` instance for a part of the application.
23
- * @public
24
- */
25
- export const provideQueryClient = tokens[1]