axioInterface接口解读

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 结合。
  • transformRequesttransformResponse: 请求和响应的转换函数或函数数组。
  • headers: 自定义的 HTTP 头。
  • params: URL 查询参数。
  • paramsSerializer: 自定义查询参数序列化函数。
  • data: 请求体数据。
  • timeout: 请求超时时间。
  • withCredentials: 跨域请求时是否携带凭证。
  • adapter: 自定义适配器。
  • auth: HTTP 基本认证信息。
  • responseType: 响应数据类型(‘json’, ‘blob’, ‘document’, ‘arraybuffer’, ‘text’, ‘stream’)。
  • xsrfCookieNamexsrfHeaderName: XSRF 相关配置。
  • onUploadProgressonDownloadProgress: 上传和下载进度事件处理函数。
  • maxContentLength: 最大响应内容长度。
  • validateStatus: 自定义状态码验证函数。
  • maxRedirects: 最大重定向次数。
  • httpAgenthttpsAgent: 定制 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;
}

AxiosInstanceAxiosStatic 接口

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 实例。
  • CancelCancelToken: 取消请求相关的静态类型。
  • isCancel: 检查一个值是否为取消请求的对象。
  • allspread: 用于处理多个 Promise。
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值