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

Not possible to transform headers per request #82

Open
ghost opened this issue May 5, 2022 · 0 comments
Open

Not possible to transform headers per request #82

ghost opened this issue May 5, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@ghost
Copy link

ghost commented May 5, 2022

I'm using transformRequest to add custom header to the request before it is sent the following way:

const apiClient = redaxios.create({
  baseURL,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json; charset=utf-8',
  },
  transformRequest: [(data, headers) => {
      console.log(headers['Payload-Signature']); // <-- here I see header set for previous request
      
      if (data) headers['Payload-Signature'] = signRequestBody(data);
  }],
});

As show in code above headers object is shared between requests and because of that it is not possible to customise headers per requests before one is sent.

The reason is that internal deepMerge function doesn't create a copy of 'options.header' property.

Expected behaviour:
I would expect it is possible to safely transform headers per request or modify other request props.

As a workaround I define a custom fetch:

const apiClient = redaxios.create({
  baseURL,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json; charset=utf-8',
  },
  fetch: (url, options) =>  fetch(url, {
    ...options,
    headers: {
      ...options.headers,
      'payload-signature': signRequestBody(options.body),
    },
  }),
});
@developit developit added the bug Something isn't working label Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant