developit / developit/redaxios

Not possible to transform headers per request

Open
#82 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
4.9k
Forks
104
PR merge metrics
No merged PRs in 30d

Description

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),
},
}),
});
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.