@tanstack/svelte-query 6.1.48 → 6.2.1

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 (50) hide show
  1. package/dist/context.d.ts +27 -2
  2. package/dist/context.d.ts.map +1 -1
  3. package/dist/context.js +27 -2
  4. package/dist/createInfiniteQuery.d.ts +97 -1
  5. package/dist/createInfiniteQuery.d.ts.map +1 -1
  6. package/dist/createMutation.svelte.d.ts +156 -2
  7. package/dist/createMutation.svelte.d.ts.map +1 -1
  8. package/dist/createMutation.svelte.js +156 -2
  9. package/dist/createQueries.svelte.d.ts +71 -0
  10. package/dist/createQueries.svelte.d.ts.map +1 -1
  11. package/dist/createQueries.svelte.js +71 -0
  12. package/dist/createQuery.d.ts +209 -0
  13. package/dist/createQuery.d.ts.map +1 -1
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/infiniteQueryOptions.d.ts +95 -2
  17. package/dist/infiniteQueryOptions.d.ts.map +1 -1
  18. package/dist/mutationOptions.d.ts +52 -0
  19. package/dist/mutationOptions.d.ts.map +1 -1
  20. package/dist/queryOptions.d.ts +73 -0
  21. package/dist/queryOptions.d.ts.map +1 -1
  22. package/dist/types.d.ts +3 -1
  23. package/dist/types.d.ts.map +1 -1
  24. package/dist/useHydrate.d.ts +28 -0
  25. package/dist/useHydrate.d.ts.map +1 -1
  26. package/dist/useHydrate.js +28 -0
  27. package/dist/useIsFetching.svelte.d.ts +35 -0
  28. package/dist/useIsFetching.svelte.d.ts.map +1 -1
  29. package/dist/useIsFetching.svelte.js +35 -0
  30. package/dist/useIsMutating.svelte.d.ts +23 -0
  31. package/dist/useIsMutating.svelte.d.ts.map +1 -1
  32. package/dist/useIsMutating.svelte.js +23 -0
  33. package/dist/useMutationState.svelte.d.ts +70 -0
  34. package/dist/useMutationState.svelte.d.ts.map +1 -1
  35. package/dist/useMutationState.svelte.js +70 -0
  36. package/package.json +3 -3
  37. package/src/context.ts +27 -2
  38. package/src/createInfiniteQuery.ts +143 -2
  39. package/src/createMutation.svelte.ts +156 -2
  40. package/src/createQueries.svelte.ts +71 -0
  41. package/src/createQuery.ts +209 -0
  42. package/src/index.ts +4 -0
  43. package/src/infiniteQueryOptions.ts +162 -4
  44. package/src/mutationOptions.ts +53 -0
  45. package/src/queryOptions.ts +73 -0
  46. package/src/types.ts +7 -0
  47. package/src/useHydrate.ts +28 -0
  48. package/src/useIsFetching.svelte.ts +35 -0
  49. package/src/useIsMutating.svelte.ts +23 -0
  50. package/src/useMutationState.svelte.ts +70 -0
@@ -1,6 +1,36 @@
1
1
  import type { DefaultError, WithRequired } from '@tanstack/query-core'
2
2
  import type { CreateMutationOptions } from './types.js'
3
3
 
4
+ /**
5
+ * You can generally pass everything to `mutationOptions` that you can also pass to `createMutation`. This
6
+ * overload requires `mutationKey`, so the resulting options can be looked up elsewhere (e.g. with
7
+ * `useMutationState`).
8
+ *
9
+ * @see {@link createMutation} to run a mutation with these options.
10
+ * @param options - The options to use — everything you can pass to `createMutation`, with `mutationKey` set.
11
+ * @returns The same options object.
12
+ *
13
+ * @example
14
+ * Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
15
+ * ```svelte
16
+ * <script lang="ts">
17
+ * import { mutationOptions, useMutationState } from '@tanstack/svelte-query'
18
+ *
19
+ * const createPostOptions = mutationOptions({
20
+ * mutationKey: ['posts', 'create'],
21
+ * mutationFn: createPost,
22
+ * })
23
+ *
24
+ * const pending = useMutationState({
25
+ * filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
26
+ * })
27
+ * </script>
28
+ *
29
+ * {#if pending.length > 0}
30
+ * <span>Saving…</span>
31
+ * {/if}
32
+ * ```
33
+ */
4
34
  export function mutationOptions<
