Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

types.ts

207 lines5.6 KB

1import type {
2 DefaultError,
3 DefinedInfiniteQueryObserverResult,
4 DefinedQueryObserverResult,
5 InfiniteQueryObserverOptions,
6 InfiniteQueryObserverResult,
7 MutateFunction,
8 Mutation,
9 MutationFilters,
10 MutationObserverOptions,
11 MutationObserverResult,
12 MutationState,
13 OmitKeyof,
14 Override,
15 QueryClient,
16 QueryKey,
17 QueryObserverOptions,
18 QueryObserverResult,
19} from '@tanstack/query-core'
20import type { Snippet } from 'svelte'
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 */
26export type Accessor<T> = () => T
27​
28/** Options for createBaseQuery */
29export type CreateBaseQueryOptions<
30 TQueryFnData = unknown,
31 TError = DefaultError,
32 TData = TQueryFnData,
33 TQueryData = TQueryFnData,
34 TQueryKey extends QueryKey = QueryKey,
35> = QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
36​
37/** Result from createBaseQuery */
38export type CreateBaseQueryResult<
39 TData = unknown,
40 TError = DefaultError,
41> = QueryObserverResult<TData, TError>
42​
43/** Options for createQuery */
44export type CreateQueryOptions<
45 TQueryFnData = unknown,
46 TError = DefaultError,
47 TData = TQueryFnData,
48 TQueryKey extends QueryKey = QueryKey,
49> = CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>
50​
51/** Result from createQuery */
52export type CreateQueryResult<
53 TData = unknown,
54 TError = DefaultError,
55> = CreateBaseQueryResult<TData, TError>
56​
57/** Options for createInfiniteQuery */
58export type CreateInfiniteQueryOptions<
59 TQueryFnData = unknown,
60 TError = DefaultError,
61 TData = TQueryFnData,
62 TQueryKey extends QueryKey = QueryKey,
63 TPageParam = unknown,
64> = InfiniteQueryObserverOptions<
65 TQueryFnData,
66 TError,
67 TData,
68 TQueryKey,
69 TPageParam
71​
72/** Result from createInfiniteQuery */
73export type CreateInfiniteQueryResult<
74 TData = unknown,
75 TError = DefaultError,
76> = InfiniteQueryObserverResult<TData, TError>
77​
78/** Result from createInfiniteQuery with initialData */
79export type DefinedCreateInfiniteQueryResult<
80 TData = unknown,
81 TError = DefaultError,
82> = DefinedInfiniteQueryObserverResult<TData, TError>
83​
84/** Options for createBaseQuery with initialData */
85export type DefinedCreateBaseQueryResult<
86 TData = unknown,
87 TError = DefaultError,
88> = DefinedQueryObserverResult<TData, TError>
89​
90/** Options for createQuery with initialData */
91export type DefinedCreateQueryResult<
92 TData = unknown,
93 TError = DefaultError,
94> = DefinedCreateBaseQueryResult<TData, TError>
95​
96/** Options for createMutation */
97export type CreateMutationOptions<
98 TData = unknown,
99 TError = DefaultError,
100 TVariables = void,
101 TOnMutateResult = unknown,
102> = OmitKeyof<
103 MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
104 '_defaulted'
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 */
112export type CreateMutateFunction<
113 TData = unknown,
114 TError = DefaultError,
115 TVariables = void,
116 TOnMutateResult = unknown,
117> = (
118 ...args: Parameters<
119 MutateFunction<TData, TError, TVariables, TOnMutateResult>
120 >
121) => void
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 */
127export type CreateMutateAsyncFunction<
128 TData = unknown,
129 TError = DefaultError,
130 TVariables = void,
131 TOnMutateResult = unknown,
132> = MutateFunction<TData, TError, TVariables, TOnMutateResult>
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 */
139export type CreateBaseMutationResult<
140 TData = unknown,
141 TError = DefaultError,
142 TVariables = unknown,
143 TOnMutateResult = unknown,
144> = Override<
145 MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
146 { mutate: CreateMutateFunction<TData, TError, TVariables, TOnMutateResult> }
147> & {
148 mutateAsync: CreateMutateAsyncFunction<
149 TData,
150 TError,
151 TVariables,
152 TOnMutateResult
153 >
155​
156/** Result from createMutation */
157export type CreateMutationResult<
158 TData = unknown,
159 TError = DefaultError,
160 TVariables = unknown,
161 TOnMutateResult = unknown,
162> = CreateBaseMutationResult<TData, TError, TVariables, TOnMutateResult>
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 */
168export type MutationTypeFromResult<TResult> = [TResult] extends [
169 MutationState<
170 infer TData,
171 infer TError,
172 infer TVariables,
173 infer TOnMutateResult
174 >,
176 ? Mutation<TData, TError, TVariables, TOnMutateResult>
177 : Mutation
178​
179/** Options for useMutationState */
180export type MutationStateOptions<
181 TResult = MutationState,
182 TMutation extends Mutation<any, any, any, any> =
183 MutationTypeFromResult<TResult>,
184> = {
185 /**
186 * The filters that select the mutations to return the state of.
187 */
188 filters?: MutationFilters
189 /**
190 * Maps each matching mutation to the value returned for it. Defaults to the mutation's `state`.
191 */
192 select?: (mutation: TMutation) => TResult
194​
195/**
196 * The props accepted by `QueryClientProvider`.
197 */
198export type QueryClientProviderProps = {
199 /**
200 * The `QueryClient` to provide to the children.
201 */
202 client: QueryClient
203 /**
204 * The children that can use the provided `QueryClient`.
205 */
206 children: Snippet