axios的最新封装,解决类型AxiosRequestConfig不能赋值给InternalAxiosReqe;CreateAxiosDefaults不能赋值给AxiosRequestConfig

在最新的axios封装中,可能会出现,以下两个问题:

① 类型CreateAxiosDefaults不能赋值给AxiosRequestConfig

类型"CreateAxiosDefaults<any>'的参数不能赋给类型“AxiosRequestConfig<any>”的参数。
属性headers'的类型不兼容。
不能将类型"AxiosHeaders|Partial<HeadersDefaults>|Partial<RawAxiosHeaders&{Accept:
AxiosHeaderValue;"Content-Length":AxiosHeaderValue;"User-Agent":AxiosHeaderValue;"Content-
Encoding'":AxiosHeaderValue;Authorization:AxiosHeaderValue;}&{..;}>|undefined'分配给类型
"AxiosHeaders (Partial<RawAxiosHeaders Accept:AxiosHeaderValue;"Content-Length":
AxiosHeaderValue;"User-Agent":AxiosHeaderValue;"Content-Encoding":AxiosHeaderValue;
Authorization:AxiosHeaderValue;}{...;}>Partial<...>)undefined".
不能将类型Partial<HeadersDefaults>P分配给类型“AxiosHeaders|(Partial<RawAxiosHeaders&{
Accept:AxiosHeaderValue;"Content-Length":AxiosHeaderValue;"User-Agent":AxiosHeaderValue;
"Content-Encoding":AxiosHeaderValue;Authorization:AxiosHeaderValue;}{...;}>
Partial<...>)undefined”。
不能将类型Partial<HeadersDefaults>”分配给类型Partial<RawAxiosHeaders&{Accept
:

通过查看源码我们可以发现 ,原来的类型AxiosRequestConfig已变成了CreateAxiosDefaults。但CreateAxiosDefaults又继承了AxiosRequestConfig。

 ② 类型AxiosRequestConfig不能赋值给InternalAxiosRequestConfig

类型“(config:AxiosRequestConfig<any>)=>AxiosRequestConfig<any>'的参数不能赋给类型“(value:
InternalAxiosRequestConfig<any>)=>InternalAxiosRequestConfig<any>
Promise<InternalAxiosRequestConfig<any>>'的参数。
不能将类型"AxiosRequestConfig<any>'分配给类型"InternalAxiosRequestConfig<any>
Promise<InternalAxiosRequestConfig<any>>".
不能将类型“AxiosRequestConfig<any>”分配给类型"InternalAxiosRequestConfig<any>”。
属性headers的类型不兼容。
不能将类型"AxiosHeaders|(Partial<RawAxiosHeaders&{Accept:AxiosHeaderValue;"Content-
Length":AxiosHeaderValue;"User-Agent":AxiosHeaderValue;"Content-Encoding":AxiosHeaderValue;
Authorization:AxiosHeaderValue;}&{...;}>&Partial<...>)|undefined分配给类型
"AxiosRequestHeaders”。
不能将类型undefined'分配给类型“AxiosRequestHeaders”。
不能将类型undefined'分配给类型"Partial<RawAxiosHeaders&{Accept:AxiosHeaderValue;

 同理查看源码,可以发现request的最新类型为InternalAxiosRequestConfig,InternalAxiosRequestConfig又继承于AxiosRequestConfig。

以上两种继承,父类的作用范围是小于子类的

解决方法:

原先axios.create中CreateAxiosDefaults类型的改变影响不大,继续使用它的父类AxiosRequestConfig。

改变的地方:在使用拦截器时,使用InternalAxiosRequestConfig而不使用AxiosRequestConfig

完整代码如下:
import axios from 'axios'
import type {
  AxiosInstance,
  AxiosRequestConfig,
  InternalAxiosRequestConfig,
  AxiosResponse
} from 'axios'

interface HYRequestInterceptors {
  requestInterceptor?: (
    config: InternalAxiosRequestConfig
  ) => InternalAxiosRequestConfig
  requestInterceptorCatch?: (error: any) => any
  responseInterceptor?: (res: AxiosResponse) => AxiosResponse
  responseInterceptorCatch?: (error: any) => any
}

interface HYRequestConfig extends AxiosRequestConfig {
  interceptors?: HYRequestInterceptors
}

class HYRequest {
  instance: AxiosInstance
  interceptors?: HYRequestInterceptors

  constructor(config: HYRequestConfig) {
    this.instance = axios.create(config)
    this.interceptors = config.interceptors

    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    )

    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )
  }

  request(config: AxiosRequestConfig) {
    this.instance.request(config).then((res) => {
      console.log(res)
    })
  }
}

export default HYRequest
 进行模块化处理后:

request/type.ts

import {
  InternalAxiosRequestConfig,
  AxiosResponse,
  AxiosRequestConfig
} from 'axios'
export interface HYRequestInterceptors {
  requestInterceptor?: (
    config: InternalAxiosRequestConfig
  ) => InternalAxiosRequestConfig
  requestInterceptorCatch?: (error: any) => any
  responseInterceptor?: (res: AxiosResponse) => AxiosResponse
  responseInterceptorCatch?: (error: any) => any
}

export interface HYRequestConfig extends AxiosRequestConfig {
  interceptors?: HYRequestInterceptors
}

 request/index.ts

import axios from 'axios'
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
import type { HYRequestInterceptors, HYRequestConfig } from './type'

class HYRequest {
  instance: AxiosInstance
  interceptors?: HYRequestInterceptors

  constructor(config: HYRequestConfig) {
    this.instance = axios.create(config)
    this.interceptors = config.interceptors

    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    )

    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )
  }

  request(config: AxiosRequestConfig) {
    this.instance.request(config).then((res) => {
      console.log(res)
    })
  }
}

export default HYRequest

使用:

  index.ts

// service统一出口
import HYRequest from './request'
import { BASE_URL, TIME_OUT } from './request/config'

const hyRequest = new HYRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config) => {
      console.log('请求成功的拦截')

      return config
    },
    requestInterceptorCatch: (err) => {
      console.log('请求失败的拦截')

      return err
    },
    responseInterceptor: (res) => {
      console.log('请求成功的拦截')

      return res
    },
    responseInterceptorCatch: (err) => {
      console.log('响应成功的拦截')

      return err
    }
  }
})

export default hyRequest
拦截器的种写法可以参考我其他博客,这里先不写了
  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
封装axios的post请求时,可以通过开放config参数来实现灵活性和定制化。在引用中的代码中,可以看到在Post方法中有一个config参数,它可以用来传递定制化的配置。当我们调用Post方法时,可以传入一个包含配置信息的config对象,例如baseURL、headers等。这样就可以根据不同的需求来定制请求的配置参数了。而如果不传入config参数,请求会使用默认的配置。在引用的解释中,提到了开放config参数后的优点和问题,包括请求的灵活性和默认值的处理方式等。在引用中的代码中,我们可以看到在Axios类的构造函数中,通过将传入的instanceConfig赋值给this.defaults来实现默认配置的设置。同时,在request方法中,使用mergeConfig方法将传入的config与默认配置进行合并,以便得到最终的请求配置。通过这些操作,我们可以实现对axios post请求的封装,并根据需要进行定制化配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [axios封装时对config参数的一点思考](https://blog.csdn.net/qq_45020818/article/details/131006517)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值