Namespace: Requests:createFetchRequest

Requests:createFetchRequest

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
Name Type Attributes Default Description
method String <optional>
<nullable>
'GET'

indicates the request method to be performed on the target resource (one of "GET", "POST", "PUT", "PATCH", "HEAD", "OPTIONS" or "DELETE")

headers Object <optional>
<nullable>

an object containing additional information to be sent with the request (e.g. {"Content-Type": "application/json"} to indicate a JSON-typed request body)

credentials String <optional>
<nullable>

accepts an "include" string, which will allow both CORS and same origin requests to work with cookies; the method won't send or receive cookies otherwise; the "same-origin" value is not supported

body Object | String <optional>

the content to be transmitted in request's body; common content types include FormData, JSON, Blob, ArrayBuffer or plain text

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:
use this via the "execute" method, which resolves to a FetchResponse or rejects with error in case of a technical request error (request is not completable)
Type
Requests.FetchRequest
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(); })
;