Adds middleware to the request processing pipeline.
The middleware function to add to the pipeline
Middleware functions execute in order of registration and can transform requests, responses, or implement cross-cutting concerns like logging, authentication, or caching.
// Add authentication middleware
client.use(async (req, next) => {
req.options.headers = {
...req.options.headers,
'Authorization': `Bearer ${getToken()}`
};
return next(req);
});
// Add response caching
client.use(async (req, next) => {
const cached = cache.get(req.params.url);
if (cached) return cached;
const res = await next(req);
if (res.status === 'success') {
cache.set(req.params.url, res);
}
return res;
});
Executes an HTTP request with full resilience features.
The URL to make the request to. Can be relative (uses baseUrl) or absolute.
Request options including method, headers, body, etc.
Promise resolving to the response data
Make a GET request
The URL to make the GET request to
Request options (excluding method)
Promise resolving to the response data
Make a POST request
The URL to make the POST request to
Optionaldata: unknownThe data to send in the request body
Request options (excluding method)
Promise resolving to the response data
Make a PUT request
The URL to make the PUT request to
Optionaldata: unknownThe data to send in the request body
Request options (excluding method)
Promise resolving to the response data
Make a DELETE request
The URL to make the DELETE request to
Request options (excluding method)
Promise resolving to the response data
Make a PATCH request
The URL to make the PATCH request to
Optionaldata: unknownThe data to send in the request body
Request options (excluding method)
Promise resolving to the response data
Get client statistics
Object containing client statistics and performance metrics
Reset client state
Provides resilient HTTP client functionality with enterprise features.
Remarks
This client implements multiple resilience patterns to ensure reliable API communication even under adverse conditions. It automatically handles transient failures, rate limits, and service outages while providing hooks for custom behavior through middleware.
Features:
Example