@tanstack/angular-query-experimental 5.34.1 → 5.34.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 (51) hide show
  1. package/README.md +19 -2
  2. package/build/README.md +19 -2
  3. package/build/create-base-query.d.ts +1 -1
  4. package/build/esm2022/create-base-query.mjs +3 -3
  5. package/build/esm2022/index.mjs +2 -2
  6. package/build/esm2022/infinite-query-options.mjs +9 -1
  7. package/build/esm2022/inject-infinite-query.mjs +11 -3
  8. package/build/esm2022/inject-is-fetching.mjs +11 -1
  9. package/build/esm2022/inject-is-mutating.mjs +10 -1
  10. package/build/esm2022/inject-mutation-state.mjs +8 -1
  11. package/build/esm2022/inject-mutation.mjs +13 -4
  12. package/build/esm2022/inject-queries.mjs +4 -1
  13. package/build/esm2022/inject-query-client.mjs +21 -3
  14. package/build/esm2022/inject-query.mjs +36 -3
  15. package/build/esm2022/providers.mjs +40 -1
  16. package/build/esm2022/query-options.mjs +23 -1
  17. package/build/esm2022/signal-proxy.mjs +2 -4
  18. package/build/esm2022/types.mjs +1 -1
  19. package/build/fesm2022/tanstack-angular-query-experimental.mjs +179 -14
  20. package/build/fesm2022/tanstack-angular-query-experimental.mjs.map +1 -1
  21. package/build/index.d.ts +1 -1
  22. package/build/infinite-query-options.d.ts +16 -0
  23. package/build/inject-infinite-query.d.ts +27 -3
  24. package/build/inject-is-fetching.d.ts +10 -0
  25. package/build/inject-is-mutating.d.ts +9 -0
  26. package/build/inject-mutation-state.d.ts +10 -0
  27. package/build/inject-mutation.d.ts +10 -1
  28. package/build/inject-queries.d.ts +5 -0
  29. package/build/inject-query-client.d.ts +20 -3
  30. package/build/inject-query.d.ts +102 -3
  31. package/build/providers.d.ts +39 -0
  32. package/build/query-options.d.ts +50 -0
  33. package/build/signal-proxy.d.ts +1 -3
  34. package/build/types.d.ts +51 -0
  35. package/package.json +2 -2
  36. package/src/__tests__/inject-mutation-state.test.ts +0 -2
  37. package/src/create-base-query.ts +2 -2
  38. package/src/index.ts +1 -5
  39. package/src/infinite-query-options.ts +24 -0
  40. package/src/inject-infinite-query.ts +37 -5
  41. package/src/inject-is-fetching.ts +10 -0
  42. package/src/inject-is-mutating.ts +9 -0
  43. package/src/inject-mutation-state.ts +10 -0
  44. package/src/inject-mutation.ts +12 -3
  45. package/src/inject-queries.ts +5 -0
  46. package/src/inject-query-client.ts +21 -3
  47. package/src/inject-query.ts +141 -9
  48. package/src/providers.ts +39 -0
  49. package/src/query-options.ts +72 -0
  50. package/src/signal-proxy.ts +1 -3
  51. package/src/types.ts +51 -0
@@ -1,7 +1,25 @@
1
1
  import { createNoopInjectionToken } from './util/create-injection-token/create-injection-token'
2
2
  import type { QueryClient } from '@tanstack/query-core'
3
3
 
4
- const [injectQueryClient, provideQueryClient, QUERY_CLIENT] =
5
- createNoopInjectionToken<QueryClient>('QueryClientToken')
4
+ const tokens = createNoopInjectionToken<QueryClient>('QueryClientToken')
6
5
 
