@tanstack/angular-query-experimental 5.84.0 → 5.84.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.
- package/package.json +7 -43
- 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.d.ts → index.d.ts} +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.d.ts → inject-mutation.d.ts} +0 -0
- /package/{dist/inject-mutation.mjs → inject-mutation.mjs} +0 -0
- /package/{dist/inject-mutation.mjs.map → inject-mutation.mjs.map} +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.d.ts → mutation-options.d.ts} +0 -0
- /package/{dist/mutation-options.mjs → mutation-options.mjs} +0 -0
- /package/{dist/mutation-options.mjs.map → mutation-options.mjs.map} +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/types.d.ts → types.d.ts} +0 -0
- /package/{dist/util → util}/is-dev-mode/is-dev-mode.d.ts +0 -0
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
|
-
> {}
|
package/src/providers.ts
DELETED
|
@@ -1,372 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DestroyRef,
|
|
3
|
-
ENVIRONMENT_INITIALIZER,
|
|
4
|
-
InjectionToken,
|
|
5
|
-
PLATFORM_ID,
|
|
6
|
-
computed,
|
|
7
|
-
effect,
|
|
8
|
-
inject,
|
|
9
|
-
} from '@angular/core'
|
|
10
|
-
import { QueryClient, noop, onlineManager } from '@tanstack/query-core'
|
|
11
|
-
import { isPlatformBrowser } from '@angular/common'
|
|
12
|
-
import { isDevMode } from './util/is-dev-mode/is-dev-mode'
|
|
13
|
-
import type { Provider } from '@angular/core'
|
|
14
|
-
import type {
|
|
15
|
-
DevtoolsButtonPosition,
|
|
16
|
-
DevtoolsErrorType,
|
|
17
|
-
DevtoolsPosition,
|
|
18
|
-
TanstackQueryDevtools,
|
|
19
|
-
} from '@tanstack/query-devtools'
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the
|
|
23
|
-
* {@link https://tanstack.com/query/latest/docs/reference/QueryClient|QueryClient}
|
|
24
|
-
* for the entire application. Internally it calls `provideQueryClient`.
|
|
25
|
-
* You can use `provideQueryClient` to provide a different `QueryClient` instance for a part
|
|
26
|
-
* of the application or for unit testing purposes.
|
|
27
|
-
* @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
|
|
28
|
-
* @returns a provider object that can be used to provide the `QueryClient` instance.
|
|
29
|
-
*/
|
|
30
|
-
export function provideQueryClient(
|
|
31
|
-
queryClient: QueryClient | InjectionToken<QueryClient>,
|
|
32
|
-
): Provider {
|
|
33
|
-
return {
|
|
34
|
-
provide: QueryClient,
|
|
35
|
-
useFactory: () => {
|
|
36
|
-
const client =
|
|
37
|
-
queryClient instanceof InjectionToken
|
|
38
|
-
? inject(queryClient)
|
|
39
|
-
: queryClient
|
|
40
|
-
// Unmount the query client on injector destroy
|
|
41
|
-
inject(DestroyRef).onDestroy(() => client.unmount())
|
|
42
|
-
client.mount()
|
|
43
|
-
return client
|
|
44
|
-
},
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Sets up providers necessary to enable TanStack Query functionality for Angular applications.
|
|
50
|
-
*
|
|
51
|
-
* Allows to configure a `QueryClient` and optional features such as developer tools.
|
|
52
|
-
*
|
|
53
|
-
* **Example - standalone**
|
|
54
|
-
*
|
|
55
|
-
* ```ts
|
|
56
|
-
* import {
|
|
57
|
-
* provideTanStackQuery,
|
|
58
|
-
* QueryClient,
|
|
59
|
-
* } from '@tanstack/angular-query-experimental'
|
|
60
|
-
*
|
|
61
|
-
* bootstrapApplication(AppComponent, {
|
|
62
|
-
* providers: [provideTanStackQuery(new QueryClient())],
|
|
63
|
-
* })
|
|
64
|
-
* ```
|
|
65
|
-
*
|
|
66
|
-
* **Example - NgModule-based**
|
|
67
|
-
*
|
|
68
|
-
* ```ts
|
|
69
|
-
* import {
|
|
70
|
-
* provideTanStackQuery,
|
|
71
|
-
* QueryClient,
|
|
72
|
-
* } from '@tanstack/angular-query-experimental'
|
|
73
|
-
*
|
|
74
|
-
* @NgModule({
|
|
75
|
-
* declarations: [AppComponent],
|
|
76
|
-
* imports: [BrowserModule],
|
|
77
|
-
* providers: [provideTanStackQuery(new QueryClient())],
|
|
78
|
-
* bootstrap: [AppComponent],
|
|
79
|
-
* })
|
|
80
|
-
* export class AppModule {}
|
|
81
|
-
* ```
|
|
82
|
-
*
|
|
83
|
-
* You can also enable optional developer tools by adding `withDevtools`. By
|
|
84
|
-
* default the tools will then be loaded when your app is in development mode.
|
|
85
|
-
* ```ts
|
|
86
|
-
* import {
|
|
87
|
-
* provideTanStackQuery,
|
|
88
|
-
* withDevtools
|
|
89
|
-
* QueryClient,
|
|
90
|
-
* } from '@tanstack/angular-query-experimental'
|
|
91
|
-
*
|
|
92
|
-
* bootstrapApplication(AppComponent,
|
|
93
|
-
* {
|
|
94
|
-
* providers: [
|
|
95
|
-
* provideTanStackQuery(new QueryClient(), withDevtools())
|
|
96
|
-
* ]
|
|
97
|
-
* }
|
|
98
|
-
* )
|
|
99
|
-
* ```
|
|
100
|
-
*
|
|
101
|
-
* **Example: using an InjectionToken**
|
|
102
|
-
*
|
|
103
|
-
* ```ts
|
|
104
|
-
* export const MY_QUERY_CLIENT = new InjectionToken('', {
|
|
105
|
-
* factory: () => new QueryClient(),
|
|
106
|
-
* })
|
|
107
|
-
*
|
|
108
|
-
* // In a lazy loaded route or lazy loaded component's providers array:
|
|
109
|
-
* providers: [provideTanStackQuery(MY_QUERY_CLIENT)]
|
|
110
|
-
* ```
|
|
111
|
-
* @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
|
|
112
|
-
* @param features - Optional features to configure additional Query functionality.
|
|
113
|
-
* @returns A set of providers to set up TanStack Query.
|
|
114
|
-
* @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
|
|
115
|
-
* @see withDevtools
|
|
116
|
-
*/
|
|
117
|
-
export function provideTanStackQuery(
|
|
118
|
-
queryClient: QueryClient | InjectionToken<QueryClient>,
|
|
119
|
-
...features: Array<QueryFeatures>
|
|
120
|
-
): Array<Provider> {
|
|
121
|
-
return [
|
|
122
|
-
provideQueryClient(queryClient),
|
|
123
|
-
features.map((feature) => feature.ɵproviders),
|
|
124
|
-
]
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Sets up providers necessary to enable TanStack Query functionality for Angular applications.
|
|
129
|
-
*
|
|
130
|
-
* Allows to configure a `QueryClient`.
|
|
131
|
-
* @param queryClient - A `QueryClient` instance.
|
|
132
|
-
* @returns A set of providers to set up TanStack Query.
|
|
133
|
-
* @public
|
|
134
|
-
* @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
|
|
135
|
-
* @deprecated Use `provideTanStackQuery` instead.
|
|
136
|
-
*/
|
|
137
|
-
export function provideAngularQuery(queryClient: QueryClient): Array<Provider> {
|
|
138
|
-
return provideTanStackQuery(queryClient)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Helper type to represent a Query feature.
|
|
143
|
-
*/
|
|
144
|
-
export interface QueryFeature<TFeatureKind extends QueryFeatureKind> {
|
|
145
|
-
ɵkind: TFeatureKind
|
|
146
|
-
ɵproviders: Array<Provider>
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Helper function to create an object that represents a Query feature.
|
|
151
|
-
* @param kind -
|
|
152
|
-
* @param providers -
|
|
153
|
-
* @returns A Query feature.
|
|
154
|
-
*/
|
|
155
|
-
export function queryFeature<TFeatureKind extends QueryFeatureKind>(
|
|
156
|
-
kind: TFeatureKind,
|
|
157
|
-
providers: Array<Provider>,
|
|
158
|
-
): QueryFeature<TFeatureKind> {
|
|
159
|
-
return { ɵkind: kind, ɵproviders: providers }
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* A type alias that represents a feature which enables developer tools.
|
|
164
|
-
* The type is used to describe the return value of the `withDevtools` function.
|
|
165
|
-
* @public
|
|
166
|
-
* @see {@link withDevtools}
|
|
167
|
-
*/
|
|
168
|
-
export type DeveloperToolsFeature = QueryFeature<'DeveloperTools'>
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* A type alias that represents a feature which enables persistence.
|
|
172
|
-
* The type is used to describe the return value of the `withPersistQueryClient` function.
|
|
173
|
-
* @public
|
|
174
|
-
*/
|
|
175
|
-
export type PersistQueryClientFeature = QueryFeature<'PersistQueryClient'>
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Options for configuring the TanStack Query devtools.
|
|
179
|
-
* @public
|
|
180
|
-
*/
|
|
181
|
-
export interface DevtoolsOptions {
|
|
182
|
-
/**
|
|
183
|
-
* Set this true if you want the devtools to default to being open
|
|
184
|
-
*/
|
|
185
|
-
initialIsOpen?: boolean
|
|
186
|
-
/**
|
|
187
|
-
* The position of the TanStack logo to open and close the devtools panel.
|
|
188
|
-
* `top-left` | `top-right` | `bottom-left` | `bottom-right` | `relative`
|
|
189
|
-
* Defaults to `bottom-right`.
|
|
190
|
-
*/
|
|
191
|
-
buttonPosition?: DevtoolsButtonPosition
|
|
192
|
-
/**
|
|
193
|
-
* The position of the TanStack Query devtools panel.
|
|
194
|
-
* `top` | `bottom` | `left` | `right`
|
|
195
|
-
* Defaults to `bottom`.
|
|
196
|
-
*/
|
|
197
|
-
position?: DevtoolsPosition
|
|
198
|
-
/**
|
|
199
|
-
* Custom instance of QueryClient
|
|
200
|
-
*/
|
|
201
|
-
client?: QueryClient
|
|
202
|
-
/**
|
|
203
|
-
* Use this so you can define custom errors that can be shown in the devtools.
|
|
204
|
-
*/
|
|
205
|
-
errorTypes?: Array<DevtoolsErrorType>
|
|
206
|
-
/**
|
|
207
|
-
* Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.
|
|
208
|
-
*/
|
|
209
|
-
styleNonce?: string
|
|
210
|
-
/**
|
|
211
|
-
* Use this so you can attach the devtool's styles to a specific element in the DOM.
|
|
212
|
-
*/
|
|
213
|
-
shadowDOMTarget?: ShadowRoot
|
|
214
|
-
/**
|
|
215
|
-
* Set this to true to hide disabled queries from the devtools panel.
|
|
216
|
-
*/
|
|
217
|
-
hideDisabledQueries?: boolean
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Whether the developer tools should load.
|
|
221
|
-
* - `auto`- (Default) Lazily loads devtools when in development mode. Skips loading in production mode.
|
|
222
|
-
* - `true`- Always load the devtools, regardless of the environment.
|
|
223
|
-
* - `false`- Never load the devtools, regardless of the environment.
|
|
224
|
-
*
|
|
225
|
-
* You can use `true` and `false` to override loading developer tools from an environment file.
|
|
226
|
-
* For example, a test environment might run in production mode but you may want to load developer tools.
|
|
227
|
-
*
|
|
228
|
-
* Additionally, you can use a signal in the callback to dynamically load the devtools based on a condition. For example,
|
|
229
|
-
* a signal created from a RxJS observable that listens for a keyboard shortcut.
|
|
230
|
-
*
|
|
231
|
-
* **Example**
|
|
232
|
-
* ```ts
|
|
233
|
-
* withDevtools(() => ({
|
|
234
|
-
* initialIsOpen: true,
|
|
235
|
-
* loadDevtools: inject(ExampleService).loadDevtools()
|
|
236
|
-
* }))
|
|
237
|
-
* ```
|
|
238
|
-
*/
|
|
239
|
-
loadDevtools?: 'auto' | boolean
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Enables developer tools.
|
|
244
|
-
*
|
|
245
|
-
* **Example**
|
|
246
|
-
*
|
|
247
|
-
* ```ts
|
|
248
|
-
* export const appConfig: ApplicationConfig = {
|
|
249
|
-
* providers: [
|
|
250
|
-
* provideTanStackQuery(new QueryClient(), withDevtools())
|
|
251
|
-
* ]
|
|
252
|
-
* }
|
|
253
|
-
* ```
|
|
254
|
-
* By default the devtools will be loaded when Angular runs in development mode and rendered in `<body>`.
|
|
255
|
-
*
|
|
256
|
-
* If you need more control over when devtools are loaded, you can use the `loadDevtools` option. This is particularly useful if you want to load devtools based on environment configurations. For instance, you might have a test environment running in production mode but still require devtools to be available.
|
|
257
|
-
*
|
|
258
|
-
* If you need more control over where devtools are rendered, consider `injectDevtoolsPanel`. This allows rendering devtools inside your own devtools for example.
|
|
259
|
-
* @param withDevtoolsFn - A function that returns `DevtoolsOptions`.
|
|
260
|
-
* @returns A set of providers for use with `provideTanStackQuery`.
|
|
261
|
-
* @public
|
|
262
|
-
* @see {@link provideTanStackQuery}
|
|
263
|
-
* @see {@link DevtoolsOptions}
|
|
264
|
-
*/
|
|
265
|
-
export function withDevtools(
|
|
266
|
-
withDevtoolsFn?: () => DevtoolsOptions,
|
|
267
|
-
): DeveloperToolsFeature {
|
|
268
|
-
let providers: Array<Provider> = []
|
|
269
|
-
if (!isDevMode() && !withDevtoolsFn) {
|
|
270
|
-
providers = []
|
|
271
|
-
} else {
|
|
272
|
-
providers = [
|
|
273
|
-
{
|
|
274
|
-
// Do not use provideEnvironmentInitializer while Angular < v19 is supported
|
|
275
|
-
provide: ENVIRONMENT_INITIALIZER,
|
|
276
|
-
multi: true,
|
|
277
|
-
useFactory: () => {
|
|
278
|
-
if (!isPlatformBrowser(inject(PLATFORM_ID))) return noop
|
|
279
|
-
const injectedClient = inject(QueryClient, {
|
|
280
|
-
optional: true,
|
|
281
|
-
})
|
|
282
|
-
const destroyRef = inject(DestroyRef)
|
|
283
|
-
|
|
284
|
-
const options = computed(() => withDevtoolsFn?.() ?? {})
|
|
285
|
-
|
|
286
|
-
let devtools: TanstackQueryDevtools | null = null
|
|
287
|
-
let el: HTMLElement | null = null
|
|
288
|
-
|
|
289
|
-
const shouldLoadToolsSignal = computed(() => {
|
|
290
|
-
const { loadDevtools } = options()
|
|
291
|
-
return typeof loadDevtools === 'boolean'
|
|
292
|
-
? loadDevtools
|
|
293
|
-
: isDevMode()
|
|
294
|
-
})
|
|
295
|
-
|
|
296
|
-
const getResolvedQueryClient = () => {
|
|
297
|
-
const client = options().client ?? injectedClient
|
|
298
|
-
if (!client) {
|
|
299
|
-
throw new Error('No QueryClient found')
|
|
300
|
-
}
|
|
301
|
-
return client
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const destroyDevtools = () => {
|
|
305
|
-
devtools?.unmount()
|
|
306
|
-
el?.remove()
|
|
307
|
-
devtools = null
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return () =>
|
|
311
|
-
effect(() => {
|
|
312
|
-
const shouldLoadTools = shouldLoadToolsSignal()
|
|
313
|
-
const {
|
|
314
|
-
client,
|
|
315
|
-
position,
|
|
316
|
-
errorTypes,
|
|
317
|
-
buttonPosition,
|
|
318
|
-
initialIsOpen,
|
|
319
|
-
} = options()
|
|
320
|
-
|
|
321
|
-
if (devtools && !shouldLoadTools) {
|
|
322
|
-
destroyDevtools()
|
|
323
|
-
return
|
|
324
|
-
} else if (devtools && shouldLoadTools) {
|
|
325
|
-
client && devtools.setClient(client)
|
|
326
|
-
position && devtools.setPosition(position)
|
|
327
|
-
errorTypes && devtools.setErrorTypes(errorTypes)
|
|
328
|
-
buttonPosition && devtools.setButtonPosition(buttonPosition)
|
|
329
|
-
initialIsOpen && devtools.setInitialIsOpen(initialIsOpen)
|
|
330
|
-
return
|
|
331
|
-
} else if (!shouldLoadTools) {
|
|
332
|
-
return
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
el = document.body.appendChild(document.createElement('div'))
|
|
336
|
-
el.classList.add('tsqd-parent-container')
|
|
337
|
-
|
|
338
|
-
import('@tanstack/query-devtools').then((queryDevtools) => {
|
|
339
|
-
devtools = new queryDevtools.TanstackQueryDevtools({
|
|
340
|
-
...options(),
|
|
341
|
-
client: getResolvedQueryClient(),
|
|
342
|
-
queryFlavor: 'Angular Query',
|
|
343
|
-
version: '5',
|
|
344
|
-
onlineManager,
|
|
345
|
-
})
|
|
346
|
-
|
|
347
|
-
el && devtools.mount(el)
|
|
348
|
-
|
|
349
|
-
// Unmount the devtools on application destroy
|
|
350
|
-
destroyRef.onDestroy(destroyDevtools)
|
|
351
|
-
})
|
|
352
|
-
})
|
|
353
|
-
},
|
|
354
|
-
},
|
|
355
|
-
]
|
|
356
|
-
}
|
|
357
|
-
return queryFeature('DeveloperTools', providers)
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* A type alias that represents all Query features available for use with `provideTanStackQuery`.
|
|
362
|
-
* Features can be enabled by adding special functions to the `provideTanStackQuery` call.
|
|
363
|
-
* See documentation for each symbol to find corresponding function name. See also `provideTanStackQuery`
|
|
364
|
-
* documentation on how to use those functions.
|
|
365
|
-
* @public
|
|
366
|
-
* @see {@link provideTanStackQuery}
|
|
367
|
-
*/
|
|
368
|
-
export type QueryFeatures = DeveloperToolsFeature | PersistQueryClientFeature
|
|
369
|
-
|
|
370
|
-
export const queryFeatures = ['DeveloperTools', 'PersistQueryClient'] as const
|
|
371
|
-
|
|
372
|
-
export type QueryFeatureKind = (typeof queryFeatures)[number]
|