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

utils.ts

67 lines1.7 KB

1/* eslint-disable @typescript-eslint/no-explicit-any */
2import type { QueryKey } from '@tanstack/query-core'
3import { isRef, unref } from 'vue-demi'
4import type { UnwrapRef } from 'vue-demi'
5​
6export const VUE_QUERY_CLIENT = 'VUE_QUERY_CLIENT'
7​
8export function getClientKey(key?: string) {
9 const suffix = key ? `:${key}` : ''
10 return `${VUE_QUERY_CLIENT}${suffix}`
12​
13export function isQueryKey(value: unknown): value is QueryKey {
14 return Array.isArray(value)
16​
17export function updateState(
18 state: Record<string, unknown>,
19 update: Record<string, any>,
20): void {
21 Object.keys(state).forEach((key) => {
22 state[key] = update[key]
23 })
25​
26export function cloneDeep<T>(
27 value: T,
28 customizer?: (val: unknown) => unknown | void,
29): T {
30 if (customizer) {
31 const result = customizer(value)
32 if (result !== undefined || isRef(value)) {
33 return result as typeof value
34 }
35 }
36​
37 if (Array.isArray(value)) {
38 return value.map((val) => cloneDeep(val, customizer)) as typeof value
39 }
40​
41 if (typeof value === 'object' && isPlainObject(value)) {
42 const entries = Object.entries(value).map(([key, val]) => [
43 key,
44 cloneDeep(val, customizer),
45 ])
46 return Object.fromEntries(entries)
47 }
48​
49 return value
51​
52export function cloneDeepUnref<T>(obj: T): UnwrapRef<T> {
53 return cloneDeep(obj, (val) => {
54 if (isRef(val)) {
55 return cloneDeepUnref(unref(val))
56 }
57 }) as UnwrapRef<typeof obj>
59​
60function isPlainObject(value: unknown): value is Object {
61 if (Object.prototype.toString.call(value) !== '[object Object]') {
62 return false
63 }
64​
65 const prototype = Object.getPrototypeOf(value)
66 return prototype === null || prototype === Object.prototype