americanexpress / fetchye

✨ If you know how to use Fetch, you know how to use Fetchye [fetch-yae]. Simple React Hooks, Centralized Cache, Infinitely Extensible.
Apache License 2.0
41 stars 21 forks source link

`headers` and `status` are not provided when response `body` is undefined #35

Closed AdamFoskit closed 3 years ago

AdamFoskit commented 3 years ago

🐞 Bug Report

If an API call has a response body of undefined, the useFetchye hook's data returned is undefined, and does not provide the status or headers from the response.

To Reproduce

Use the useFetchye hook to query an API that returns an undefined body. (Possible for status 201 and 204.)

Expected behavior

The data returned from the useFetchye hook should always contain the status and headers when there is a response from the API call.

Screenshots

Console log of output response from useFetchye hook for a response with an undefined body. image

Console log of output for the fetch for the same API call (before .json() was called on the response) image

System information

Additional context

ghost commented 3 years ago

Looks like the issue may be here.

try {
    res = await fetchClient(key, options);
    const body = await res.json();
    payload = {
      body,
      ok: res.ok,
      headers: headersToObject(res.headers),
      status: res.status,
    };
  } catch (requestError) {
    // eslint-disable-next-line no-console
    console.error(requestError);
    error = requestError;
  }

We're expecting the response to have a body retrievable by .json()

We should probably check if the response is ok, if not, get the error from res.text() and return the rest of the payload.

AdamFoskit commented 3 years ago

@jgolden17 Yeah, that looks like the problem.

oneamexbot commented 3 years ago

This issue is stale because it has been open 30 days with no activity.

oneamexbot commented 3 years ago

This issue is stale because it has been open 30 days with no activity.

JAdshead commented 3 years ago

this should be resolved by https://github.com/americanexpress/fetchye/pull/38