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

Axios instance not callable from within custom class #4606

Open
lekterable opened this issue Apr 18, 2022 · 1 comment
Open

Axios instance not callable from within custom class #4606

lekterable opened this issue Apr 18, 2022 · 1 comment

Comments

@lekterable
Copy link

lekterable commented Apr 18, 2022

Describe the bug

Axios instance call doesn't return when called from within a custom class. I suspect it's related to the internal this keyword.

To Reproduce

The custom Axios class:

class Axios {
  #instance: AxiosInstance

  constructor({ baseURL }: Config) {
    this.#instance = axios.create({
      baseURL,
      headers: { 'Content-Type': 'application/json' },
    })

    this.#setupInterceptors()
  }

...

  async #interceptResponseError(
    error: AxiosError,
  ): Promise<AxiosError | AxiosResponse> {
    if (error.response.status === 401) {
      await refreshAuthorization()

      return await this.#instance(error.config) // This never returns
    }

    return await Promise.reject(error.response)
  }


}

export default Axios

Expected behavior

I would expect this code to work without errors and if it's necessary to use this internally I would somehow make it bound permanently so that it doesn't break when used with classes.

Environment

  • Axios Version 0.26.1
  • Adapter default
  • Browser IOS Simulator
  • Browser Version -
  • Node.js Version 16.5.0
  • OS: -
  • Additional Library Versions: React 17.0.1, React Native 0.64.3

Additional context/Screenshots

I've confirmed that it's not a problem with my code by moving the const instance = axios.create outside of class and using this variable instead of this.#instance. It does make the code work, but it creates a monolith which I wanted to avoid.

@lekterable lekterable changed the title Axios instance not callable from custom class Axios instance not callable from within custom class Apr 18, 2022
@lekterable
Copy link
Author

I think I managed to repro this on codesandbox:
https://codesandbox.io/s/optimistic-tdd-sipe0u?file=/src/index.ts

The expected output is:

[ERROR]
[RETRYING]
[ERROR]

but with the this.onResponseError uncommented it never logs the second [ERROR] after [RETRYING]

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

2 participants