7
- export { injectQueryClient, provideQueryClient, QUERY_CLIENT }
6
+ /**
7
+ * Injects the `QueryClient` instance into the component or service.
8
+ *
9
+ * **Example**
10
+ * ```ts
11
+ * const queryClient = injectQueryClient();
12
+ * ```
13
+ * @returns The `QueryClient` instance.
14
+ * @public
15
+ */
16
+ export const injectQueryClient = tokens[0]
17
+
18
+ /**
19
+ * Usually {@link provideAngularQuery} is used once to set up TanStack Query and the
20
+ * {@link https://tanstack.com/query/latest/docs/reference/QueryClient|QueryClient}
21
+ * for the entire application. You can use `provideQueryClient` to provide a
22
+ * different `QueryClient` instance for a part of the application.
23
+ * @public
24
+ */
25
+ export const provideQueryClient = tokens[1]
@@ -15,44 +15,176 @@ import type {
15
15
  UndefinedInitialDataOptions,
16
16
  } from './query-options'
17
17
 
18
+ /**
19
+ * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
20
+ *
21
+ * **Basic example**
22
+ * ```ts
23
+ * class ServiceOrComponent {
24
+ * query = injectQuery(() => ({
25
+ * queryKey: ['repoData'],
26
+ * queryFn: () =>
27
+ * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
28
+ * }))
29
+ * }
30
+ * ```
31
+ *
32
+ * **The options function can utilize signals**
33
+ * ```ts
34
+ * class ServiceOrComponent {
35
+ * filter = signal('')
36
+ *
37
+ * todosQuery = injectQuery(() => ({
38
+ * queryKey: ['todos', this.filter()],
39
+ * queryFn: () => fetchTodos(this.filter()),
40
+ * // Signals can be combined with expressions
41
+ * enabled: !!this.filter(),
42
+ * }))
43
+ * }
44
+ * ```
45
+ * @param optionsFn - A function that returns query options.
46
+ * @param injector - The Angular injector to use.
47
+ * @returns The query result.
48
+ * @public
49
+ * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
50
+ */
18
51
  export function injectQuery<
19
52
  TQueryFnData = unknown,
20
53
  TError = DefaultError,
21
54
  TData = TQueryFnData,
22
55
  TQueryKey extends QueryKey = QueryKey,
23
56
  >(
24
- options: (
57
+ optionsFn: (
25
58
  client: QueryClient,
26
- ) => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
59
+ ) => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
27
60
  injector?: Injector,
28
- ): CreateQueryResult<TData, TError>
61
+ ): DefinedCreateQueryResult<TData, TError>
29
62
 
63
+ /**
64
+ * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
65
+ *
66
+ * **Basic example**
67
+ * ```ts
68
+ * class ServiceOrComponent {
69
+ * query = injectQuery(() => ({
70
+ * queryKey: ['repoData'],
71
+ * queryFn: () =>
72
+ * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
73
+ * }))
74
+ * }
75
+ * ```
76
+ *
77
+ * **The options function can utilize signals**
78
+ * ```ts
79
+ * class ServiceOrComponent {
80
+ * filter = signal('')
81
+ *
82
+ * todosQuery = injectQuery(() => ({
83
+ * queryKey: ['todos', this.filter()],
84
+ * queryFn: () => fetchTodos(this.filter()),
85
+ * // Signals can be combined with expressions
86
+ * enabled: !!this.filter(),
87
+ * }))
88
+ * }
89
+ * ```
90
+ * @param optionsFn - A function that returns query options.
91
+ * @param injector - The Angular injector to use.
92
+ * @returns The query result.
93
+ * @public
94
+ * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
95
+ */
30
96
  export function injectQuery<
31
97
  TQueryFnData = unknown,
32
98
  TError = DefaultError,
33
99
  TData = TQueryFnData,
34
100
  TQueryKey extends QueryKey = QueryKey,
35
101
  >(
36
- options: (
102
+ optionsFn: (
37
103
  client: QueryClient,
38
- ) => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
104
+ ) => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
39
105
  injector?: Injector,
40
- ): DefinedCreateQueryResult<TData, TError>
106
+ ): CreateQueryResult<TData, TError>
41
107
 
