Skip to content

undefined responses when used with React Native #324

@qnp

Description

@qnp

Environment

React Native (all versions)

Reproduction

It is difficult to provide a react-native starter as I'm not a specialist, and it will require that you install and setup everything to start a react-native project. Instead, you could rely on my detailed investigation

Describe the bug

Hi,

in the light of this issue,

Looking deeper into the commit linked by @pi0 to close the issue, and having the same issue as @gtjamesa (responses are all undefined), it seems that this commit will not fix the undefined responses issue.

I dug into ofetch source code and did my own investigation. It appears that there is a boolean hasBody which checks if response.body exists, as expected in fetch Response spec, see:

ofetch/src/fetch.ts

Lines 172 to 173 in bb2d72b

const hasBody =
context.response.body &&

Yet, only these fields are available in the response object returned by fetch in React Native:
_bodyBlob, _bodyInit, bodyUsed, headers, status, statusText, type.

Indeed, react-native uses whatwg-fetch polyfill.

We can easily see in whatwg-fetch source code that all these keys are defined by the polyfill on this, but not body.

The correct answer would be to create an issue on whatwg-fetch so that they update their code and comply with Fetch API, and then create an issue on React Native to use this updated library, and then update React Native. Nevertheless, this process can take a while, and (as it is the case for our projects) many React Native projects are stuck to older versions because they depend on other libs that breaks and haven't updated yet and an update to the latest is very difficult.

The shorter answer would be to adapt ofetch so that it also checks specific whatwg-fetch fields in the response to know if a body is available.

What do you think ?

Additional context

No response

Logs

No response

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