Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

norm-axios

Package Overview
Dependencies
Maintainers
1
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

norm-axios - npm Package Compare versions

Package was removed
Sorry, it seems this package was removed from the registry
Comparing version
1.0.16
to
1.0.17
+1
-1
package.json
{
"name": "norm-axios",
"version": "1.0.16",
"version": "1.0.17",
"description": "Norm Axios 是一个基于 Axios 的约定式请求库,提供了约定式的请求方式与强大的 Hook API,帮助你更高效的开发。",

@@ -5,0 +5,0 @@ "main": "cjs/index.cjs",

import type { AxiosError } from 'axios';
import type { AxiosInstance } from 'axios';
import type { AxiosRequestConfig } from 'axios';
import type { AxiosResponse } from 'axios';
import type { ComputedRef } from 'vue';
import type { CreateAxiosDefaults } from 'axios';
import type { EffectScope } from 'vue';
import type { InjectionKey } from 'vue';
import type { MaybeRef } from 'vue';
import type { Ref } from 'vue';
import type { ShallowRef } from 'vue';
import type { WatchSource } from 'vue';
declare interface CachedData<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
data: TFormatData;
rawData: TRawData;
response?: AxiosResponse<TRawData>;
params: TParams;
time: number;
timer?: NodeJS.Timeout;
}
export declare const clearCache: (key?: string | string[]) => void;
declare interface DebouncedFunction<F extends (...args: any[]) => any> {
(...args: Parameters<F>): ReturnType<F> | undefined;
cancel(): void;
flush(): void;
}
export declare interface DebounceOptions {
/**
* If `true`, the function will be invoked on the leading edge of the timeout.
* @default false
*/
leading?: MaybeRef<boolean>;
/**
* If `true`, the function will be invoked on the trailing edge of the timeout.
* @default true
*/
trailing?: MaybeRef<boolean>;
/**
* The maximum time `func` is allowed to be delayed before it's invoked.
* @default Infinity
*/
maxWait?: MaybeRef<number>;
}
export declare function definePlugin<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any>(options: RequestPluginImplement<TData, TParams, TFormatData, TRawData>): RequestPluginImplement<TData, TParams, TFormatData, TRawData>;
export declare const getCacheAll: () => {
[k: string]: CachedData<any, any[], any, any>;
};
export declare const GLOBAL_CONFIG_PROVIDER_SYMBOL: InjectionKey<GlobalConfigProvider<any, any>>;
export declare interface GlobalConfigProvider<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
/**
* 通用配置
*/
common?: RequestOptions<TData, TParams, TFormatData, TRawData>;
/**
* 分页配置
*/
pagination?: PaginationOptions;
/**
* 插件
*/
plugins?: RequestPluginImplement<TData, TParams, TFormatData, TRawData>[];
}
export declare interface Meta {
}
export declare class NormAxios<TResponse extends Recordable = Recordable> {
axiosInstance: AxiosInstance;
axiosConfig: NormAxiosConfig<TResponse>;
constructor(config: NormAxiosConfig<TResponse>);
request<TData = any, TParams extends Recordable = Recordable>(config: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
get<TData = any, TParams extends Recordable = Recordable>(url: string, params?: TParams, config?: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
post<TData = any, TParams extends Recordable = Recordable>(url: string, data?: TParams, config?: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
put<TData = any, TParams extends Recordable = Recordable>(url: string, data?: TParams, config?: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
delete<TData = any, TParams extends Recordable = Recordable>(url: string, data?: TParams, config?: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
static extend<Result extends Recordable = Recordable>(instance: NormAxios<Result>, config?: NormAxiosConfig<Result>): NormAxios<Result>;
}
export declare interface NormAxiosConfig<TResponse extends Recordable = Recordable> extends CreateAxiosDefaults {
interceptor?: NormAxiosInterceptor<TResponse>;
}
export declare interface NormAxiosInterceptor<TResponse extends Recordable = Recordable> {
onBeforeRequest?: (config: AxiosRequestConfig) => void | Promise<void>;
/**
* 响应拦截器
* 在 onResponse 中,请不要直接抛出异常,因为下层请求捕获不到该异常。
* 这可能导致请求流程中断或出现未预期的错误行为。
* 不要出现该类似的操作:return Promise.reject(responseContent),请直接 return responseContent
* @param response
*/
onResponse?: (response: AxiosResponse<TResponse>) => ResponseContent<any, TResponse> | Promise<ResponseContent<any, TResponse>>;
/**
* 响应错误
* 在 onResponseError 中,请不要直接抛出异常,因为下层请求捕获不到该异常。
* 这可能导致请求流程中断或出现未预期的错误行为。
* 不要出现该类似的操作:return Promise.reject(responseContent),请直接 return responseContent
* @param response
*/
onResponseError?: (error: AxiosError) => ResponseContent | Promise<ResponseContent>;
}
export declare type PaginationAndFetchOptions<TData extends PaginationResponse = PaginationResponse, TParams extends any[] = any[], TFormatData extends PaginationResponse = TData, TRawData = any> = PaginationOptions & RequestOptions<TData, TParams, TFormatData, TRawData>;
export declare interface PaginationOptions {
/**
* 父级容器,如果存在,则在滚动到底部时,分页自动+1,然后加载数据
* 避免重复触发,请求期间不会触发滚动到底部的事件
*/
target?: MaybeRef<HTMLElement> | ShallowRef<HTMLElement | null>;
/**
* 加载更多偏移量
* @default 100
*/
loadMoreOffset?: number;
/**
* 初始页码
* @default 1
*/
initialPage?: number;
/**
* 初始每页数据条数
* @default 10
*/
initialPageSize?: number;
/**
* 追加模式
* 启用后会自动将新请求的数据追加到已有列表中,例如第一次请求返回 [1, 2, 3],第二次返回 [4, 5, 6],最终列表将合并为 [1, 2, 3, 4, 5, 6]。该功能常用于「加载更多」场景。
* @default false
*/
addedMode?: boolean;
/**
* 当 page 变化的时候自动调用服务
* 当 pageWatch 与 watchSource 同时设置为 true 后,page 或者 pageSize变化的时候会调用两遍服务,这个问题可以设置 pageWatch 或者 watchSource来解决
* @default true
*/
pageWatch?: boolean;
/**
* 当 pageSize 变化的时候重置 page
* @default true
*/
resetPageWhenPageSizeChange?: boolean;
}
export declare interface PaginationResponse<TResponse = any> {
list: TResponse[];
total: number;
}
export declare interface PaginationResult<TData extends PaginationResponse = PaginationResponse, TParams extends any[] = any[], TFormatData extends PaginationResponse = TData, TRawData = any> extends RequestResult<TData, TParams, TFormatData, TRawData> {
/**
* 列表数据
*/
list: ComputedRef<TFormatData['list']>;
/**
* 当前分页
* @default 1
*/
page: Ref<number>;
/**
* 分页数量
* @default 10
*/
pageSize: Ref<number>;
/**
* 数据总数
*/
total: ComputedRef<number>;
/**
* 分页总数
*/
totalPage: ComputedRef<number>;
/**
* 是否是最后一页
*/
isLastPage: ComputedRef<boolean>;
}
export declare type PaginationServiceFn<TData extends PaginationResponse = PaginationResponse, TRawData = any> = (pagination: {
page: number;
pageSize: number;
}) => Promise<ResponseContent<TData, TRawData>>;
declare type Recordable<T = any> = Record<string, T>;
export declare interface RequestContext<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> extends RequestResult<TData, TParams, TFormatData, TRawData> {
scope: EffectScope;
options: RequestOptions<TData, TParams, TFormatData, TRawData>;
rawState: RequestState<TData, TParams, TFormatData, TRawData>;
setState: (state: Partial<RequestState<TData, TParams, TFormatData, TRawData>>) => void;
}
export declare interface RequestMethod<TData = any, TParams extends any[] = any[], TFormatData = TData> {
/**
* 手动触发 service 执行,参数会传递给 service。异常自动处理,通过 onError 反馈或者使用run.catch() 进行反馈
* @param args 请求参数
*/
run: (...args: TParams) => Promise<Undefinable<TFormatData>>;
/**
* 与 run 用法一致,加了防抖功能
* @param args 请求参数
*/
debounceRun: DebouncedFunction<(...args: TParams) => Promise<Undefinable<TFormatData>>>;
/**
* 与 run 用法一致,加了节流功能
* @param args 请求参数
*/
throttleRun: DebouncedFunction<(...args: TParams) => Promise<Undefinable<TFormatData>>>;
/**
* 使用上次的 params,重新调用 run
*/
refresh: () => Promise<Undefinable<TFormatData>>;
/**
* 手动取消当前正在进行中的请求
* 不是真正的取消请求,已发出的请求后台还是会接受到的
* 该方法只是取消了 data、response 的赋值以及 loading 重置为 false
*/
cancel: () => void;
/**
* 突变,立即更改 data 数据
* 不会更改 rawData 和 response 中的数据
*/
mutate: (newData: TFormatData | ((oldData: TFormatData) => TFormatData)) => void;
/**
* 乐观更新,立即更改 data 数据,并且自动在背后发起请求
* 如果更新失败,则会还原到更新之前的数据
* 不会更改 rawData 和 response 中的数据
*/
optimisticUpdate: (newData: TFormatData | ((oldData: TFormatData) => TFormatData), params?: TParams) => void;
}
export declare interface RequestOptions<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
/**
* data 初始的数据
*/
initialData?: TFormatData;
/**
* 传递给 service 的参数
*/
defaultParams?: TParams;
/**
* 手动执行
* @desc 默认 false。 即在初始化时自动执行 service。如果设置为 true, 则需要手动调用 run 或 runAsync 触发执行
* @default false
*/
manual?: boolean;
/**
* 当前请求是否准备好了,准备好后才可以发送请求
* @default true
*/
ready?: Ref<boolean>;
/**
* 侦听一个或多个响应式数据源,如果传入 true 会自动收集 server 中的响应式数据源。当响应式数据源变化时会自动刷新服务
*/
watchSource?: true | WatchSource | WatchSource[];
/**
* 是否深度观察,与 vue watch 中的 deep一致
* @default false
*/
watchDeep?: boolean;
/**
* 窗口获取焦点的时候刷新请求
* @default false
*/
refreshOnWindowFocus?: MaybeRef<boolean>;
/**
* 重新请求间隔(毫秒)
* @default 5000ms
*/
focusTimespan?: MaybeRef<number>;
/**
* 指定 loading 的延时打开时间 (毫秒),可以防止接口加载速度非常快,loading出现闪烁的情况
*/
loadingDelay?: MaybeRef<number>;
/**
* 可以让 loading 持续指定的时间 (毫秒),可以防止 loading 一闪而过
* 如果请求时间少于指定的时间,则最终时间为指定的时间
* 如果请求时间大于指定的时间,则最终时间为请求的时间
*/
loadingKeep?: MaybeRef<number>;
/**
* 设置防抖等待时间 (毫秒)
* @default 500ms
*/
debounceWait?: MaybeRef<number>;
/**
* 防抖允许被延迟的最大值
*/
debounceMaxWait?: MaybeRef<number>;
/**
* 在延迟开始前执行调用
* @default false
*/
debounceLeading?: MaybeRef<boolean>;
/**
* 在延迟结束后执行调用
* @default true
*/
debounceTrailing?: MaybeRef<boolean>;
/**
* 设置节流等待时间 (毫秒)
* @default 500ms
*/
throttleWait?: MaybeRef<number>;
/**
* 在节流开始前执行调用
* @default true
*/
throttleLeading?: MaybeRef<boolean>;
/**
* 在节流结束后执行调用
* @default true
*/
throttleTrailing?: MaybeRef<boolean>;
/**
* 轮询间隔(毫秒),如果值大于 0,则启动轮询模式。
* @default 0
*/
pollingInterval?: MaybeRef<number>;
/**
* 屏幕不可见时轮询,当 pollingInterval 大于 0 时才生效。
* 默认情况下,轮询在屏幕不可见时,会暂停轮询。当设置成 true 时,在屏幕不可见时,轮询任务依旧会定时执行。
* @default false
*/
pollingWhenDocumentHidden?: MaybeRef<boolean>;
/**
* 轮询错误重试次数。如果设置为 Infinity,则无限次
* @default 3
*/
pollingErrorRetryCount?: MaybeRef<number>;
/**
* 错误重试次数。如果设置为 Infinity,则无限次重试。
*/
errorRetryCount?: MaybeRef<number>;
/**
* 重试时间间隔,单位为毫秒
*/
errorRetryInterval?: MaybeRef<number>;
/**
* 请求唯一标识。如果设置了 cacheKey,我们会启用缓存机制。同一个 cacheKey 的数据全局同步。
*/
cacheKey?: string;
/**
* 缓存过期时间(毫秒),超过该时间会自动清除该缓存数据。
* 设置 Infinity 表示永不过期
* @default 600000
*/
cacheTime?: number;
/**
* 设置数据保持新鲜时间,在该时间内,我们认为数据是新鲜的,不会重新发起请求。
* 设置 Infinity 表示永不过期
*/
staleTime?: number;
/**
* 获取自定义缓存
*/
getCache?: (cacheKey: string, params: TParams) => CachedData<TData, TParams, TFormatData, TRawData>;
/**
* 设置自定义缓存
*/
setCache?: (cacheKey: string, cacheData: CachedData<TData, TParams, TFormatData, TRawData>) => void;
/**
* 格式化数据
*/
formatData?: (data: TData, params: TParams, response?: AxiosResponse<TRawData>) => TFormatData;
/**
* 请求之前执行
* @param params 请求参数
*/
onBefore?: (params: TParams) => void;
/**
* promise resolve 的时候执行
* @param data 响应数据
* @param params 请求参数
* @param response axios响应内容
*/
onSuccess?: (data: TFormatData, params: TParams, response?: AxiosResponse<TRawData>) => void;
/**
* 请求错误的时候执行
* @param error 错误信息
* @param params 请求参数
* @param response axios响应内容
*/
onError?: (error: ResponseError, params: TParams, response?: AxiosResponse<TRawData>) => void;
/**
* 最后执行,不管 service 成功失败都会执行
* @param params 参数
*/
onFinally?: (params: TParams) => void;
/**
* 当连续请求的时候,最后一个服务请求完成之后触发
* @param params
*/
onFinallyFetchDone?: (params: TParams) => void;
}
export declare interface RequestPluginHooks<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
/**
* 请求之前触发
*/
onBefore?: (params: TParams) => {
isReturned?: boolean;
} | void;
/**
* 请求开始时触发
*/
onRequest?: (service: (...params: TParams) => Promise<ResponseContent<TData, TRawData>>, params: TParams) => {
servicePromise: Promise<ResponseContent<TData, TRawData>>;
};
/**
* 请求失败时触发
*/
onError?: (error: ResponseError, params: TParams, response?: AxiosResponse<TRawData>) => void;
/**
* 请求成功时触发
*/
onSuccess?: (data: TFormatData, params: TParams, response?: AxiosResponse<TRawData>) => void;
/**
* 当连续请求的时候,最后一个服务请求完成之后触发
*/
onFinallyFetchDone?: (params: TParams) => void;
/**
* 最后执行,不管 server 成功还是失败都会执行
*/
onFinally?: (params: TParams) => void;
/**
* 通过 mutate 修改数据时触发
*/
onMutate?: (newData: TFormatData) => void;
/**
* 通过 cancel 取消请求时触发
*/
onCancel?: () => void;
}
export declare interface RequestPluginImplement<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
(context: RequestContext<TData, TParams, TFormatData, TRawData>): RequestPluginHooks<TData, TParams, TFormatData, TRawData> | void;
}
export declare type RequestResult<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> = WrapWithComputed<RequestState<TData, TParams, TFormatData, TRawData>> & RequestMethod<TData, TParams, TFormatData>;
export declare type RequestServiceFn<TData = any, TParams extends any[] = any[], TRawData = any> = (...args: TParams) => Promise<ResponseContent<TData, TRawData>>;
export declare interface RequestState<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
/**
* service 返回的数据 | 格式化后的数据
*/
data: Undefinable<TFormatData>;
/**
* service 返回的原始数据,取自 response.data
*/
rawData: Undefinable<TRawData>;
/**
* service 返回的错误
*/
error: Undefinable<ResponseError>;
/**
* axios 原始响应内容
*/
response: Undefinable<AxiosResponse<TRawData>>;
/**
* service 是否正在执行
*/
loading: boolean;
/**
* 请求是否已经完成
*/
finished: boolean;
/**
* 当次执行的 service 的参数数组。比如你触发了 run(1, 2, 3),则 params 等于 [1, 2, 3]
*/
params: TParams;
}
export declare type ResponseContent<TData = any, TResponse = any> = [
TData,
ResponseError?,
AxiosResponse<TResponse>?
];
export declare interface ResponseError {
code: number | string;
msg: string;
axiosError?: AxiosError;
}
export declare interface ThrottleOptions {
/**
* If `true`, the function will be invoked on the leading edge of the timeout.
* @default true
*/
leading?: MaybeRef<boolean>;
/**
* If `true`, the function will be invoked on the trailing edge of the timeout.
* @default true
*/
trailing?: MaybeRef<boolean>;
}
declare type Undefinable<T> = T | undefined;
export declare function useGlobalConfigProvider<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any>(config: GlobalConfigProvider<TData, TParams, TFormatData, TRawData>): void;
export declare function usePagination<TData extends PaginationResponse = PaginationResponse, TParams extends any[] = any[], TFormatData extends PaginationResponse = TData, TRawData = any>(service: PaginationServiceFn<TData, TRawData>, options?: PaginationAndFetchOptions<TData, TParams, TFormatData, TRawData>): PaginationResult<TData, TParams, TFormatData, TRawData>;
export declare function useRequest<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any>(service: RequestServiceFn<TData, TParams, TRawData>, options?: RequestOptions<TData, TParams, TFormatData, TRawData>, plugins?: RequestPluginImplement<TData, TParams, TFormatData, TRawData>[]): RequestResult<TData, TParams, TFormatData, TRawData>;
declare type WrapWithComputed<T extends Recordable> = {
[K in keyof T]: ComputedRef<T[K]>;
};
export { }
declare module 'axios' {
interface AxiosRequestConfig extends Partial<Meta> {}
}
import type { AxiosError } from 'axios';
import type { AxiosInstance } from 'axios';
import type { AxiosRequestConfig } from 'axios';
import type { AxiosResponse } from 'axios';
import type { ComputedRef } from 'vue';
import type { CreateAxiosDefaults } from 'axios';
import type { EffectScope } from 'vue';
import type { InjectionKey } from 'vue';
import type { MaybeRef } from 'vue';
import type { Ref } from 'vue';
import type { ShallowRef } from 'vue';
import type { WatchSource } from 'vue';
declare interface CachedData<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
data: TFormatData;
rawData: TRawData;
response?: AxiosResponse<TRawData>;
params: TParams;
time: number;
timer?: NodeJS.Timeout;
}
export declare const clearCache: (key?: string | string[]) => void;
declare interface DebouncedFunction<F extends (...args: any[]) => any> {
(...args: Parameters<F>): ReturnType<F> | undefined;
cancel(): void;
flush(): void;
}
export declare interface DebounceOptions {
/**
* If `true`, the function will be invoked on the leading edge of the timeout.
* @default false
*/
leading?: MaybeRef<boolean>;
/**
* If `true`, the function will be invoked on the trailing edge of the timeout.
* @default true
*/
trailing?: MaybeRef<boolean>;
/**
* The maximum time `func` is allowed to be delayed before it's invoked.
* @default Infinity
*/
maxWait?: MaybeRef<number>;
}
export declare function definePlugin<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any>(options: RequestPluginImplement<TData, TParams, TFormatData, TRawData>): RequestPluginImplement<TData, TParams, TFormatData, TRawData>;
export declare const getCacheAll: () => {
[k: string]: CachedData<any, any[], any, any>;
};
export declare const GLOBAL_CONFIG_PROVIDER_SYMBOL: InjectionKey<GlobalConfigProvider<any, any>>;
export declare interface GlobalConfigProvider<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
/**
* 通用配置
*/
common?: RequestOptions<TData, TParams, TFormatData, TRawData>;
/**
* 分页配置
*/
pagination?: PaginationOptions;
/**
* 插件
*/
plugins?: RequestPluginImplement<TData, TParams, TFormatData, TRawData>[];
}
export declare interface Meta {
}
export declare class NormAxios<TResponse extends Recordable = Recordable> {
axiosInstance: AxiosInstance;
axiosConfig: NormAxiosConfig<TResponse>;
constructor(config: NormAxiosConfig<TResponse>);
request<TData = any, TParams extends Recordable = Recordable>(config: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
get<TData = any, TParams extends Recordable = Recordable>(url: string, params?: TParams, config?: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
post<TData = any, TParams extends Recordable = Recordable>(url: string, data?: TParams, config?: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
put<TData = any, TParams extends Recordable = Recordable>(url: string, data?: TParams, config?: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
delete<TData = any, TParams extends Recordable = Recordable>(url: string, data?: TParams, config?: AxiosRequestConfig<TParams>): Promise<ResponseContent<TData, TResponse>>;
static extend<Result extends Recordable = Recordable>(instance: NormAxios<Result>, config?: NormAxiosConfig<Result>): NormAxios<Result>;
}
export declare interface NormAxiosConfig<TResponse extends Recordable = Recordable> extends CreateAxiosDefaults {
interceptor?: NormAxiosInterceptor<TResponse>;
}
export declare interface NormAxiosInterceptor<TResponse extends Recordable = Recordable> {
onBeforeRequest?: (config: AxiosRequestConfig) => void | Promise<void>;
/**
* 响应拦截器
* 在 onResponse 中,请不要直接抛出异常,因为下层请求捕获不到该异常。
* 这可能导致请求流程中断或出现未预期的错误行为。
* 不要出现该类似的操作:return Promise.reject(responseContent),请直接 return responseContent
* @param response
*/
onResponse?: (response: AxiosResponse<TResponse>) => ResponseContent<any, TResponse> | Promise<ResponseContent<any, TResponse>>;
/**
* 响应错误
* 在 onResponseError 中,请不要直接抛出异常,因为下层请求捕获不到该异常。
* 这可能导致请求流程中断或出现未预期的错误行为。
* 不要出现该类似的操作:return Promise.reject(responseContent),请直接 return responseContent
* @param response
*/
onResponseError?: (error: AxiosError) => ResponseContent | Promise<ResponseContent>;
}
export declare type PaginationAndFetchOptions<TData extends PaginationResponse = PaginationResponse, TParams extends any[] = any[], TFormatData extends PaginationResponse = TData, TRawData = any> = PaginationOptions & RequestOptions<TData, TParams, TFormatData, TRawData>;
export declare interface PaginationOptions {
/**
* 父级容器,如果存在,则在滚动到底部时,分页自动+1,然后加载数据
* 避免重复触发,请求期间不会触发滚动到底部的事件
*/
target?: MaybeRef<HTMLElement> | ShallowRef<HTMLElement | null>;
/**
* 加载更多偏移量
* @default 100
*/
loadMoreOffset?: number;
/**
* 初始页码
* @default 1
*/
initialPage?: number;
/**
* 初始每页数据条数
* @default 10
*/
initialPageSize?: number;
/**
* 追加模式
* 启用后会自动将新请求的数据追加到已有列表中,例如第一次请求返回 [1, 2, 3],第二次返回 [4, 5, 6],最终列表将合并为 [1, 2, 3, 4, 5, 6]。该功能常用于「加载更多」场景。
* @default false
*/
addedMode?: boolean;
/**
* 当 page 变化的时候自动调用服务
* 当 pageWatch 与 watchSource 同时设置为 true 后,page 或者 pageSize变化的时候会调用两遍服务,这个问题可以设置 pageWatch 或者 watchSource来解决
* @default true
*/
pageWatch?: boolean;
/**
* 当 pageSize 变化的时候重置 page
* @default true
*/
resetPageWhenPageSizeChange?: boolean;
}
export declare interface PaginationResponse<TResponse = any> {
list: TResponse[];
total: number;
}
export declare interface PaginationResult<TData extends PaginationResponse = PaginationResponse, TParams extends any[] = any[], TFormatData extends PaginationResponse = TData, TRawData = any> extends RequestResult<TData, TParams, TFormatData, TRawData> {
/**
* 列表数据
*/
list: ComputedRef<TFormatData['list']>;
/**
* 当前分页
* @default 1
*/
page: Ref<number>;
/**
* 分页数量
* @default 10
*/
pageSize: Ref<number>;
/**
* 数据总数
*/
total: ComputedRef<number>;
/**
* 分页总数
*/
totalPage: ComputedRef<number>;
/**
* 是否是最后一页
*/
isLastPage: ComputedRef<boolean>;
}
export declare type PaginationServiceFn<TData extends PaginationResponse = PaginationResponse, TRawData = any> = (pagination: {
page: number;
pageSize: number;
}) => Promise<ResponseContent<TData, TRawData>>;
declare type Recordable<T = any> = Record<string, T>;
export declare interface RequestContext<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> extends RequestResult<TData, TParams, TFormatData, TRawData> {
scope: EffectScope;
options: RequestOptions<TData, TParams, TFormatData, TRawData>;
rawState: RequestState<TData, TParams, TFormatData, TRawData>;
setState: (state: Partial<RequestState<TData, TParams, TFormatData, TRawData>>) => void;
}
export declare interface RequestMethod<TData = any, TParams extends any[] = any[], TFormatData = TData> {
/**
* 手动触发 service 执行,参数会传递给 service。异常自动处理,通过 onError 反馈或者使用run.catch() 进行反馈
* @param args 请求参数
*/
run: (...args: TParams) => Promise<Undefinable<TFormatData>>;
/**
* 与 run 用法一致,加了防抖功能
* @param args 请求参数
*/
debounceRun: DebouncedFunction<(...args: TParams) => Promise<Undefinable<TFormatData>>>;
/**
* 与 run 用法一致,加了节流功能
* @param args 请求参数
*/
throttleRun: DebouncedFunction<(...args: TParams) => Promise<Undefinable<TFormatData>>>;
/**
* 使用上次的 params,重新调用 run
*/
refresh: () => Promise<Undefinable<TFormatData>>;
/**
* 手动取消当前正在进行中的请求
* 不是真正的取消请求,已发出的请求后台还是会接受到的
* 该方法只是取消了 data、response 的赋值以及 loading 重置为 false
*/
cancel: () => void;
/**
* 突变,立即更改 data 数据
* 不会更改 rawData 和 response 中的数据
*/
mutate: (newData: TFormatData | ((oldData: TFormatData) => TFormatData)) => void;
/**
* 乐观更新,立即更改 data 数据,并且自动在背后发起请求
* 如果更新失败,则会还原到更新之前的数据
* 不会更改 rawData 和 response 中的数据
*/
optimisticUpdate: (newData: TFormatData | ((oldData: TFormatData) => TFormatData), params?: TParams) => void;
}
export declare interface RequestOptions<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
/**
* data 初始的数据
*/
initialData?: TFormatData;
/**
* 传递给 service 的参数
*/
defaultParams?: TParams;
/**
* 手动执行
* @desc 默认 false。 即在初始化时自动执行 service。如果设置为 true, 则需要手动调用 run 或 runAsync 触发执行
* @default false
*/
manual?: boolean;
/**
* 当前请求是否准备好了,准备好后才可以发送请求
* @default true
*/
ready?: Ref<boolean>;
/**
* 侦听一个或多个响应式数据源,如果传入 true 会自动收集 server 中的响应式数据源。当响应式数据源变化时会自动刷新服务
*/
watchSource?: true | WatchSource | WatchSource[];
/**
* 是否深度观察,与 vue watch 中的 deep一致
* @default false
*/
watchDeep?: boolean;
/**
* 窗口获取焦点的时候刷新请求
* @default false
*/
refreshOnWindowFocus?: MaybeRef<boolean>;
/**
* 重新请求间隔(毫秒)
* @default 5000ms
*/
focusTimespan?: MaybeRef<number>;
/**
* 指定 loading 的延时打开时间 (毫秒),可以防止接口加载速度非常快,loading出现闪烁的情况
*/
loadingDelay?: MaybeRef<number>;
/**
* 可以让 loading 持续指定的时间 (毫秒),可以防止 loading 一闪而过
* 如果请求时间少于指定的时间,则最终时间为指定的时间
* 如果请求时间大于指定的时间,则最终时间为请求的时间
*/
loadingKeep?: MaybeRef<number>;
/**
* 设置防抖等待时间 (毫秒)
* @default 500ms
*/
debounceWait?: MaybeRef<number>;
/**
* 防抖允许被延迟的最大值
*/
debounceMaxWait?: MaybeRef<number>;
/**
* 在延迟开始前执行调用
* @default false
*/
debounceLeading?: MaybeRef<boolean>;
/**
* 在延迟结束后执行调用
* @default true
*/
debounceTrailing?: MaybeRef<boolean>;
/**
* 设置节流等待时间 (毫秒)
* @default 500ms
*/
throttleWait?: MaybeRef<number>;
/**
* 在节流开始前执行调用
* @default true
*/
throttleLeading?: MaybeRef<boolean>;
/**
* 在节流结束后执行调用
* @default true
*/
throttleTrailing?: MaybeRef<boolean>;
/**
* 轮询间隔(毫秒),如果值大于 0,则启动轮询模式。
* @default 0
*/
pollingInterval?: MaybeRef<number>;
/**
* 屏幕不可见时轮询,当 pollingInterval 大于 0 时才生效。
* 默认情况下,轮询在屏幕不可见时,会暂停轮询。当设置成 true 时,在屏幕不可见时,轮询任务依旧会定时执行。
* @default false
*/
pollingWhenDocumentHidden?: MaybeRef<boolean>;
/**
* 轮询错误重试次数。如果设置为 Infinity,则无限次
* @default 3
*/
pollingErrorRetryCount?: MaybeRef<number>;
/**
* 错误重试次数。如果设置为 Infinity,则无限次重试。
*/
errorRetryCount?: MaybeRef<number>;
/**
* 重试时间间隔,单位为毫秒
*/
errorRetryInterval?: MaybeRef<number>;
/**
* 请求唯一标识。如果设置了 cacheKey,我们会启用缓存机制。同一个 cacheKey 的数据全局同步。
*/
cacheKey?: string;
/**
* 缓存过期时间(毫秒),超过该时间会自动清除该缓存数据。
* 设置 Infinity 表示永不过期
* @default 600000
*/
cacheTime?: number;
/**
* 设置数据保持新鲜时间,在该时间内,我们认为数据是新鲜的,不会重新发起请求。
* 设置 Infinity 表示永不过期
*/
staleTime?: number;
/**
* 获取自定义缓存
*/
getCache?: (cacheKey: string, params: TParams) => CachedData<TData, TParams, TFormatData, TRawData>;
/**
* 设置自定义缓存
*/
setCache?: (cacheKey: string, cacheData: CachedData<TData, TParams, TFormatData, TRawData>) => void;
/**
* 格式化数据
*/
formatData?: (data: TData, params: TParams, response?: AxiosResponse<TRawData>) => TFormatData;
/**
* 请求之前执行
* @param params 请求参数
*/
onBefore?: (params: TParams) => void;
/**
* promise resolve 的时候执行
* @param data 响应数据
* @param params 请求参数
* @param response axios响应内容
*/
onSuccess?: (data: TFormatData, params: TParams, response?: AxiosResponse<TRawData>) => void;
/**
* 请求错误的时候执行
* @param error 错误信息
* @param params 请求参数
* @param response axios响应内容
*/
onError?: (error: ResponseError, params: TParams, response?: AxiosResponse<TRawData>) => void;
/**
* 最后执行,不管 service 成功失败都会执行
* @param params 参数
*/
onFinally?: (params: TParams) => void;
/**
* 当连续请求的时候,最后一个服务请求完成之后触发
* @param params
*/
onFinallyFetchDone?: (params: TParams) => void;
}
export declare interface RequestPluginHooks<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
/**
* 请求之前触发
*/
onBefore?: (params: TParams) => {
isReturned?: boolean;
} | void;
/**
* 请求开始时触发
*/
onRequest?: (service: (...params: TParams) => Promise<ResponseContent<TData, TRawData>>, params: TParams) => {
servicePromise: Promise<ResponseContent<TData, TRawData>>;
};
/**
* 请求失败时触发
*/
onError?: (error: ResponseError, params: TParams, response?: AxiosResponse<TRawData>) => void;
/**
* 请求成功时触发
*/
onSuccess?: (data: TFormatData, params: TParams, response?: AxiosResponse<TRawData>) => void;
/**
* 当连续请求的时候,最后一个服务请求完成之后触发
*/
onFinallyFetchDone?: (params: TParams) => void;
/**
* 最后执行,不管 server 成功还是失败都会执行
*/
onFinally?: (params: TParams) => void;
/**
* 通过 mutate 修改数据时触发
*/
onMutate?: (newData: TFormatData) => void;
/**
* 通过 cancel 取消请求时触发
*/
onCancel?: () => void;
}
export declare interface RequestPluginImplement<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
(context: RequestContext<TData, TParams, TFormatData, TRawData>): RequestPluginHooks<TData, TParams, TFormatData, TRawData> | void;
}
export declare type RequestResult<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> = WrapWithComputed<RequestState<TData, TParams, TFormatData, TRawData>> & RequestMethod<TData, TParams, TFormatData>;
export declare type RequestServiceFn<TData = any, TParams extends any[] = any[], TRawData = any> = (...args: TParams) => Promise<ResponseContent<TData, TRawData>>;
export declare interface RequestState<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any> {
/**
* service 返回的数据 | 格式化后的数据
*/
data: Undefinable<TFormatData>;
/**
* service 返回的原始数据,取自 response.data
*/
rawData: Undefinable<TRawData>;
/**
* service 返回的错误
*/
error: Undefinable<ResponseError>;
/**
* axios 原始响应内容
*/
response: Undefinable<AxiosResponse<TRawData>>;
/**
* service 是否正在执行
*/
loading: boolean;
/**
* 请求是否已经完成
*/
finished: boolean;
/**
* 当次执行的 service 的参数数组。比如你触发了 run(1, 2, 3),则 params 等于 [1, 2, 3]
*/
params: TParams;
}
export declare type ResponseContent<TData = any, TResponse = any> = [
TData,
ResponseError?,
AxiosResponse<TResponse>?
];
export declare interface ResponseError {
code: number | string;
msg: string;
axiosError?: AxiosError;
}
export declare interface ThrottleOptions {
/**
* If `true`, the function will be invoked on the leading edge of the timeout.
* @default true
*/
leading?: MaybeRef<boolean>;
/**
* If `true`, the function will be invoked on the trailing edge of the timeout.
* @default true
*/
trailing?: MaybeRef<boolean>;
}
declare type Undefinable<T> = T | undefined;
export declare function useGlobalConfigProvider<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any>(config: GlobalConfigProvider<TData, TParams, TFormatData, TRawData>): void;
export declare function usePagination<TData extends PaginationResponse = PaginationResponse, TParams extends any[] = any[], TFormatData extends PaginationResponse = TData, TRawData = any>(service: PaginationServiceFn<TData, TRawData>, options?: PaginationAndFetchOptions<TData, TParams, TFormatData, TRawData>): PaginationResult<TData, TParams, TFormatData, TRawData>;
export declare function useRequest<TData = any, TParams extends any[] = any[], TFormatData = TData, TRawData = any>(service: RequestServiceFn<TData, TParams, TRawData>, options?: RequestOptions<TData, TParams, TFormatData, TRawData>, plugins?: RequestPluginImplement<TData, TParams, TFormatData, TRawData>[]): RequestResult<TData, TParams, TFormatData, TRawData>;
declare type WrapWithComputed<T extends Recordable> = {
[K in keyof T]: ComputedRef<T[K]>;
};
export { }
declare module 'axios' {
interface AxiosRequestConfig extends Partial<Meta> {}
}