-
Notifications
You must be signed in to change notification settings - Fork 44
Description
It would be useful to be able to get the status code, headers, cookies, etc from the response as sometimes that information is required to make decisions based on the response. For example, an API I use at the moment sometimes returns 204 status codes, which are successful responses that contain no data. Currently this just returns a data as an empty string from the smart body parsing, but the API can also return a 200 with an empty string, which has a different meaning. The only way to discern between the two is to check the status code, which isn't currently provided.
I think the best solution is to make the return type of the fetch function always be an object, even when throw: true is set, so it can return extra fields:
const { data, status, headers } = $fetch(....);This would be a breaking change. The only solution I can think of that would be "less breaking" is returning the response data wrapped in a Proxy instance, and making the extra information as getter functions, so you can do something like:
const data = $fetch(....);
data.my_field;
data.satus();
data.cookies();
// etc...but that's quite a janky solution, and in theory could break someones codes if they were relying on some behaviour like this.