@tanstack/svelte-query 6.2.4 → 6.3.0

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 ADDED
@@ -0,0 +1,61 @@
1
+ <img src="https://static.scarf.sh/a.png?x-pxid=be2d8a11-9712-4c1d-9963-580b2d4fb133" />
2
+
3
+ <picture>
4
+ <source
5
+ media="(prefers-color-scheme: dark)"
6
+ srcset="https://tanstack.com/api/readme/query.png?framework=svelte&theme=dark"
7
+ />
8
+ <source
9
+ media="(prefers-color-scheme: light)"
10
+ srcset="https://tanstack.com/api/readme/query.png?framework=svelte"
11
+ />
12
+ <img
13
+ src="https://tanstack.com/api/readme/query.png?framework=svelte"
14
+ alt="TanStack Svelte Query"
15
+ width="900"
16
+ />
17
+ </picture>
18
+
19
+ Functions for fetching, caching and updating asynchronous data in Svelte
20
+
21
+ <a href="https://twitter.com/intent/tweet?button_hashtag=TanStack" target="\_parent">
22
+ <img alt="#TanStack" src="https://img.shields.io/twitter/url?color=%2308a0e9&label=%23TanStack&style=social&url=https%3A%2F%2Ftwitter.com%2Fintent%2Ftweet%3Fbutton_hashtag%3DTanStack">
23
+ </a><a href="https://discord.com/invite/WrRKjPJ" target="\_parent">
24
+ <img alt="" src="https://img.shields.io/badge/Discord-TanStack-%235865F2" />
25
+ </a><a href="https://github.com/TanStack/query/actions/workflows/release.yml?query=branch%3Amain">
26
+ <img alt="Release status" src="https://github.com/TanStack/query/actions/workflows/release.yml/badge.svg?branch=main" />
27
+ </a><a href="https://www.npmjs.com/package/@tanstack/svelte-query" target="\_parent">
28
+ <img alt="" src="https://img.shields.io/npm/dm/@tanstack/svelte-query.svg" />
29
+ </a><a href="https://bundlejs.com/?q=%40tanstack%2Fsvelte-query&config=%7B%22esbuild%22%3A%7B%22external%22%3A%5B%22svelte%22%2C%5D%7D%7D&badge=" target="\_parent">
30
+ <img alt="" src="https://deno.bundlejs.com/?q=@tanstack/svelte-query&config={%22esbuild%22:{%22external%22:[%22svelte%22]}}&badge=detailed" />
31
+ </a><a href="#badge">
32
+ <img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
33
+ </a><a href="https://github.com/TanStack/query/discussions">
34
+ <img alt="Join the discussion on Github" src="https://img.shields.io/badge/Github%20Discussions%20%26%20Support-Chat%20now!-blue" />
35
+ </a><a href="https://bestofjs.org/projects/tanstack-query"><img alt="Best of JS" src="https://img.shields.io/endpoint?url=https://bestofjs-serverless.now.sh/api/project-badge?fullName=TanStack%2Fquery%26since=daily" /></a><a href="https://github.com/TanStack/query/" target="\_parent">
36
+ <img alt="" src="https://img.shields.io/github/stars/TanStack/query.svg?style=social&label=Star" />
37
+ </a><a href="https://twitter.com/tannerlinsley" target="\_parent">
38
+ <img alt="" src="https://img.shields.io/twitter/follow/tannerlinsley.svg?style=social&label=Follow" />
39
+ </a> <a href="https://gitpod.io/from-referrer/">
40
+ <img src="https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod" alt="Gitpod Ready-to-Code"/>
41
+ </a>
42
+
43
+ Enjoy this library? Try the entire [TanStack](https://tanstack.com)! [TanStack Table](https://github.com/TanStack/table), [TanStack Router](https://github.com/tanstack/router), [TanStack Virtual](https://github.com/tanstack/virtual)
44
+
45
+ ## Visit [tanstack.com/query](https://tanstack.com/query) for docs, guides, API and more!
46
+
47
+ ## Quick Features
48
+
49
+ - Transport/protocol/backend agnostic data fetching (REST, GraphQL, promises, whatever!)
50
+ - Auto Caching + Refetching (stale-while-revalidate, Window Refocus, Polling/Realtime)
51
+ - Parallel + Dependent Queries
52
+ - Mutations + Reactive Query Refetching
53
+ - Multi-layer Cache + Automatic Garbage Collection
54
+ - Paginated + Cursor-based Queries
55
+ - Load-More + Infinite Scroll Queries w/ Scroll Recovery
56
+ - Request Cancellation
57
+ - Dedicated Devtools
58
+
59
+ ### [Become a Sponsor!](https://github.com/sponsors/tannerlinsley/)
60
+
61
+ <!-- Use the force, Luke -->
@@ -5,7 +5,7 @@ import type { DefinedInitialDataInfiniteOptions, UndefinedInitialDataInfiniteOpt
5
5
  * The options for `createInfiniteQuery` are identical to `createQuery`, with the addition of
6
6
  * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
7
7
  *
8
- * This overload is selected when `initialData` is set.
8
+ * This overload is selected when `initialData` is known to be defined.
9
9
  *
10
10
  * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
11
11
  * like `queryClient.infiniteQuery`.
@@ -21,7 +21,7 @@ export declare function createInfiniteQuery<TQueryFnData = unknown, TError = Def
21
21
  * The options for `createInfiniteQuery` are identical to `createQuery`, with the addition of
22
22
  * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
23
23
  *
24
- * This overload is selected when `initialData` is not set.
24
+ * This overload is selected when `initialData` is omitted or may be `undefined`.
25
25
  *
26
26
  * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
27
27
  * like `queryClient.infiniteQuery`.
@@ -1,6 +1,47 @@
1
1
  import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core';
2
2
  import type { Accessor, CreateQueryOptions, CreateQueryResult, DefinedCreateQueryResult } from './types.js';
3
3
  import type { DefinedInitialDataOptions, UndefinedInitialDataOptions } from './queryOptions.js';
4
+ /**
5
+ * Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
6
+ * The query runs when the options call for it — `enabled: false` skips the initial fetch.
7
+ *
8
+ * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
9
+ * a `select` changes `TData` to include `undefined`).
10
+ *
11
+ * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
12
+ * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
13
+ * with `initialData` set, wrapped in an {@link Accessor} so options can be reactive.
14
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
15
+ * be used.
16
+ * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt
17
+ * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,
18
+ * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.
19
+ *
20
+ * @example
21
+ * ```svelte
22
+ * <script lang="ts">
23
+ * import { createQuery } from '@tanstack/svelte-query'
24
+ *
25
+ * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
26
+ * // so the list stays visible alongside the error.
27
+ * const query = createQuery(() => ({
28
+ * queryKey: ['posts'],
29
+ * queryFn: fetchPosts,
30
+ * initialData: [],
31
+ * }))
32
+ * </script>
33
+ *
34
+ * {#if query.isError}
35
+ * <span>Error: {query.error.message}</span>
36
+ * {/if}
37
+ * <ul>
38
+ * {#each query.data as post (post.id)}
39
+ * <li>{post.title}</li>
40
+ * {/each}
41
+ * </ul>
42
+ * ```
43
+ */
44
+ export declare function createQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: Accessor<DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>>, queryClient?: Accessor<QueryClient>): DefinedCreateQueryResult<TData, TError>;
4
45
  /**
5
46
  * Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
6
47
  * The query runs when the options call for it — `enabled: false` skips the initial fetch.
@@ -64,46 +105,6 @@ import type { DefinedInitialDataOptions, UndefinedInitialDataOptions } from './q
64
105
  * ```
65
106
  */
66
107
  export declare function createQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: Accessor<UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>>, queryClient?: Accessor<QueryClient>): CreateQueryResult<TData, TError>;
