@tanstack/angular-query-experimental 5.84.0 → 5.84.2
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.
- package/{dist/index.d.ts → index.d.ts} +1 -1
- package/{dist/inject-mutation.d.ts → inject-mutation.d.ts} +1 -2
- package/inject-mutation.mjs.map +1 -0
- package/{dist/mutation-options.d.ts → mutation-options.d.ts} +4 -8
- package/mutation-options.mjs.map +1 -0
- package/package.json +5 -41
- package/{dist/types.d.ts → types.d.ts} +3 -1
- package/dist/inject-mutation.mjs.map +0 -1
- package/dist/mutation-options.mjs.map +0 -1
- package/src/create-base-query.ts +0 -142
- package/src/index.ts +0 -64
- package/src/infinite-query-options.ts +0 -187
- package/src/inject-infinite-query.ts +0 -129
- package/src/inject-is-fetching.ts +0 -66
- package/src/inject-is-mutating.ts +0 -65
- package/src/inject-is-restoring.ts +0 -52
- package/src/inject-mutation-state.ts +0 -125
- package/src/inject-mutation.ts +0 -169
- package/src/inject-queries.ts +0 -271
- package/src/inject-query-client.ts +0 -23
- package/src/inject-query.ts +0 -230
- package/src/mutation-options.ts +0 -60
- package/src/providers.ts +0 -372
- package/src/query-options.ts +0 -178
- package/src/signal-proxy.ts +0 -46
- package/src/types.ts +0 -309
- package/src/util/is-dev-mode/is-dev-mode.ts +0 -3
- /package/{dist/create-base-query.d.ts → create-base-query.d.ts} +0 -0
- /package/{dist/create-base-query.mjs → create-base-query.mjs} +0 -0
- /package/{dist/create-base-query.mjs.map → create-base-query.mjs.map} +0 -0
- /package/{dist/index.mjs → index.mjs} +0 -0
- /package/{dist/index.mjs.map → index.mjs.map} +0 -0
- /package/{dist/infinite-query-options.d.ts → infinite-query-options.d.ts} +0 -0
- /package/{dist/infinite-query-options.mjs → infinite-query-options.mjs} +0 -0
- /package/{dist/infinite-query-options.mjs.map → infinite-query-options.mjs.map} +0 -0
- /package/{dist/inject-infinite-query.d.ts → inject-infinite-query.d.ts} +0 -0
- /package/{dist/inject-infinite-query.mjs → inject-infinite-query.mjs} +0 -0
- /package/{dist/inject-infinite-query.mjs.map → inject-infinite-query.mjs.map} +0 -0
- /package/{dist/inject-is-fetching.d.ts → inject-is-fetching.d.ts} +0 -0
- /package/{dist/inject-is-fetching.mjs → inject-is-fetching.mjs} +0 -0
- /package/{dist/inject-is-fetching.mjs.map → inject-is-fetching.mjs.map} +0 -0
- /package/{dist/inject-is-mutating.d.ts → inject-is-mutating.d.ts} +0 -0
- /package/{dist/inject-is-mutating.mjs → inject-is-mutating.mjs} +0 -0
- /package/{dist/inject-is-mutating.mjs.map → inject-is-mutating.mjs.map} +0 -0
- /package/{dist/inject-is-restoring.d.ts → inject-is-restoring.d.ts} +0 -0
- /package/{dist/inject-is-restoring.mjs → inject-is-restoring.mjs} +0 -0
- /package/{dist/inject-is-restoring.mjs.map → inject-is-restoring.mjs.map} +0 -0
- /package/{dist/inject-mutation-state.d.ts → inject-mutation-state.d.ts} +0 -0
- /package/{dist/inject-mutation-state.mjs → inject-mutation-state.mjs} +0 -0
- /package/{dist/inject-mutation-state.mjs.map → inject-mutation-state.mjs.map} +0 -0
- /package/{dist/inject-mutation.mjs → inject-mutation.mjs} +0 -0
- /package/{dist/inject-queries.d.ts → inject-queries.d.ts} +0 -0
- /package/{dist/inject-queries.mjs → inject-queries.mjs} +0 -0
- /package/{dist/inject-queries.mjs.map → inject-queries.mjs.map} +0 -0
- /package/{dist/inject-query-client.d.ts → inject-query-client.d.ts} +0 -0
- /package/{dist/inject-query-client.mjs → inject-query-client.mjs} +0 -0
- /package/{dist/inject-query-client.mjs.map → inject-query-client.mjs.map} +0 -0
- /package/{dist/inject-query.d.ts → inject-query.d.ts} +0 -0
- /package/{dist/inject-query.mjs → inject-query.mjs} +0 -0
- /package/{dist/inject-query.mjs.map → inject-query.mjs.map} +0 -0
- /package/{dist/mutation-options.mjs → mutation-options.mjs} +0 -0
- /package/{dist/providers.d.ts → providers.d.ts} +0 -0
- /package/{dist/providers.mjs → providers.mjs} +0 -0
- /package/{dist/providers.mjs.map → providers.mjs.map} +0 -0
- /package/{dist/query-options.d.ts → query-options.d.ts} +0 -0
- /package/{dist/query-options.mjs → query-options.mjs} +0 -0
- /package/{dist/query-options.mjs.map → query-options.mjs.map} +0 -0
- /package/{dist/signal-proxy.d.ts → signal-proxy.d.ts} +0 -0
- /package/{dist/signal-proxy.mjs → signal-proxy.mjs} +0 -0
- /package/{dist/signal-proxy.mjs.map → signal-proxy.mjs.map} +0 -0
- /package/{dist/util → util}/is-dev-mode/is-dev-mode.d.ts +0 -0
package/src/inject-queries.ts
DELETED
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
QueriesObserver,
|
|
3
|
-
QueryClient,
|
|
4
|
-
notifyManager,
|
|
5
|
-
} from '@tanstack/query-core'
|
|
6
|
-
import {
|
|
7
|
-
DestroyRef,
|
|
8
|
-
Injector,
|
|
9
|
-
NgZone,
|
|
10
|
-
assertInInjectionContext,
|
|
11
|
-
computed,
|
|
12
|
-
effect,
|
|
13
|
-
inject,
|
|
14
|
-
runInInjectionContext,
|
|
15
|
-
signal,
|
|
16
|
-
} from '@angular/core'
|
|
17
|
-
import { injectIsRestoring } from './inject-is-restoring'
|
|
18
|
-
import type { Signal } from '@angular/core'
|
|
19
|
-
import type {
|
|
20
|
-
DefaultError,
|
|
21
|
-
OmitKeyof,
|
|
22
|
-
QueriesObserverOptions,
|
|
23
|
-
QueriesPlaceholderDataFunction,
|
|
24
|
-
QueryFunction,
|
|
25
|
-
QueryKey,
|
|
26
|
-
QueryObserverOptions,
|
|
27
|
-
QueryObserverResult,
|
|
28
|
-
ThrowOnError,
|
|
29
|
-
} from '@tanstack/query-core'
|
|
30
|
-
|
|
31
|
-
// This defines the `CreateQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
|
|
32
|
-
// `placeholderData` function does not have a parameter
|
|
33
|
-
type QueryObserverOptionsForCreateQueries<
|
|
34
|
-
TQueryFnData = unknown,
|
|
35
|
-
TError = DefaultError,
|
|
36
|
-
TData = TQueryFnData,
|
|
37
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
38
|
-
> = OmitKeyof<
|
|
39
|
-
QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
|
|
40
|
-
'placeholderData'
|
|
41
|
-
> & {
|
|
42
|
-
placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Avoid TS depth-limit error in case of large array literal
|
|
46
|
-
type MAXIMUM_DEPTH = 20
|
|
47
|
-
|
|
48
|
-
// Widen the type of the symbol to enable type inference even if skipToken is not immutable.
|
|
49
|
-
type SkipTokenForUseQueries = symbol
|
|
50
|
-
|
|
51
|
-
type GetOptions<T> =
|
|
52
|
-
// Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }
|
|
53
|
-
T extends {
|
|
54
|
-
queryFnData: infer TQueryFnData
|
|
55
|
-
error?: infer TError
|
|
56
|
-
data: infer TData
|
|
57
|
-
}
|
|
58
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
|
|
59
|
-
: T extends { queryFnData: infer TQueryFnData; error?: infer TError }
|
|
60
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
|
|
61
|
-
: T extends { data: infer TData; error?: infer TError }
|
|
62
|
-
? QueryObserverOptionsForCreateQueries<unknown, TError, TData>
|
|
63
|
-
: // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]
|
|
64
|
-
T extends [infer TQueryFnData, infer TError, infer TData]
|
|
65
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
|
|
66
|
-
: T extends [infer TQueryFnData, infer TError]
|
|
67
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
|
|
68
|
-
: T extends [infer TQueryFnData]
|
|
69
|
-
? QueryObserverOptionsForCreateQueries<TQueryFnData>
|
|
70
|
-
: // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided
|
|
71
|
-
T extends {
|
|
72
|
-
queryFn?:
|
|
73
|
-
| QueryFunction<infer TQueryFnData, infer TQueryKey>
|
|
74
|
-
| SkipTokenForUseQueries
|
|
75
|
-
select: (data: any) => infer TData
|
|
76
|
-
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
77
|
-
}
|
|
78
|
-
? QueryObserverOptionsForCreateQueries<
|
|
79
|
-
TQueryFnData,
|
|
80
|
-
unknown extends TError ? DefaultError : TError,
|
|
81
|
-
unknown extends TData ? TQueryFnData : TData,
|
|
82
|
-
TQueryKey
|
|
83
|
-
>
|
|
84
|
-
: // Fallback
|
|
85
|
-
QueryObserverOptionsForCreateQueries
|
|
86
|
-
|
|
87
|
-
type GetResults<T> =
|
|
88
|
-
// Part 1: responsible for mapping explicit type parameter to function result, if object
|
|
89
|
-
T extends { queryFnData: any; error?: infer TError; data: infer TData }
|
|
90
|
-
? QueryObserverResult<TData, TError>
|
|
91
|
-
: T extends { queryFnData: infer TQueryFnData; error?: infer TError }
|
|
92
|
-
? QueryObserverResult<TQueryFnData, TError>
|
|
93
|
-
: T extends { data: infer TData; error?: infer TError }
|
|
94
|
-
? QueryObserverResult<TData, TError>
|
|
95
|
-
: // Part 2: responsible for mapping explicit type parameter to function result, if tuple
|
|
96
|
-
T extends [any, infer TError, infer TData]
|
|
97
|
-
? QueryObserverResult<TData, TError>
|
|
98
|
-
: T extends [infer TQueryFnData, infer TError]
|
|
99
|
-
? QueryObserverResult<TQueryFnData, TError>
|
|
100
|
-
: T extends [infer TQueryFnData]
|
|
101
|
-
? QueryObserverResult<TQueryFnData>
|
|
102
|
-
: // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided
|
|
103
|
-
T extends {
|
|
104
|
-
queryFn?:
|
|
105
|
-
| QueryFunction<infer TQueryFnData, any>
|
|
106
|
-
| SkipTokenForUseQueries
|
|
107
|
-
select: (data: any) => infer TData
|
|
108
|
-
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
109
|
-
}
|
|
110
|
-
? QueryObserverResult<
|
|
111
|
-
unknown extends TData ? TQueryFnData : TData,
|
|
112
|
-
unknown extends TError ? DefaultError : TError
|
|
113
|
-
>
|
|
114
|
-
: // Fallback
|
|
115
|
-
QueryObserverResult
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
|
|
119
|
-
* @public
|
|
120
|
-
*/
|
|
121
|
-
export type QueriesOptions<
|
|
122
|
-
T extends Array<any>,
|
|
123
|
-
TResult extends Array<any> = [],
|
|
124
|
-
TDepth extends ReadonlyArray<number> = [],
|
|
125
|
-
> = TDepth['length'] extends MAXIMUM_DEPTH
|
|
126
|
-
? Array<QueryObserverOptionsForCreateQueries>
|
|
127
|
-
: T extends []
|
|
128
|
-
? []
|
|
129
|
-
: T extends [infer Head]
|
|
130
|
-
? [...TResult, GetOptions<Head>]
|
|
131
|
-
: T extends [infer Head, ...infer Tail]
|
|
132
|
-
? QueriesOptions<
|
|
133
|
-
[...Tail],
|
|
134
|
-
[...TResult, GetOptions<Head>],
|
|
135
|
-
[...TDepth, 1]
|
|
136
|
-
>
|
|
137
|
-
: ReadonlyArray<unknown> extends T
|
|
138
|
-
? T
|
|
139
|
-
: // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!
|
|
140
|
-
// use this to infer the param types in the case of Array.map() argument
|
|
141
|
-
T extends Array<
|
|
142
|
-
QueryObserverOptionsForCreateQueries<
|
|
143
|
-
infer TQueryFnData,
|
|
144
|
-
infer TError,
|
|
145
|
-
infer TData,
|
|
146
|
-
infer TQueryKey
|
|
147
|
-
>
|
|
148
|
-
>
|
|
149
|
-
? Array<
|
|
150
|
-
QueryObserverOptionsForCreateQueries<
|
|
151
|
-
TQueryFnData,
|
|
152
|
-
TError,
|
|
153
|
-
TData,
|
|
154
|
-
TQueryKey
|
|
155
|
-
>
|
|
156
|
-
>
|
|
157
|
-
: // Fallback
|
|
158
|
-
Array<QueryObserverOptionsForCreateQueries>
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* QueriesResults reducer recursively maps type param to results
|
|
162
|
-
* @public
|
|
163
|
-
*/
|
|
164
|
-
export type QueriesResults<
|
|
165
|
-
T extends Array<any>,
|
|
166
|
-
TResult extends Array<any> = [],
|
|
167
|
-
TDepth extends ReadonlyArray<number> = [],
|
|
168
|
-
> = TDepth['length'] extends MAXIMUM_DEPTH
|
|
169
|
-
? Array<QueryObserverResult>
|
|
170
|
-
: T extends []
|
|
171
|
-
? []
|
|
172
|
-
: T extends [infer Head]
|
|
173
|
-
? [...TResult, GetResults<Head>]
|
|
174
|
-
: T extends [infer Head, ...infer Tail]
|
|
175
|
-
? QueriesResults<
|
|
176
|
-
[...Tail],
|
|
177
|
-
[...TResult, GetResults<Head>],
|
|
178
|
-
[...TDepth, 1]
|
|
179
|
-
>
|
|
180
|
-
: T extends Array<
|
|
181
|
-
QueryObserverOptionsForCreateQueries<
|
|
182
|
-
infer TQueryFnData,
|
|
183
|
-
infer TError,
|
|
184
|
-
infer TData,
|
|
185
|
-
any
|
|
186
|
-
>
|
|
187
|
-
>
|
|
188
|
-
? // Dynamic-size (homogenous) CreateQueryOptions array: map directly to array of results
|
|
189
|
-
Array<
|
|
190
|
-
QueryObserverResult<
|
|
191
|
-
unknown extends TData ? TQueryFnData : TData,
|
|
192
|
-
unknown extends TError ? DefaultError : TError
|
|
193
|
-
>
|
|
194
|
-
>
|
|
195
|
-
: // Fallback
|
|
196
|
-
Array<QueryObserverResult>
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* @param root0
|
|
200
|
-
* @param root0.queries
|
|
201
|
-
* @param root0.combine
|
|
202
|
-
* @param injector
|
|
203
|
-
* @param injector
|
|
204
|
-
* @public
|
|
205
|
-
*/
|
|
206
|
-
export function injectQueries<
|
|
207
|
-
T extends Array<any>,
|
|
208
|
-
TCombinedResult = QueriesResults<T>,
|
|
209
|
-
>(
|
|
210
|
-
{
|
|
211
|
-
queries,
|
|
212
|
-
...options
|
|
213
|
-
}: {
|
|
214
|
-
queries: Signal<[...QueriesOptions<T>]>
|
|
215
|
-
combine?: (result: QueriesResults<T>) => TCombinedResult
|
|
216
|
-
},
|
|
217
|
-
injector?: Injector,
|
|
218
|
-
): Signal<TCombinedResult> {
|
|
219
|
-
!injector && assertInInjectionContext(injectQueries)
|
|
220
|
-
return runInInjectionContext(injector ?? inject(Injector), () => {
|
|
221
|
-
const destroyRef = inject(DestroyRef)
|
|
222
|
-
const ngZone = inject(NgZone)
|
|
223
|
-
const queryClient = inject(QueryClient)
|
|
224
|
-
const isRestoring = injectIsRestoring()
|
|
225
|
-
|
|
226
|
-
const defaultedQueries = computed(() => {
|
|
227
|
-
return queries().map((opts) => {
|
|
228
|
-
const defaultedOptions = queryClient.defaultQueryOptions(opts)
|
|
229
|
-
// Make sure the results are already in fetching state before subscribing or updating options
|
|
230
|
-
defaultedOptions._optimisticResults = isRestoring()
|
|
231
|
-
? 'isRestoring'
|
|
232
|
-
: 'optimistic'
|
|
233
|
-
|
|
234
|
-
return defaultedOptions as QueryObserverOptions
|
|
235
|
-
})
|
|
236
|
-
})
|
|
237
|
-
|
|
238
|
-
const observer = new QueriesObserver<TCombinedResult>(
|
|
239
|
-
queryClient,
|
|
240
|
-
defaultedQueries(),
|
|
241
|
-
options as QueriesObserverOptions<TCombinedResult>,
|
|
242
|
-
)
|
|
243
|
-
|
|
244
|
-
// Do not notify on updates because of changes in the options because
|
|
245
|
-
// these changes should already be reflected in the optimistic result.
|
|
246
|
-
effect(() => {
|
|
247
|
-
observer.setQueries(
|
|
248
|
-
defaultedQueries(),
|
|
249
|
-
options as QueriesObserverOptions<TCombinedResult>,
|
|
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
|
-
effect(() => {
|
|
261
|
-
const unsubscribe = isRestoring()
|
|
262
|
-
? () => undefined
|
|
263
|
-
: ngZone.runOutsideAngular(() =>
|
|
264
|
-
observer.subscribe(notifyManager.batchCalls(result.set)),
|
|
265
|
-
)
|
|
266
|
-
destroyRef.onDestroy(unsubscribe)
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
return result
|
|
270
|
-
})
|
|
271
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Injector, inject } from '@angular/core'
|
|
2
|
-
import { QueryClient } from '@tanstack/query-core'
|
|
3
|
-
import type { InjectOptions } from '@angular/core'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Injects a `QueryClient` instance and allows passing a custom injector.
|
|
7
|
-
* @param injectOptions - Type of the options argument to inject and optionally a custom injector.
|
|
8
|
-
* @returns The `QueryClient` instance.
|
|
9
|
-
* @public
|
|
10
|
-
* @deprecated Use `inject(QueryClient)` instead.
|
|
11
|
-
* If you need to get a `QueryClient` from a custom injector, use `injector.get(QueryClient)`.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* **Example**
|
|
15
|
-
* ```ts
|
|
16
|
-
* const queryClient = injectQueryClient();
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export function injectQueryClient(
|
|
20
|
-
injectOptions: InjectOptions & { injector?: Injector } = {},
|
|
21
|
-
) {
|
|
22
|
-
return (injectOptions.injector ?? inject(Injector)).get(QueryClient)
|
|
23
|
-
}
|
package/src/inject-query.ts
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import { QueryObserver } from '@tanstack/query-core'
|
|
2
|
-
import {
|
|
3
|
-
Injector,
|
|
4
|
-
assertInInjectionContext,
|
|
5
|
-
inject,
|
|
6
|
-
runInInjectionContext,
|
|
7
|
-
} from '@angular/core'
|
|
8
|
-
import { createBaseQuery } from './create-base-query'
|
|
9
|
-
import type { DefaultError, QueryKey } from '@tanstack/query-core'
|
|
10
|
-
import type {
|
|
11
|
-
CreateQueryOptions,
|
|
12
|
-
CreateQueryResult,
|
|
13
|
-
DefinedCreateQueryResult,
|
|
14
|
-
} from './types'
|
|
15
|
-
import type {
|
|
16
|
-
DefinedInitialDataOptions,
|
|
17
|
-
UndefinedInitialDataOptions,
|
|
18
|
-
} from './query-options'
|
|
19
|
-
|
|
20
|
-
export interface InjectQueryOptions {
|
|
21
|
-
/**
|
|
22
|
-
* The `Injector` in which to create the query.
|
|
23
|
-
*
|
|
24
|
-
* If this is not provided, the current injection context will be used instead (via `inject`).
|
|
25
|
-
*/
|
|
26
|
-
injector?: Injector
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
|
|
31
|
-
*
|
|
32
|
-
* **Basic example**
|
|
33
|
-
* ```ts
|
|
34
|
-
* class ServiceOrComponent {
|
|
35
|
-
* query = injectQuery(() => ({
|
|
36
|
-
* queryKey: ['repoData'],
|
|
37
|
-
* queryFn: () =>
|
|
38
|
-
* this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
|
|
39
|
-
* }))
|
|
40
|
-
* }
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
|
|
44
|
-
* In the example below, the query will be automatically enabled and executed when the filter signal changes
|
|
45
|
-
* to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
|
|
46
|
-
*
|
|
47
|
-
* **Reactive example**
|
|
48
|
-
* ```ts
|
|
49
|
-
* class ServiceOrComponent {
|
|
50
|
-
* filter = signal('')
|
|
51
|
-
*
|
|
52
|
-
* todosQuery = injectQuery(() => ({
|
|
53
|
-
* queryKey: ['todos', this.filter()],
|
|
54
|
-
* queryFn: () => fetchTodos(this.filter()),
|
|
55
|
-
* // Signals can be combined with expressions
|
|
56
|
-
* enabled: !!this.filter(),
|
|
57
|
-
* }))
|
|
58
|
-
* }
|
|
59
|
-
* ```
|
|
60
|
-
* @param injectQueryFn - A function that returns query options.
|
|
61
|
-
* @param options - Additional configuration
|
|
62
|
-
* @returns The query result.
|
|
63
|
-
* @public
|
|
64
|
-
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
|
|
65
|
-
*/
|
|
66
|
-
export function injectQuery<
|
|
67
|
-
TQueryFnData = unknown,
|
|
68
|
-
TError = DefaultError,
|
|
69
|
-
TData = TQueryFnData,
|
|
70
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
71
|
-
>(
|
|
72
|
-
injectQueryFn: () => DefinedInitialDataOptions<
|
|
73
|
-
TQueryFnData,
|
|
74
|
-
TError,
|
|
75
|
-
TData,
|
|
76
|
-
TQueryKey
|
|
77
|
-
>,
|
|
78
|
-
options?: InjectQueryOptions,
|
|
79
|
-
): DefinedCreateQueryResult<TData, TError>
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
|
|
83
|
-
*
|
|
84
|
-
* **Basic example**
|
|
85
|
-
* ```ts
|
|
86
|
-
* class ServiceOrComponent {
|
|
87
|
-
* query = injectQuery(() => ({
|
|
88
|
-
* queryKey: ['repoData'],
|
|
89
|
-
* queryFn: () =>
|
|
90
|
-
* this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
|
|
91
|
-
* }))
|
|
92
|
-
* }
|
|
93
|
-
* ```
|
|
94
|
-
*
|
|
95
|
-
* Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
|
|
96
|
-
* In the example below, the query will be automatically enabled and executed when the filter signal changes
|
|
97
|
-
* to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
|
|
98
|
-
*
|
|
99
|
-
* **Reactive example**
|
|
100
|
-
* ```ts
|
|
101
|
-
* class ServiceOrComponent {
|
|
102
|
-
* filter = signal('')
|
|
103
|
-
*
|
|
104
|
-
* todosQuery = injectQuery(() => ({
|
|
105
|
-
* queryKey: ['todos', this.filter()],
|
|
106
|
-
* queryFn: () => fetchTodos(this.filter()),
|
|
107
|
-
* // Signals can be combined with expressions
|
|
108
|
-
* enabled: !!this.filter(),
|
|
109
|
-
* }))
|
|
110
|
-
* }
|
|
111
|
-
* ```
|
|
112
|
-
* @param injectQueryFn - A function that returns query options.
|
|
113
|
-
* @param options - Additional configuration
|
|
114
|
-
* @returns The query result.
|
|
115
|
-
* @public
|
|
116
|
-
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
|
|
117
|
-
*/
|
|
118
|
-
export function injectQuery<
|
|
119
|
-
TQueryFnData = unknown,
|
|
120
|
-
TError = DefaultError,
|
|
121
|
-
TData = TQueryFnData,
|
|
122
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
123
|
-
>(
|
|
124
|
-
injectQueryFn: () => UndefinedInitialDataOptions<
|
|
125
|
-
TQueryFnData,
|
|
126
|
-
TError,
|
|
127
|
-
TData,
|
|
128
|
-
TQueryKey
|
|
129
|
-
>,
|
|
130
|
-
options?: InjectQueryOptions,
|
|
131
|
-
): CreateQueryResult<TData, TError>
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
|
|
135
|
-
*
|
|
136
|
-
* **Basic example**
|
|
137
|
-
* ```ts
|
|
138
|
-
* class ServiceOrComponent {
|
|
139
|
-
* query = injectQuery(() => ({
|
|
140
|
-
* queryKey: ['repoData'],
|
|
141
|
-
* queryFn: () =>
|
|
142
|
-
* this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
|
|
143
|
-
* }))
|
|
144
|
-
* }
|
|
145
|
-
* ```
|
|
146
|
-
*
|
|
147
|
-
* Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
|
|
148
|
-
* In the example below, the query will be automatically enabled and executed when the filter signal changes
|
|
149
|
-
* to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
|
|
150
|
-
*
|
|
151
|
-
* **Reactive example**
|
|
152
|
-
* ```ts
|
|
153
|
-
* class ServiceOrComponent {
|
|
154
|
-
* filter = signal('')
|
|
155
|
-
*
|
|
156
|
-
* todosQuery = injectQuery(() => ({
|
|
157
|
-
* queryKey: ['todos', this.filter()],
|
|
158
|
-
* queryFn: () => fetchTodos(this.filter()),
|
|
159
|
-
* // Signals can be combined with expressions
|
|
160
|
-
* enabled: !!this.filter(),
|
|
161
|
-
* }))
|
|
162
|
-
* }
|
|
163
|
-
* ```
|
|
164
|
-
* @param injectQueryFn - A function that returns query options.
|
|
165
|
-
* @param options - Additional configuration
|
|
166
|
-
* @returns The query result.
|
|
167
|
-
* @public
|
|
168
|
-
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
|
|
169
|
-
*/
|
|
170
|
-
export function injectQuery<
|
|
171
|
-
TQueryFnData = unknown,
|
|
172
|
-
TError = DefaultError,
|
|
173
|
-
TData = TQueryFnData,
|
|
174
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
175
|
-
>(
|
|
176
|
-
injectQueryFn: () => CreateQueryOptions<
|
|
177
|
-
TQueryFnData,
|
|
178
|
-
TError,
|
|
179
|
-
TData,
|
|
180
|
-
TQueryKey
|
|
181
|
-
>,
|
|
182
|
-
options?: InjectQueryOptions,
|
|
183
|
-
): CreateQueryResult<TData, TError>
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
|
|
187
|
-
*
|
|
188
|
-
* **Basic example**
|
|
189
|
-
* ```ts
|
|
190
|
-
* class ServiceOrComponent {
|
|
191
|
-
* query = injectQuery(() => ({
|
|
192
|
-
* queryKey: ['repoData'],
|
|
193
|
-
* queryFn: () =>
|
|
194
|
-
* this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
|
|
195
|
-
* }))
|
|
196
|
-
* }
|
|
197
|
-
* ```
|
|
198
|
-
*
|
|
199
|
-
* Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
|
|
200
|
-
* In the example below, the query will be automatically enabled and executed when the filter signal changes
|
|
201
|
-
* to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
|
|
202
|
-
*
|
|
203
|
-
* **Reactive example**
|
|
204
|
-
* ```ts
|
|
205
|
-
* class ServiceOrComponent {
|
|
206
|
-
* filter = signal('')
|
|
207
|
-
*
|
|
208
|
-
* todosQuery = injectQuery(() => ({
|
|
209
|
-
* queryKey: ['todos', this.filter()],
|
|
210
|
-
* queryFn: () => fetchTodos(this.filter()),
|
|
211
|
-
* // Signals can be combined with expressions
|
|
212
|
-
* enabled: !!this.filter(),
|
|
213
|
-
* }))
|
|
214
|
-
* }
|
|
215
|
-
* ```
|
|
216
|
-
* @param injectQueryFn - A function that returns query options.
|
|
217
|
-
* @param options - Additional configuration
|
|
218
|
-
* @returns The query result.
|
|
219
|
-
* @public
|
|
220
|
-
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
|
|
221
|
-
*/
|
|
222
|
-
export function injectQuery(
|
|
223
|
-
injectQueryFn: () => CreateQueryOptions,
|
|
224
|
-
options?: InjectQueryOptions,
|
|
225
|
-
) {
|
|
226
|
-
!options?.injector && assertInInjectionContext(injectQuery)
|
|
227
|
-
return runInInjectionContext(options?.injector ?? inject(Injector), () =>
|
|
228
|
-
createBaseQuery(injectQueryFn, QueryObserver),
|
|
229
|
-
) as unknown as CreateQueryResult
|
|
230
|
-
}
|
package/src/mutation-options.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
DefaultError,
|
|
3
|
-
MutationObserverOptions,
|
|
4
|
-
OmitKeyof,
|
|
5
|
-
} from '@tanstack/query-core'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Allows to share and re-use mutation options in a type-safe way.
|
|
9
|
-
*
|
|
10
|
-
* **Example**
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* export class QueriesService {
|
|
14
|
-
* private http = inject(HttpClient);
|
|
15
|
-
*
|
|
16
|
-
* updatePost(id: number) {
|
|
17
|
-
* return mutationOptions({
|
|
18
|
-
* mutationFn: (post: Post) => Promise.resolve(post),
|
|
19
|
-
* mutationKey: ["updatePost", id],
|
|
20
|
-
* onSuccess: (newPost) => {
|
|
21
|
-
* // ^? newPost: Post
|
|
22
|
-
* this.queryClient.setQueryData(["posts", id], newPost);
|
|
23
|
-
* },
|
|
24
|
-
* });
|
|
25
|
-
* }
|
|
26
|
-
* }
|
|
27
|
-
*
|
|
28
|
-
* queries = inject(QueriesService)
|
|
29
|
-
* idSignal = new Signal(0);
|
|
30
|
-
* mutation = injectMutation(() => this.queries.updatePost(this.idSignal()))
|
|
31
|
-
*
|
|
32
|
-
* mutation.mutate({ title: 'New Title' })
|
|
33
|
-
* ```
|
|
34
|
-
* @param options - The mutation options.
|
|
35
|
-
* @returns Mutation options.
|
|
36
|
-
* @public
|
|
37
|
-
*/
|
|
38
|
-
export function mutationOptions<
|
|
39
|
-
TData = unknown,
|
|
40
|
-
TError = DefaultError,
|
|
41
|
-
TVariables = void,
|
|
42
|
-
TContext = unknown,
|
|
43
|
-
>(
|
|
44
|
-
options: MutationObserverOptions<TData, TError, TVariables, TContext>,
|
|
45
|
-
): CreateMutationOptions<TData, TError, TVariables, TContext> {
|
|
46
|
-
return options
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @public
|
|
51
|
-
*/
|
|
52
|
-
export interface CreateMutationOptions<
|
|
53
|
-
TData = unknown,
|
|
54
|
-
TError = DefaultError,
|
|
55
|
-
TVariables = void,
|
|
56
|
-
TContext = unknown,
|
|
57
|
-
> extends OmitKeyof<
|
|
58
|
-
MutationObserverOptions<TData, TError, TVariables, TContext>,
|
|
59
|
-
'_defaulted'
|
|
60
|
-
> {}
|