kwhitley / itty-fetcher

An even simpler wrapper around native Fetch to strip boilerplate from your fetching code!
MIT License
99 stars 4 forks source link

query params not properly reconstructed if method != GET #42

Closed lettucebowler closed 1 year ago

lettucebowler commented 1 year ago

I believe the issue is in this following code snipped from index.ts:

let [urlBase, queryParams = ''] = url_or_path.split('?')

if (method === 'GET' && payload && typeof payload === 'object') {
    const merged = new URLSearchParams(queryParams)

    // @ts-expect-error ignore this
    const entries = (payload instanceof URLSearchParams ? payload : new URLSearchParams(payload)).entries()
    for (let [key, value] of entries) merged.append(key, value)

    queryParams = '?' + merged.toString()
    payload = null
}

const full_url = (options.base || '') + urlBase + queryParams

if the method is not GET, then when the url is reconstructed, the ? is lost, so a url like https://api.example.com/example?query=string becomes https://api.example.com/examplequery=string

This seems like a pretty simple fix, just need to move the logic to prepend ? to the querystring from inside the conditional out into the assignment to full_url. I'd be happy to submit a PR to make this change if preferred.

kwhitley commented 1 year ago

This should be addressed in the latest patch version - lemme know!

kwhitley commented 1 year ago

specifically v0.9.4 :)

kwhitley commented 1 year ago

commit (for reference): d9e714c