Fetch Client
The Aurelia Fetch Client wraps the native browser Fetch API and provides helpers and configuration options to make working with it more "Aurelia friendly"
Aurelia provides a fetch client which wraps the native Fetch API. It allows you to work with Fetch in an Aurelia-style way and provides some helper methods and configuration options to make working with Fetch easier.
Using the Fetch Client
The Fetch client ships with Aurelia and requires no additional installation. To use it, import the HttpClient
from the @aurelia/fetch-client
package.
In most cases, you want to inject a new instance of the Fetch client into your application. You can achieve this in a couple of different ways.
The first approach you can take is creating a new instance of the HttpClient
and referencing it like the native Fetch API.
You can also inject a new instance into your component or service class using the following approach: injecting the Fetch client with the newInstanceOf
decorator. This will ensure our component gets a new instance of the Fetch client.
Taking a service-based approach to encapsulating your HTTP calls, you might create something like this:
Configuring the fetch client
Many of the configuration options available to the native Fetch API are also available in the Aurelia Fetch Client. You can set default headers, create interceptors (more on that further down) and more.
requestError
acts as a Promise rejection handler during Request creation and request interceptor execution. It will receive the rejection reason and can either re-throw, or recover by returning a valid Request.responseError
is similar torequestError
, and acts as a Promise rejection handler for response rejections.
These methods on the interceptor object can also return a Promise
for their respective return values.
Fetch helpers
There are some caveats with the default Fetch implementation around error handling that Aurelia provides helper methods to work with conveniently.
config.rejectErrorResponses()
will add a response interceptor that causes responses with unsuccessful status codes to result in a rejected Promise.config.useStandardConfiguration()
will applyrejectErrorResponses()
, and also configurecredentials: 'same-origin'
as a default on all requests.The Fetch API has no convenient way of sending JSON in the body of a request. Objects must be manually serialized to JSON, and the
Content-Type
header must be set appropriately. the Fetch package includes a helper calledjson
for this.
Posting JSON
Working with authentication bearer tokens
A common scenario in applications using authentication is to use bearer tokens sent in the headers of each request.
To achieve this, we can use a request interceptor to add the token to the headers of each request. The following example assumes you are getting the bearer token from somewhere (session or local storage, etc.).
Last updated
Was this helpful?