108
+ /**
109
+ * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
110
+ *
111
+ * **Basic example**
112
+ * ```ts
113
+ * class ServiceOrComponent {
114
+ * query = injectQuery(() => ({
115
+ * queryKey: ['repoData'],
116
+ * queryFn: () =>
117
+ * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
118
+ * }))
119
+ * }
120
+ * ```
121
+ *
122
+ * **The options function can utilize signals**
123
+ * ```ts
124
+ * class ServiceOrComponent {
125
+ * filter = signal('')
126
+ *
127
+ * todosQuery = injectQuery(() => ({
128
+ * queryKey: ['todos', this.filter()],
129
+ * queryFn: () => fetchTodos(this.filter()),
130
+ * // Signals can be combined with expressions
131
+ * enabled: !!this.filter(),
132
+ * }))
133
+ * }
134
+ * ```
135
+ * @param optionsFn - A function that returns query options.
136
+ * @param injector - The Angular injector to use.
137
+ * @returns The query result.
138
+ * @public
139
+ * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
140
+ */
42
141
  export function injectQuery<
43
142
  TQueryFnData = unknown,
44
143
  TError = DefaultError,
45
144
  TData = TQueryFnData,
46
145
  TQueryKey extends QueryKey = QueryKey,
47
146
  >(
48
- options: (
147
+ optionsFn: (
49
148
  client: QueryClient,
50
149
  ) => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
51
150
  injector?: Injector,
52
151
  ): CreateQueryResult<TData, TError>
53
152
 
153
+ /**
154
+ * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
155
+ *
156
+ * **Basic example**
157
+ * ```ts
158
+ * class ServiceOrComponent {
159
+ * query = injectQuery(() => ({
160
+ * queryKey: ['repoData'],
161
+ * queryFn: () =>
162
+ * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
163
+ * }))
164
+ * }
165
+ * ```
166
+ *
167
+ * **The options function can utilize signals**
168
+ * ```ts
169
+ * class ServiceOrComponent {
170
+ * filter = signal('')
171
+ *
172
+ * todosQuery = injectQuery(() => ({
173
+ * queryKey: ['todos', this.filter()],
174
+ * queryFn: () => fetchTodos(this.filter()),
175
+ * // Signals can be combined with expressions
176
+ * enabled: !!this.filter(),
177
+ * }))
178
+ * }
179
+ * ```
180
+ * @param optionsFn - A function that returns query options.
181
+ * @param injector - The Angular injector to use.
182
+ * @returns The query result.
183
+ * @public
184
+ * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
185
+ */
54
186
  export function injectQuery(
55
- options: (client: QueryClient) => CreateQueryOptions,
187
+ optionsFn: (client: QueryClient) => CreateQueryOptions,
56
188
  injector?: Injector,
57
189
  ) {
58
190
  const assertedInjector = assertInjector(injectQuery, injector)
@@ -60,7 +192,7 @@ export function injectQuery(
60
192
  const queryClient = injectQueryClient()
61
193
  return createBaseQuery(
62
194
  (client) =>
63
- runInInjectionContext(assertedInjector, () => options(client)),
195
+ runInInjectionContext(assertedInjector, () => optionsFn(client)),
64
196
  QueryObserver,
65
197
  queryClient,
66
198
  )
package/src/providers.ts CHANGED
@@ -8,6 +8,45 @@ import { provideQueryClient } from './inject-query-client'
8
8
  import type { EnvironmentProviders } from '@angular/core'
9
9
  import type { QueryClient } from '@tanstack/query-core'
10
10
 
11
+ /**
12
+ * Sets up providers necessary to enable TanStack Query functionality for Angular applications.
13
+ *
14
+ * Allows to configure a `QueryClient`.
15
+ *
16
+ * **Example - standalone**
17
+ *
18
+ * ```ts
19
+ * import {
20
+ * provideAngularQuery,
21
+ * QueryClient,
22
+ * } from '@tanstack/angular-query-experimental'
23
+ *
24
+ * bootstrapApplication(AppComponent, {
25
+ * providers: [provideAngularQuery(new QueryClient())],
26
+ * })
27
+ * ```
28
+ *
29
+ * **Example - NgModule-based**
30
+ *
31
+ * ```ts
32
+ * import {
33
+ * provideAngularQuery,
34
+ * QueryClient,
35
+ * } from '@tanstack/angular-query-experimental'
36
+ *
37
+ * @NgModule({
38
+ * declarations: [AppComponent],
39
+ * imports: [BrowserModule],
40
+ * providers: [provideAngularQuery(new QueryClient())],
41
+ * bootstrap: [AppComponent],
42
+ * })
43
+ * export class AppModule {}
44
+ * ```
45
+ * @param queryClient - A `QueryClient` instance.
46
+ * @returns A set of providers to set up TanStack Query.
47
+ * @public
48
+ * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
49
+ */
11
50
  export function provideAngularQuery(
12
51
  queryClient: QueryClient,
13
52
  ): EnvironmentProviders {
@@ -1,6 +1,9 @@
1
1
  import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
2
2
  import type { CreateQueryOptions } from './types'
3
3
 
4
+ /**
5
+ * @public
6
+ */
4
7
  export type UndefinedInitialDataOptions<
5
8
  TQueryFnData = unknown,
6
9
  TError = DefaultError,
@@ -12,6 +15,9 @@ export type UndefinedInitialDataOptions<
12
15
 
13
16
  type NonUndefinedGuard<T> = T extends undefined ? never : T
14
17
 
18
+ /**
19
+ * @public
20
+ */
15
21
  export type DefinedInitialDataOptions<
16
22
  TQueryFnData = unknown,
17
23
  TError = DefaultError,
@@ -23,6 +29,28 @@ export type DefinedInitialDataOptions<
23
29
  | (() => NonUndefinedGuard<TQueryFnData>)
24
30
  }
25
31
 
32
+ /**
33
+ * Allows to share and re-use query options in a type-safe way.
34
+ *
35
+ * The `queryKey` will be tagged with the type from `queryFn`.
36
+ *
37
+ * **Example**
38
+ *
39
+ * ```ts
40
+ * const { queryKey } = queryOptions({
41
+ * queryKey: ['key'],
42
+ * queryFn: () => Promise.resolve(5),
43
+ * // ^? Promise<number>
44
+ * })
45
+ *
46
+ * const queryClient = new QueryClient()
47
+ * const data = queryClient.getQueryData(queryKey)
48
+ * // ^? number | undefined
49
+ * ```
50
+ * @param options - The query options to tag with the type from `queryFn`.
51
+ * @returns The tagged query options.
52
+ * @public
53
+ */
26
54
  export function queryOptions<
27
55
  TQueryFnData = unknown,
28
56
  TError = DefaultError,
@@ -34,6 +62,28 @@ export function queryOptions<
34
62
  queryKey: DataTag<TQueryKey, TQueryFnData>
35
63
  }
36
64
 
65
+ /**
66
+ * Allows to share and re-use query options in a type-safe way.
67
+ *
68
+ * The `queryKey` will be tagged with the type from `queryFn`.
69
+ *
70
+ * **Example**
71
+ *
72
+ * ```ts
73
+ * const { queryKey } = queryOptions({
74
+ * queryKey: ['key'],
75
+ * queryFn: () => Promise.resolve(5),
76
+ * // ^? Promise<number>
77
+ * })
78
+ *
79
+ * const queryClient = new QueryClient()
80
+ * const data = queryClient.getQueryData(queryKey)
81
+ * // ^? number | undefined
82
+ * ```
83
+ * @param options - The query options to tag with the type from `queryFn`.
84
+ * @returns The tagged query options.
85
+ * @public
86
+ */
37
87
  export function queryOptions<
38
88
  TQueryFnData = unknown,
39
89
  TError = DefaultError,
@@ -45,6 +95,28 @@ export function queryOptions<
45
95
  queryKey: DataTag<TQueryKey, TQueryFnData>
46
96
  }
47
97
 
98
+ /**
99
+ * Allows to share and re-use query options in a type-safe way.
100
+ *
101
+ * The `queryKey` will be tagged with the type from `queryFn`.
102
+ *
103
+ * **Example**
104
+ *
105
+ * ```ts
106
+ * const { queryKey } = queryOptions({
107
+ * queryKey: ['key'],
108
+ * queryFn: () => Promise.resolve(5),
109
+ * // ^? Promise<number>
110
+ * })
111
+ *
112
+ * const queryClient = new QueryClient()
113
+ * const data = queryClient.getQueryData(queryKey)
114
+ * // ^? number | undefined
115
+ * ```
116
+ * @param options - The query options to tag with the type from `queryFn`.
117
+ * @returns The tagged query options.
118
+ * @public
119
+ */
48
120
  export function queryOptions(options: unknown) {
49
121
  return options
50
122
  }
@@ -7,11 +7,9 @@ export type MapToSignals<T> = {
7
7
 
8
8
  /**
9
9
  * Exposes fields of an object passed via an Angular `Signal` as `Computed` signals.
10
- *
11
10
  * Functions on the object are passed through as-is.
12
- *
13
11
  * @param inputSignal - `Signal` that must return an object.
14
- *
12
+ * @returns A proxy object with the same fields as the input object, but with each field wrapped in a `Computed` signal.
15
13
  */
16
14
  export function signalProxy<TInput extends Record<string | symbol, any>>(
17
15
  inputSignal: Signal<TInput>,
package/src/types.ts CHANGED
@@ -16,6 +16,9 @@ import type {
16
16
  } from '@tanstack/query-core'
17
17
  import type { MapToSignals } from './signal-proxy'
18
18
 
19
+ /**
20
+ * @public
21
+ */
19
22
  export interface CreateBaseQueryOptions<
20
23
  TQueryFnData = unknown,
21
24
  TError = DefaultError,
@@ -30,6 +33,9 @@ export interface CreateBaseQueryOptions<
30
33
  TQueryKey
31
34
  > {}
32
35
 
36
+ /**
37
+ * @public
38
+ */
33
39
  export interface CreateQueryOptions<
34
40
  TQueryFnData = unknown,
35
41
  TError = DefaultError,
@@ -46,12 +52,18 @@ export interface CreateQueryOptions<
46
52
  'suspense'
47
53
  > {}
48
54
 
55
+ /**
56
+ * @public
57
+ */
49
58
  type CreateStatusBasedQueryResult<
50
59
  TStatus extends QueryObserverResult['status'],
51
60
  TData = unknown,
52
61
  TError = DefaultError,
53
62
  > = Extract<QueryObserverResult<TData, TError>, { status: TStatus }>
54
63
 
64
+ /**
65
+ * @public
66
+ */
55
67
  export interface BaseQueryNarrowing<TData = unknown, TError = DefaultError> {
56
68
  isSuccess: (
57
69
  this: CreateBaseQueryResult<TData, TError>,
@@ -76,6 +88,9 @@ export interface BaseQueryNarrowing<TData = unknown, TError = DefaultError> {
76
88
  >
77
89
  }
78
90
 
91
+ /**
92
+ * @public
93
+ */
79
94
  export interface CreateInfiniteQueryOptions<
80
95
  TQueryFnData = unknown,
81
96
  TError = DefaultError,
@@ -95,6 +110,9 @@ export interface CreateInfiniteQueryOptions<
95
110
  'suspense'
96
111
  > {}
97
112
 
113
+ /**
114
+ * @public
115
+ */
98
116
  export type CreateBaseQueryResult<
99
117
  TData = unknown,
100
118
  TError = DefaultError,
@@ -102,22 +120,34 @@ export type CreateBaseQueryResult<
102
120
  > = BaseQueryNarrowing<TData, TError> &
103
121
  MapToSignals<OmitKeyof<TState, keyof BaseQueryNarrowing, 'safely'>>
104
122
 
123
+ /**
124
+ * @public
125
+ */
105
126
  export type CreateQueryResult<
106
127
  TData = unknown,
107
128
  TError = DefaultError,
108
129
  > = CreateBaseQueryResult<TData, TError>
109
130
 
131
+ /**
132
+ * @public
133
+ */
110
134
  export type DefinedCreateQueryResult<
111
135
  TData = unknown,
112
136
  TError = DefaultError,
113
137
  TDefinedQueryObserver = DefinedQueryObserverResult<TData, TError>,
114
138
  > = MapToSignals<TDefinedQueryObserver>
115
139
 
140
+ /**
141
+ * @public
142
+ */
116
143
  export type CreateInfiniteQueryResult<
117
144
  TData = unknown,
118
145
  TError = DefaultError,
119
146
  > = MapToSignals<InfiniteQueryObserverResult<TData, TError>>
120
147
 
148
+ /**
149
+ * @public
150
+ */
121
151
  export type DefinedCreateInfiniteQueryResult<
122
152
  TData = unknown,
123
153
  TError = DefaultError,
@@ -127,6 +157,9 @@ export type DefinedCreateInfiniteQueryResult<
127
157
  >,
128
158
  > = MapToSignals<TDefinedInfiniteQueryObserver>
129
159
 
160
+ /**
161
+ * @public
162
+ */
130
163
  export interface CreateMutationOptions<
131
164
  TData = unknown,
132
165
  TError = DefaultError,
@@ -137,6 +170,9 @@ export interface CreateMutationOptions<
137
170
  '_defaulted'
138
171
  > {}
139
172
 
173
+ /**
174
+ * @public
175
+ */
140
176
  export type CreateMutateFunction<
141
177
  TData = unknown,
142
178
  TError = DefaultError,
@@ -146,6 +182,9 @@ export type CreateMutateFunction<
146
182
  ...args: Parameters<MutateFunction<TData, TError, TVariables, TContext>>
147
183
  ) => void
148
184
 
185
+ /**
186
+ * @public
187
+ */
149
188
  export type CreateMutateAsyncFunction<
150
189
  TData = unknown,
151
190
  TError = DefaultError,
@@ -153,6 +192,9 @@ export type CreateMutateAsyncFunction<
153
192
  TContext = unknown,
154
193
  > = MutateFunction<TData, TError, TVariables, TContext>
155
194
 
195
+ /**
196
+ * @public
197
+ */
156
198
  export type CreateBaseMutationResult<
157
199
  TData = unknown,
158
200
  TError = DefaultError,
@@ -165,6 +207,9 @@ export type CreateBaseMutationResult<
165
207
  mutateAsync: CreateMutateAsyncFunction<TData, TError, TVariables, TContext>
166
208
  }
167
209
 
210
+ /**
211
+ * @public
212
+ */
168
213
  type CreateStatusBasedMutationResult<
169
214
  TStatus extends CreateBaseMutationResult['status'],
170
215
  TData = unknown,
@@ -176,6 +221,9 @@ type CreateStatusBasedMutationResult<
176
221
  { status: TStatus }
177
222
  >
178
223
 
224
+ /**
225
+ * @public
226
+ */
179
227
  export interface BaseMutationNarrowing<
180
228
  TData = unknown,
181
229
  TError = DefaultError,
@@ -238,6 +286,9 @@ export interface BaseMutationNarrowing<
238
286
  >
239
287
  }
240
288
 
289
+ /**
290
+ * @public
291
+ */
241
292
  export type CreateMutationResult<
242
293
  TData = unknown,
243
294
  TError = DefaultError,