Open History to see the last commit
1import { DestroyRef, InjectionToken, inject } from '@angular/core'
2import { QueryClient } from '@tanstack/query-core'
3import type { Provider } from '@angular/core'
4​
5/**
6 * Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the
7 * [`QueryClient`](https://tanstack.com/query/latest/docs/reference/QueryClient) for the entire application —
8 * it calls `provideQueryClient` internally. Use `provideQueryClient` directly to provide a different
9 * `QueryClient` instance for part of the application, or for unit testing.
10 * @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
11 * @returns A provider object that can be used to provide the `QueryClient` instance.
12 *
13 * @example
14 * Providing a test-only `QueryClient` in a component test, without wiring up `provideTanStackQuery`'s other
15 * defaults:
16 * ```ts
17 * TestBed.configureTestingModule({
18 * providers: [provideQueryClient(new QueryClient())],
19 * })
20 * ```
21 */
22export function provideQueryClient(
23 queryClient: QueryClient | InjectionToken<QueryClient>,
24): Provider {
25 return {
26 provide: QueryClient,
27 useFactory: () => {
28 const client =
29 queryClient instanceof InjectionToken
30 ? inject(queryClient)
31 : queryClient
32 // Unmount the query client on injector destroy
33 inject(DestroyRef).onDestroy(() => client.unmount())
34 client.mount()
35 return client
36 },
37 }
38}
39​
40/**
41 * Sets up providers necessary to enable TanStack Query functionality for Angular applications. Allows
42 * configuring a `QueryClient` and optional features such as developer tools.
43 *
44 * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
45 * @see {@link withDevtools}
46 * @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
47 * @param features - Optional features to configure additional Query functionality.
48 * @returns A set of providers to set up TanStack Query.
49 *
50 * @example
51 * ```ts
52 * import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query-experimental'
53 *
54 * bootstrapApplication(AppComponent, {
55 * providers: [provideTanStackQuery(new QueryClient())],
56 * })
57 * ```
58 *
59 * @example
60 * The same, in an `NgModule`-based application:
61 * ```ts
62 * import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query-experimental'
63 *
64 * @NgModule({
65 * declarations: [AppComponent],
66 * imports: [BrowserModule],
67 * providers: [provideTanStackQuery(new QueryClient())],
68 * bootstrap: [AppComponent],
69 * })
70 * export class AppModule {}
71 * ```
72 *
73 * @example
74 * Enabling optional developer tools by adding `withDevtools` — by default, the tools are then loaded when
75 * your app is in development mode:
76 * ```ts
77 * import {
78 * provideTanStackQuery,
79 * withDevtools,
80 * QueryClient,
81 * } from '@tanstack/angular-query-experimental'
82 *
83 * bootstrapApplication(AppComponent, {
84 * providers: [provideTanStackQuery(new QueryClient(), withDevtools())],
85 * })
86 * ```
87 *
88 * @example
89 * Using an `InjectionToken` for the `QueryClient` — an advanced optimization that lets TanStack Query be
90 * absent from the main application bundle, useful for including it on lazy-loaded routes only while still
91 * sharing a `QueryClient`. This is a small optimization; for most applications it's preferable to provide
92 * the `QueryClient` in the main application config, as in the examples above:
93 * ```ts
94 * export const MY_QUERY_CLIENT = new InjectionToken('', {
95 * factory: () => new QueryClient(),
96 * })
97 *
98 * // In a lazy loaded route or lazy loaded component's providers array:
99 * providers: [provideTanStackQuery(MY_QUERY_CLIENT)]
100 * ```
101 */
102export function provideTanStackQuery(
103 queryClient: QueryClient | InjectionToken<QueryClient>,
104 ...features: Array<QueryFeatures>
105): Array<Provider> {
106 return [
107 provideQueryClient(queryClient),
108 features.map((feature) => feature.ɵproviders),
109 ]
110}
111​
112/**
113 * Sets up providers necessary to enable TanStack Query functionality for Angular applications.
114 *
115 * Allows configuring a `QueryClient`.
116 * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
117 * @param queryClient - A `QueryClient` instance.
118 * @returns A set of providers to set up TanStack Query.
119 * @deprecated Use `provideTanStackQuery` instead.
120 */
121export function provideAngularQuery(queryClient: QueryClient): Array<Provider> {
122 return provideTanStackQuery(queryClient)
123}
124​
125const queryFeatures = [
126 'Devtools',
127 'PersistQueryClient',
128 'BroadcastQueryClient',
129] as const
130​
131type QueryFeatureKind = (typeof queryFeatures)[number]
132​
133/**
134 * Helper type to represent a Query feature.
135 */
136export interface QueryFeature<TFeatureKind extends QueryFeatureKind> {
137 ɵkind: TFeatureKind
138 ɵproviders: Array<Provider>
139}
140​
141/**
142 * Helper function to create an object that represents a Query feature.
143 * @param kind - The kind of feature, e.g. `'Devtools'`.
144 * @param providers - The Angular providers this feature contributes to `provideTanStackQuery`.
145 * @returns A Query feature.
146 */
147export function queryFeature<TFeatureKind extends QueryFeatureKind>(
148 kind: TFeatureKind,
149 providers: Array<Provider>,
150): QueryFeature<TFeatureKind> {
151 return { ɵkind: kind, ɵproviders: providers }
152}
153​
154/**
155 * A type alias that represents a feature which enables developer tools.
156 * The type is used to describe the return value of the `withDevtools` function.
157 * @see {@link withDevtools}
158 */
159export type DevtoolsFeature = QueryFeature<'Devtools'>
160​
161/**
162 * A type alias that represents a feature which enables persistence.
163 * The type is used to describe the return value of the `withPersistQueryClient` function.
164 */
165export type PersistQueryClientFeature = QueryFeature<'PersistQueryClient'>
166​
167/**
168 * A type alias that represents a feature which enables broadcast bootstrap.
169 */
170export type BroadcastQueryClientFeature = QueryFeature<'BroadcastQueryClient'>
171​
172/**
173 * A type alias that represents all Query features available for use with `provideTanStackQuery`.
174 * Features can be enabled by adding special functions to the `provideTanStackQuery` call.
175 * See documentation for each symbol to find corresponding function name. See also `provideTanStackQuery`
176 * documentation on how to use those functions.
177 * @see {@link provideTanStackQuery}
178 */
179export type QueryFeatures =
180 | DevtoolsFeature
181 | PersistQueryClientFeature
182 | BroadcastQueryClientFeature