Skip to content

chore(afterFetch & beforeFetch): improve types #14

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 14, 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
18 changes: 9 additions & 9 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,34 @@ export interface HistoryOptions<K> {
ignore?: Array<K>
}

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

export interface UseConditionWatcherReturn<O, Result> {
conditions: UnwrapNestedRefs<O>
export interface UseConditionWatcherReturn<Result, Cond> {
conditions: UnwrapNestedRefs<Cond>
readonly isFetching: Ref<boolean>
readonly loading: Ref<boolean>
readonly data: DeepReadonly<Ref<Result | undefined>>
readonly error: Ref<any | undefined>
execute: (throwOnFailed?: boolean) => void
mutate: Mutate
resetConditions: (conditions?: object) => void
onConditionsChange: (fn: OnConditionsChangeContext<O>) => void
onConditionsChange: (fn: OnConditionsChangeContext<Cond>) => void
onFetchSuccess: (fn: (response: any) => void) => void
onFetchError: (fn: (error: any) => void) => void
onFetchFinally: (fn: (error: any) => void) => void
Expand Down
24 changes: 14 additions & 10 deletions src/core/useConditionWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ 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, Result = unknown>(
config: Config<O, Result>
): UseConditionWatcherReturn<O, Result> {
function isFetchConfig(obj: object): obj is Config<O, Result> {
export default function useConditionWatcher<
Result extends unknown,
Cond extends Record<string, any>,
AfterResult extends unknown = Result
>(
config: Config<Result, Cond, AfterResult>
): UseConditionWatcherReturn<AfterResult extends Result ? Result : AfterResult, Cond> {
function isFetchConfig(obj: object): obj is typeof config {
return containsProp(
obj,
'fetcher',
Expand All @@ -50,7 +54,7 @@ export default function useConditionWatcher<O extends object, Result = unknown>(
}

// default config
let watcherConfig: Config<O, Result> = {
let watcherConfig: typeof config = {
fetcher: config.fetcher,
conditions: config.conditions,
immediate: true,
Expand All @@ -70,13 +74,13 @@ export default function useConditionWatcher<O extends object, Result = unknown>(
const cache = useCache(watcherConfig.fetcher, watcherConfig.cacheProvider())

const backupIntiConditions = deepClone(watcherConfig.conditions)
const _conditions = reactive<O>(watcherConfig.conditions)
const _conditions = reactive<Cond>(watcherConfig.conditions)

const isFetching = ref(false)
const isOnline = ref(true)
const isActive = ref(true)

const data: ShallowRef<Result> = shallowRef(
const data: ShallowRef<any> = shallowRef(
cache.cached(backupIntiConditions) ? cache.get(backupIntiConditions) : watcherConfig.initialData || undefined
)
const error = ref(undefined)
Expand Down Expand Up @@ -110,9 +114,9 @@ export default function useConditionWatcher<O extends object, Result = unknown>(
if (isFetching.value) return
isFetching.value = true
error.value = undefined
const conditions2Object: Conditions<O> = conditions
const conditions2Object: Conditions<Cond> = conditions
let customConditions: object = {}
const deepCopyCondition: Conditions<O> = deepClone(conditions2Object)
const deepCopyCondition: Conditions<Cond> = deepClone(conditions2Object)

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

return {
conditions: _conditions as UnwrapNestedRefs<O>,
conditions: _conditions as UnwrapNestedRefs<Cond>,
data: readonly(data),
error: readonly(error),
isFetching: readonly(isFetching),
Expand Down