Skip to content

chore(types): improve types #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref, UnwrapNestedRefs } from 'vue-demi'
import { DeepReadonly, Ref, UnwrapNestedRefs } from 'vue-demi'

export type ConditionsType = {
[key: string]: any
Expand Down Expand Up @@ -45,29 +45,29 @@ export interface HistoryOptions<K> {
ignore?: Array<K>
}

export interface Config<O, K> {
fetcher: (params: object) => Promise<any>
export interface Config<O, Result> {
fetcher: (params: object) => Promise<Result>
conditions?: O
defaultParams?: object
immediate?: boolean
manual?: boolean
initialData?: any
history?: HistoryOptions<K>
history?: HistoryOptions<keyof O>
pollingInterval?: number | Ref<number>
pollingWhenHidden?: boolean
pollingWhenOffline?: boolean
revalidateOnFocus?: boolean
cacheProvider?: () => Cache<any>
beforeFetch?: (conditions: O & ConditionsType, cancel: Fn) => ConditionsType
afterFetch?: (data: any) => any
afterFetch?: (data: Result) => any
onFetchError?: (ctx: OnFetchErrorContext) => Promise<Partial<OnFetchErrorContext>> | Partial<OnFetchErrorContext>
}

export interface UseConditionWatcherReturn<O> {
export interface UseConditionWatcherReturn<O, Result> {
conditions: UnwrapNestedRefs<O>
readonly isFetching: Ref<boolean>
readonly loading: Ref<boolean>
readonly data: Ref<any | undefined>
readonly data: DeepReadonly<Ref<Result | undefined>>
readonly error: Ref<any | undefined>
execute: (throwOnFailed?: boolean) => void
mutate: Mutate
Expand Down
16 changes: 8 additions & 8 deletions src/core/useConditionWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<O extends object, K extends keyof O>(
config: Config<O, K>
): UseConditionWatcherReturn<O> {
function isFetchConfig(obj: object): obj is Config<O, K> {
export default function useConditionWatcher<O extends object, Result = unknown>(
config: Config<O, Result>
): UseConditionWatcherReturn<O, Result> {
function isFetchConfig(obj: object): obj is Config<O, Result> {
return containsProp(
obj,
'fetcher',
Expand All @@ -50,7 +50,7 @@ export default function useConditionWatcher<O extends object, K extends keyof O>
}

// default config
let watcherConfig: Config<O, K> = {
let watcherConfig: Config<O, Result> = {
fetcher: config.fetcher,
conditions: config.conditions,
immediate: true,
Expand All @@ -76,7 +76,7 @@ export default function useConditionWatcher<O extends object, K extends keyof O>
const isOnline = ref(true)
const isActive = ref(true)

const data: ShallowRef = shallowRef(
const data: ShallowRef<Result> = shallowRef(
cache.cached(backupIntiConditions) ? cache.get(backupIntiConditions) : watcherConfig.initialData || undefined
)
const error = ref(undefined)
Expand Down Expand Up @@ -255,15 +255,15 @@ export default function useConditionWatcher<O extends object, K extends keyof O>
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))
} else {
data.value = arg
}
cache.set({ ..._conditions }, data.value)
return data.value
return data.value as any
}

// - History mode base on vue-router
Expand Down