Aurelia Fetch Client

1.8.2 · active · verified Tue Apr 21

aurelia-fetch-client provides a straightforward HTTP client built upon the standard Fetch API, tailored for use within the Aurelia framework ecosystem. It is currently at version 1.8.2 and receives updates with a relatively moderate cadence, primarily for bug fixes and minor feature enhancements. Key differentiators include its seamless integration with Aurelia's dependency injection and plugin system, offering features like request interception, retry functionality (since 1.4.0), and helper methods for common HTTP verbs. While designed for Aurelia, it can function in any JavaScript environment, though a Fetch API polyfill (e.g., `whatwg-fetch`) is often required for broader browser or Node.js compatibility. It ships with TypeScript types, facilitating robust development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing HttpClient, configuring a base URL, and performing GET and POST requests with JSON data.

import { HttpClient, json } from 'aurelia-fetch-client';

// For environments without native Fetch, a polyfill is needed.
// import 'whatwg-fetch'; // Polyfill example

async function fetchData() {
  const client = new HttpClient();

  try {
    // Configure the client globally or per-request
    client.configure(config => {
      config
        .useStandardConfiguration()
        .withBaseUrl('https://jsonplaceholder.typicode.com/');
    });

    // Make a GET request
    const getResponse = await client.fetch('posts/1');
    const getData = await getResponse.json();
    console.log('GET Data:', getData);

    // Make a POST request with JSON payload
    const postResponse = await client.post('posts', json({
      title: 'foo',
      body: 'bar',
      userId: 1,
    }));
    const postData = await postResponse.json();
    console.log('POST Data:', postData);

  } catch (error) {
    console.error('Fetch error:', error);
  }
}

fetchData();

view raw JSON →