-
Notifications
You must be signed in to change notification settings - Fork 158
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
fetch support #230
fetch support #230
Conversation
tl;dr - This commit adds support for fetch, fix pretenderjs#60. - pretender swap native fetch related API if exists - pretender.shutdown() restore native fetch related API - doesn't work with AbortController Changes ------ Include a fetch ponyfill and swap the native fetch during pretender creation, then restore them when `shutdown`. Since fetch polyfill uses xhr behind the scene, pretender should "just work". Caveats ------ 1. The supplement set of yetch impl and spec includes (not complete): - Inability to [set the redirect mode](JakeChampion/fetch#137) - Inability to [change the cache directive](JakeChampion/fetch#438 (comment)) - Inability to [disable same-origin cookies](JakeChampion/fetch#56 (comment)) 2. Abort - `xhr.abort()` first set state to done, finally response to a [network error](https://xhr.spec.whatwg.org/#the-abort()-method); - [fetch](https://dom.spec.whatwg.org/#aborting-ongoing-activities) will reject promise with a new "AbortError" DOMException. As implemented in `fake_xml_http_request`, the request is resolved once its state is changed to `DONE`. So the scenario happens in pretender is: 1). state changes to `DONE`, trigger resolve request 2). abort, trigger reject 3). xhr.onerror, trigger reject The first resolve wins, error thus not rejected but an empty request is resolved. 3. Though polyfilled by xhr, fetch returns a Promise and is asynchronous by nature.
Yetch doesn't provide a ponyfill now, include the file temporarily. |
Github/fetch has merged PR for UMD distribution: JakeChampion/fetch#616 Also abort support has been added: JakeChampion/fetch#592 We can use Github fetch instead of yetch now. But AbortableFetch is still not supported, see comment in test.
I released a fork version to have umd available, |
Any updates on this PR? I am super keen to consume fetch-pretender or ideally, pretender from a cdn like http://cdn.jsdelivr.net/npm/pretender. |
CI error: |
bower is migrating registry away from heroku after v1.8.2 https://github.com/bower/bower/releases/tag/v1.8.2
This is ready now. Need review @stefanpenner @rwjblue |
false | ||
); | ||
|
||
fetch('/some/path').then(function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is async, so this needs to block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is qunit lets use the async promise assert helpers
setTimeout(function() { | ||
controller.abort(); | ||
}, 10); | ||
fetch('/downloads', { signal: signal }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is qunit lets use the async promise assert helpers
Oops, I merged this prematurely. @xg-wang can you address the above two comments today? If not, i'll have to revert until addressed. |
tl;dr
fetch
. #60.Changes
Include a fetch ponyfill and swap the native fetch during pretender
creation, then restore them when
shutdown
. Since fetch polyfill usesxhr behind the scene, pretender should "just work".
Caveats
AbortUpdate: Fixed by Enable Abortable fetch #234xhr.abort()
first set state to done, finally response to anetwork error;
As implemented infake_xml_http_request
, the request is resolved once itsstate is changed to
DONE
.So the scenario happens in pretender is:
1). state changes to
DONE
, trigger resolve request2). abort, trigger reject
3). xhr.onerror, trigger reject
The first resolve wins, error thus not rejected but an empty request is resolved.
Though polyfilled by xhr, fetch returns a Promise and is asynchronous by
nature.
No streaming Streaming support JakeChampion/fetch#198