Skip to main content

typeway_client/
interceptor.rs

1//! Request and response interceptors for the [`Client`](crate::Client).
2//!
3//! Interceptors allow modifying outgoing requests and inspecting incoming
4//! responses without changing individual call sites.
5//!
6//! # Example
7//!
8//! ```
9//! use typeway_client::{ClientConfig, RequestInterceptor, ResponseInterceptor};
10//! use std::sync::Arc;
11//!
12//! let config = ClientConfig::default()
13//!     .request_interceptor(Arc::new(|req| {
14//!         req.header("X-Custom", "value")
15//!     }))
16//!     .response_interceptor(Arc::new(|resp| {
17//!         println!("Response status: {}", resp.status());
18//!     }));
19//! ```
20
21use std::sync::Arc;
22
23/// A function that modifies outgoing requests before they are sent.
24///
25/// Interceptors are applied in the order they are added. Each interceptor
26/// receives and returns a [`reqwest::RequestBuilder`], allowing it to add
27/// headers, query parameters, or other modifications.
28pub type RequestInterceptor =
29    Arc<dyn Fn(reqwest::RequestBuilder) -> reqwest::RequestBuilder + Send + Sync>;
30
31/// A function that inspects responses after they are received.
32///
33/// Response interceptors cannot modify the response; they are intended for
34/// logging, metrics, or side-effect-based observation. They are called in
35/// the order they are added.
36pub type ResponseInterceptor = Arc<dyn Fn(&reqwest::Response) + Send + Sync>;