Overview
TheFetchHooks interface provides lifecycle hooks that allow you to intercept and handle different stages of the fetch request process.
Type Signature
Hooks
onRequest
Called before the request is sent. Use this to modify request options, add headers, or log requests.
The request URL or Request object
Resolved request options that will be used for the fetch
Undefined at this stage
Undefined at this stage
onRequestError
onRequestError
FetchHook<FetchContext<T, R> & { error: Error }> | FetchHook<FetchContext<T, R> & { error: Error }>[]
Called when the request fails to execute (network error, timeout, etc.). Does not include HTTP error responses (4xx, 5xx) - use
onResponseError for those.The request URL or Request object
Request options used for the fetch
The error that occurred during the request
Undefined for network errors
onResponse
onResponse
FetchHook<FetchContext<T, R> & { response: FetchResponse<T> }> | FetchHook<FetchContext<T, R> & { response: FetchResponse<T> }>[]
Called after a successful response is received (any status code). Use this to log responses, modify response data, or handle specific status codes.
The request URL or Request object
Request options used for the fetch
The fetch response object with parsed data in
_data property. See FetchResponse.Undefined for successful responses
onResponseError
onResponseError
FetchHook<FetchContext<T, R> & { response: FetchResponse<T> }> | FetchHook<FetchContext<T, R> & { response: FetchResponse<T> }>[]
Called when the response has an HTTP error status (4xx or 5xx). Use this to handle specific error cases before the error is thrown.
The request URL or Request object
Request options used for the fetch
The fetch response object with error status. See FetchResponse.
May contain the error that will be thrown
Multiple Hooks
You can provide multiple hooks as an array. They will be called in order:Async Hooks
Hooks can be asynchronous:Global Hooks
Set hooks globally when creating a fetch instance:Type Parameters
The expected type of the response data
The response type. See ResponseType.
Related
- FetchContext - Context object passed to hooks
- FetchResponse - Response interface
- FetchOptions - Complete options interface