@tanstack/svelte-query 6.3.1 → 6.3.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 (66) hide show
  1. package/dist/containers.svelte.d.ts +16 -0
  2. package/dist/containers.svelte.d.ts.map +1 -1
  3. package/dist/containers.svelte.js +26 -0
  4. package/dist/context.d.ts +10 -6
  5. package/dist/context.d.ts.map +1 -1
  6. package/dist/context.js +10 -6
  7. package/dist/createBaseQuery.svelte.d.ts +1 -0
  8. package/dist/createBaseQuery.svelte.d.ts.map +1 -1
  9. package/dist/createBaseQuery.svelte.js +6 -0
  10. package/dist/createInfiniteQuery.d.ts +6 -11
  11. package/dist/createInfiniteQuery.d.ts.map +1 -1
  12. package/dist/createMutation.svelte.d.ts +2 -8
  13. package/dist/createMutation.svelte.d.ts.map +1 -1
  14. package/dist/createMutation.svelte.js +2 -8
  15. package/dist/createQueries.svelte.d.ts +0 -3
  16. package/dist/createQueries.svelte.d.ts.map +1 -1
  17. package/dist/createQueries.svelte.js +4 -3
  18. package/dist/createQuery.d.ts +6 -12
  19. package/dist/createQuery.d.ts.map +1 -1
  20. package/dist/infiniteQueryOptions.d.ts +10 -6
  21. package/dist/infiniteQueryOptions.d.ts.map +1 -1
  22. package/dist/mutationOptions.d.ts +2 -6
  23. package/dist/mutationOptions.d.ts.map +1 -1
  24. package/dist/queryOptions.d.ts +10 -6
  25. package/dist/queryOptions.d.ts.map +1 -1
  26. package/dist/types.d.ts +37 -0
  27. package/dist/types.d.ts.map +1 -1
  28. package/dist/useHydrate.d.ts +0 -2
  29. package/dist/useHydrate.d.ts.map +1 -1
  30. package/dist/useHydrate.js +0 -2
  31. package/dist/useIsFetching.svelte.d.ts +0 -3
  32. package/dist/useIsFetching.svelte.d.ts.map +1 -1
  33. package/dist/useIsFetching.svelte.js +0 -3
  34. package/dist/useIsMutating.svelte.d.ts +0 -2
  35. package/dist/useIsMutating.svelte.d.ts.map +1 -1
  36. package/dist/useIsMutating.svelte.js +0 -2
  37. package/dist/useIsRestoring.d.ts +0 -1
  38. package/dist/useIsRestoring.d.ts.map +1 -1
  39. package/dist/useIsRestoring.js +0 -1
  40. package/dist/useMutationState.svelte.d.ts +0 -4
  41. package/dist/useMutationState.svelte.d.ts.map +1 -1
  42. package/dist/useMutationState.svelte.js +7 -4
  43. package/dist/useQueryClient.d.ts +2 -2
  44. package/dist/useQueryClient.js +2 -2
  45. package/dist/utils.svelte.d.ts +8 -0
  46. package/dist/utils.svelte.d.ts.map +1 -1
  47. package/dist/utils.svelte.js +13 -0
  48. package/package.json +4 -4
  49. package/src/containers.svelte.ts +35 -1
  50. package/src/context.ts +10 -6
  51. package/src/createBaseQuery.svelte.ts +6 -0
  52. package/src/createInfiniteQuery.ts +6 -11
  53. package/src/createMutation.svelte.ts +2 -8
  54. package/src/createQueries.svelte.ts +4 -3
  55. package/src/createQuery.ts +6 -12
  56. package/src/infiniteQueryOptions.ts +10 -6
  57. package/src/mutationOptions.ts +2 -6
  58. package/src/queryOptions.ts +12 -7
  59. package/src/types.ts +37 -0
  60. package/src/useHydrate.ts +0 -2
  61. package/src/useIsFetching.svelte.ts +0 -3
  62. package/src/useIsMutating.svelte.ts +0 -2
  63. package/src/useIsRestoring.ts +0 -1
  64. package/src/useMutationState.svelte.ts +7 -4
  65. package/src/useQueryClient.ts +2 -2
  66. package/src/utils.svelte.ts +13 -0
