Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require interceptors to return values breaks some interceptor usage #5117

Open
KieraDOG opened this issue Oct 13, 2022 · 4 comments
Open

Require interceptors to return values breaks some interceptor usage #5117

KieraDOG opened this issue Oct 13, 2022 · 4 comments

Comments

@KieraDOG
Copy link

KieraDOG commented Oct 13, 2022

Describe the bug

Here is how we use interceptor to extract data from response rather than duplicate the same logic .then((res) => res.data) everywhere in the project

api.interceptors.response.use((res) => res.data)

however because of the new changes from #4874,

Sensible interceptors must return either an AxiosRequestConfig or an AxiosResponse or else they break other interceptors and Axios internal logic.

The PR is to required interceptor to return data, but it ends up not only require to return data, but require to return axios response type data, thus it limited the usage of interceptors, just wanna to confirm whether this is in purpose or it is a bug

To Reproduce

// Example code here
// In Typescript, write interceptor like this:

api.interceptors.response.use((response: AxiosResponse): unknown => response.data)

// Type 'unknown' is not assignable to type 'AxiosResponse<any, any> | Promise<AxiosResponse<any, any>>'.

Expected behavior

Expected code running without type error

Environment

  • Axios Version [1.1.2]
  • Adapter [e.g. XHR/HTTP]
@ghost
Copy link

ghost commented Nov 3, 2022

I'm using version 1.1.3, and Ican use it like this:

ai.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    return Promise.reject(error)
  }
)

But, when I use it like this:

interface CustomAxiosResponseData<T = unknown> {
  code: string
  data: T
  message: string
}

ai.interceptors.response.use(
  (response) => {
    const { data }: { data: CustomAxiosResponseData } = response
    return data
  },
  (error) => {
    return Promise.reject(error)
  }
)

When I switch to using version 0.27.2, it works well.
Is this right? Or is there any way to change the returned value type in a response interceptor at version 1.1.3?

@lxy2018
Copy link

lxy2018 commented Nov 18, 2022

I have the same problem and cannot fix it.
So, I chose to ignore this stupid bug(or feature) by using // @ts-ignore.

@alili
Copy link

alili commented Dec 13, 2022

api.interceptors.response.use((res) => res.data)

I think it is a very common and reasonable use that should not be completely prohibited by the framework

@KieraDOG
Copy link
Author

KieraDOG commented Sep 5, 2023

Hi axios team, any update on this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants