Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to text body #96

Merged
merged 7 commits into from
Sep 16, 2021
Merged

Fall back to text body #96

merged 7 commits into from
Sep 16, 2021

Conversation

chawes13
Copy link
Contributor

@chawes13 chawes13 commented Sep 6, 2021

Note: Changes dependent on #95

Resolves #93

This adds a new option which will determine whether or not to perform a "strict" JSON parse on the response's body. Strict in this sense essentially means response.json().catch(() => null). This will be the default behavior, which matches how the behavior currently works in v4.1.9. If the option is set to false, then a failed parsing will default to returning the text of the body, which will be left to the client to handle appropriately.

The use case that we're intending to solve for here is when a 3rd Party API doesn't escape a string response. E.g.,

getThirdPartyToken() // response: 123AZY
// vs.
getThirdPartyToken() // response: "123AZY"

The first response will fail JSON.parse, while the other will succeed.

This implementation was inspired by Axios' implementation:
Image 2021-09-06 at 9 24 40 AM

Copy link

@bhennes2 bhennes2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Was a bit turned around on the mockResponse within the http.js file but I follow the ?? operator now.

@chawes13 chawes13 merged commit 36a6bbb into master Sep 16, 2021
@chawes13 chawes13 deleted the feature/fallback-to-text-body branch September 16, 2021 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't consume api responses returning single strings
2 participants