@tanstack/angular-query-experimental 5.60.0 → 5.60.3

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 (51) hide show
  1. package/build/{rollup.d.ts → index.d.ts} +280 -361
  2. package/build/index.js +572 -0
  3. package/build/index.js.map +1 -0
  4. package/package.json +12 -15
  5. package/src/create-base-query.ts +117 -0
  6. package/src/index.ts +28 -0
  7. package/src/infinite-query-options.ts +125 -0
  8. package/src/inject-infinite-query.ts +119 -0
  9. package/src/inject-is-fetching.ts +49 -0
  10. package/src/inject-is-mutating.ts +48 -0
  11. package/src/inject-mutation-state.ts +102 -0
  12. package/src/inject-mutation.ts +121 -0
  13. package/src/inject-queries.ts +246 -0
  14. package/src/inject-query-client.ts +22 -0
  15. package/src/inject-query.ts +207 -0
  16. package/src/providers.ts +351 -0
  17. package/src/query-options.ts +125 -0
  18. package/src/signal-proxy.ts +46 -0
  19. package/src/test-setup.ts +12 -0
  20. package/src/types.ts +328 -0
  21. package/src/util/assert-injector/assert-injector.test.ts +74 -0
  22. package/src/util/assert-injector/assert-injector.ts +81 -0
  23. package/src/util/index.ts +13 -0
  24. package/src/util/is-dev-mode/is-dev-mode.ts +3 -0
  25. package/src/util/lazy-init/lazy-init.ts +34 -0
  26. package/src/util/lazy-signal-initializer/lazy-signal-initializer.ts +23 -0
  27. package/build/README.md +0 -133
  28. package/build/esm2022/create-base-query.mjs +0 -62
  29. package/build/esm2022/index.mjs +0 -16
  30. package/build/esm2022/infinite-query-options.mjs +0 -12
  31. package/build/esm2022/inject-infinite-query.mjs +0 -15
  32. package/build/esm2022/inject-is-fetching.mjs +0 -38
  33. package/build/esm2022/inject-is-mutating.mjs +0 -37
  34. package/build/esm2022/inject-mutation-state.mjs +0 -47
  35. package/build/esm2022/inject-mutation.mjs +0 -51
  36. package/build/esm2022/inject-queries.mjs +0 -33
  37. package/build/esm2022/inject-query-client.mjs +0 -23
  38. package/build/esm2022/inject-query.mjs +0 -44
  39. package/build/esm2022/providers.mjs +0 -206
  40. package/build/esm2022/query-options.mjs +0 -26
  41. package/build/esm2022/signal-proxy.mjs +0 -38
  42. package/build/esm2022/tanstack-angular-query-experimental.mjs +0 -5
  43. package/build/esm2022/types.mjs +0 -3
  44. package/build/esm2022/util/assert-injector/assert-injector.mjs +0 -21
  45. package/build/esm2022/util/create-injection-token/create-injection-token.mjs +0 -61
  46. package/build/esm2022/util/index.mjs +0 -9
  47. package/build/esm2022/util/is-dev-mode/is-dev-mode.mjs +0 -3
  48. package/build/esm2022/util/lazy-init/lazy-init.mjs +0 -31
  49. package/build/esm2022/util/lazy-signal-initializer/lazy-signal-initializer.mjs +0 -14
  50. package/build/fesm2022/tanstack-angular-query-experimental.mjs +0 -738
  51. package/build/fesm2022/tanstack-angular-query-experimental.mjs.map +0 -1
