From 2a23d601e2277a51d5f1e9c0a77cf054f0624f2c Mon Sep 17 00:00:00 2001 From: Hiro Date: Mon, 11 Apr 2022 17:41:24 +0800 Subject: [PATCH] chore(types): improve types --- src/core/types.ts | 14 +++++++------- src/core/useConditionWatcher.ts | 16 ++++++++-------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/core/types.ts b/src/core/types.ts index 6c5b2f5..21c481f 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -1,4 +1,4 @@ -import { Ref, UnwrapNestedRefs } from 'vue-demi' +import { DeepReadonly, Ref, UnwrapNestedRefs } from 'vue-demi' export type ConditionsType = { [key: string]: any @@ -45,29 +45,29 @@ export interface HistoryOptions { ignore?: Array } -export interface Config { - fetcher: (params: object) => Promise +export interface Config { + fetcher: (params: object) => Promise conditions?: O defaultParams?: object immediate?: boolean manual?: boolean initialData?: any - history?: HistoryOptions + history?: HistoryOptions pollingInterval?: number | Ref pollingWhenHidden?: boolean pollingWhenOffline?: boolean revalidateOnFocus?: boolean cacheProvider?: () => Cache beforeFetch?: (conditions: O & ConditionsType, cancel: Fn) => ConditionsType - afterFetch?: (data: any) => any + afterFetch?: (data: Result) => any onFetchError?: (ctx: OnFetchErrorContext) => Promise> | Partial } -export interface UseConditionWatcherReturn { +export interface UseConditionWatcherReturn { conditions: UnwrapNestedRefs readonly isFetching: Ref readonly loading: Ref - readonly data: Ref + readonly data: DeepReadonly> readonly error: Ref execute: (throwOnFailed?: boolean) => void mutate: Mutate diff --git a/src/core/useConditionWatcher.ts b/src/core/useConditionWatcher.ts index 8017e73..f16fdc4 100644 --- a/src/core/useConditionWatcher.ts +++ b/src/core/useConditionWatcher.ts @@ -20,10 +20,10 @@ import { createEvents } from './utils/createEvents' import { filterNoneValueObject, createParams, syncQuery2Conditions, isEquivalent, deepClone } from './utils/common' import { containsProp, isNoData as isDataEmpty, isObject, isServer, rAF } from './utils/helper' -export default function useConditionWatcher( - config: Config -): UseConditionWatcherReturn { - function isFetchConfig(obj: object): obj is Config { +export default function useConditionWatcher( + config: Config +): UseConditionWatcherReturn { + function isFetchConfig(obj: object): obj is Config { return containsProp( obj, 'fetcher', @@ -50,7 +50,7 @@ export default function useConditionWatcher } // default config - let watcherConfig: Config = { + let watcherConfig: Config = { fetcher: config.fetcher, conditions: config.conditions, immediate: true, @@ -76,7 +76,7 @@ export default function useConditionWatcher const isOnline = ref(true) const isActive = ref(true) - const data: ShallowRef = shallowRef( + const data: ShallowRef = shallowRef( cache.cached(backupIntiConditions) ? cache.get(backupIntiConditions) : watcherConfig.initialData || undefined ) const error = ref(undefined) @@ -255,7 +255,7 @@ export default function useConditionWatcher const mutate = (...args): Mutate => { const arg = args[0] if (arg === undefined) { - return data.value + return data.value as any } if (typeof arg === 'function') { data.value = arg(deepClone(data.value)) @@ -263,7 +263,7 @@ export default function useConditionWatcher data.value = arg } cache.set({ ..._conditions }, data.value) - return data.value + return data.value as any } // - History mode base on vue-router