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