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