5
35
  TData = unknown,
6
36
  TError = DefaultError,
@@ -15,6 +45,29 @@ export function mutationOptions<
15
45
  CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
16
46
  'mutationKey'
17
47
  >
48
+
49
+ /**
50
+ * 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
+ * @param options - The options to use — everything you can pass to `createMutation`.
54
+ * @returns The same options object.
55
+ *
56
+ * @example
57
+ * ```svelte
58
+ * <script lang="ts">
59
+ * import { mutationOptions, createMutation } from '@tanstack/svelte-query'
60
+ *
61
+ * const createPostOptions = mutationOptions({
62
+ * mutationFn: createPost,
63
+ * })
64
+ *
65
+ * const mutation = createMutation(() => createPostOptions)
66
+ * </script>
67
+ *
68
+ * <button onclick={() => mutation.mutate({ title: 'Hello' })}>Create</button>
69
+ * ```
70
+ */
18
71
  export function mutationOptions<
19
72
  TData = unknown,
20
73
  TError = DefaultError,
@@ -27,6 +27,44 @@ export type DefinedInitialDataOptions<
27
27
  | (() => NonUndefinedGuard<TQueryFnData>)
28
28
  }
29
29
 
30
+ /**
31
+ * You can generally pass everything to `queryOptions` that you can also pass to `createQuery`. These options
32
+ * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
33
+ * is required and is the query key to generate options for.
34
+ *
35
+ * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
36
+ *
37
+ * @see {@link createQuery} to run a query with these options.
38
+ * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
39
+ * with `initialData` set.
40
+ * @returns The same options object, typed so that `queryKey` carries the inferred data type.
41
+ *
42
+ * @example
43
+ * ```svelte
44
+ * <script lang="ts">
45
+ * import { queryOptions, createQuery } from '@tanstack/svelte-query'
46
+ *
47
+ * const postsOptions = queryOptions({
48
+ * queryKey: ['posts'],
49
+ * queryFn: fetchPosts,
50
+ * initialData: [],
51
+ * })
52
+ *
53
+ * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
54
+ * // so the list stays visible alongside the error.
55
+ * const query = createQuery(() => postsOptions)
56
+ * </script>
57
+ *
58
+ * {#if query.isError}
59
+ * <span>Error: {query.error.message}</span>
60
+ * {/if}
61
+ * <ul>
62
+ * {#each query.data as post (post.id)}
63
+ * <li>{post.title}</li>
64
+ * {/each}
65
+ * </ul>
66
+ * ```
67
+ */
30
68
  export function queryOptions<
31
69
  TQueryFnData = unknown,
32
70
  TError = DefaultError,
@@ -37,6 +75,41 @@ export function queryOptions<
37
75
  ): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
38
76
  QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
39
77
 
78
+ /**
79
+ * You can generally pass everything to `queryOptions` that you can also pass to `createQuery`. These options
80
+ * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
81
+ * is required and is the query key to generate options for.
82
+ *
83
+ * @see {@link createQuery} to run a query with these options.
84
+ * @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `createQuery`.
85
+ * @returns The same options object, typed so that `queryKey` carries the inferred data type.
86
+ *
87
+ * @example
88
+ * A parameterized factory, so the same options object can be reused per `id`:
89
+ * ```svelte
90
+ * <script lang="ts">
91
+ * import { queryOptions, createQuery } from '@tanstack/svelte-query'
92
+ *
93
+ * let { id }: { id: string } = $props()
94
+ *
95
+ * const postOptions = (id: string) =>
96
+ * queryOptions({
97
+ * queryKey: ['post', id],
98
+ * queryFn: () => fetchPost(id),
99
+ * })
100
+ *
101
+ * const query = createQuery(() => postOptions(id))
102
+ * </script>
103
+ *
104
+ * {#if query.isPending}
105
+ * Loading...
106
+ * {:else if query.isError}
107
+ * <span>Error: {query.error.message}</span>
108
+ * {:else}
109
+ * <h1>{query.data.title}</h1>
110
+ * {/if}
111
+ * ```
112
+ */
40
113
  export function queryOptions<
41
114
  TQueryFnData = unknown,
42
115
  TError = DefaultError,