67
- /**
68
- * Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
69
- * The query runs when the options call for it — `enabled: false` skips the initial fetch.
70
- *
71
- * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
72
- *
73
- * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
74
- * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
75
- * with `initialData` set, wrapped in an {@link Accessor} so options can be reactive.
76
- * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
77
- * be used.
78
- * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt
79
- * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,
80
- * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.
81
- *
82
- * @example
83
- * ```svelte
84
- * <script lang="ts">
85
- * import { createQuery } from '@tanstack/svelte-query'
86
- *
87
- * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
88
- * // so the list stays visible alongside the error.
89
- * const query = createQuery(() => ({
90
- * queryKey: ['posts'],
91
- * queryFn: fetchPosts,
92
- * initialData: [],
93
- * }))
94
- * </script>
95
- *
96
- * {#if query.isError}
97
- * <span>Error: {query.error.message}</span>
98
- * {/if}
99
- * <ul>
100
- * {#each query.data as post (post.id)}
101
- * <li>{post.title}</li>
102
- * {/each}
103
- * </ul>
104
- * ```
105
- */
106
- export declare function createQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: Accessor<DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>>, queryClient?: Accessor<QueryClient>): DefinedCreateQueryResult<TData, TError>;
107
108
  /**
108
109
  * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
109
110
  * @param options - The {@link CreateQueryOptions} to use — everything you can pass to `createQuery`, wrapped
@@ -1 +1 @@
1
- {"version":3,"file":"createQuery.d.ts","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAA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,CACpE,EACD,WAAW,CAAC,EAAE,QAAQ,CAAC,WAAW,CAAC,GAClC,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiHG;AACH,wBAAgB,WAAW,CACzB,YAAY,EACZ,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,OAAO,EAAE,QAAQ,CAAC,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,EAC7E,WAAW,CAAC,EAAE,QAAQ,CAAC,WAAW,CAAC,GAClC,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA"}
@@ -11,7 +11,7 @@ export type DefinedInitialDataInfiniteOptions<TQueryFnData = unknown, TError = D
11
11
  * These options can be shared across `createInfiniteQuery` calls and imperative APIs such as
12
12
  * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
13
13
  *
14
- * This overload is selected when `initialData` is set.
14
+ * This overload is selected when `initialData` is known to be defined.
15
15
  *
16
16
  * @see {@link createInfiniteQuery} to run an infinite query with these options.
17
17
  * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to
@@ -1,7 +1,7 @@
1
1
  import type { DefaultError, InitialDataFunction, NonUndefinedGuard, QueryKey, QueryKeyWithDataTag } from '@tanstack/query-core';
2
2
  import type { CreateQueryOptions } from './types.js';
3
3
  export type UndefinedInitialDataOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
4
- initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>;
4
+ initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>> | NonUndefinedGuard<TQueryFnData>;
5
5
  };
6
6
  export type DefinedInitialDataOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
7
7
  initialData: NonUndefinedGuard<TQueryFnData> | (() => NonUndefinedGuard<TQueryFnData>);
@@ -11,7 +11,8 @@ export type DefinedInitialDataOptions<TQueryFnData = unknown, TError = DefaultEr
11
11
  * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
12
12
  * is required and is the query key to generate options for.
13
13
  *
14
- * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
14
+ * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
15
+ * a `select` changes `TData` to include `undefined`).
15
16
  *
16
17
  * @see {@link createQuery} to run a query with these options.
17
18
  * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
@@ -1 +1 @@
1
- {"version":3,"file":"queryOptions.d.ts","sourceRoot":"","sources":["../src/queryOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,QAAQ,EACR,mBAAmB,EACpB,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAEpD,MAAM,MAAM,2BAA2B,CACrC,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,IACnC,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,GAAG;IAC/D,WAAW,CAAC,EAAE,SAAS,GAAG,mBAAmB,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC,CAAA;CAC/E,CAAA;AAED,MAAM,MAAM,yBAAyB,CACnC,YAAY,GAAG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AAY,CAC1B,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,EAErC,OAAO,EAAE,2BAA2B,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,GAC3E,2BAA2B,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,GACpE,mBAAmB,CAAC,SAAS,EAAE,YAAY,EAAE,MAAM,CAAC,CAAA"}
@@ -1,8 +1,9 @@
1
1
  import type { Mutation, MutationState, QueryClient } from '@tanstack/query-core';
2
2
  import type { MutationStateOptions, MutationTypeFromResult } from './types.js';
3
3
  /**
4
- * `useMutationState` gives you access to all mutations (matching the given `filters`), including ones that
5
- * were created by a different component or hook instance, or even ones no longer mounted.
4
+ * `useMutationState` is a function that gives you access to all mutations in the `MutationCache`. You can pass
5
+ * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
6
+ * state.
6
7
  *
7
8
  * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
8
9
  * mutation state.
@@ -1 +1 @@
1
- {"version":3,"file":"useMutationState.svelte.d.ts","sourceRoot":"","sources":["../src/useMutationState.svelte.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,QAAQ,EAER,aAAa,EACb,WAAW,EACZ,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAoB9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,GAAG,aAAa,EACvB,SAAS,SAAS,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAC5C,sBAAsB,CAAC,OAAO,CAAC,EAEjC,OAAO,GAAE,oBAAoB,CAAC,OAAO,EAAE,SAAS,CAAM,EACtD,WAAW,CAAC,EAAE,WAAW,GACxB,KAAK,CAAC,OAAO,CAAC,CA+BhB"}
1
+ {"version":3,"file":"useMutationState.svelte.d.ts","sourceRoot":"","sources":["../src/useMutationState.svelte.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,QAAQ,EAER,aAAa,EACb,WAAW,EACZ,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAoB9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,GAAG,aAAa,EACvB,SAAS,SAAS,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAC5C,sBAAsB,CAAC,OAAO,CAAC,EAEjC,OAAO,GAAE,oBAAoB,CAAC,OAAO,EAAE,SAAS,CAAM,EACtD,WAAW,CAAC,EAAE,WAAW,GACxB,KAAK,CAAC,OAAO,CAAC,CA+BhB"}
@@ -8,8 +8,9 @@ function getResult(mutationCache, options) {
8
8
  : mutation.state));
9
9
  }
10
10
  /**
11
- * `useMutationState` gives you access to all mutations (matching the given `filters`), including ones that
12
- * were created by a different component or hook instance, or even ones no longer mounted.
11
+ * `useMutationState` is a function that gives you access to all mutations in the `MutationCache`. You can pass
12
+ * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
13
+ * state.
13
14
  *
14
15
  * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
15
16
  * mutation state.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/svelte-query",
3
- "version": "6.2.4",
3
+ "version": "6.3.0",
4
4
  "description": "Primitives for managing, caching and syncing asynchronous and remote data in Svelte",
5
5
  "author": "Lachlan Collins",
6
6
  "license": "MIT",
@@ -39,18 +39,18 @@
39
39
  "!src/__tests__"
40
40
  ],
41
41
  "dependencies": {
42
- "@tanstack/query-core": "5.103.2"
42
+ "@tanstack/query-core": "5.104.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@sveltejs/package": "^2.5.8",
46
- "@sveltejs/vite-plugin-svelte": "^5.1.1",
46
+ "@sveltejs/vite-plugin-svelte": "^7.3.1",
47
+ "@tanstack/query-test-utils": "0.0.0",
47
48
  "@testing-library/svelte": "^5.2.8",
48
- "@typescript-eslint/parser": "^8.48.0",
49
- "eslint-plugin-svelte": "^3.11.0",
50
- "svelte": "^5.39.3",
49
+ "@typescript-eslint/parser": "^8.70.1",
50
+ "eslint-plugin-svelte": "^3.23.0",
51
+ "svelte": "^5.57.1",
51
52
  "svelte-check": "^4.4.5",
52
- "typescript": "5.9.3",
53
- "@tanstack/query-test-utils": "0.0.0"
53
+ "typescript": "5.9.3"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "svelte": "^5.25.0"
@@ -22,7 +22,7 @@ import type {
22
22
  * The options for `createInfiniteQuery` are identical to `createQuery`, with the addition of
23
23
  * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
24
24
  *
25
- * This overload is selected when `initialData` is set.
25
+ * This overload is selected when `initialData` is known to be defined.
26
26
  *
27
27
  * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
28
28
  * like `queryClient.infiniteQuery`.
@@ -56,7 +56,7 @@ export function createInfiniteQuery<
56
56
  * The options for `createInfiniteQuery` are identical to `createQuery`, with the addition of
57
57
  * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
58
58
  *
59
- * This overload is selected when `initialData` is not set.
59
+ * This overload is selected when `initialData` is omitted or may be `undefined`.
60
60
  *
61
61
  * @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
62
62
  * like `queryClient.infiniteQuery`.
@@ -12,6 +12,58 @@ import type {
12
12
  UndefinedInitialDataOptions,
13
13
  } from './queryOptions.js'
14
14
 
15
+ /**
16
+ * Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
17
+ * The query runs when the options call for it — `enabled: false` skips the initial fetch.
18
+ *
19
+ * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
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
+ * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
24
+ * with `initialData` set, wrapped in an {@link Accessor} so options can be reactive.
25
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
26
+ * be used.
27
+ * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt
28
+ * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,
29
+ * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.
30
+ *
31
+ * @example
32
+ * ```svelte
33
+ * <script lang="ts">
34
+ * import { createQuery } from '@tanstack/svelte-query'
35
+ *
36
+ * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
37
+ * // so the list stays visible alongside the error.
38
+ * const query = createQuery(() => ({
39
+ * queryKey: ['posts'],
40
+ * queryFn: fetchPosts,
41
+ * initialData: [],
42
+ * }))
43
+ * </script>
44
+ *
45
+ * {#if query.isError}
46
+ * <span>Error: {query.error.message}</span>
47
+ * {/if}
48
+ * <ul>
49
+ * {#each query.data as post (post.id)}
50
+ * <li>{post.title}</li>
51
+ * {/each}
52
+ * </ul>
53
+ * ```
54
+ */
55
+ export function createQuery<
56
+ TQueryFnData = unknown,
57
+ TError = DefaultError,
58
+ TData = TQueryFnData,
59
+ TQueryKey extends QueryKey = QueryKey,
60
+ >(
61
+ options: Accessor<
62
+ DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
63
+ >,
64
+ queryClient?: Accessor<QueryClient>,
65
+ ): DefinedCreateQueryResult<TData, TError>
66
+
15
67
  /**
16
68
  * Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
17
69
  * The query runs when the options call for it — `enabled: false` skips the initial fetch.
@@ -86,57 +138,6 @@ export function createQuery<
86
138
  queryClient?: Accessor<QueryClient>,
87
139
  ): CreateQueryResult<TData, TError>
88
140
 
89
- /**
90
- * Subscribes to a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
91
- * The query runs when the options call for it — `enabled: false` skips the initial fetch.
92
- *
93
- * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
94
- *
95
- * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
96
- * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
97
- * with `initialData` set, wrapped in an {@link Accessor} so options can be reactive.
98
- * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
99
- * be used.
100
- * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt
101
- * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,
102
- * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.
103
- *
104
- * @example
105
- * ```svelte
106
- * <script lang="ts">
107
- * import { createQuery } from '@tanstack/svelte-query'
108
- *
109
- * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
110
- * // so the list stays visible alongside the error.
111
- * const query = createQuery(() => ({
112
- * queryKey: ['posts'],
113
- * queryFn: fetchPosts,
114
- * initialData: [],
115
- * }))
116
- * </script>
117
- *
118
- * {#if query.isError}
119
- * <span>Error: {query.error.message}</span>
120
- * {/if}
121
- * <ul>
122
- * {#each query.data as post (post.id)}
123
- * <li>{post.title}</li>
124
- * {/each}
125
- * </ul>
126
- * ```
127
- */
128
- export function createQuery<
129
- TQueryFnData = unknown,
130
- TError = DefaultError,
131
- TData = TQueryFnData,
132
- TQueryKey extends QueryKey = QueryKey,
133
- >(
134
- options: Accessor<
135
- DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
136
- >,
137
- queryClient?: Accessor<QueryClient>,
138
- ): DefinedCreateQueryResult<TData, TError>
139
-
140
141
  /**
141
142
  * @see {@link queryOptions} to share these options between `createQuery` and imperative APIs like `queryClient.query`.
142
143
  * @param options - The {@link CreateQueryOptions} to use — everything you can pass to `createQuery`, wrapped
@@ -52,7 +52,7 @@ export type DefinedInitialDataInfiniteOptions<
52
52
  * These options can be shared across `createInfiniteQuery` calls and imperative APIs such as
53
53
  * `queryClient.infiniteQuery`. `options.queryKey` is required and is the query key to generate options for.
54
54
  *
55
- * This overload is selected when `initialData` is set.
55
+ * This overload is selected when `initialData` is known to be defined.
56
56
  *
57
57
  * @see {@link createInfiniteQuery} to run an infinite query with these options.
58
58
  * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to
@@ -13,7 +13,10 @@ export type UndefinedInitialDataOptions<
13
13
  TData = TQueryFnData,
14
14
  TQueryKey extends QueryKey = QueryKey,
15
15
  > = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
16
- initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
16
+ initialData?:
17
+ | undefined
18
+ | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
19
+ | NonUndefinedGuard<TQueryFnData>
17
20
  }
18
21
 
19
22
  export type DefinedInitialDataOptions<
@@ -23,8 +26,7 @@ export type DefinedInitialDataOptions<
23
26
  TQueryKey extends QueryKey = QueryKey,
24
27
  > = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
25
28
  initialData:
26
- | NonUndefinedGuard<TQueryFnData>
27
- | (() => NonUndefinedGuard<TQueryFnData>)
29
+ NonUndefinedGuard<TQueryFnData> | (() => NonUndefinedGuard<TQueryFnData>)
28
30
  }
29
31
 
30
32
  /**
@@ -32,7 +34,8 @@ export type DefinedInitialDataOptions<
32
34
  * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
33
35
  * is required and is the query key to generate options for.
34
36
  *
35
- * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
37
+ * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
38
+ * a `select` changes `TData` to include `undefined`).
36
39
  *
37
40
  * @see {@link createQuery} to run a query with these options.
38
41
  * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `createQuery`,
@@ -27,8 +27,9 @@ function getResult<
27
27
  }
28
28
 
29
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.
30
+ * `useMutationState` is a function that gives you access to all mutations in the `MutationCache`. You can pass
31
+ * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
32
+ * state.
32
33
  *
33
34
  * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
34
35
  * mutation state.