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

utils.ts

176 lines5.4 KB

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