Each generated client exposes low-level interceptors for requests, responses, and errors. They run for every generated operation made with that client. The SDK does not export logging, validation, or error-transform interceptor factories.
Use a custom fetch when you need a transport wrapper, retry policy, timeout policy, or tracing around the platform call. Use interceptors when you need access to the constructed Request, returned Response, or error lifecycle for one client instance.
Register hooks
use returns a numeric identifier. Return the request, response, or error from the hook so the generated pipeline can continue. Hooks may be synchronous or async:
The callback signatures are low-level Fetch hooks:
- Request hooks receive
(request, options) and return a Request.
- Response hooks receive
(response, request, options) and return a Response.
- Error hooks receive
(error, response, request, options) and return the error. response and request can be undefined.
The options value is the generated client’s resolved request configuration. Keep hooks focused on transport concerns; operation parameters and generated result handling remain at the operation call site.
Request hooks can return a new Request with application metadata. Do not overwrite auth-owned headers or log their values:
Authorization, X-API-Key, Cookie, and other secrets may be present. Redact values before logging. Prefer client-level headers for static metadata and a request hook for dynamic correlation IDs.
Observe responses without consuming them
A response body is a stream. If a response interceptor reads it, clone it first or return a replacement response so the generated parser still receives a body:
Avoid logging response bodies in production when they may contain private metadata. A response hook can observe status and headers without parsing the body.
Error hooks
Error hooks run for response errors, Fetch/network failures, request construction failures, and parser failures. Preserve the original error unless your application deliberately maps it:
Returning a replacement value changes what the generated client places in result.error or throws when generated throwOnError is enabled. If callers rely on generated error payload types, do not replace them globally.
Remove or replace hooks
Use the returned identifier with eject, or clear a whole interceptor collection:
update returns the identifier when it replaces an existing hook and false when the identifier is not active. clear removes every hook in that collection. Register hooks once during client setup, not inside a request handler, or a long-lived process can accumulate duplicate logging and mutations.
Interceptors versus custom Fetch
Use a custom Fetch wrapper for policies that must surround the actual transport call:
Use an interceptor for Request/Response lifecycle visibility. Neither mechanism supplies Komga schemas, domain services, or a universal retry/cache policy.
A response interceptor can change the Response passed to every generated operation on the client. Scope clients carefully when different features need different transport behavior, and clone bodies before inspecting them.
For response validation, see Runtime validation. For result branches and KomgaApiError, see Result handling.