Open History to see the last commit
1import type {
2 DefaultError,
3 DehydrateOptions,
4 HydrateOptions,
5 MutationCache,
6 MutationObserverOptions,
7 OmitKeyof,
8 QueryCache,
9 QueryObserverOptions,
10} from '@tanstack/query-core'
11import type { ComputedRef, Ref, UnwrapRef } from 'vue-demi'
12β
13type Primitive = string | number | boolean | bigint | symbol | undefined | null
14type UnwrapLeaf =
15 | Primitive
16 // oxlint-disable-next-line typescript/no-unsafe-function-type -- leaves every function-like value, including constructors, unwrapped
17 | Function
18 | Date
19 | Error
20 | RegExp
21 | Map<any, any>
22 | WeakMap<any, any>
23 | Set<any>
24 | WeakSet<any>
25β
26/** A plain value or a reactive getter (`() => T`) that returns one. */
27export type MaybeGetter<T> = T | (() => T)
28β
29/**
30 * A plain value, a `Ref`, or a `ComputedRef`. Accepting this instead of a bare `T` lets a composable take
31 * either a reactive or a static value for a given option β see the [Reactivity guide](../../reactivity.md).
32 */
33export type MaybeRef<T> = Ref<T> | ComputedRef<T> | T
34β
35/**
36 * A {@link MaybeRef}, or a reactive getter (`() => T`). Reactive getters are a lighter-weight alternative to
37 * `computed` for deriving a value from other reactive state β see the
38 * [Reactivity guide](../../reactivity.md#using-derived-state-inside-queries).
39 */
40export type MaybeRefOrGetter<T> = MaybeRef<T> | (() => T)
41β
42/**
43 * Like {@link MaybeRef}, but applied recursively to every property of `T` β so each field of an options object
44 * (for example, an entry inside a `queryKey` array) can independently be a plain value or a `ref`, not just the
45 * object as a whole.
46 */
47export type MaybeRefDeep<T> = MaybeRef<
48 // oxlint-disable-next-line typescript/no-unsafe-function-type -- keeps every function-like value, including constructors, as it is
49 T extends Function
50 ? T
51 : T extends object
52 ? {
53 [Property in keyof T]: MaybeRefDeep<T[Property]>
54 }
55 : T
56>
57β
58/**
59 * Rejects `unknown`, collapsing it to `never` β used to keep generic inference from silently widening.
60 * @internal
61 */
62export type NoUnknown<T> = Equal<unknown, T> extends true ? never : T
63β
64/**
65 * Type-level equality check between `TTargetA` and `TTargetB`.
66 * @internal
67 */
68export type Equal<TTargetA, TTargetB> =
69 (<T>() => T extends TTargetA ? 1 : 2) extends <T>() => T extends TTargetB
70 ? 1
71 : 2
72 ? true
73 : false
74β
75/** The inverse of {@link MaybeRefDeep} β recursively unwraps any `Ref`s in `T` back to their plain value types. */
76export type DeepUnwrapRef<T> = T extends UnwrapLeaf
77 ? T
78 : T extends Ref<infer U>
79 ? DeepUnwrapRef<U>
80 : T extends {}
81 ? {
82 [Property in keyof T]: DeepUnwrapRef<T[Property]>
83 }
84 : UnwrapRef<T>
85β
86/**
87 * The `vue-query`-specific `shallow` option, accepted by query and mutation options.
88 */
89export type ShallowOption = {
90 /**
91 * Return data in a shallow ref object (it is `false` by default). It can be set to `true` to return data in a shallow ref object, which can improve performance if your data does not need to be deeply reactive.
92 */
93 shallow?: boolean
94}
95β
96/**
97 * The options of a mutation. Same as {@link MutationObserverOptions} from `@tanstack/query-core`, minus the
98 * internal `_defaulted` flag, plus the `shallow` option.
99 */
100export type MutationOptions<
101 TData = unknown,
102 TError = DefaultError,
103 TVariables = void,
104 TOnMutateResult = unknown,
105> = OmitKeyof<
106 MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>,
107 '_defaulted'
108> &
109 ShallowOption
110β
111/**
112 * The default options of a `QueryClient`, applied to every query (`queries`), mutation
113 * (`mutations`), `hydrate`, and `dehydrate` call unless overridden. Query and mutation defaults also
114 * accept the `shallow` option.
115 */
116export interface DefaultOptions<TError = DefaultError> {
117 /**
118 * Default options applied to every query, unless overridden per-query. Also accepts the
119 * `shallow` option.
120 */
121 queries?: OmitKeyof<QueryObserverOptions<unknown, TError>, 'queryKey'> &
122 ShallowOption
123 /**
124 * Default options applied to every mutation, unless overridden per-mutation. Also accepts the
125 * `shallow` option.
126 */
127 mutations?: MutationObserverOptions<unknown, TError, unknown, unknown> &
128 ShallowOption
129 /**
130 * Default options used when hydrating queries and mutations; see {@link HydrateOptions}.
131 */
132 hydrate?: HydrateOptions['defaultOptions']
133 /**
134 * Default options used when dehydrating the client's caches; see {@link DehydrateOptions}.
135 */
136 dehydrate?: DehydrateOptions
137}
138β
139/**
140 * The options of `new QueryClient()`: the `queryCache` and `mutationCache` to use, and the
141 * `defaultOptions` for its queries and mutations.
142 */
143export interface QueryClientConfig {
144 /**
145 * The `QueryCache` to use. A new one is created if omitted.
146 */
147 queryCache?: QueryCache
148 /**
149 * The `MutationCache` to use. A new one is created if omitted.
150 */
151 mutationCache?: MutationCache
152 /**
153 * The default options of the queries and mutations of this `QueryClient`.
154 */
155 defaultOptions?: DefaultOptions
156}