💪 Interceptors & Hooks
Hooks allow you to hook into the request lifecycle with simplified helper methods.
Looking for interceptors?
You want to hook into some lifecycle, great, they're called hooks!
Hooks are a powerful feature that allow you to hook into the request lifecycle in order to intercept and modify requests or responses. The new simplified hook system provides helper methods for easy manipulation without complex context returns.
Hooks can be used to implement features such as authentication, logging, custom caching, error handling, and more.
Recommended Approach
Use the new helper methods (setHeaders, setBody, setOptions, etc.) for cleaner, more intuitive code. The old context return pattern is still supported for backwards compatibility.
How Hooks Work
- Set hooks when creating your instance
- Receive context - each hook gets a context object with helper methods
- Use helpers - modify requests/responses using simple helper methods
- Changes applied automatically - no complex return objects needed
The Context Object
Each hook receives a context object with:
config- Current z-fetch configurationrequest- Request details (method, url, options)result- Response result (null during onRequest)error- Error information (null if no error)
Helper Methods
The context provides convenient helper methods for easy manipulation:
setHeaders()- Update request headers (supports in-place or return-based modification)setBody()- Update request bodysetOptions()- Update request options (cache, mode, credentials, etc.)setUrl()- Update request URLsetMethod()- Update request methodsetError()- Update error information (onError hook only)
Request Hooks (onRequest)
Request hooks allow you to modify the request before it's sent using helper methods:
Response Hooks (onResponse)
Response hooks allow you to modify the response before it's returned:
Error Hooks (onError)
Error hooks allow you to handle and modify errors when they occur:
Combining Multiple Hooks
You can use all three hooks together for comprehensive request handling:
Advanced Helper Usage
Modifying Request Body
Changing HTTP Method
In-Place Modifications
Helper methods support both return-based and in-place modifications:
Backwards Compatibility
The hook system maintains backwards compatibility with the old context return pattern (v0.0.10):
Migration Guide
Recommended: Use the new helper methods for cleaner, more intuitive code. The old pattern is supported but the new helpers are easier to use and maintain.
Related Features
Hooks work seamlessly with other Z-Fetch features:
- 🟡 Streaming Support - Process response streams with built-in utilities
- ⭐ Progress Tracking - Real-time upload and download progress monitoring
- 🚨 Error Handling - Comprehensive error handling with onError hook and error mapping
- 🔧 Advanced Features - Polling, caching, cancellation, and more advanced capabilities
- 👉 Request Methods - All HTTP methods support hooks for request lifecycle management
For comprehensive error handling with error mapping and advanced features, see the 🚨 Error Handling documentation.
With these powerful hooks and helper methods, you can implement complex request/response processing, authentication flows, logging, error handling, and data transformation in a clean and intuitive way.
Any more hooks you think would be useful? Feel free to suggest them!