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

utils.ts

174 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 as Record<string, any>).map(
74 ([key, val]) => [key, _cloneDeep(val, customize, key, currentLevel + 1)],
75 )
76 return Object.fromEntries(entries)
77 }
78​
79 return value as T
81​
82/**
83 * Deep-clones `value`, recursing into arrays and plain objects. `customize`, if provided, can
84 * intercept any node (by key and nesting level) and substitute its own return value instead of recursing
85 * further.
86 * @internal
87 * @param value - The value to clone.
88 * @param customize - Called for every node with its key and nesting level. If it returns a value
89 * other than `undefined`, that value is used instead of recursing.
90 * @returns The cloned value.
91 */
92export function cloneDeep<T>(
93 value: MaybeRefDeep<T>,
94 customize?: (
95 val: MaybeRefDeep<T>,
96 key: string,
97 level: number,
98 ) => T | undefined,
99): T {
100 return _cloneDeep(value, customize)
102​
103/**
104 * Deep-clones `value` like {@link cloneDeep}, additionally unwrapping any `ref`s it encounters (and,
105 * if `unrefGetters` is `true`, calling any functions it encounters and unwrapping their result too). Always
106 * resolves `queryKey` this way, regardless of `unrefGetters` β€” this is what lets a `queryKey` containing `ref`s
107 * be passed straight through to `@tanstack/query-core`.
108 * @internal
109 * @param obj - The value to clone.
110 * @param unrefGetters - Whether to also call functions and unwrap their result.
111 * @returns The cloned value, with `ref`s unwrapped.
112 */
113export function cloneDeepUnref<T>(
114 obj: MaybeRefDeep<T>,
115 unrefGetters = false,
116): T {
117 return cloneDeep(obj, (val, key, level) => {
118 // Check if we're at the top level and the key is 'queryKey'
119 //
120 // If so, take the recursive descent where we resolve
121 // getters to values as well as refs.
122 if (level === 1 && key === 'queryKey') {
123 return cloneDeepUnref(val, true)
124 }
125​
126 // Resolve getters to values if specified.
127 if (unrefGetters && isFunction(val)) {
128 // Cast due to older TS versions not allowing calling
129 // on certain intersection types.
130 return cloneDeepUnref((val as () => any)(), unrefGetters)
131 }
132​
133 // Unref refs and continue to recurse into the value.
134 if (isRef(val)) {
135 return cloneDeepUnref(unref(val), unrefGetters)
136 }
137​
138 return undefined
139 })
141​
142/**
143 * Checks whether a value is a plain object, created with an object literal or
144 * `Object.create(null)`.
145 * @param value - The value to check.
146 * @returns `true` if `value` is a plain object.
147 */
148function isPlainObject(value: unknown): value is Record<PropertyKey, unknown> {
149 if (Object.prototype.toString.call(value) !== '[object Object]') {
150 return false
151 }
152​
153 const prototype = Object.getPrototypeOf(value)
154 return prototype === null || prototype === Object.prototype
156​
157/**
158 * Checks whether a value is a function.
159 * @param value - The value to check.
160 * @returns `true` if `value` is a function.
161 */
162function isFunction(value: unknown): value is (...args: Array<any>) => any {
163 return typeof value === 'function'
165​
166/**
167 * Resolves `source` to a plain value β€” calls it if it's a function, otherwise deep-unwraps it.
168 * @internal
169 * @param source - A getter, or a value that may contain `ref`s.
170 * @returns The resolved plain value.
171 */
172export function toValueDeep<T>(source: (() => T) | MaybeRefDeep<T>): T {
173 return isFunction(source) ? source() : cloneDeepUnref(source)