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

utils.merge does not merge headers correctly if config.headers is an instance of AxiosHeaders #5283

Open
magom001 opened this issue Nov 18, 2022 · 3 comments

Comments

@magom001
Copy link

magom001 commented Nov 18, 2022

Describe the bug

contextHeaders = headers && utils.merge(

if headers.common is not an instance of AxiosHeaders then headers[config.method] (config.method === 'get') will return not the headers object but a reference to the getter method of the AxiosHeaders class instance.

In that case the utils.merge will fall into the default else case:

const assignValue = (val, key) => {

And assign a function to the headers object. The request is malformed at this point.

To Reproduce

https://github.com/magom001/axios-jwt-auth/tree/1682dd6bdd20b1d4a619aff71554f92f14ae1b1e, update axios to 1.1.3 and run unit tests.

Code snippet

const responseErrorInterceptor =
  (
    axios: AxiosInstance,
    {
      tokensStorage = defaultTokensStorage,
      shouldRefresh = defaultShouldRefresh,
      refreshTokens,
      onFailedToRefresh,
    }: Config,
  ) =>
  async (error: AxiosError) => {
    if (await shouldRefresh(error)) {
      if (refreshPromise) {
        await refreshPromise;

        return axios.request(error.config as any);
      }

      refreshPromise = new Promise(async (resolve, reject) => {
        try {
          const refreshToken = await tokensStorage.getRefreshToken();
          if (!refreshToken) {
            throw error;
          }

          const tokens = await refreshTokens(refreshToken);

          await tokensStorage.saveTokens(tokens);

          resolve(tokens.accessToken);
        } catch (e) {
          await tokensStorage.clearTokens();

          reject(e);

          onFailedToRefresh?.(e as AxiosError<any>);
        } finally {
          refreshPromise = null;
        }
      });

      await refreshPromise;

      return axios.request(error.config as any);
    }

    throw error;
  };


### Expected behavior

Headers are merged correctly. Headers object is well formed.

### Axios Version

1.1.3

### Adapter Version

n/a

### Browser

n/a

### Browser Version

n/a

### Node.js Version

n/a

### OS

n/a

### Additional Library Versions

_No response_

### Additional context/Screenshots

_No response_
@DigitalBrainJS
Copy link
Collaborator

1.1.3

I believe this has been fixed in the latest version.

@timothymctim
Copy link

1.1.3

I believe this has been fixed in the latest version.

Indeed, fixed in #5224, I guess.

@philBrown
Copy link

philBrown commented Dec 8, 2022

@DigitalBrainJS Shouldn't bug fixes appear in 1.1.4? This approach to "versioning" is just painful.

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

4 participants