package/src/context.ts CHANGED
@@ -7,9 +7,8 @@ const _contextKey = Symbol('QueryClient')
7
7
  /**
8
8
  * Retrieves the `QueryClient` set on Svelte's context by `QueryClientProvider` (or by
9
9
  * {@link setQueryClientContext} directly). This is what {@link useQueryClient} calls internally.
10
- *
11
- * @throws If no `QueryClient` was found in context.
12
10
  * @returns The `QueryClient` set on context, whether by `QueryClientProvider` or {@link setQueryClientContext}.
11
+ * @throws {Error} If no `QueryClient` was found in context.
13
12
  */
14
13
  export const getQueryClientContext = (): QueryClient => {
15
14
  const client = getContext<QueryClient | undefined>(_contextKey)
@@ -26,9 +25,7 @@ export const getQueryClientContext = (): QueryClient => {
26
25
  * Sets a `QueryClient` on Svelte's context, so it can be read with {@link getQueryClientContext} (or
27
26
  * {@link useQueryClient}) from any descendant component. `QueryClientProvider` wraps this — use it directly
28
27
  * only if you need to set the client from your own component instead.
29
- *
30
28
  * @param client - The `QueryClient` to make available to descendant components.
31
- *
32
29
  * @example
33
30
  * ```svelte
34
31
  * <script lang="ts">
@@ -48,7 +45,11 @@ export const setQueryClientContext = (client: QueryClient): void => {
48
45
 
49
46
  const _isRestoringContextKey = Symbol('isRestoring')
50
47
 
51
- /** Retrieves a `isRestoring` from Svelte's context */
48
+ /**
49
+ * Retrieves a `isRestoring` from Svelte's context.
50
+ * @returns The `isRestoring` box set on context, or a box holding `false` if none was set or the
51
+ * context is unavailable.
52
+ */
52
53
  export const getIsRestoringContext = (): Box<boolean> => {
53
54
  try {
54
55
  const isRestoring = getContext<Box<boolean> | undefined>(
@@ -60,7 +61,10 @@ export const getIsRestoringContext = (): Box<boolean> => {
60
61
  }
61
62
  }
62
63
 
63
- /** Sets a `isRestoring` on Svelte's context */
64
+ /**
65
+ * Sets a `isRestoring` on Svelte's context.
66
+ * @param isRestoring - The box holding whether the cache is being restored, e.g. by a persister.
67
+ */
64
68
  export const setIsRestoringContext = (isRestoring: Box<boolean>): void => {
65
69
  setContext(_isRestoringContextKey, isRestoring)
66
70
  }
@@ -15,6 +15,7 @@ import type {
15
15
  * @param options - A function that returns query options
16
16
  * @param Observer - The observer from query-core
17
17
  * @param queryClient - Custom query client which overrides provider
18
+ * @returns The reactive query result.
18
19
  */
19
20
  export function createBaseQuery<
20
21
  TQueryFnData,
@@ -61,6 +62,11 @@ export function createBaseQuery<
61
62
  },
62
63
  )
63
64
 
65
+ /**
66
+ * Computes the observer's current result for the resolved options, tracking property access unless
67
+ * `notifyOnChangeProps` is set.
68
+ * @returns The current query result.
69
+ */
64
70
  function createResult() {
65
71
  const result = observer.getOptimisticResult(resolvedOptions)
66
72
  return !resolvedOptions.notifyOnChangeProps
@@ -23,15 +23,14 @@ import type {
23
23
  * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
24
24
  *
25
25
  * This overload is selected when `initialData` is known to be defined.
26
- *
27
- * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
28
- * like `queryClient.infiniteQuery`.
29
26
  * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to
30
27
  * `createInfiniteQuery`, with `initialData` set, wrapped in an {@link Accessor} so options can be reactive.
31
28
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
32
29
  * be used.
33
30
  * @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
34
31
  * to page through the query.
32
+ * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
33
+ * like `queryClient.infiniteQuery`.
35
34
  */
36
35
  export function createInfiniteQuery<
37
36
  TQueryFnData = unknown,
@@ -57,15 +56,14 @@ export function createInfiniteQuery<
57
56
  * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
58
57
  *
59
58
  * This overload is selected when `initialData` is omitted or may be `undefined`.
60
- *
61
- * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
62
- * like `queryClient.infiniteQuery`.
63
59
  * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to
64
60
  * `createInfiniteQuery`, wrapped in an {@link Accessor} so options can be reactive.
65
61
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
66
62
  * be used.
67
63
  * @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
68
64
  * to page through the query.
65
+ * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
66
+ * like `queryClient.infiniteQuery`.
69
67
  */
70
68
  export function createInfiniteQuery<
71
69
  TQueryFnData = unknown,
@@ -89,16 +87,14 @@ export function createInfiniteQuery<
89
87
  /**
90
88
  * The options for `createInfiniteQuery` are identical to `createQuery`, with the addition of
91
89
  * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
92
- *
93
- * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
94
- * like `queryClient.infiniteQuery`.
95
90
  * @param options - The {@link CreateInfiniteQueryOptions} to use — everything you can pass to
96
91
  * `createInfiniteQuery`, wrapped in an {@link Accessor} so options can be reactive.
97
92
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
98
93
  * be used.
99
94
  * @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
100
95
  * to page through the query.
101
- *
96
+ * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
97
+ * like `queryClient.infiniteQuery`.
102
98
  * @example
103
99
  * Fetching the next page from a "Load More" button click:
104
100
  * ```svelte
@@ -137,7 +133,6 @@ export function createInfiniteQuery<
137
133
  * </button>
138
134
  * {/if}
139
135
  * ```
140
- *
141
136
  * @example
142
137
  * Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
143
138
  * sentinel element after the list:
@@ -13,9 +13,6 @@ import type { DefaultError, QueryClient } from '@tanstack/query-core'
13
13
  /**
14
14
  * Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
15
15
  * `createMutation` is the function for that.
16
- *
17
- * @see {@link mutationOptions} to share these options across multiple `createMutation` call sites, or to look
18
- * the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
19
16
  * @param options - The {@link CreateMutationOptions} to use, wrapped in an {@link Accessor} so options can be
20
17
  * reactive.
21
18
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
@@ -24,7 +21,8 @@ import type { DefaultError, QueryClient } from '@tanstack/query-core'
24
21
  * argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared
25
22
  * mutation definition. If you make multiple requests, `onSuccess` will fire only after the latest call you've
26
23
  * made.
27
- *
24
+ * @see {@link mutationOptions} to share these options across multiple `createMutation` call sites, or to look
25
+ * the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
28
26
  * @example
29
27
  * ```svelte
30
28
  * <script lang="ts">
@@ -48,7 +46,6 @@ import type { DefaultError, QueryClient } from '@tanstack/query-core'
48
46
  * Add
49
47
  * </button>
50
48
  * ```
51
- *
52
49
  * @example
53
50
  * Rendering the mutation's own state, rather than just firing it off:
54
51
  * ```svelte
@@ -72,7 +69,6 @@ import type { DefaultError, QueryClient } from '@tanstack/query-core'
72
69
  * <button onclick={() => addMutation.mutate('Item')}>Add</button>
73
70
  * {/if}
74
71
  * ```
75
- *
76
72
  * @example
77
73
  * Optimistic update via `onMutate`, rolling back on `onError`:
78
74
  * ```svelte
@@ -106,7 +102,6 @@ import type { DefaultError, QueryClient } from '@tanstack/query-core'
106
102
  *
107
103
  * <button onclick={() => addMutation.mutate('Item')}>Add</button>
108
104
  * ```
109
- *
110
105
  * @example
111
106
  * Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
112
107
  * promise per call instead, so you can wait for all of them when they succeed:
@@ -134,7 +129,6 @@ import type { DefaultError, QueryClient } from '@tanstack/query-core'
134
129
  * Add all
135
130
  * </button>
136
131
  * ```
137
- *
138
132
  * @example
139
133
  * If some of the mutations above can fail independently of the others, and you want to know which ones
140
134
  * did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
@@ -188,14 +188,12 @@ export type QueriesResults<
188
188
 
189
189
  /**
190
190
  * The `createQueries` function can be used to fetch a variable number of queries.
191
- *
192
191
  * @param createQueriesOptions - The `queries` array to run, and an optional `combine` function, wrapped in an
193
192
  * {@link Accessor} so options can be reactive.
194
193
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context
195
194
  * will be used.
196
195
  * @returns An array with one result per query, in the same order as `queries` — or, if `combine` is provided,
197
196
  * whatever `combine` returns.
198
- *
199
197
  * @example
200
198
  * ```svelte
201
199
  * <script lang="ts">
@@ -224,7 +222,6 @@ export type QueriesResults<
224
222
  * {/each}
225
223
  * </ul>
226
224
  * ```
227
- *
228
225
  * @example
229
226
  * Combining results into a single value:
230
227
  * ```svelte
@@ -297,6 +294,10 @@ export function createQueries<
297
294
  ),
298
295
  )
299
296
 
297
+ /**
298
+ * Computes the combined result of the observer's queries for the resolved options.
299
+ * @returns The combined result, built from the tracked per-query results.
300
+ */
300
301
  function createResult() {
301
302
  const [_, getCombinedResult, trackResult] = observer.getOptimisticResult(
302
303
  resolvedQueryOptions,
@@ -18,8 +18,6 @@ import type {
18
18
  *
19
19
  * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
20
20
  * a `select` changes `TData` to include `undefined`).
21
- *
22
- * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
23
21
  * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
24
22
  * with `initialData` set, wrapped in an {@link Accessor} so options can be reactive.
25
23
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
@@ -27,7 +25,7 @@ import type {
27
25
  * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt
28
26
  * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,
29
27
  * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.
30
- *
28
+ * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
31
29
  * @example
32
30
  * ```svelte
33
31
  * <script lang="ts">
@@ -67,8 +65,6 @@ export function createQuery<
67
65
  /**
68
66
  * Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
69
67
  * The query runs when the options call for it — `enabled: false` skips the initial fetch.
70
- *
71
- * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
72
68
  * @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
73
69
  * wrapped in an {@link Accessor} so options can be reactive.
74
70
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
@@ -76,7 +72,7 @@ export function createQuery<
76
72
  * @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
77
73
  * the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`
78
74
  * are derived booleans for convenience.
79
- *
75
+ * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
80
76
  * @example
81
77
  * ```svelte
82
78
  * <script lang="ts">
@@ -100,7 +96,6 @@ export function createQuery<
100
96
  * </ul>
101
97
  * {/if}
102
98
  * ```
103
- *
104
99
  * @example
105
100
  * The same query, checking `isPending`/`isError` instead of `status` — pick whichever reads better to you:
106
101
  * ```svelte
@@ -139,7 +134,9 @@ export function createQuery<
139
134
  ): CreateQueryResult<TData, TError>
140
135
 
141
136
  /**
142
- * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
137
+ * Fallback overload for options whose `initialData` presence isn't statically known — for example, an
138
+ * object typed as {@link CreateQueryOptions} rather than an object literal. Prefer one of the other
139
+ * overloads when possible, since they infer whether `data` can be `undefined` from `initialData` directly.
143
140
  * @param options - The {@link CreateQueryOptions} to use — everything you can pass to `createQuery`, wrapped
144
141
  * in an {@link Accessor} so options can be reactive.
145
142
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
@@ -147,7 +144,7 @@ export function createQuery<
147
144
  * @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
148
145
  * the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`
149
146
  * are derived booleans for convenience.
150
- *
147
+ * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
151
148
  * @example
152
149
  * `select` derives whatever `data` a component needs from the cached value, without changing what's
153
150
  * actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:
@@ -170,7 +167,6 @@ export function createQuery<
170
167
  * <span>{query.data} posts</span>
171
168
  * {/if}
172
169
  * ```
173
- *
174
170
  * @example
175
171
  * A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the
176
172
  * loading state doesn't show while the query is disabled:
@@ -197,7 +193,6 @@ export function createQuery<
197
193
  * <h1>{query.data?.title}</h1>
198
194
  * {/if}
199
195
  * ```
200
- *
201
196
  * @example
202
197
  * Seeding a detail query from an already-cached list, to skip the loading state:
203
198
  * ```svelte
@@ -223,7 +218,6 @@ export function createQuery<
223
218
  * {/if}
224
219
  * <h1>{query.data?.title}</h1>
225
220
  * ```
226
- *
227
221
  * @example
228
222
  * Paginated data, keeping the previous page's data visible while the next page loads:
229
223
  * ```svelte
@@ -8,6 +8,10 @@ import type {
8
8
  } from '@tanstack/query-core'
9
9
  import type { CreateInfiniteQueryOptions } from './types.js'
10
10
 
11
+ /**
12
+ * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is omitted or may
13
+ * be `undefined` — `data` may be `undefined` while the query is `pending`.
14
+ */
11
15
  export type UndefinedInitialDataInfiniteOptions<
12
16
  TQueryFnData = unknown,
13
17
  TError = DefaultError,
@@ -29,6 +33,10 @@ export type UndefinedInitialDataInfiniteOptions<
29
33
  >
30
34
  }
31
35
 
36
+ /**
37
+ * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is known to be
38
+ * defined — `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`).
39
+ */
32
40
  export type DefinedInitialDataInfiniteOptions<
33
41
  TQueryFnData = unknown,
34
42
  TError = DefaultError,
@@ -53,12 +61,10 @@ export type DefinedInitialDataInfiniteOptions<
53
61
  * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
54
62
  *
55
63
  * This overload is selected when `initialData` is known to be defined.
56
- *
57
- * @see {@link createInfiniteQuery} to run an infinite query with these options.
58
64
  * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to
59
65
  * `createInfiniteQuery`, with `initialData` set.
60
66
  * @returns The same options object, typed so that `queryKey` carries the inferred data type.
61
- *
67
+ * @see {@link createInfiniteQuery} to run an infinite query with these options.
62
68
  * @example
63
69
  * `initialData` skips the loading state on first render — even if a refetch fails, the list stays
64
70
  * visible alongside the error:
@@ -116,12 +122,10 @@ export function infiniteQueryOptions<
116
122
  * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `createInfiniteQuery`.
117
123
  * These options can be shared across `createInfiniteQuery` calls and imperative APIs such as
118
124
  * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
119
- *
120
- * @see {@link createInfiniteQuery} to run an infinite query with these options.
121
125
  * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to
122
126
  * `createInfiniteQuery`.
123
127
  * @returns The same options object, typed so that `queryKey` carries the inferred data type.
124
- *
128
+ * @see {@link createInfiniteQuery} to run an infinite query with these options.
125
129
  * @example
126
130
  * A parameterized factory, so the same options object can be reused per `postId`:
127
131
  * ```svelte
@@ -5,11 +5,9 @@ import type { CreateMutationOptions } from './types.js'
5
5
  * You can generally pass everything to `mutationOptions` that you can also pass to `createMutation`. This
6
6
  * overload requires `mutationKey`, so the resulting options can be looked up elsewhere (e.g. with
7
7
  * `useMutationState`).
8
- *
9
- * @see {@link createMutation} to run a mutation with these options.
10
8
  * @param options - The options to use — everything you can pass to `createMutation`, with `mutationKey` set.
11
9
  * @returns The same options object.
12
- *
10
+ * @see {@link createMutation} to run a mutation with these options.
13
11
  * @example
14
12
  * Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
15
13
  * ```svelte
@@ -48,11 +46,9 @@ export function mutationOptions<
48
46
 
49
47
  /**
50
48
  * You can generally pass everything to `mutationOptions` that you can also pass to `createMutation`.
51
- *
52
- * @see {@link createMutation} to run a mutation with these options.
53
49
  * @param options - The options to use — everything you can pass to `createMutation`.
54
50
  * @returns The same options object.
55
- *
51
+ * @see {@link createMutation} to run a mutation with these options.
56
52
  * @example
57
53
  * ```svelte
58
54
  * <script lang="ts">
@@ -7,6 +7,10 @@ import type {
7
7
  } from '@tanstack/query-core'
8
8
  import type { CreateQueryOptions } from './types.js'
9
9
 
10
+ /**
11
+ * The options accepted by the `queryOptions` overload selected when `initialData` is omitted or may be
12
+ * `undefined` — `data` may be `undefined` while the query is `pending`.
13
+ */
10
14
  export type UndefinedInitialDataOptions<
11
15
  TQueryFnData = unknown,
12
16
  TError = DefaultError,
@@ -19,6 +23,10 @@ export type UndefinedInitialDataOptions<
19
23
  | NonUndefinedGuard<TQueryFnData>
20
24
  }
21
25
 
26
+ /**
27
+ * The options accepted by the `queryOptions` overload selected when `initialData` is set — `data` is never
28
+ * `undefined` (unless a `select` changes `TData` to include `undefined`).
29
+ */
22
30
  export type DefinedInitialDataOptions<
23
31
  TQueryFnData = unknown,
24
32
  TError = DefaultError,
@@ -26,7 +34,8 @@ export type DefinedInitialDataOptions<
26
34
  TQueryKey extends QueryKey = QueryKey,
27
35
  > = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
28
36
  initialData:
29
- NonUndefinedGuard<TQueryFnData> | (() => NonUndefinedGuard<TQueryFnData>)
37
+ | NonUndefinedGuard<TQueryFnData>
38
+ | (() => NonUndefinedGuard<TQueryFnData>)
30
39
  }
31
40
 
32
41
  /**
@@ -36,12 +45,10 @@ export type DefinedInitialDataOptions<
36
45
  *
37
46
  * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
38
47
  * a `select` changes `TData` to include `undefined`).
39
- *
40
- * @see {@link createQuery} to run a query with these options.
41
48
  * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
42
49
  * with `initialData` set.
43
50
  * @returns The same options object, typed so that `queryKey` carries the inferred data type.
44
- *
51
+ * @see {@link createQuery} to run a query with these options.
45
52
  * @example
46
53
  * ```svelte
47
54
  * <script lang="ts">
@@ -82,11 +89,9 @@ export function queryOptions<
82
89
  * You can generally pass everything to `queryOptions` that you can also pass to `createQuery`. These options
83
90
  * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
84
91
  * is required and is the query key to generate options for.
85
- *
86
- * @see {@link createQuery} to run a query with these options.
87
92
  * @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `createQuery`.
88
93
  * @returns The same options object, typed so that `queryKey` carries the inferred data type.
89
- *
94
+ * @see {@link createQuery} to run a query with these options.
90
95
  * @example
91
96
  * A parameterized factory, so the same options object can be reused per `id`:
92
97
  * ```svelte
package/src/types.ts CHANGED
@@ -19,6 +19,10 @@ import type {
19
19
  QueryObserverResult,
20
20
  } from '@tanstack/query-core'
21
21
 
22
+ /**
23
+ * A function that returns a value. Options passed as an accessor are read inside reactive contexts, so they
24
+ * update when the state they read changes.
25
+ */
22
26
  export type Accessor<T> = () => T
23
27
 
24
28
  /** Options for createBaseQuery */
@@ -100,6 +104,11 @@ export type CreateMutationOptions<
100
104
  '_defaulted'
101
105
  >
102
106
 
107
+ /**
108
+ * The type of `mutate`, as returned by `createMutation`. Forwards the variables (and an optional per-call
109
+ * `onSuccess`/`onError`/`onSettled`) to the underlying `mutate` call. Fire-and-forget — errors are surfaced
110
+ * through the mutation result, not thrown.
111
+ */
103
112
  export type CreateMutateFunction<
104
113
  TData = unknown,
105
114
  TError = DefaultError,
@@ -111,6 +120,10 @@ export type CreateMutateFunction<
111
120
  >
112
121
  ) => void
113
122
 
123
+ /**
124
+ * The type of `mutateAsync`, as returned by `createMutation`. Similar to {@link CreateMutateFunction}, but
125
+ * returns a promise which can be awaited.
126
+ */
114
127
  export type CreateMutateAsyncFunction<
115
128
  TData = unknown,
116
129
  TError = DefaultError,
@@ -118,6 +131,11 @@ export type CreateMutateAsyncFunction<
118
131
  TOnMutateResult = unknown,
119
132
  > = MutateFunction<TData, TError, TVariables, TOnMutateResult>
120
133
 
134
+ /**
135
+ * The result of `createMutation`. Same as {@link MutationObserverResult} from `@tanstack/query-core`, with
136
+ * `mutate` narrowed to the fire-and-forget {@link CreateMutateFunction} signature, plus the added
137
+ * `mutateAsync`.
138
+ */
121
139
  export type CreateBaseMutationResult<
122
140
  TData = unknown,
123
141
  TError = DefaultError,
@@ -143,6 +161,10 @@ export type CreateMutationResult<
143
161
  TOnMutateResult = unknown,
144
162
  > = CreateBaseMutationResult<TData, TError, TVariables, TOnMutateResult>
145
163
 
164
+ /**
165
+ * Infers the `Mutation` type passed to `useMutationState`'s `select` from the `MutationState` type it
166
+ * returns, falling back to `Mutation` otherwise.
167
+ */
146
168
  export type MutationTypeFromResult<TResult> = [TResult] extends [
147
169
  MutationState<
148
170
  infer TData,
@@ -160,11 +182,26 @@ export type MutationStateOptions<
160
182
  TMutation extends Mutation<any, any, any, any> =
161
183
  MutationTypeFromResult<TResult>,
162
184
  > = {
185
+ /**
186
+ * The filters that select the mutations to return the state of.
187
+ */
163
188
  filters?: MutationFilters
189
+ /**
190
+ * Maps each matching mutation to the value returned for it. Defaults to the mutation's `state`.
191
+ */
164
192
  select?: (mutation: TMutation) => TResult
165
193
  }
166
194
 
195
+ /**
196
+ * The props accepted by `QueryClientProvider`.
197
+ */
167
198
  export type QueryClientProviderProps = {
199
+ /**
200
+ * The `QueryClient` to provide to the children.
201
+ */
168
202
  client: QueryClient
203
+ /**
204
+ * The children that can use the provided `QueryClient`.
205
+ */
169
206
  children: Snippet
170
207
  }
package/src/useHydrate.ts CHANGED
@@ -7,12 +7,10 @@ import type { HydrateOptions, QueryClient } from '@tanstack/query-core'
7
7
  * passed explicitly). If the client already contains data, the new queries will be intelligently merged based
8
8
  * on update timestamp. `HydrationBoundary` wraps this — use it directly only if you need to hydrate from your
9
9
  * own component instead.
10
- *
11
10
  * @param state - The dehydrated state to hydrate into the cache, as produced by `dehydrate`.
12
11
  * @param options - {@link HydrateOptions} to control the hydration.
13
12
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
14
13
  * be used.
15
- *
16
14
  * @example
17
15
  * Server-side prefetch handed off to the client via `dehydrate` — `dehydratedState` would typically come
18
16
  * from a server load function that prefetched with `queryClient.query` and called `dehydrate(queryClient)`:
@@ -5,13 +5,11 @@ import type { QueryClient, QueryFilters } from '@tanstack/query-core'
5
5
  /**
6
6
  * The `useIsFetching` function returns the `number` of the queries that your application is loading or
7
7
  * fetching in the background (useful for app-wide loading indicators).
8
- *
9
8
  * @param filters - {@link QueryFilters} to narrow down which queries to count. Omit to count every fetching
10
9
  * query.
11
10
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
12
11
  * be used.
13
12
  * @returns A reactive value — read `.current` to get how many matching queries are currently fetching.
14
- *
15
13
  * @example
16
14
  * ```svelte
17
15
  * <script lang="ts">
@@ -25,7 +23,6 @@ import type { QueryClient, QueryFilters } from '@tanstack/query-core'
25
23
  * <span>Refreshing posts...</span>
26
24
  * {/if}
27
25
  * ```
28
- *
29
26
  * @example
30
27
  * A global loading indicator for any query fetching in the background, not just the ones on screen:
31
28
  * ```svelte
@@ -5,12 +5,10 @@ import type { MutationFilters, QueryClient } from '@tanstack/query-core'
5
5
  /**
6
6
  * `useIsMutating` is an optional function that returns the `number` of mutations that your application is
7
7
  * running (useful for app-wide loading indicators).
8
- *
9
8
  * @param filters - {@link MutationFilters} to narrow down which mutations to count.
10
9
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
11
10
  * be used.
12
11
  * @returns A reactive value — read `.current` to get how many matching mutations are currently running.
13
- *
14
12
  * @example
15
13
  * ```svelte
16
14
  * <script lang="ts">
@@ -5,7 +5,6 @@ import type { Box } from './containers.svelte.js'
5
5
  * If you are using `PersistQueryClientProvider`, you can also use the `useIsRestoring` function alongside it to
6
6
  * check if a restore is currently in progress. `createQuery` and friends also check this internally to avoid
7
7
  * race conditions between the restore and mounting queries.
8
- *
9
8
  * @returns A reactive box — read `.current` for `true` while a persisted client is being restored, `false`
10
9
  * otherwise.
11
10
  * @example
@@ -8,6 +8,13 @@ import type {
8
8
  } from '@tanstack/query-core'
9
9
  import type { MutationStateOptions, MutationTypeFromResult } from './types.js'
10
10
 
11
+ /**
12
+ * Collects the mutations in the cache that match `options.filters`, mapped with `options.select`
13
+ * (or to their state, by default).
14
+ * @param mutationCache - The mutation cache to read.
15
+ * @param options - The `filters` to match and the `select` function to map each mutation with.
16
+ * @returns The selected value of every matching mutation.
17
+ */
11
18
  function getResult<
12
19
  TResult = MutationState,
13
20
  TMutation extends Mutation<any, any, any, any> =
@@ -30,13 +37,11 @@ function getResult<
30
37
  * `useMutationState` is a function that gives you access to all mutations in the `MutationCache`. You can pass
31
38
  * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
32
39
  * state.
33
- *
34
40
  * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
35
41
  * mutation state.
36
42
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
37
43
  * be used.
38
44
  * @returns An Array of whatever `select` returns for each matching mutation.
39
- *
40
45
  * @example
41
46
  * Get all variables of all running mutations:
42
47
  * ```svelte
@@ -51,7 +56,6 @@ function getResult<
51
56
  *
52
57
  * {pendingVariables.length} posts saving...
53
58
  * ```
54
- *
55
59
  * @example
56
60
  * Get all data for specific mutations via the `mutationKey`:
57
61
  * ```svelte
@@ -77,7 +81,6 @@ function getResult<
77
81
  * Create post ({savedPosts.length} saved so far)
78
82
  * </button>
79
83
  * ```
80
- *
81
84
  * @example
82
85
  * Access the latest mutation data via the `mutationKey`. Each invocation of `mutate` adds a new entry to the
83
86
  * mutation cache for `gcTime` milliseconds — check the last item that `useMutationState` returns to get the
@@ -3,11 +3,11 @@ import type { QueryClient } from '@tanstack/query-core'
3
3
 
4
4
  /**
5
5
  * The `useQueryClient` function returns the current `QueryClient` instance.
6
- *
7
6
  * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
8
7
  * be used.
9
8
  * @returns The current `QueryClient` instance.
10
- * @throws If no `queryClient` argument is passed and no `QueryClientProvider` is found in the component tree.
9
+ * @throws {Error} If no `queryClient` argument is passed and no `QueryClientProvider` is found in the
10
+ * component tree.
11
11
  * @example
12
12
  * ```svelte
13
13
  * <script lang="ts">
@@ -1,5 +1,10 @@
1
1
  import { untrack } from 'svelte'
2
2
  // modified from the great https://github.com/svecosystem/runed
3
+ /**
4
+ * Runs an effect after (`'post'`) or before (`'pre'`) the DOM updates.
5
+ * @param flush - When to run the effect: `'post'` uses `$effect`, `'pre'` uses `$effect.pre`.
6
+ * @param effect - The effect to run. It may return a cleanup function.
7
+ */
3
8
  function runEffect(
4
9
  flush: 'post' | 'pre',
5
10
  effect: () => void | VoidFunction,
@@ -14,6 +19,14 @@ function runEffect(
14
19
  }
15
20
  }
16
21
  type Getter<T> = () => T
22
+ /**
23
+ * Runs `effect` whenever the reactive state read by `sources` changes, even if they return the
24
+ * same values, skipping the first run. The effect receives the new and previous values, and is
25
+ * called untracked, so only `sources` are tracked.
26
+ * @param sources - The getter, or array of getters, to watch.
27
+ * @param flush - Whether to run after (`'post'`) or before (`'pre'`) the DOM updates.
28
+ * @param effect - Called with the new and previous values. It may return a cleanup function.
29
+ */
17
30
  export const watchChanges = <T>(
18
31
  sources: Getter<T> | Array<Getter<T>>,
19
32
  flush: 'post' | 'pre',