elbywan / wretch

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

wretch throws error on redirect response #240

Closed nounder closed 3 months ago

nounder commented 3 months ago

Similar to fetch, wretch should follow redirects and only then resolve the response. Currently, without any user-defined error handling, it throws an error with unhelpful message of response body.

elbywan commented 3 months ago

Similar to fetch, wretch should follow redirects and only then resolve the response.

wretch uses fetch under the hood.

const api = wretch("https://httpstat.us")
const cb = res => console.log({ status: res.status, redirected: res.redirected, url: res.url });

[301, 302, 303, 307, 308].forEach(code => {
  api.url(`/${code}`).get().res(cb)
  // { status: 200, redirected: true, url: 'https://httpstat.us/' }
})