elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.83k stars 98 forks source link

QueryStringAddon converts `undefined` values into empty strings instead of omitting them #229

Closed dankochetov closed 5 months ago

dankochetov commented 6 months ago

For a query like this

wretch('https://example.com').addon(QueryStringAddon).query({ bar: 'qwe', foo: undefined }).post().json();

The produced URL is https://example.com?bar=qwe&foo= instead of https://example.com?bar=qwe

dankochetov commented 6 months ago

I checked the source code and saw the stringify function implementation that specifically converts undefined to an empty string, but IMO that's a misleading and dangerous behavior (at least for me, coming from axios, which removes keys with undefined values from the query string). I think it's a common pattern to pass optional query params to a request like that. Does it make sense to omit null and undefined values from the result? I can make a PR with this change. In fact, I've already done it since it's a one line change, feel free to close it if you disagree that it should work like that.

elbywan commented 6 months ago

Hey @dankochetov,

Does it make sense to omit null and undefined values from the result? I can make a PR with this change.

Thanks for the PR! I'm fine with adding this as long as it does not break existing code. Can you add an argument to the addon which would toggle on the new behaviour while preserving the existing as the default?