elbywan / wretch

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

Don't add empty queryString to URL #114

Closed xpuu closed 3 years ago

xpuu commented 3 years ago

Hi, once again thanks for your great work. I use this lib in our 3D Printer UI already for 3 years and love it. I'm used to do nasty things with her e.g.:

// id = undefined  - all printers
// id = string     - specific printer
// id = string[]   - specific printers
function getPrinterState(id) {
     return wretcher(api).query(id && { id }).get().json(res => typeof id === string ? res[id] : res)
}

But, as my overly attentive colleague pointed out, when you run .query(false) wretcher adds '?' or '&', which might confuse the api server.

I propose to add simple check to appendQueryParams on line 325, something like:

const split = url.split("?")

if (!queryString) 
    return replace ? split[0] : url

if (replace || split.length < 2)
    return split[0] + "?" + queryString

return url + "&" + queryString
elbywan commented 3 years ago

Hey @xpuu,

Hi, once again thanks for your great work. I use this lib in our 3D Printer UI already for 3 years and love it.

❤️

But, as my overly attentive colleague pointed out, when you run .query(false) wretcher adds '?' or '&', which might confuse the api server.

Thanks for reporting the issue! I've just pushed commit 15464a8 containing the fix. I'll also make a a patch release in a few minutes 📦.