Skip to content

Commit d80dbd6

Browse files
committed
chore: improve types
1 parent bef746b commit d80dbd6

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

src/core/types.ts

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

3-
export type ConditionsType = {
4-
[key: string]: any
5-
}
6-
73
type ArgumentsTuple = [any, ...unknown[]] | readonly [any, ...unknown[]]
84
export type Arguments = string | ArgumentsTuple | Record<any, any> | null | undefined | false
95
export type Key = Arguments | (() => Arguments)
@@ -13,13 +9,13 @@ export interface Cache<Data = any> {
139
delete(key: Key): void
1410
}
1511

16-
export type Fn = () => void
12+
export type VoidFn = () => void
1713

1814
export type Conditions<T> = {
1915
[K in keyof T]: T[K]
2016
}
2117

22-
export type OnConditionsChangeReturnValue<C> = Partial<UnwrapNestedRefs<C>> & ConditionsType
18+
export type OnConditionsChangeReturnValue<C> = Partial<UnwrapNestedRefs<C>>
2319

2420
export type OnConditionsChangeContext<O> = (
2521
newConditions: OnConditionsChangeReturnValue<O>,
@@ -45,10 +41,10 @@ export interface HistoryOptions<K> {
4541
ignore?: Array<K>
4642
}
4743

48-
export interface Config<Result = unknown, Cond = object, AfterResult extends unknown = Result> {
44+
export interface Config<Cond = Record<string, any>, Result = unknown, AfterFetchResult extends unknown = Result> {
4945
fetcher: (...args: any) => Promise<Result>
5046
conditions?: Cond
51-
defaultParams?: object
47+
defaultParams?: Record<string, any>
5248
immediate?: boolean
5349
manual?: boolean
5450
initialData?: any
@@ -58,12 +54,16 @@ export interface Config<Result = unknown, Cond = object, AfterResult extends unk
5854
pollingWhenOffline?: boolean
5955
revalidateOnFocus?: boolean
6056
cacheProvider?: () => Cache<any>
61-
beforeFetch?: (conditions: Cond, cancel: Fn) => Partial<Cond>
62-
afterFetch?: (data: Result) => AfterResult extends Result ? Result : AfterResult
57+
beforeFetch?: (conditions: Cond, cancel: VoidFn) => Promise<Partial<Cond>> | Partial<Cond>
58+
afterFetch?: (
59+
data: Result
60+
) => Promise<AfterFetchResult extends Result ? Result : AfterFetchResult> | AfterFetchResult extends Result
61+
? Result
62+
: AfterFetchResult
6363
onFetchError?: (ctx: OnFetchErrorContext) => Promise<Partial<OnFetchErrorContext>> | Partial<OnFetchErrorContext>
6464
}
6565

66-
export interface UseConditionWatcherReturn<Result, Cond> {
66+
export interface UseConditionWatcherReturn<Cond, Result> {
6767
conditions: UnwrapNestedRefs<Cond>
6868
readonly isFetching: Ref<boolean>
6969
readonly loading: Ref<boolean>

src/core/useConditionWatcher.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import { filterNoneValueObject, createParams, syncQuery2Conditions, isEquivalent
2121
import { containsProp, isNoData as isDataEmpty, isObject, isServer, rAF } from './utils/helper'
2222

2323
export default function useConditionWatcher<
24-
Result extends unknown,
2524
Cond extends Record<string, any>,
26-
AfterResult extends unknown = Result
25+
Result extends unknown,
26+
AfterFetchResult extends unknown = Result
2727
>(
28-
config: Config<Result, Cond, AfterResult>
29-
): UseConditionWatcherReturn<AfterResult extends Result ? Result : AfterResult, Cond> {
30-
function isFetchConfig(obj: object): obj is typeof config {
28+
config: Config<Cond, Result, AfterFetchResult>
29+
): UseConditionWatcherReturn<Cond, AfterFetchResult extends Result ? Result : AfterFetchResult> {
30+
function isFetchConfig(obj: Record<string, any>): obj is typeof config {
3131
return containsProp(
3232
obj,
3333
'fetcher',
@@ -103,7 +103,7 @@ export default function useConditionWatcher<
103103
stopVisibilityEvent,
104104
} = createEvents()
105105

106-
const resetConditions = (cond?: object): void => {
106+
const resetConditions = (cond?: Record<string, any>): void => {
107107
Object.assign(_conditions, isObject(cond) && !cond.type ? cond : backupIntiConditions)
108108
}
109109

@@ -115,7 +115,7 @@ export default function useConditionWatcher<
115115
isFetching.value = true
116116
error.value = undefined
117117
const conditions2Object: Conditions<Cond> = conditions
118-
let customConditions: object = {}
118+
let customConditions: Record<string, any> = {}
119119
const deepCopyCondition: Conditions<Cond> = deepClone(conditions2Object)
120120

121121
if (typeof watcherConfig.beforeFetch === 'function') {
@@ -144,7 +144,7 @@ export default function useConditionWatcher<
144144
* return result will be {age: 0}
145145
*/
146146
query.value = filterNoneValueObject(validateCustomConditions ? customConditions : conditions2Object)
147-
const finalConditions: object = createParams(query.value, watcherConfig.defaultParams)
147+
const finalConditions: Record<string, any> = createParams(query.value, watcherConfig.defaultParams)
148148

149149
let responseData: any = undefined
150150

@@ -276,7 +276,7 @@ export default function useConditionWatcher<
276276
sync: config.history.sync,
277277
ignore: config.history.ignore || [],
278278
navigation: config.history.navigation || 'push',
279-
listener(parsedQuery: object) {
279+
listener(parsedQuery: Record<string, any>) {
280280
const queryObject = Object.keys(parsedQuery).length ? parsedQuery : backupIntiConditions
281281
syncQuery2Conditions(_conditions, queryObject, backupIntiConditions)
282282
},

src/core/utils/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConditionsType } from '../types'
1+
type ConditionsType = Record<string, any>
22

33
declare global {
44
interface ObjectConstructor {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export { default as useConditionWatcher } from './core/useConditionWatcher'
22

33
export type {
4-
Fn,
4+
VoidFn,
55
Conditions,
66
OnFetchErrorContext,
77
Config,

0 commit comments

Comments
 (0)