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

Headers merging issue when using fetch headers object #205

Closed
Phoenixmatrix opened this issue Nov 17, 2023 · 2 comments
Closed

Headers merging issue when using fetch headers object #205

Phoenixmatrix opened this issue Nov 17, 2023 · 2 comments
Labels

Comments

@Phoenixmatrix
Copy link

The headers merging logic assumes JavaScript objects and key value pairs. The TypeScript types though allow using fetch Headers. When using fetch Headers, since they're not plain object, anything that sets headers will override them (eg: using a .post with an object as argument, or the .content helper).

I think the easiest fix is to tweak the types as to now allow fetch Headers as argument in the types, but alternatively a slightly smarter merge logic that accounts for Headers object would work too. Threw me in for a loop digging why my authorization headers were going poof right before a post request but not a get.

@elbywan
Copy link
Owner

elbywan commented Nov 19, 2023

Hey @Phoenixmatrix,

The headers merging logic assumes JavaScript objects and key value pairs. The TypeScript types though allow using fetch Headers. When using fetch Headers

Thanks for reporting this issue, I just published v2.7.1 which now converts the headers() argument to a record and will merge properly no matter the type.

const headers1 = new Headers({ "hello": "world" });
const headers2 = new Headers({ "bonjour": "le monde" });
const headers3 = { "hola": "mundo " };
const headers4 = [["hallo", "welt"]]

let w = wretch().headers(headers1);
console.log(w._options.headers);
// Object { hello: "world" }
w = w.headers(headers2);
console.log(w._options.headers);
// Object { hello: "world", bonjour: "le monde" }
w = w.headers(headers3);
console.log(w._options.headers);
// Object { hello: "world", bonjour: "le monde", hola: "mundo " }
w = w.headers(headers4);
console.log(w._options.headers);
// Object { hello: "world", bonjour: "le monde", hola: "mundo ", hallo: "welt" }

Threw me in for a loop digging why my authorization headers were going poof right before a post request but not a get.

And sorry about this 😬

@elbywan elbywan closed this as completed Nov 19, 2023
@Phoenixmatrix
Copy link
Author

Awesome, thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants