Open History to see the last commit
1import { isRef, unref } from 'vue-demi'
2import type { MaybeRefDeep } from './types'
3β
4/**
5 * Base Vue injection key `VueQueryPlugin` provides the `QueryClient` under.
6 *
7 * @internal
8 */
9export const VUE_QUERY_CLIENT = 'VUE_QUERY_CLIENT'
10β
11/**
12 * Builds the injection key `useQueryClient`/`VueQueryPlugin` use for a given
13 * `queryClientKey`.
14 *
15 * @param key - The `queryClientKey`. Without it, the base key is used.
16 * @returns The injection key.
17 * @internal
18 */
19export function getClientKey(key?: string) {
20 const suffix = key ? `:${key}` : ''
21 return `${VUE_QUERY_CLIENT}${suffix}`
22}
23β
24/**
25 * Copies each property from `update` onto `state`, in place, for every key
26 * already on `state`.
27 *
28 * @param state - The object to update.
29 * @param update - The object to copy the values from.
30 * @internal
31 */
32export function updateState(
33 state: Record<string, any>,
34 update: Record<string, any>,
35): void {
36 Object.keys(state).forEach((key) => {
37 state[key] = update[key]
38 })
39}
40β
41/**
42 * Recursive implementation of {@link cloneDeep}, which also tracks the key and
43 * nesting level of the current node for `customize`.
44 *
45 * @param value - The value to clone.
46 * @param customize - Called for every node with its key and nesting level. If
47 * it returns a value other than `undefined`, that value is used instead of
48 * recursing.
49 * @param currentKey - The key of the current node.
50 * @param currentLevel - The nesting level of the current node.
51 * @returns The cloned value.
52 */
53function _cloneDeep<T>(
54 value: MaybeRefDeep<T>,
55 customize?: (
56 val: MaybeRefDeep<T>,
57 key: string,
58 level: number,
59 ) => T | undefined,
60 currentKey: string = '',
61 currentLevel: number = 0,
62): T {
63 if (customize) {
64 const result = customize(value, currentKey, currentLevel)
65 if (result === undefined && isRef(value)) {
66 return result as T
67 }
68 if (result !== undefined) {
69 return result
70 }
71 }
72β
73 if (Array.isArray(value)) {
74 return value.map((val, index) =>
75 _cloneDeep(val, customize, String(index), currentLevel + 1),
76 ) as unknown as T
77 }
78β
79 if (typeof value === 'object' && isPlainObject(value)) {
80 const entries = Object.entries(value).map(([key, val]) => [
81 key,
82 _cloneDeep(val, customize, key, currentLevel + 1),
83 ])
84 return Object.fromEntries(entries)
85 }
86β
87 return value as T
88}
89β
90/**
91 * Deep-clones `value`, recursing into arrays and plain objects. `customize`, if
92 * provided, can intercept any node (by key and nesting level) and substitute
93 * its own return value instead of recursing further.
94 *
95 * @param value - The value to clone.
96 * @param customize - Called for every node with its key and nesting level. If
97 * it returns a value other than `undefined`, that value is used instead of
98 * recursing.
99 * @returns The cloned value.
100 * @internal
101 */
102export function cloneDeep<T>(
103 value: MaybeRefDeep<T>,
104 customize?: (
105 val: MaybeRefDeep<T>,
106 key: string,
107 level: number,
108 ) => T | undefined,
109): T {
110 return _cloneDeep(value, customize)
111}
112β
113/**
114 * Deep-clones `value` like {@link cloneDeep}, additionally unwrapping any `ref`s
115 * it encounters (and, if `unrefGetters` is `true`, calling any functions it
116 * encounters and unwrapping their result too). Always resolves `queryKey` this
117 * way, regardless of `unrefGetters` β this is what lets a `queryKey` containing
118 * `ref`s be passed straight through to `@tanstack/query-core`.
119 *
120 * @param obj - The value to clone.
121 * @param unrefGetters - Whether to also call functions and unwrap their result.
122 * @returns The cloned value, with `ref`s unwrapped.
123 * @internal
124 */
125export function cloneDeepUnref<T>(
126 obj: MaybeRefDeep<T>,
127 unrefGetters = false,
128): T {
129 return cloneDeep(obj, (val, key, level) => {
130 // Check if we're at the top level and the key is 'queryKey'
131 //
132 // If so, take the recursive descent where we resolve
133 // getters to values as well as refs.
134 if (level === 1 && key === 'queryKey') {
135 return cloneDeepUnref(val, true)
136 }
137β
138 // Resolve getters to values if specified.
139 if (unrefGetters && isFunction(val)) {
140 // Cast due to older TS versions not allowing calling
141 // on certain intersection types.
142 return cloneDeepUnref((val as Function)(), unrefGetters)
143 }
144β
145 // Unref refs and continue to recurse into the value.
146 if (isRef(val)) {
147 return cloneDeepUnref(unref(val), unrefGetters)
148 }
149β
150 return undefined
151 })
152}
153β
154/**
155 * Checks whether a value is a plain object, created with an object literal or
156 * `Object.create(null)`.
157 *
158 * @param value - The value to check.
159 * @returns `true` if `value` is a plain object.
160 */
161// oxlint-disable-next-line typescript/no-wrapper-object-types
162function isPlainObject(value: unknown): value is Object {
163 if (Object.prototype.toString.call(value) !== '[object Object]') {
164 return false
165 }
166β
167 const prototype = Object.getPrototypeOf(value)
168 return prototype === null || prototype === Object.prototype
169}
170β
171/**
172 * Checks whether a value is a function.
173 *
174 * @param value - The value to check.
175 * @returns `true` if `value` is a function.
176 */
177function isFunction(value: unknown): value is Function {
178 return typeof value === 'function'
179}
180β
181/**
182 * Resolves `source` to a plain value β calls it if it's a function, otherwise
183 * deep-unwraps it.
184 *
185 * @param source - A getter, or a value that may contain `ref`s.
186 * @returns The resolved plain value.
187 * @internal
188 */
189export function toValueDeep<T>(source: (() => T) | MaybeRefDeep<T>): T {
190 return isFunction(source) ? source() : cloneDeepUnref(source)
191}