axios 轴url模板拦截器抛出错误

yc0p9oo0  于 2023-06-22  发布在  iOS
关注(0)|答案(1)|浏览(155)

根据文档axios-url-template,我正在尝试为我的reacttypescript应用程序使用url模板拦截器,

axios.interceptors.request.use(urlTemplateInterceptor({
  urlAsTemplate: true,
}));

但它抛出以下错误。我错过了什么?

Argument of type '(config: AxiosRequestConfig<any>) => AxiosRequestConfig<any>' is not assignable to parameter of type '(value: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>'.
      Type 'AxiosRequestConfig<any>' is not assignable to type 'InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>'.
        Type 'AxiosRequestConfig<any>' is not assignable to type 'InternalAxiosRequestConfig<any>'.
          Types of property 'headers' are incompatible.
            Type 'AxiosHeaders | (Partial<RawAxiosHeaders & { Accept: AxiosHeaderValue; "Content-Length": AxiosHeaderValue; "User-Agent": AxiosHeaderValue; "Content-Encoding": AxiosHeaderValue; Authorization: AxiosHeaderValue; } & { ...; }> & Partial<...>) | undefined' is not assignable to type 'AxiosRequestHeaders'.
              Type 'undefined' is not assignable to type 'AxiosRequestHeaders'.
                Type 'undefined' is not assignable to type 'Partial<RawAxiosHeaders & { Accept: AxiosHeaderValue; "Content-Length": AxiosHeaderValue; "User-Agent": AxiosHeaderValue; "Content-Encoding": AxiosHeaderValue; Authorization: AxiosHeaderValue; } & { ...; }>'.ts(2345)
    (alias) urlTemplateInterceptor(options?: UrlTemplateInterceptorOptions | undefined): (config: AxiosRequestConfig<any>) => AxiosRequestConfig<any>
    import urlTemplateInterceptor
3duebb1j

3duebb1j1#

根据需要实现了自定义拦截器,并且工作正常。

// just remove the templates as we wont be using /{id} pattern
axiosClient.interceptors.request.use((config) => {
  if (!config.url) {
    return config;
  }

  if (config.url.indexOf("{") === -1) {
    return {
      ...config,
      url: config.url,
    };
  } else {
    return {
      ...config,
      url: config.url.split("{")[0],
    };
  }
});

相关问题