Skip to content

Return more data about the response #71

@DexterHill0

Description

@DexterHill0

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions