alloy-request
Version:
alloy-request 基于`axios`和`ahooks的useRequest`封装的 react 请求库
119 lines (118 loc) • 3.95 kB
TypeScript
type CachedKeyType = string | number;
export type noop = (...args: any[]) => void;
export type Service<R, P extends any[]> = (...args: P) => Promise<R>;
export type Subscribe<R, P extends any[]> = (data: FetchResult<R, P>) => void;
export type Mutate<R> = (x: R | undefined | ((data: R) => R)) => void;
export interface RequestServiceObject extends RequestInit {
url?: string;
[key: string]: any;
}
export type RequestService = string | RequestServiceObject;
export type CombineService<R, P extends any[]> = RequestService | ((...args: P) => RequestService) | Service<R, P>;
export interface Fetches<R, P extends any[]> {
[key: string]: FetchResult<R, P>;
}
export interface FetchResult<R, P extends any[]> {
loading: boolean;
data: R | undefined;
error: Error | undefined;
params: P;
cancel: noop;
refresh: () => Promise<R>;
mutate: Mutate<R>;
run: (...args: P) => Promise<R>;
runAsync: (...args: P) => Promise<R>;
unmount: () => void;
}
export interface FetchConfig<R, P extends any[]> {
formatResult?: (res: any) => R;
onSuccess?: (data: R, params: P) => void;
onError?: (e: Error, params: P) => void;
loadingDelay?: number;
pollingInterval?: number;
pollingWhenHidden?: boolean;
refreshOnWindowFocus?: boolean;
focusTimespan: number;
debounceInterval?: number;
throttleInterval?: number;
throwOnError?: boolean;
}
export interface BaseResult<R, P extends any[]> extends FetchResult<R, P> {
reset: () => void;
fetches: {
[key in string]: FetchResult<R, P>;
};
}
export type BaseOptions<R, P extends any[]> = {
manual?: boolean;
onSuccess?: (data: R, params: P) => void;
onError?: (e: Error, params: P) => void;
defaultLoading?: boolean;
loadingDelay?: number;
defaultParams?: P;
pollingInterval?: number;
pollingWhenHidden?: boolean;
fetchKey?: (...args: P) => string;
paginated?: false;
loadMore?: false;
refreshOnWindowFocus?: boolean;
focusTimespan?: number;
cacheKey?: CachedKeyType;
cacheTime?: number;
staleTime?: number;
debounceInterval?: number;
throttleInterval?: number;
initialData?: R;
requestMethod?: (service: any) => Promise<any>;
ready?: boolean;
throwOnError?: boolean;
};
export type OptionsWithFormat<R, P extends any[], U, UU extends U> = {
formatResult: (res: R) => U;
} & BaseOptions<UU, P>;
export type Options<R, P extends any[], U, UU extends U> = BaseOptions<R, P> | OptionsWithFormat<R, P, U, UU>;
export type PaginatedParams = [
{
current: number;
pageSize: number;
},
...any[]
];
export interface PaginatedFormatReturn<Item> {
total: number;
list: Item[];
[key: string]: any;
}
export interface PaginatedResult<Item> extends BaseResult<PaginatedFormatReturn<Item>, PaginatedParams> {
pagination: {
current: number;
pageSize: number;
total: number;
totalPage: number;
onChange: (current: number, pageSize: number) => void;
changeCurrent: (current: number) => void;
changePageSize: (pageSize: number) => void;
[key: string]: any;
};
}
export interface BasePaginatedOptions<U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams>, 'paginated'> {
paginated: true;
defaultPageSize?: number;
}
export interface PaginatedOptionsWithFormat<R, Item, U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams>, 'paginated'> {
paginated: true;
defaultPageSize?: number;
formatResult: (data: R) => PaginatedFormatReturn<Item>;
}
export type LoadMoreParams<R> = [R | undefined, ...any[]];
export interface LoadMoreFormatReturn {
list: any[];
[key: string]: any;
}
export interface LoadMoreResult<R> extends BaseResult<R, LoadMoreParams<R>> {
noMore?: boolean;
loadMore: () => void;
reload: () => void;
loadingMore: boolean;
}
export {};