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

utils.ts

130 lines4.0 KB

1import { isRef, unref } from 'vue-demi'
2import type { MaybeRefDeep } from './types'
3​
4/** @internal Base Vue injection key `VueQueryPlugin` provides the `QueryClient` under. */
5export const VUE_QUERY_CLIENT = 'VUE_QUERY_CLIENT'
6​
7/** @internal Builds the injection key `useQueryClient`/`VueQueryPlugin` use for a given `queryClientKey`. */
8export function getClientKey(key?: string) {
9 const suffix = key ? `:${key}` : ''
10 return `${VUE_QUERY_CLIENT}${suffix}`
12​
13/** @internal Copies each property from `update` onto `state`, in place, for every key already on `state`. */
14export function updateState(
15 state: Record<string, any>,
16 update: Record<string, any>,
17): void {
18 Object.keys(state).forEach((key) => {
19 state[key] = update[key]
20 })
22​
23// Helper function for cloning deep objects where
24// the level and key is provided to the callback function.
25function _cloneDeep<T>(
26 value: MaybeRefDeep<T>,
27 customize?: (
28 val: MaybeRefDeep<T>,
29 key: string,
30 level: number,
31 ) => T | undefined,
32 currentKey: string = '',
33 currentLevel: number = 0,
34): T {
35 if (customize) {
36 const result = customize(value, currentKey, currentLevel)
37 if (result === undefined && isRef(value)) {
38 return result as T
39 }
40 if (result !== undefined) {
41 return result
42 }
43 }
44​
45 if (Array.isArray(value)) {
46 return value.map((val, index) =>
47 _cloneDeep(val, customize, String(index), currentLevel + 1),
48 ) as unknown as T
49 }
50​
51 if (typeof value === 'object' && isPlainObject(value)) {
52 const entries = Object.entries(value).map(([key, val]) => [
53 key,
54 _cloneDeep(val, customize, key, currentLevel + 1),
55 ])
56 return Object.fromEntries(entries)
57 }
58​
59 return value as T
61​
62/**
63 * @internal Deep-clones `value`, recursing into arrays and plain objects. `customize`, if provided, can
64 * intercept any node (by key and nesting level) and substitute its own return value instead of recursing
65 * further.
66 */
67export function cloneDeep<T>(
68 value: MaybeRefDeep<T>,
69 customize?: (
70 val: MaybeRefDeep<T>,
71 key: string,
72 level: number,
73 ) => T | undefined,
74): T {
75 return _cloneDeep(value, customize)
77​
78/**
79 * @internal Deep-clones `value` like {@link cloneDeep}, additionally unwrapping any `ref`s it encounters (and,
80 * if `unrefGetters` is `true`, calling any functions it encounters and unwrapping their result too). Always
81 * resolves `queryKey` this way, regardless of `unrefGetters` β€” this is what lets a `queryKey` containing `ref`s
82 * be passed straight through to `@tanstack/query-core`.
83 */
84export function cloneDeepUnref<T>(
85 obj: MaybeRefDeep<T>,
86 unrefGetters = false,
87): T {
88 return cloneDeep(obj, (val, key, level) => {
89 // Check if we're at the top level and the key is 'queryKey'
90 //
91 // If so, take the recursive descent where we resolve
92 // getters to values as well as refs.
93 if (level === 1 && key === 'queryKey') {
94 return cloneDeepUnref(val, true)
95 }
96​
97 // Resolve getters to values if specified.
98 if (unrefGetters && isFunction(val)) {
99 // Cast due to older TS versions not allowing calling
100 // on certain intersection types.
101 return cloneDeepUnref((val as Function)(), unrefGetters)
102 }
103​
104 // Unref refs and continue to recurse into the value.
105 if (isRef(val)) {
106 return cloneDeepUnref(unref(val), unrefGetters)
107 }
108​
109 return undefined
110 })
112​
113// eslint-disable-next-line @typescript-eslint/no-wrapper-object-types
114function isPlainObject(value: unknown): value is Object {
115 if (Object.prototype.toString.call(value) !== '[object Object]') {
116 return false
117 }
118​
119 const prototype = Object.getPrototypeOf(value)
120 return prototype === null || prototype === Object.prototype
122​
123function isFunction(value: unknown): value is Function {
124 return typeof value === 'function'
126​
127/** @internal Resolves `source` to a plain value β€” calls it if it's a function, otherwise deep-unwraps it. */
128export function toValueDeep<T>(source: (() => T) | MaybeRefDeep<T>): T {
129 return isFunction(source) ? source() : cloneDeepUnref(source)