import type * as HTTP from '../types/http'; import type GlobalRequestConfig from './GlobalRequestConfig'; import isCanceled from './utils/isCanceled'; import debounce from './utils/debounce'; /** * http request class. */ declare class Http { static instance: Http; /** * 请求统一配置,会合并到每次请求配置中. */ config: GlobalRequestConfig; /** * 注册拦截器. */ interceptors: { /** * 请求拦截器. */ readonly request: { /** * 注册请求拦截器, 返回移除拦截器函数. */ readonly use: (interceptor: HTTP.RequestInterceptor) => () => void; }; /** * 响应拦截器. */ readonly response: { /** * 注册响应拦截器, 返回移除拦截器函数. * * @template D 原始数据类型. */ readonly use: (interceptor: HTTP.ResponseInterceptor) => () => void; }; }; /** * 检查是否为取消请求错误. */ isCanceled: typeof isCanceled; /** * 请求防抖函数. */ debounce: typeof debounce; constructor(options?: GlobalRequestConfig); /** * 错误处理,错误参数经过错误转换中间件为HttpError或者Error类型。 * * @param error * @returns */ private handleError; /** * 处理响应. * * @template D 业务数据类型. * @param response * @param options * @param url * @returns 业务数据. */ private handleResponse; /** * 响应中间件,下一个中间件为最内层的请求处理,返回业务数据给后续的中间件使用。 * * @param options * @param next * @returns */ private responseMiddleware; /** * 请求处理中间件,位于最里层执行,直接传递给中间件组合函数,返回结果给响应中间件使用。 * * @param options * @returns */ private requestMiddleware; /** * 默认请求. * * @param options * @returns */ private defaultRequest; /** * 新增或者修改请求默认配置, 如果值是对象或者数组会进行合并(对象覆盖, 数组拼接). * * @param config */ useConfig(config?: GlobalRequestConfig): void; /** * 注册中间件. * * @template T 中间件返回值类型. * @template NT 下一个中间件返回值类型. * @param middleware 中间件函数. * @returns 返回移除中间件的函数. */ use(middleware: HTTP.Middleware): () => void; /** * 请求处理, 外部可重写该方法实现自定义请求处理. * * @template D Type of 业务数据. * @param options Request options, 如果为字符串, 则将其作为`url`. * @returns Promise of D extended with abort method for cancel request. */ request(options: string | HTTP.RequestConfig): HTTP.IRequestTask; /** * 使用`GET`方法发送请求. * * @template D Type of 业务数据. * @param options Request options, 如果为字符串, 则将其作为`url`. * @returns Promise of D extended with abort method for cancel request. */ get(options: string | HTTP.RequestConfig): HTTP.IRequestTask; /** * 使用`POST`方法发送请求. * * @template D Type of 业务数据. * @param options Request options, 如果为字符串, 则将其作为`url`. * @returns Promise of D extended with abort method for cancel request. */ post(options: string | HTTP.RequestConfig): HTTP.IRequestTask; /** * 使用`PUT`方法发送请求. * * @template D Type of 业务数据. * @param options Request options, 如果为字符串, 则将其作为`url`. * @returns Promise of D extended with abort method for cancel request. */ put(options: string | HTTP.RequestConfig): HTTP.IRequestTask; /** * 使用`DELETE`方法发送请求. * * @template D Type of 业务数据. * @param options Request options, 如果为字符串, 则将其作为`url`. * @returns Promise of D extended with abort method for cancel request. */ delete(options: string | HTTP.RequestConfig): HTTP.IRequestTask; static getInstance(options?: GlobalRequestConfig): Http; } export default Http;