Intercepting responses & requests
Interceptors are a powerful feature of Aurelia's Fetch Client, allowing you to modify requests and responses or perform side effects like logging and authentication. They enable developers to implement centralized logic that handles various aspects of HTTP communication.
Understanding Interceptors
Interceptors can be attached to the Fetch Client configuration and consist of four optional methods: request, requestError, response, and responseError. Here’s how each method operates:
request: Invoked before a request is sent. This method receives theRequestobject and can modify it or return a new one. It can also return aResponseobject to short-circuit the fetch operation.requestError: Triggered if an error occurs during the request generation or in arequestinterceptor. This method can handle the error and potentially recover by returning a newRequestobject.response: Called after the server responds. This method receives theResponseobject, which can be manipulated or replaced before being returned to the original caller.responseError: Invoked when a fetch request fails due to network errors or when aresponseinterceptor throws an error. It can handle the error and perform tasks like retrying the request or returning an alternative response.
Each method can return either their respective expected object (Request or Response) or a Promise that resolves to it.
Example Interceptors
Logging Interceptor
The logging interceptor tracks all outgoing requests and incoming responses, which is useful for debugging and monitoring.
import { HttpClient } from '@aurelia/fetch-client';
const http = new HttpClient();
http.configure(config => {
config.withInterceptor({
request(request) {
console.log(`Requesting: ${request.method} ${request.url}`);
return request;
},
response(response) {
console.log(`Received: ${response.status} ${response.url}`);
return response;
}
});
});Authentication Interceptor
The authentication interceptor appends a bearer token to each request, centralizing the authentication handling for secured API endpoints.
Error Handling Interceptor
A robust error handling interceptor intercepts responses and response errors to manage API errors centrally.
Advanced Interceptor Patterns
Async Interceptors and Promise Handling
All interceptor methods support async operations and Promise returns:
Request Transformation Chain
Create sophisticated request transformation pipelines:
Conditional Interceptor Application
Apply interceptors selectively based on request characteristics:
Interceptor Disposal and Cleanup
Properly manage interceptor lifecycle and cleanup:
Best Practices and Advanced Considerations
Interceptor Ordering Strategy
Performance Optimization
Debugging and Testing Support
Key Takeaways
Interceptor Order Matters: Design your interceptor chain thoughtfully
Async Support: All interceptor methods can be async and return Promises
Performance Impact: Monitor and optimize interceptor performance
Resource Management: Implement proper cleanup in interceptor dispose methods
Conditional Logic: Use URL patterns and headers to apply logic selectively
Debugging Support: Add comprehensive logging for development environments
Error Recovery: Implement sophisticated error handling and recovery strategies
Retry Limitations: Be aware of AbortController compatibility issues with the retry interceptor
Interceptors are the most powerful feature of the Aurelia Fetch Client, enabling sophisticated HTTP request/response processing pipelines that can handle authentication, caching, metrics, error recovery, and much more.
Last updated
Was this helpful?