wretch icon indicating copy to clipboard operation
wretch copied to clipboard

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

Open dankochetov opened this issue 9 months ago • 2 comments

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 avatar May 10 '24 15:05 dankochetov

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.

dankochetov avatar May 10 '24 16:05 dankochetov

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?

elbywan avatar May 11 '24 08:05 elbywan