Axios接口文档详解
基础接口定义
AxiosTransformer:用于在请求或响应被处理之前,对它们进行转换。可以是单个函数或函数数组。
export interface AxiosTransformer {
(data: any, headers?: any): any;
}
AxiosAdapter:定义 axios
适配器的接口,适配器负责实际发送请求。
export interface AxiosAdapter {
(config: AxiosRequestConfig): AxiosPromise<any>;
}
AxiosBasicCredentials:用于 HTTP 基本认证的用户名和密码。
export interface AxiosBasicCredentials {
username: string;
password: string;
}
AxiosProxyConfig:定义代理服务器的主机名和端口号。
export interface AxiosProxyConfig {
host: string;
port: number;
}
请求配置接口
AxiosRequestConfig:描述axios
请求的各种配置选项
export interface AxiosRequestConfig {
url?: string;
method?: string;
baseURL?: string;
transformRequest?: AxiosTransformer | AxiosTransformer[];
transformResponse?: AxiosTransformer | AxiosTransformer[];
headers?: any;
params?: any;
paramsSerializer?: (params: any) => string;
data?: any;
timeout?: number;
withCredentials?: boolean;
adapter?: AxiosAdapter;
auth?: AxiosBasicCredentials;
responseType?: string;
xsrfCookieName?: string;
xsrfHeaderName?: string;
onUploadProgress?: (progressEvent: any) => void;
onDownloadProgress?: (progressEvent: any) => void;
maxContentLength?: number;
validateStatus?: (status: number) => boolean;
maxRedirects?: number;
httpAgent?: any;
httpsAgent?: any;
proxy?: AxiosProxyConfig;
cancelToken?: CancelToken;
}
url
: 请求的 URL。method
: 请求方法(GET、POST 等)。baseURL
: 基础 URL,会与url
结合。transformRequest
和transformResponse
: 请求和响应的转换函数或函数数组。headers
: 自定义的 HTTP 头。params
: URL 查询参数。paramsSerializer
: 自定义查询参数序列化函数。data
: 请求体数据。timeout
: 请求超时时间。withCredentials
: 跨域请求时是否携带凭证。adapter
: 自定义适配器。auth
: HTTP 基本认证信息。responseType
: 响应数据类型(‘json’, ‘blob’, ‘document’, ‘arraybuffer’, ‘text’, ‘stream’)。xsrfCookieName
和xsrfHeaderName
: XSRF 相关配置。onUploadProgress
和onDownloadProgress
: 上传和下载进度事件处理函数。maxContentLength
: 最大响应内容长度。validateStatus
: 自定义状态码验证函数。maxRedirects
: 最大重定向次数。httpAgent
和httpsAgent
: 定制 Node.js 的 HTTP 和 HTTPS 请求行为。proxy
: 代理配置。cancelToken
: 用于取消请求的标记。
响应和错误接口
AxiosResponse:描述 axios
响应的结构
export interface AxiosResponse<T = any> {
data: T;
status: number;
statusText: string;
headers: any;
config: AxiosRequestConfig;
request?: any;
}
data
: 响应数据。status
: HTTP 状态码。statusText
: HTTP 状态文本。headers
: 响应头。config
: 请求配置。request
: 请求对象。
AxiosError:描述 axios
错误的结构。
export interface AxiosError extends Error {
config: AxiosRequestConfig;
code?: string;
request?: any;
response?: AxiosResponse;
}
拦截器接口
AxiosInterceptorManager:管理 axios
拦截器的接口。
use
: 添加拦截器函数,返回拦截器 ID。
eject
: 移除拦截器函数。
export interface AxiosInterceptorManager<V> {
use(onFulfilled?: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
eject(id: number): void;
}
AxiosInstance
和 AxiosStatic
接口
AxiosInstance:描述 axios
实例的方法和属性
export interface AxiosInstance {
defaults: AxiosRequestConfig;
interceptors: {
request: AxiosInterceptorManager<AxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
};
request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
head(url: string, config?: AxiosRequestConfig): AxiosPromise;
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
}
描述 axios
实例的方法和属性。* defaults
: 默认配置。
interceptors
: 请求和响应的拦截器管理器。- 各种 HTTP 方法(
request
,get
,delete
,head
,post
,put
,patch
)。
AxiosStatic:描述 axios
静态方法和属性
export interface AxiosStatic extends AxiosInstance {
(config: AxiosRequestConfig): AxiosPromise;
(url: string, config?: AxiosRequestConfig): AxiosPromise;
create(config?: AxiosRequestConfig): AxiosInstance;
Cancel: CancelStatic;
CancelToken: CancelTokenStatic;
isCancel(value: any): boolean;
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
}
create
: 创建一个新的axios
实例。Cancel
和CancelToken
: 取消请求相关的静态类型。isCancel
: 检查一个值是否为取消请求的对象。all
和spread
: 用于处理多个 Promise。