From 51dd3f258fd819ec17399cffaf9ed25ecbe26138 Mon Sep 17 00:00:00 2001 From: krutoo Date: Sat, 28 Sep 2024 16:14:15 +0500 Subject: [PATCH] defaultHeaders revision - now default headers adds to request.headers, not the other way around --- src/middleware/default-headers.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/middleware/default-headers.ts b/src/middleware/default-headers.ts index 0b733e6..e139de7 100644 --- a/src/middleware/default-headers.ts +++ b/src/middleware/default-headers.ts @@ -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 })); };