package/src/types.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Snippet } from 'svelte'
2
2
  import type {
3
3
  DefaultError,
4
+ DefinedInfiniteQueryObserverResult,
4
5
  DefinedQueryObserverResult,
5
6
  InfiniteQueryObserverOptions,
6
7
  InfiniteQueryObserverResult,
@@ -70,6 +71,12 @@ export type CreateInfiniteQueryResult<
70
71
  TError = DefaultError,
71
72
  > = InfiniteQueryObserverResult<TData, TError>
72
73
 
74
+ /** Result from createInfiniteQuery with initialData */
75
+ export type DefinedCreateInfiniteQueryResult<
76
+ TData = unknown,
77
+ TError = DefaultError,
78
+ > = DefinedInfiniteQueryObserverResult<TData, TError>
79
+
73
80
  /** Options for createBaseQuery with initialData */
74
81
  export type DefinedCreateBaseQueryResult<
75
82
  TData = unknown,
package/src/useHydrate.ts CHANGED
@@ -2,6 +2,34 @@ import { hydrate } from '@tanstack/query-core'
2
2
  import { useQueryClient } from './useQueryClient.js'
3
3
  import type { HydrateOptions, QueryClient } from '@tanstack/query-core'
4
4
 
5
+ /**
6
+ * Adds a previously dehydrated `state` into the `queryClient` (from the nearest context, or the one
7
+ * passed explicitly). If the client already contains data, the new queries will be intelligently merged based
8
+ * on update timestamp. `HydrationBoundary` wraps this — use it directly only if you need to hydrate from your
9
+ * own component instead.
10
+ *
11
+ * @param state - The dehydrated state to hydrate into the cache, as produced by `dehydrate`.
12
+ * @param options - {@link HydrateOptions} to control the hydration.
13
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
14
+ * be used.
15
+ *
16
+ * @example
17
+ * Server-side prefetch handed off to the client via `dehydrate` — `dehydratedState` would typically come
18
+ * from a server load function that prefetched with `queryClient.query` and called `dehydrate(queryClient)`:
19
+ * ```svelte
20
+ * <script lang="ts">
21
+ * import { HydrationBoundary } from '@tanstack/svelte-query'
22
+ * import type { DehydratedState } from '@tanstack/svelte-query'
23
+ * import Posts from './Posts.svelte'
24
+ *
25
+ * let { dehydratedState }: { dehydratedState: DehydratedState } = $props()
26
+ * </script>
27
+ *
28
+ * <HydrationBoundary state={dehydratedState}>
29
+ * <Posts />
30
+ * </HydrationBoundary>
31
+ * ```
32
+ */
5
33
  export function useHydrate(
6
34
  state?: unknown,
7
35
  options?: HydrateOptions,
@@ -2,6 +2,41 @@ import { ReactiveValue } from './containers.svelte.js'
2
2
  import { useQueryClient } from './useQueryClient.js'
3
3
  import type { QueryClient, QueryFilters } from '@tanstack/query-core'
4
4
 
5
+ /**
6
+ * @param filters - {@link QueryFilters} to narrow down which queries to count. Omit to count every fetching
7
+ * query.
8
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
9
+ * be used.
10
+ * @returns A reactive value — read `.current` to get how many matching queries are currently fetching.
11
+ *
12
+ * @example
13
+ * ```svelte
14
+ * <script lang="ts">
15
+ * import { useIsFetching } from '@tanstack/svelte-query'
16
+ *
17
+ * // How many queries matching the posts prefix are fetching?
18
+ * const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
19
+ * </script>
20
+ *
21
+ * {#if isFetchingPosts.current}
22
+ * <span>Refreshing posts...</span>
23
+ * {/if}
24
+ * ```
25
+ *
26
+ * @example
27
+ * A global loading indicator for any query fetching in the background, not just the ones on screen:
28
+ * ```svelte
29
+ * <script lang="ts">
30
+ * import { useIsFetching } from '@tanstack/svelte-query'
31
+ *
32
+ * const isFetching = useIsFetching()
33
+ * </script>
34
+ *
35
+ * {#if isFetching.current}
36
+ * <div>Queries are fetching in the background...</div>
37
+ * {/if}
38
+ * ```
39
+ */
5
40
  export function useIsFetching(
6
41
  filters?: QueryFilters,
7
42
  queryClient?: QueryClient,
@@ -2,6 +2,29 @@ import { useQueryClient } from './useQueryClient.js'
2
2
  import { ReactiveValue } from './containers.svelte.js'
3
3
  import type { MutationFilters, QueryClient } from '@tanstack/query-core'
4
4
 
5
+ /**
6
+ * `useIsMutating` is an optional hook that returns the `number` of mutations that your application is
7
+ * running (useful for app-wide loading indicators).
8
+ *
9
+ * @param filters - {@link MutationFilters} to narrow down which mutations to count.
10
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
11
+ * be used.
12
+ * @returns A reactive value — read `.current` to get how many matching mutations are currently running.
13
+ *
14
+ * @example
15
+ * ```svelte
16
+ * <script lang="ts">
17
+ * import { useIsMutating } from '@tanstack/svelte-query'
18
+ *
19
+ * // How many mutations matching the posts prefix are in progress?
20
+ * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })
21
+ * </script>
22
+ *
23
+ * {#if isMutatingPosts.current}
24
+ * <span>Saving posts...</span>
25
+ * {/if}
26
+ * ```
27
+ */
5
28
  export function useIsMutating(
6
29
  filters?: MutationFilters,
7
30
  queryClient?: QueryClient,
@@ -26,6 +26,76 @@ function getResult<
26
26
  )
27
27
  }
28
28
 
29
+ /**
30
+ * `useMutationState` gives you access to all mutations (matching the given `filters`), including ones that
31
+ * were created by a different component or hook instance, or even ones no longer mounted.
32
+ *
33
+ * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
34
+ * mutation state.
35
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
36
+ * be used.
37
+ * @returns An Array of whatever `select` returns for each matching mutation.
38
+ *
39
+ * @example
40
+ * Get all variables of all running mutations:
41
+ * ```svelte
42
+ * <script lang="ts">
43
+ * import { useMutationState } from '@tanstack/svelte-query'
44
+ *
45
+ * const pendingVariables = useMutationState({
46
+ * filters: { status: 'pending' },
47
+ * select: (mutation) => mutation.state.variables,
48
+ * })
49
+ * </script>
50
+ *
51
+ * {pendingVariables.length} posts saving...
52
+ * ```
53
+ *
54
+ * @example
55
+ * Get all data for specific mutations via the `mutationKey`:
56
+ * ```svelte
57
+ * <script lang="ts">
58
+ * import { createMutation, useMutationState } from '@tanstack/svelte-query'
59
+ *
60
+ * const mutationKey = ['posts']
61
+ *
62
+ * // Some mutation that we want to get the state for
63
+ * const mutation = createMutation(() => ({
64
+ * mutationKey,
65
+ * mutationFn: createPosts,
66
+ * }))
67
+ *
68
+ * const savedPosts = useMutationState({
69
+ * // this mutation key needs to match the mutation key of the given mutation (see above)
70
+ * filters: { mutationKey, status: 'success' },
71
+ * select: (mutation) => mutation.state.data,
72
+ * })
73
+ * </script>
74
+ *
75
+ * <button onclick={() => mutation.mutate(['New Post'])}>
76
+ * Create post ({savedPosts.length} saved so far)
77
+ * </button>
78
+ * ```
79
+ *
80
+ * @example
81
+ * Access the latest mutation data via the `mutationKey`. Each invocation of `mutate` adds a new entry to the
82
+ * mutation cache for `gcTime` milliseconds — check the last item that `useMutationState` returns to get the
83
+ * latest successful mutation (the `status: 'success'` filter above excludes pending/errored ones):
84
+ * ```svelte
85
+ * <script lang="ts">
86
+ * import { useMutationState } from '@tanstack/svelte-query'
87
+ *
88
+ * const savedPosts = useMutationState({
89
+ * filters: { mutationKey: ['posts'], status: 'success' },
90
+ * select: (mutation) => mutation.state.data,
91
+ * })
92
+ *
93
+ * const latestSavedPost = $derived(savedPosts[savedPosts.length - 1])
94
+ * </script>
95
+ *
96
+ * {latestSavedPost ? 'Saved' : 'Nothing saved yet'}
97
+ * ```
98
+ */
29
99
  export function useMutationState<
30
100
  TResult = MutationState,
31
101
  TMutation extends Mutation<any, any, any, any> =