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