concept aurelia - fetch - client in category aurelia

This is an excerpt from Manning's book Aurelia in Action.
Suppose that when your page loads, you want to retrieve a greeting from a REST API and render it in the app view. To load this data, you can hook into the created callback method from the component lifecycle (as shown in figure 1.11), or the activate callback method from the router lifecycle in the case of a routed component (covered in chapter 9). Zooming in on the data-retrieval area of your map shows the app.js view-model using an external service called app-service to retrieve data from a backend API using aurelia-fetch-client. Figure 1.11 illustrates a typical workflow used to retrieve data from an HTTP API and render it in the DOM.
Listing 2.9. Initial implementation of BookAPI (book-api.js)
import {HttpClient} from 'aurelia-fetch-client'; #1 import {inject} from 'aurelia-framework'; #2 @inject(HttpClient) #3 export class BookApi{ constructor(http){ #3 this.http = http; #3 } getBooks(){ return this.http.fetch('books.json') #4 .then(response => response.json()) #5 .then(books => { return books; #6 }); } }
aurelia-fetch-client is a wrapper over the browser’s native Fetch API. This article on the Mozilla Developer Network is a great resource if you’re interested in learning more about the Fetch API: https://developer.mozilla.org/en/docs/Web/API/Fetch_API. aurelia-fetch-client supports the same methods as the native Fetch API, but it also provides the following advantages:
Request tracking— Every request sent from aurelia-fetch-client is tracked, giving you an overview of the HTTP interaction across your application.