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 theRequest
object and can modify it or return a new one. It can also return aResponse
object to short-circuit the fetch operation.requestError
: Triggered if an error occurs during the request generation or in arequest
interceptor. This method can handle the error and potentially recover by returning a newRequest
object.response
: Called after the server responds. This method receives theResponse
object, 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 aresponse
interceptor 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.
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.
Best Practices for Using Interceptors
Single Responsibility: Each interceptor should have a single responsibility, whether it's logging, adding headers, or error handling.
Order Matters: The order in which interceptors are added can affect their behavior, as they are executed in a pipeline.
Error Recovery: Consider implementing strategies within
requestError
andresponseError
interceptors for error recovery, such as retrying failed requests.Avoid Side Effects: While interceptors can perform side effects, it's best to avoid them unless necessary for functionality like logging.
Short-Circuiting: Interceptors can short-circuit the fetch process by returning a
Response
object in therequest
interceptor.
Considerations for Interceptors
When implementing interceptors, it is important to be aware of their full potential and how to avoid common pitfalls:
Interceptor Lifecycle: Interceptors are executed in the order they are registered. Understanding this order is crucial when you have dependencies between interceptors' operations.
Asynchronous Interceptors: Interceptors can return
Promise
s, allowing for asynchronous operations. Ensure that any asynchronous code is handled properly to avoid unexpected behavior.Pitfalls to Avoid: Be cautious when changing shared request configurations within an interceptor, as this might affect other application parts. Always treat the
Request
andResponse
objects as immutable.Performance Impacts: Remember that complex logic within interceptors can impact the performance of your HTTP requests. Monitor the performance to ensure that interceptors do not introduce significant delays.
Debugging Interceptors: Use logging within interceptors to help identify the flow of requests and responses. This can be invaluable when debugging unexpected behavior.
Organizing Interceptors: For better maintainability, organize interceptors in a logical structure, such as grouping related interceptors together or keeping them close to the feature modules they relate to.
Advanced Use Cases: For more complex scenarios, consider using interceptors for response caching, request retries with backoff strategies, or implementing custom prioritization of outgoing requests.
Last updated