Fetch client
The Aurelia Fetch Client (aurelia-fetch-client) is a powerful HTTP client that wraps the browser's native Fetch API while providing additional features essential for modern web applications. It offers default configuration management, interceptors, centralized request tracking, and other utilities while maintaining compatibility with the standard Fetch API specification.
Installation
Install the package using npm:
npm install aurelia-fetch-clientKey Features
Fully compatible with the Fetch API specification
Configurable defaults for requests
Powerful interceptor system
Request tracking
JSON handling utilities
Base URL configuration
Credential management
Error handling helpers
Basic Usage
Creating an HttpClient Instance
To use the Fetch Client, first import and create an instance of HttpClient:
You can inject a new instance into your component or service class by injecting the Fetch client into your components and services. This will ensure our component gets a new instance of the Fetch client.
This pattern is preferable when encapsulating logic for interacting with an API or backend and wanting to share the same interceptors, base URL and other configuration aspects.
Making HTTP Requests
The fetch method is the primary way to make HTTP requests. It accepts the same parameters as the native fetch API:
Working with JSON
Aurelia's Fetch Client provides a json helper function to simplify JSON requests:
Response Handling
Responses can be processed in various formats:
Error Handling
Implement proper error handling using try/catch or Promise chains:
Configuration
Global Configuration
Configure the HttpClient instance using the configure method:
Available Configuration Options
Base URL Configuration
Set a base URL for all requests:
Default Settings
Configure default options for all requests:
Standard Configuration
Apply recommended default settings:
This applies:
Rejection of error responses (4xx, 5xx status codes)
Same-origin credentials
Default headers
Credentials Configuration
Configure how credentials are handled:
Available credential options:
'same-origin': Send credentials only to same-origin URLs'include': Send credentials to all URLs'omit': Never send credentials
Request Defaults
Set default parameters for specific types of requests:
When using withBaseUrl, ensure the URL ends with a forward slash (/) if you want path segments to be appended correctly.
The configuration system is chainable, allowing you to combine multiple configuration options:
Interceptors
Interceptors provide a powerful way to transform requests and responses, handle errors, and add cross-cutting concerns to HTTP communications. They can intercept requests before they are sent and responses before they are handled by your application.
Interceptor Structure
An interceptor can implement any of these four methods:
request(request)requestError(error)response(response)responseError(error)
Adding Interceptors
Add interceptors during configuration:
Request Interceptors
Request interceptors can modify requests before they are sent:
Response Interceptors
Response interceptors can transform responses before they reach your application:
Error Handling in Interceptors
Handle errors in both request and response phases:
Async Interceptors
Interceptors can return promises for asynchronous operations:
Advanced Features
AbortController Integration
Use AbortController to cancel requests:
Request Timeout
Implement request timeout using AbortController:
Progress Tracking
Track upload progress using fetch with XMLHttpRequest:
Working with FormData
Handle file uploads and form data:
Custom Request Initialization
Create custom request configurations for specific use cases:
Don't manually set the header when working with FormData and file uploads. The browser needs to set this automatically to include the correct boundary parameter.
API Reference
HttpClient Class
Configuration Options
Interceptor Interfaces
Helper Functions
RequestInit Interface
Common Use Cases
Authentication
JWT Authentication
Basic Authentication
RESTful API Integration
File Downloads
Request Cancellation Pattern
Consider implementing chunking and resume capabilities for large file downloads to handle network interruptions gracefully.
Last updated
Was this helpful?