diff --git a/axum/src/extract/mod.rs b/axum/src/extract/mod.rs index fcdc08df..00a11387 100644 --- a/axum/src/extract/mod.rs +++ b/axum/src/extract/mod.rs @@ -72,11 +72,8 @@ pub use self::request_parts::OriginalUri; pub use self::ws::WebSocketUpgrade; #[cfg(feature = "headers")] -mod typed_header; - -#[cfg(feature = "headers")] -#[doc(inline)] -pub use self::typed_header::TypedHeader; +#[doc(no_inline)] +pub use crate::TypedHeader; pub(crate) fn has_content_type( req: &RequestParts, diff --git a/axum/src/extract/rejection.rs b/axum/src/extract/rejection.rs index 516260a0..780327c8 100644 --- a/axum/src/extract/rejection.rs +++ b/axum/src/extract/rejection.rs @@ -235,4 +235,4 @@ where } #[cfg(feature = "headers")] -pub use super::typed_header::{TypedHeaderRejection, TypedHeaderRejectionReason}; +pub use crate::typed_header::{TypedHeaderRejection, TypedHeaderRejectionReason}; diff --git a/axum/src/lib.rs b/axum/src/lib.rs index a76a7bad..e7fd2880 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -395,6 +395,8 @@ pub(crate) mod macros; mod extension; #[cfg(feature = "json")] mod json; +#[cfg(feature = "headers")] +mod typed_header; mod util; pub mod body; @@ -426,5 +428,9 @@ pub use self::json::Json; #[doc(inline)] pub use self::routing::Router; +#[doc(inline)] +#[cfg(feature = "headers")] +pub use self::typed_header::TypedHeader; + #[doc(inline)] pub use axum_core::{BoxError, Error}; diff --git a/axum/src/response/mod.rs b/axum/src/response/mod.rs index 1cfbdfba..e5bb465b 100644 --- a/axum/src/response/mod.rs +++ b/axum/src/response/mod.rs @@ -11,6 +11,10 @@ pub mod sse; #[cfg(feature = "json")] pub use crate::Json; +#[doc(no_inline)] +#[cfg(feature = "headers")] +pub use crate::TypedHeader; + #[doc(no_inline)] pub use crate::Extension; diff --git a/axum/src/extract/typed_header.rs b/axum/src/typed_header.rs similarity index 89% rename from axum/src/extract/typed_header.rs rename to axum/src/typed_header.rs index d710b184..5c311678 100644 --- a/axum/src/extract/typed_header.rs +++ b/axum/src/typed_header.rs @@ -1,20 +1,20 @@ -use super::{FromRequest, RequestParts}; +use crate::extract::{FromRequest, RequestParts}; use async_trait::async_trait; use axum_core::response::{IntoResponse, IntoResponseParts, Response, ResponseParts}; use headers::HeaderMapExt; use http::header::{HeaderName, HeaderValue}; use std::ops::Deref; -/// Extractor that extracts a typed header value from [`headers`]. +/// Extractor and response that works with typed header values from [`headers`]. +/// +/// # As extractor /// /// In general, it's recommended to extract only the needed headers via `TypedHeader` rather than /// removing all headers with the `HeaderMap` extractor. /// -/// # Example -/// /// ```rust,no_run /// use axum::{ -/// extract::TypedHeader, +/// TypedHeader, /// headers::UserAgent, /// routing::get, /// Router, @@ -31,7 +31,24 @@ use std::ops::Deref; /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` -#[cfg_attr(docsrs, doc(cfg(feature = "headers")))] +/// +/// # As response +/// +/// ```rust +/// use axum::{ +/// TypedHeader, +/// response::IntoResponse, +/// headers::ContentType, +/// }; +/// +/// async fn handler() -> impl IntoResponse { +/// ( +/// TypedHeader(ContentType::text_utf8()), +/// "Hello, World!", +/// ) +/// } +/// ``` +#[cfg(feature = "headers")] #[derive(Debug, Clone, Copy)] pub struct TypedHeader(pub T); @@ -127,7 +144,8 @@ impl TypedHeaderRejection { } } -/// Additional information regarding a [`TypedHeaderRejection`](super::TypedHeaderRejection) +/// Additional information regarding a [`TypedHeaderRejection`] +#[cfg(feature = "headers")] #[derive(Debug)] #[non_exhaustive] pub enum TypedHeaderRejectionReason {