Skip to content

Commit 5ced9ac

Browse files
authored
Merge pull request #13 from jedy05097952/master
chore(types): improve types
2 parents 83b06a1 + 2a23d60 commit 5ced9ac

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/core/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Ref, UnwrapNestedRefs } from 'vue-demi'
1+
import { DeepReadonly, Ref, UnwrapNestedRefs } from 'vue-demi'
22

33
export type ConditionsType = {
44
[key: string]: any
@@ -45,29 +45,29 @@ export interface HistoryOptions<K> {
4545
ignore?: Array<K>
4646
}
4747

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

66-
export interface UseConditionWatcherReturn<O> {
66+
export interface UseConditionWatcherReturn<O, Result> {
6767
conditions: UnwrapNestedRefs<O>
6868
readonly isFetching: Ref<boolean>
6969
readonly loading: Ref<boolean>
70-
readonly data: Ref<any | undefined>
70+
readonly data: DeepReadonly<Ref<Result | undefined>>
7171
readonly error: Ref<any | undefined>
7272
execute: (throwOnFailed?: boolean) => void
7373
mutate: Mutate

src/core/useConditionWatcher.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ 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, K extends keyof O>(
24-
config: Config<O, K>
25-
): UseConditionWatcherReturn<O> {
26-
function isFetchConfig(obj: object): obj is Config<O, K> {
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> {
2727
return containsProp(
2828
obj,
2929
'fetcher',
@@ -50,7 +50,7 @@ export default function useConditionWatcher<O extends object, K extends keyof O>
5050
}
5151

5252
// default config
53-
let watcherConfig: Config<O, K> = {
53+
let watcherConfig: Config<O, Result> = {
5454
fetcher: config.fetcher,
5555
conditions: config.conditions,
5656
immediate: true,
@@ -76,7 +76,7 @@ export default function useConditionWatcher<O extends object, K extends keyof O>
7676
const isOnline = ref(true)
7777
const isActive = ref(true)
7878

79-
const data: ShallowRef = shallowRef(
79+
const data: ShallowRef<Result> = shallowRef(
8080
cache.cached(backupIntiConditions) ? cache.get(backupIntiConditions) : watcherConfig.initialData || undefined
8181
)
8282
const error = ref(undefined)
@@ -255,15 +255,15 @@ export default function useConditionWatcher<O extends object, K extends keyof O>
255255
const mutate = (...args): Mutate => {
256256
const arg = args[0]
257257
if (arg === undefined) {
258-
return data.value
258+
return data.value as any
259259
}
260260
if (typeof arg === 'function') {
261261
data.value = arg(deepClone(data.value))
262262
} else {
263263
data.value = arg
264264
}
265265
cache.set({ ..._conditions }, data.value)
266-
return data.value
266+
return data.value as any
267267
}
268268

269269
// - History mode base on vue-router

0 commit comments

Comments
 (0)