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

fix to handle the json parsing error that occurs when an api response is empty (no content) #38

Closed sowmyab9 closed 3 years ago

sowmyab9 commented 3 years ago

Description

This PR fixes an error that is thrown when api responds with a 204 or empty response.

Motivation and Context

When an API returns 204 or a response that is empty, currently an error is thrown as "Unexpected end of JSON input". Resulting in the data object being returned as undefined and triggering the ERROR action. This PR catches this error and returns a .text() version. So that the result is consistent with Fetchye response. The response will have a data object with a status, ok, headers and an empty body. Error object will remain undefined.

How Has This Been Tested?

i have added unit tests to test this scenario and also updated existing tests. simulated the scenario with fetch and real apis returning 204 and 201(with empty response)

Types of Changes

Checklist:

What is the Impact to Developers Using Fetchye?

Developers can continue using Fetchye even when their APIs return no data :)

CLAassistant commented 3 years ago

CLA assistant check
All committers have signed the CLA.

codesandbox-ci[bot] commented 3 years ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit db0aee1445cea2bab00374b6f4345875c074195e:

Sandbox Source
quick-install Configuration
fetchye-provider-install Configuration
fetchye-redux-provider-install Configuration
nextjs-fetchye-ssr Configuration