- Source:
Methods
createFetchRequest(url, optionsopt, nullable, useNativeopt) → {Requests.FetchRequest}
This method creates a ponyfilled fetch request based on "unfetch", but basically fulfilling the signature of a native fetch request.
The reasoning for this is to provide a baseline fetch implementation for all requests of annex, as long as we still support non ES6 browsers or old implementations in any way. During transpilation with core js, fetch does not automatically get polyfilled, so we need to do this ourselves and to actually stay testable, we provide the polyfill as long as we might target legacy contexts. As soon as we drop legacy contexts, we can immediately also remove this method and its uses.
The function signature is the same as "unfetch"'s and all non-implemented features are absent here as well.
All usual responses (40X and 50X as well) resolve, only uncompletable requests, such as those being prevented by a general network error, reject with the provided error.
Set ANNEX_USE_NATIVE_FETCH on window (true/false/'auto') to force useNative setting for all annex requests globally.
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
url |
String | the complete URL to query |
|||||||||||||||||||||||||||
options |
Object |
<optional> <nullable> |
null | the request options Properties
|
|||||||||||||||||||||||||
useNative |
Boolean | String |
<optional> |
false | determines if the native Fetch implementation of the browser should be used, true forces usage, "auto" uses it only if available |
- Source:
- See:
Returns:
Example
createFetchRequest('/foo').execute()
.then(r => r.text())
.then(txt => console.log(txt))
;
createFetchRequest(
'/bear',
{
method : 'POST',
headers : {'Content-Type' : 'application/json'},
body : JSON.stringify({hungry : true})
})
.execute()
.then(r => { open(r.headers.get('location')); return r.json(); })
;