Skip to content

Commit daddb71

Browse files
HiroTsairunkids
authored andcommitted
chore(afterFetch): improve types
1 parent 2355300 commit daddb71

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/core/types.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,34 @@ export interface HistoryOptions<K> {
4545
ignore?: Array<K>
4646
}
4747

48-
export interface Config<O, Result> {
49-
fetcher: (params: object) => Promise<Result>
50-
conditions?: O
48+
export interface Config<Result = unknown, Cond = object, AfterResult extends unknown = Result> {
49+
fetcher: (...args: any) => Promise<Result>
50+
conditions?: Cond
5151
defaultParams?: object
5252
immediate?: boolean
5353
manual?: boolean
5454
initialData?: any
55-
history?: HistoryOptions<keyof O>
55+
history?: HistoryOptions<keyof Cond>
5656
pollingInterval?: number | Ref<number>
5757
pollingWhenHidden?: boolean
5858
pollingWhenOffline?: boolean
5959
revalidateOnFocus?: boolean
6060
cacheProvider?: () => Cache<any>
61-
beforeFetch?: (conditions: O & ConditionsType, cancel: Fn) => ConditionsType
62-
afterFetch?: (data: Result) => any
61+
beforeFetch?: (conditions: Cond & ConditionsType, cancel: Fn) => ConditionsType
62+
afterFetch?: (data: Result) => AfterResult extends Result ? Result : AfterResult
6363
onFetchError?: (ctx: OnFetchErrorContext) => Promise<Partial<OnFetchErrorContext>> | Partial<OnFetchErrorContext>
6464
}
6565

66-
export interface UseConditionWatcherReturn<O, Result> {
67-
conditions: UnwrapNestedRefs<O>
66+
export interface UseConditionWatcherReturn<Result, Cond> {
67+
conditions: UnwrapNestedRefs<Cond>
6868
readonly isFetching: Ref<boolean>
6969
readonly loading: Ref<boolean>
7070
readonly data: DeepReadonly<Ref<Result | undefined>>
7171
readonly error: Ref<any | undefined>
7272
execute: (throwOnFailed?: boolean) => void
7373
mutate: Mutate
7474
resetConditions: (conditions?: object) => void
75-
onConditionsChange: (fn: OnConditionsChangeContext<O>) => void
75+
onConditionsChange: (fn: OnConditionsChangeContext<Cond>) => void
7676
onFetchSuccess: (fn: (response: any) => void) => void
7777
onFetchError: (fn: (error: any) => void) => void
7878
onFetchFinally: (fn: (error: any) => void) => void

src/core/useConditionWatcher.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ import { createEvents } from './utils/createEvents'
2020
import { filterNoneValueObject, createParams, syncQuery2Conditions, isEquivalent, deepClone } from './utils/common'
2121
import { containsProp, isNoData as isDataEmpty, isObject, isServer, rAF } from './utils/helper'
2222

23-
export default function useConditionWatcher<O extends object, Result = unknown>(
24-
config: Config<O, Result>
25-
): UseConditionWatcherReturn<O, Result> {
26-
function isFetchConfig(obj: object): obj is Config<O, Result> {
23+
export default function useConditionWatcher<
24+
Result extends unknown,
25+
Cond extends Record<string, any>,
26+
AfterResult extends unknown = Result
27+
>(
28+
config: Config<Result, Cond, AfterResult>
29+
): UseConditionWatcherReturn<AfterResult extends Result ? Result : AfterResult, Cond> {
30+
function isFetchConfig(obj: object): obj is typeof config {
2731
return containsProp(
2832
obj,
2933
'fetcher',
@@ -50,7 +54,7 @@ export default function useConditionWatcher<O extends object, Result = unknown>(
5054
}
5155

5256
// default config
53-
let watcherConfig: Config<O, Result> = {
57+
let watcherConfig: typeof config = {
5458
fetcher: config.fetcher,
5559
conditions: config.conditions,
5660
immediate: true,
@@ -70,13 +74,13 @@ export default function useConditionWatcher<O extends object, Result = unknown>(
7074
const cache = useCache(watcherConfig.fetcher, watcherConfig.cacheProvider())
7175

7276
const backupIntiConditions = deepClone(watcherConfig.conditions)
73-
const _conditions = reactive<O>(watcherConfig.conditions)
77+
const _conditions = reactive<Cond>(watcherConfig.conditions)
7478

7579
const isFetching = ref(false)
7680
const isOnline = ref(true)
7781
const isActive = ref(true)
7882

79-
const data: ShallowRef<Result> = shallowRef(
83+
const data: ShallowRef<any> = shallowRef(
8084
cache.cached(backupIntiConditions) ? cache.get(backupIntiConditions) : watcherConfig.initialData || undefined
8185
)
8286
const error = ref(undefined)
@@ -110,9 +114,9 @@ export default function useConditionWatcher<O extends object, Result = unknown>(
110114
if (isFetching.value) return
111115
isFetching.value = true
112116
error.value = undefined
113-
const conditions2Object: Conditions<O> = conditions
117+
const conditions2Object: Conditions<Cond> = conditions
114118
let customConditions: object = {}
115-
const deepCopyCondition: Conditions<O> = deepClone(conditions2Object)
119+
const deepCopyCondition: Conditions<Cond> = deepClone(conditions2Object)
116120

117121
if (typeof watcherConfig.beforeFetch === 'function') {
118122
let isCanceled = false
@@ -325,7 +329,7 @@ export default function useConditionWatcher<O extends object, Result = unknown>(
325329
})
326330

327331
return {
328-
conditions: _conditions as UnwrapNestedRefs<O>,
332+
conditions: _conditions as UnwrapNestedRefs<Cond>,
329333
data: readonly(data),
330334
error: readonly(error),
331335
isFetching: readonly(isFetching),

0 commit comments

Comments
 (0)