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

fix: use response._bodyInit as fallback for react-native and qq #398

Merged
merged 11 commits into from
Oct 8, 2024

Conversation

joshmossas
Copy link
Contributor

πŸ”— Linked issue

Fixes #324

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This pr is essentially a one line change to add react native support. It checks response._bodyInit in addition to response.body to see if a body exists in the response.

Normally I would say that this is an upstream issue that react native should fix, but the reality is fetch has not been spec compliant in react native for a long time, and it doesn't seem like there has been any meaningful progress in that department either. So I don't think it's practical to wait on them.

I would also argue that even though this is a runtime specific check it shouldn't add any overhead for maintainers because its such a small change. I've also included comments explaining what the additional _bodyInit check is for and under what conditions it can be removed.

Additionally this repo does have a precedent for runtime specific check. In ./src/fetch.ts line 84, there is a check for a chromium specific API. So this change would be in line with that.

if (Error.captureStackTrace) {

In all I think this is a worthwhile change as it would let users use the same fetch library across, node, browsers, and react native.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@pi0 pi0 changed the title feat: add react native support fix: react native body support Oct 8, 2024
@pi0 pi0 changed the title fix: react native body support fix: react native _bodyInit support Oct 8, 2024
Copy link

codecov bot commented Oct 8, 2024

Codecov Report

All modified and coverable lines are covered by tests βœ…

Project coverage is 70.14%. Comparing base (27996d3) to head (6c5317b).
Report is 53 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #398       +/-   ##
===========================================
+ Coverage   56.86%   70.14%   +13.28%     
===========================================
  Files          16       17        +1     
  Lines         728      536      -192     
  Branches      113      138       +25     
===========================================
- Hits          414      376       -38     
+ Misses        303      149      -154     
  Partials       11       11               

β˜” View full report in Codecov by Sentry.
πŸ“’ Have feedback on the report? Share it here.

src/fetch.ts Show resolved Hide resolved
@pi0 pi0 changed the title fix: react native _bodyInit support fix: use response._bodyInit as fallback for react-native and qq Oct 8, 2024
@pi0 pi0 merged commit 3617666 into unjs:main Oct 8, 2024
6 checks passed
@joshmossas
Copy link
Contributor Author

@pi0 so I just checked with React Native running on my android phone

When responseType is set to "stream" the _bodyInit is an instance of Blob. However it doesn't implement any of the methods specified in the Web standard such as _bodyInit.stream() or _bodyInit.text(). It only implements the following interface:

interface Blob {
  readonly size: number;
  readonly type: string;
  slice(start?: number, end?: number, contentType?: string): Blob;
}

So none of the standard methods are present. As far as I can tell there's no way to get the actual data out of it.

@joshmossas
Copy link
Contributor Author

Additionally logging the _bodyInit to the console outputs the following:

{"_data": {"__collector": {}, "blobId": "244dfb46-0c73-45f8-99be-cfd8b2bb971f", "offset": 0, "size": 1256}}

When normally logging a Blob would look like this:

Blob { size: 11, type: '' }

@pi0
Copy link
Member

pi0 commented Oct 8, 2024

I see, basically seems stream mode is broken.

Although now other types (text/json) work right?

@joshmossas
Copy link
Contributor Author

joshmossas commented Oct 8, 2024

Yes I both text and json work as expected

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.

undefined responses when used with React Native
2 participants