Skip to content

Commit

Permalink
Add map_request and friends (#1408)
Browse files Browse the repository at this point in the history
* Add `map_request` and friends

* finish it

* changelog ref

* Apply suggestions from code review

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>

* address review feedback

* Apply suggestions from code review

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
  • Loading branch information
davidpdrsn and jplatte committed Sep 25, 2022
1 parent 896ffc5 commit 2077d50
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 2 deletions.
4 changes: 4 additions & 0 deletions axum/CHANGELOG.md
Expand Up @@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **added:** Add `middleware::from_extractor_with_state` and
`middleware::from_extractor_with_state_arc` ([#1396])
- **added:** Add `DefaultBodyLimit::max` for changing the default body limit ([#1397])
- **added:** Add `map_request`, `map_request_with_state`, and
`map_request_with_state_arc` for transforming the request with an async
function ([#1408])
- **breaking:** `ContentLengthLimit` has been removed. `Use DefaultBodyLimit` instead ([#1400])

[#1371]: https://github.com/tokio-rs/axum/pull/1371
Expand All @@ -26,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#1396]: https://github.com/tokio-rs/axum/pull/1396
[#1397]: https://github.com/tokio-rs/axum/pull/1397
[#1400]: https://github.com/tokio-rs/axum/pull/1400
[#1408]: https://github.com/tokio-rs/axum/pull/1408

# 0.6.0-rc.2 (10. September, 2022)

Expand Down
11 changes: 9 additions & 2 deletions axum/src/middleware/from_fn.rs
Expand Up @@ -77,6 +77,9 @@ use tower_service::Service;
/// async fn my_middleware<B>(
/// TypedHeader(auth): TypedHeader<Authorization<Bearer>>,
/// Query(query_params): Query<HashMap<String, String>>,
/// // you can add more extractors here but the last
/// // extractor must implement `FromRequest` which
/// // `Request` does
/// req: Request<B>,
/// next: Next<B>,
/// ) -> Response {
Expand Down Expand Up @@ -117,7 +120,9 @@ pub fn from_fn<F, T>(f: F) -> FromFnLayer<F, (), T> {
///
/// async fn my_middleware<B>(
/// State(state): State<AppState>,
/// // you can add more extractors here...
/// // you can add more extractors here but the last
/// // extractor must implement `FromRequest` which
/// // `Request` does
/// req: Request<B>,
/// next: Next<B>,
/// ) -> Response {
Expand All @@ -140,6 +145,8 @@ pub fn from_fn_with_state<F, S, T>(state: S, f: F) -> FromFnLayer<F, S, T> {

/// Create a middleware from an async function with the given [`Arc`]'ed state.
///
/// See [`from_fn_with_state`] for an example.
///
/// See [`State`](crate::extract::State) for more details about accessing state.
pub fn from_fn_with_state_arc<F, S, T>(state: Arc<S>, f: F) -> FromFnLayer<F, S, T> {
FromFnLayer {
Expand Down Expand Up @@ -396,7 +403,7 @@ mod tests {
}

async fn handle(headers: HeaderMap) -> String {
(&headers["x-axum-test"]).to_str().unwrap().to_owned()
headers["x-axum-test"].to_str().unwrap().to_owned()
}

let app = Router::new()
Expand Down

0 comments on commit 2077d50

Please sign in to comment.