Skip to content

Commit

Permalink
Merge pull request #11 from krutoo/default-headers-revision
Browse files Browse the repository at this point in the history
defaultHeaders revision
  • Loading branch information
krutoo authored Sep 28, 2024
2 parents 235fa3a + 51dd3f2 commit 9c9a3a0
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/middleware/default-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ export function defaultHeaders(
): Middleware {
return (request, next) => {
// IMPORTANT: for avoid mutate request, just create new Headers and Request here
const headers = new Headers(defaults);
const headers = new Headers(request.headers);

if (request.headers) {
new Headers(request.headers).forEach((value, key) => {
headers[strategy](key, value);
});
}
/*
Previously, there was a different approach here: headers were created based on "defaults" argument, then headers from the request were added to them.
This was done so that the "default headers" were truly default and were overridden by what was set by the developer in the request itself.
But it didn't work well because browser always had the "Content-Type" header set by default, which always overridden the option that was in the middleware factory arguments.
To fix this, default headers are now added to the request headers
*/
new Headers(defaults).forEach((value, key) => {
headers[strategy](key, value);
});

return next(new Request(request, { headers }));
};
Expand Down

0 comments on commit 9c9a3a0

Please sign in to comment.