@@ -0,0 +1,207 @@
1
+ import { QueryObserver } from '@tanstack/query-core'
2
+ import { assertInjector } from './util/assert-injector/assert-injector'
3
+ import { createBaseQuery } from './create-base-query'
4
+ import type { Injector } from '@angular/core'
5
+ import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
6
+ import type {
7
+ CreateQueryOptions,
8
+ CreateQueryResult,
9
+ DefinedCreateQueryResult,
10
+ } from './types'
11
+ import type {
12
+ DefinedInitialDataOptions,
13
+ UndefinedInitialDataOptions,
14
+ } from './query-options'
15
+
16
+ /**
17
+ * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
18
+ *
19
+ * **Basic example**
20
+ * ```ts
21
+ * class ServiceOrComponent {
22
+ * query = injectQuery(() => ({
23
+ * queryKey: ['repoData'],
24
+ * queryFn: () =>
25
+ * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
26
+ * }))
27
+ * }
28
+ * ```
29
+ *
30
+ * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
31
+ * In the example below, the query will be automatically enabled and executed when the filter signal changes
32
+ * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
33
+ *
34
+ * **Reactive example**
35
+ * ```ts
36
+ * class ServiceOrComponent {
37
+ * filter = signal('')
38
+ *
39
+ * todosQuery = injectQuery(() => ({
40
+ * queryKey: ['todos', this.filter()],
41
+ * queryFn: () => fetchTodos(this.filter()),
42
+ * // Signals can be combined with expressions
43
+ * enabled: !!this.filter(),
44
+ * }))
45
+ * }
46
+ * ```
47
+ * @param optionsFn - A function that returns query options.
48
+ * @param injector - The Angular injector to use.
49
+ * @returns The query result.
50
+ * @public
51
+ * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
52
+ */
53
+ export function injectQuery<
54
+ TQueryFnData = unknown,
55
+ TError = DefaultError,
56
+ TData = TQueryFnData,
57
+ TQueryKey extends QueryKey = QueryKey,
58
+ >(
59
+ optionsFn: (
60
+ client: QueryClient,
61
+ ) => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
62
+ injector?: Injector,
63
+ ): DefinedCreateQueryResult<TData, TError>
64
+
65
+ /**
66
+ * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
67
+ *
68
+ * **Basic example**
69
+ * ```ts
70
+ * class ServiceOrComponent {
71
+ * query = injectQuery(() => ({
72
+ * queryKey: ['repoData'],
73
+ * queryFn: () =>
74
+ * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
75
+ * }))
76
+ * }
77
+ * ```
78
+ *
79
+ * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
80
+ * In the example below, the query will be automatically enabled and executed when the filter signal changes
81
+ * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
82
+ *
83
+ * **Reactive example**
84
+ * ```ts
85
+ * class ServiceOrComponent {
86
+ * filter = signal('')
87
+ *
88
+ * todosQuery = injectQuery(() => ({
89
+ * queryKey: ['todos', this.filter()],
90
+ * queryFn: () => fetchTodos(this.filter()),
91
+ * // Signals can be combined with expressions
92
+ * enabled: !!this.filter(),
93
+ * }))
94
+ * }
95
+ * ```
96
+ * @param optionsFn - A function that returns query options.
97
+ * @param injector - The Angular injector to use.
98
+ * @returns The query result.
99
+ * @public
100
+ * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
101
+ */
102
+ export function injectQuery<
103
+ TQueryFnData = unknown,
104
+ TError = DefaultError,
105
+ TData = TQueryFnData,
106
+ TQueryKey extends QueryKey = QueryKey,
107
+ >(
108
+ optionsFn: (
109
+ client: QueryClient,
110
+ ) => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
111
+ injector?: Injector,
112
+ ): CreateQueryResult<TData, TError>
113
+
114
+ /**
115
+ * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
116
+ *
117
+ * **Basic example**
118
+ * ```ts
119
+ * class ServiceOrComponent {
120
+ * query = injectQuery(() => ({
121
+ * queryKey: ['repoData'],
122
+ * queryFn: () =>
123
+ * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
124
+ * }))
125
+ * }
126
+ * ```
127
+ *
128
+ * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
129
+ * In the example below, the query will be automatically enabled and executed when the filter signal changes
130
+ * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
131
+ *
132
+ * **Reactive example**
133
+ * ```ts
134
+ * class ServiceOrComponent {
135
+ * filter = signal('')
136
+ *
137
+ * todosQuery = injectQuery(() => ({
138
+ * queryKey: ['todos', this.filter()],
139
+ * queryFn: () => fetchTodos(this.filter()),
140
+ * // Signals can be combined with expressions
141
+ * enabled: !!this.filter(),
142
+ * }))
143
+ * }
144
+ * ```
145
+ * @param optionsFn - A function that returns query options.
146
+ * @param injector - The Angular injector to use.
147
+ * @returns The query result.
148
+ * @public
149
+ * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
150
+ */
151
+ export function injectQuery<
152
+ TQueryFnData = unknown,
153
+ TError = DefaultError,
154
+ TData = TQueryFnData,
155
+ TQueryKey extends QueryKey = QueryKey,
156
+ >(
157
+ optionsFn: (
158
+ client: QueryClient,
159
+ ) => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
160
+ injector?: Injector,
161
+ ): CreateQueryResult<TData, TError>
162
+
163
+ /**
164
+ * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
165
+ *
166
+ * **Basic example**
167
+ * ```ts
168
+ * class ServiceOrComponent {
169
+ * query = injectQuery(() => ({
170
+ * queryKey: ['repoData'],
171
+ * queryFn: () =>
172
+ * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
173
+ * }))
174
+ * }
175
+ * ```
176
+ *
177
+ * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
178
+ * In the example below, the query will be automatically enabled and executed when the filter signal changes
179
+ * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
180
+ *
181
+ * **Reactive example**
182
+ * ```ts
183
+ * class ServiceOrComponent {
184
+ * filter = signal('')
185
+ *
186
+ * todosQuery = injectQuery(() => ({
187
+ * queryKey: ['todos', this.filter()],
188
+ * queryFn: () => fetchTodos(this.filter()),
189
+ * // Signals can be combined with expressions
190
+ * enabled: !!this.filter(),
191
+ * }))
192
+ * }
193
+ * ```
194
+ * @param optionsFn - A function that returns query options.
195
+ * @param injector - The Angular injector to use.
196
+ * @returns The query result.
197
+ * @public
198
+ * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
199
+ */
200
+ export function injectQuery(
201
+ optionsFn: (client: QueryClient) => CreateQueryOptions,
202
+ injector?: Injector,
203
+ ) {
204
+ return assertInjector(injectQuery, injector, () =>
205
+ createBaseQuery(optionsFn, QueryObserver),
206
+ )
207
+ }
@@ -0,0 +1,351 @@
1
+ import {
2
+ DestroyRef,
3
+ ENVIRONMENT_INITIALIZER,
4
+ Injector,
5
+ PLATFORM_ID,
6
+ computed,
7
+ effect,
8
+ inject,
9
+ makeEnvironmentProviders,
10
+ runInInjectionContext,
11
+ } from '@angular/core'
12
+ import { QueryClient, onlineManager } from '@tanstack/query-core'
13
+ import { isPlatformBrowser } from '@angular/common'
14
+ import { isDevMode } from './util/is-dev-mode/is-dev-mode'
15
+ import type { EnvironmentProviders, Provider } from '@angular/core'
16
+ import type {
17
+ DevtoolsButtonPosition,
18
+ DevtoolsErrorType,
19
+ DevtoolsPosition,
20
+ TanstackQueryDevtools,
21
+ } from '@tanstack/query-devtools'
22
+
23
+ /**
24
+ * Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the
25
+ * {@link https://tanstack.com/query/latest/docs/reference/QueryClient|QueryClient}
26
+ * for the entire application. You can use `provideQueryClient` to provide a
27
+ * different `QueryClient` instance for a part of the application.
28
+ * @param queryClient - the `QueryClient` instance to provide.
29
+ * @public
30
+ */
31
+ export function provideQueryClient(queryClient: QueryClient) {
32
+ return { provide: QueryClient, useValue: queryClient }
33
+ }
34
+
35
+ /**
36
+ * Sets up providers necessary to enable TanStack Query functionality for Angular applications.
37
+ *
38
+ * Allows to configure a `QueryClient` and optional features such as developer tools.
39
+ *
40
+ * **Example - standalone**
41
+ *
42
+ * ```ts
43
+ * import {
44
+ * provideTanStackQuery,
45
+ * QueryClient,
46
+ * } from '@tanstack/angular-query-experimental'
47
+ *
48
+ * bootstrapApplication(AppComponent, {
49
+ * providers: [provideTanStackQuery(new QueryClient())],
50
+ * })
51
+ * ```
52
+ *
53
+ * **Example - NgModule-based**
54
+ *
55
+ * ```ts
56
+ * import {
57
+ * provideTanStackQuery,
58
+ * QueryClient,
59
+ * } from '@tanstack/angular-query-experimental'
60
+ *
61
+ * @NgModule({
62
+ * declarations: [AppComponent],
63
+ * imports: [BrowserModule],
64
+ * providers: [provideTanStackQuery(new QueryClient())],
65
+ * bootstrap: [AppComponent],
66
+ * })
67
+ * export class AppModule {}
68
+ * ```
69
+ *
70
+ * You can also enable optional developer tools by adding `withDevtools`. By
71
+ * default the tools will then be loaded when your app is in development mode.
72
+ * ```ts
73
+ * import {
74
+ * provideTanStackQuery,
75
+ * withDevtools
76
+ * QueryClient,
77
+ * } from '@tanstack/angular-query-experimental'
78
+ *
79
+ * bootstrapApplication(AppComponent,
80
+ * {
81
+ * providers: [
82
+ * provideTanStackQuery(new QueryClient(), withDevtools())
83
+ * ]
84
+ * }
85
+ * )
86
+ * ```
87
+ * @param queryClient - A `QueryClient` instance.
88
+ * @param features - Optional features to configure additional Query functionality.
89
+ * @returns A set of providers to set up TanStack Query.
90
+ * @public
91
+ * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
92
+ * @see withDevtools
93
+ */
94
+ export function provideTanStackQuery(
95
+ queryClient: QueryClient,
96
+ ...features: Array<QueryFeatures>
97
+ ): EnvironmentProviders {
98
+ return makeEnvironmentProviders([
99
+ provideQueryClient(queryClient),
100
+ {
101
+ provide: ENVIRONMENT_INITIALIZER,
102
+ multi: true,
103
+ useValue: () => {
104
+ queryClient.mount()
105
+ // Unmount the query client on application destroy
106
+ inject(DestroyRef).onDestroy(() => queryClient.unmount())
107
+ },
108
+ },
109
+ features.map((feature) => feature.ɵproviders),
110
+ ])
111
+ }
112
+
113
+ /**
114
+ * Sets up providers necessary to enable TanStack Query functionality for Angular applications.
115
+ *
116
+ * Allows to configure a `QueryClient`.
117
+ * @param queryClient - A `QueryClient` instance.
118
+ * @returns A set of providers to set up TanStack Query.
119
+ * @public
120
+ * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
121
+ * @deprecated Use `provideTanStackQuery` instead.
122
+ */
123
+ export function provideAngularQuery(
124
+ queryClient: QueryClient,
125
+ ): EnvironmentProviders {
126
+ return provideTanStackQuery(queryClient)
127
+ }
128
+
129
+ /**
130
+ * Helper type to represent a Query feature.
131
+ */
132
+ export interface QueryFeature<TFeatureKind extends QueryFeatureKind> {
133
+ ɵkind: TFeatureKind
134
+ ɵproviders: Array<Provider>
135
+ }
136
+
137
+ /**
138
+ * Helper function to create an object that represents a Query feature.
139
+ * @param kind -
140
+ * @param providers -
141
+ * @returns A Query feature.
142
+ */
143
+ function queryFeature<TFeatureKind extends QueryFeatureKind>(
144
+ kind: TFeatureKind,
145
+ providers: Array<Provider>,
146
+ ): QueryFeature<TFeatureKind> {
147
+ return { ɵkind: kind, ɵproviders: providers }
148
+ }
149
+
150
+ /**
151
+ * A type alias that represents a feature which enables developer tools.
152
+ * The type is used to describe the return value of the `withDevtools` function.
153
+ * @public
154
+ * @see {@link withDevtools}
155
+ */
156
+ export type DeveloperToolsFeature = QueryFeature<'DeveloperTools'>
157
+
158
+ /**
159
+ * Options for configuring the TanStack Query devtools.
160
+ * @public
161
+ */
162
+ export interface DevtoolsOptions {
163
+ /**
164
+ * Set this true if you want the devtools to default to being open
165
+ */
166
+ initialIsOpen?: boolean
167
+ /**
168
+ * The position of the TanStack logo to open and close the devtools panel.
169
+ * `top-left` | `top-right` | `bottom-left` | `bottom-right` | `relative`
170
+ * Defaults to `bottom-right`.
171
+ */
172
+ buttonPosition?: DevtoolsButtonPosition
173
+ /**
174
+ * The position of the TanStack Query devtools panel.
175
+ * `top` | `bottom` | `left` | `right`
176
+ * Defaults to `bottom`.
177
+ */
178
+ position?: DevtoolsPosition
179
+ /**
180
+ * Custom instance of QueryClient
181
+ */
182
+ client?: QueryClient
183
+ /**
184
+ * Use this so you can define custom errors that can be shown in the devtools.
185
+ */
186
+ errorTypes?: Array<DevtoolsErrorType>
187
+ /**
188
+ * 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.
189
+ */
190
+ styleNonce?: string
191
+ /**
192
+ * Use this so you can attach the devtool's styles to a specific element in the DOM.
193
+ */
194
+ shadowDOMTarget?: ShadowRoot
195
+
196
+ /**
197
+ * Whether the developer tools should load.
198
+ * - `auto`- (Default) Lazily loads devtools when in development mode. Skips loading in production mode.
199
+ * - `true`- Always load the devtools, regardless of the environment.
200
+ * - `false`- Never load the devtools, regardless of the environment.
201
+ *
202
+ * You can use `true` and `false` to override loading developer tools from an environment file.
203
+ * For example, a test environment might run in production mode but you may want to load developer tools.
204
+ *
205
+ * Additionally, you can use a signal in the callback to dynamically load the devtools based on a condition. For example,
206
+ * a signal created from a RxJS observable that listens for a keyboard shortcut.
207
+ *
208
+ * **Example**
209
+ * ```ts
210
+ * withDevtools(() => ({
211
+ * initialIsOpen: true,
212
+ * loadDevtools: inject(ExampleService).loadDevtools()
213
+ * }))
214
+ * ```
215
+ */
216
+ loadDevtools?: 'auto' | boolean
217
+ }
218
+
219
+ /**
220
+ * Enables developer tools.
221
+ *
222
+ * **Example**
223
+ *
224
+ * ```ts
225
+ * export const appConfig: ApplicationConfig = {
226
+ * providers: [
227
+ * provideTanStackQuery(new QueryClient(), withDevtools())
228
+ * ]
229
+ * }
230
+ * ```
231
+ * By default the devtools will be loaded when Angular runs in development mode and rendered in `<body>`.
232
+ *
233
+ * 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.
234
+ *
235
+ * If you need more control over where devtools are rendered, consider `injectDevtoolsPanel`. This allows rendering devtools inside your own devtools for example.
236
+ * @param optionsFn - A function that returns `DevtoolsOptions`.
237
+ * @returns A set of providers for use with `provideTanStackQuery`.
238
+ * @public
239
+ * @see {@link provideTanStackQuery}
240
+ * @see {@link DevtoolsOptions}
241
+ */
242
+ export function withDevtools(
243
+ optionsFn?: () => DevtoolsOptions,
244
+ ): DeveloperToolsFeature {
245
+ let providers: Array<Provider> = []
246
+ if (!isDevMode() && !optionsFn) {
247
+ providers = []
248
+ } else {
249
+ providers = [
250
+ {
251
+ provide: ENVIRONMENT_INITIALIZER,
252
+ multi: true,
253
+ useFactory: () => {
254
+ if (!isPlatformBrowser(inject(PLATFORM_ID))) return () => {}
255
+ const injector = inject(Injector)
256
+ const options = computed(() =>
257
+ runInInjectionContext(injector, () => optionsFn?.() ?? {}),
258
+ )
259
+
260
+ let devtools: TanstackQueryDevtools | null = null
261
+ let el: HTMLElement | null = null
262
+
263
+ const shouldLoadToolsSignal = computed(() => {
264
+ const { loadDevtools } = options()
265
+ return typeof loadDevtools === 'boolean'
266
+ ? loadDevtools
267
+ : isDevMode()
268
+ })
269
+
270
+ const destroyRef = inject(DestroyRef)
271
+
272
+ const getResolvedQueryClient = () => {
273
+ const injectedClient = injector.get(QueryClient, null)
274
+ const client = options().client ?? injectedClient
275
+ if (!client) {
276
+ throw new Error('No QueryClient found')
277
+ }
278
+ return client
279
+ }
280
+
281
+ const destroyDevtools = () => {
282
+ devtools?.unmount()
283
+ el?.remove()
284
+ devtools = null
285
+ }
286
+
287
+ return () =>
288
+ effect(() => {
289
+ const shouldLoadTools = shouldLoadToolsSignal()
290
+ const {
291
+ client,
292
+ position,
293
+ errorTypes,
294
+ buttonPosition,
295
+ initialIsOpen,
296
+ } = options()
297
+
298
+ if (devtools && !shouldLoadTools) {
299
+ destroyDevtools()
300
+ return
301
+ } else if (devtools && shouldLoadTools) {
302
+ client && devtools.setClient(client)
303
+ position && devtools.setPosition(position)
304
+ errorTypes && devtools.setErrorTypes(errorTypes)
305
+ buttonPosition && devtools.setButtonPosition(buttonPosition)
306
+ initialIsOpen && devtools.setInitialIsOpen(initialIsOpen)
307
+ return
308
+ } else if (!shouldLoadTools) {
309
+ return
310
+ }
311
+
312
+ el = document.body.appendChild(document.createElement('div'))
313
+ el.classList.add('tsqd-parent-container')
314
+
315
+ import('@tanstack/query-devtools').then((queryDevtools) =>
316
+ runInInjectionContext(injector, () => {
317
+ devtools = new queryDevtools.TanstackQueryDevtools({
318
+ ...options(),
319
+ client: getResolvedQueryClient(),
320
+ queryFlavor: 'Angular Query',
321
+ version: '5',
322
+ onlineManager,
323
+ })
324
+
325
+ el && devtools.mount(el)
326
+
327
+ // Unmount the devtools on application destroy
328
+ destroyRef.onDestroy(destroyDevtools)
329
+ }),
330
+ )
331
+ })
332
+ },
333
+ },
334
+ ]
335
+ }
336
+ return queryFeature('DeveloperTools', providers)
337
+ }
338
+
339
+ /**
340
+ * A type alias that represents all Query features available for use with `provideTanStackQuery`.
341
+ * Features can be enabled by adding special functions to the `provideTanStackQuery` call.
342
+ * See documentation for each symbol to find corresponding function name. See also `provideTanStackQuery`
343
+ * documentation on how to use those functions.
344
+ * @public
345
+ * @see {@link provideTanStackQuery}
346
+ */
347
+ export type QueryFeatures = DeveloperToolsFeature // Union type of features but just one now
348
+
349
+ export const queryFeatures = ['DeveloperTools'] as const
350
+
351
+ export type QueryFeatureKind = (typeof queryFeatures)[number]
@@ -0,0 +1,125 @@
1
+ import type {
2
+ DataTag,
3
+ DefaultError,
4
+ InitialDataFunction,
5
+ QueryKey,
6
+ } from '@tanstack/query-core'
7
+ import type { CreateQueryOptions, NonUndefinedGuard } from './types'
8
+
9
+ /**
10
+ * @public
11
+ */
12
+ export type UndefinedInitialDataOptions<
13
+ TQueryFnData = unknown,
14
+ TError = DefaultError,
15
+ TData = TQueryFnData,
16
+ TQueryKey extends QueryKey = QueryKey,
17
+ > = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
18
+ initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
19
+ }
20
+
21
+ /**
22
+ * @public
23
+ */
24
+ export type DefinedInitialDataOptions<
25
+ TQueryFnData = unknown,
26
+ TError = DefaultError,
27
+ TData = TQueryFnData,
28
+ TQueryKey extends QueryKey = QueryKey,
29
+ > = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
30
+ initialData:
31
+ | NonUndefinedGuard<TQueryFnData>
32
+ | (() => NonUndefinedGuard<TQueryFnData>)
33
+ }
34
+
35
+ /**
36
+ * Allows to share and re-use query options in a type-safe way.
37
+ *
38
+ * The `queryKey` will be tagged with the type from `queryFn`.
39
+ *
40
+ * **Example**
41
+ *
42
+ * ```ts
43
+ * const { queryKey } = queryOptions({
44
+ * queryKey: ['key'],
45
+ * queryFn: () => Promise.resolve(5),
46
+ * // ^? Promise<number>
47
+ * })
48
+ *
49
+ * const queryClient = new QueryClient()
50
+ * const data = queryClient.getQueryData(queryKey)
51
+ * // ^? number | undefined
52
+ * ```
53
+ * @param options - The query options to tag with the type from `queryFn`.
54
+ * @returns The tagged query options.
55
+ * @public
56
+ */
57
+ export function queryOptions<
58
+ TQueryFnData = unknown,
59
+ TError = DefaultError,
60
+ TData = TQueryFnData,
61
+ TQueryKey extends QueryKey = QueryKey,
62
+ >(
63
+ options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
64
+ ): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & {
65
+ queryKey: DataTag<TQueryKey, TQueryFnData>
66
+ }
67
+
68
+ /**
69
+ * Allows to share and re-use query options in a type-safe way.
70
+ *
71
+ * The `queryKey` will be tagged with the type from `queryFn`.
72
+ *
73
+ * **Example**
74
+ *
75
+ * ```ts
76
+ * const { queryKey } = queryOptions({
77
+ * queryKey: ['key'],
78
+ * queryFn: () => Promise.resolve(5),
79
+ * // ^? Promise<number>
80
+ * })
81
+ *
82
+ * const queryClient = new QueryClient()
83
+ * const data = queryClient.getQueryData(queryKey)
84
+ * // ^? number | undefined
85
+ * ```
86
+ * @param options - The query options to tag with the type from `queryFn`.
87
+ * @returns The tagged query options.
88
+ * @public
89
+ */
90
+ export function queryOptions<
91
+ TQueryFnData = unknown,
92
+ TError = DefaultError,
93
+ TData = TQueryFnData,
94
+ TQueryKey extends QueryKey = QueryKey,
95
+ >(
96
+ options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
97
+ ): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & {
98
+ queryKey: DataTag<TQueryKey, TQueryFnData>
99
+ }
100
+
101
+ /**
102
+ * Allows to share and re-use query options in a type-safe way.
103
+ *
104
+ * The `queryKey` will be tagged with the type from `queryFn`.
105
+ *
106
+ * **Example**
107
+ *
108
+ * ```ts
109
+ * const { queryKey } = queryOptions({
110
+ * queryKey: ['key'],
111
+ * queryFn: () => Promise.resolve(5),
112
+ * // ^? Promise<number>
113
+ * })
114
+ *
115
+ * const queryClient = new QueryClient()
116
+ * const data = queryClient.getQueryData(queryKey)
117
+ * // ^? number | undefined
118
+ * ```
119
+ * @param options - The query options to tag with the type from `queryFn`.
120
+ * @returns The tagged query options.
121
+ * @public
122
+ */
123
+ export function queryOptions(options: unknown) {
124
+ return options
125
+ }