lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.67k stars 105 forks source link

It is not a ponyfill! #184

Open iegik opened 5 months ago

iegik commented 5 months ago

Whats the difference between Ponyfill and Polyfill?

Polyfill - checks if functionality is not available and add it:

if (!String.prototype.trim) String.prototype.trim = ...

Ponyfill - just the adds own same functionality:

function trim(){...}
import { trim } from '...'

What about whatwg-fetch on this project?

whatwg-fetch is Polyfill, but on acts like Ponyfill. Why?

WARNING: On the build phase global this is passed without fetch, so it hardcoded to overwrite the fetch function!!!! https://github.com/lquixada/cross-fetch/blob/v4.x/rollup.config.js

This will cause some unexpected behaviour of the fetch function.

For example, it throws an Error when request is aborted. Should it?

Abort should not throw the error:

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

It returns a Promise that resolves to the Response to that request — as soon as the server responds with headers — even if the server response is an HTTP error status. 

At the heart of Fetch are the Interface abstractions of HTTP Requests, Responses, and Headers, along with a fetch() method for initiating asynchronous resource requests

Fetch takes the asynchronous nature of such requests one step further. The API is completely Promise-based https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Basic_concepts#in_a_nutshell

https://xhr.spec.whatwg.org/#the-abort()-method


In other words, it should fire the 'abort' event and cancels the request and DO NOT throw an error. Because fetch is asynchronous* 
fetch is asynchronous, based on Promise, but resolved only when response is received.

iegik commented 5 months ago

Related: https://github.com/jasonkuhrt/graphql-request/pull/212

iegik commented 5 months ago

https://github.com/lquixada/cross-fetch/issues/172