diff --git a/viz-router/src/resources.rs b/viz-router/src/resources.rs index 3c03c7a6..2a5fff44 100644 --- a/viz-router/src/resources.rs +++ b/viz-router/src/resources.rs @@ -1,7 +1,8 @@ //! Resource use viz_core::{ - BoxHandler, Handler, HandlerExt, IntoResponse, Method, Request, Response, Result, Transform, + BoxHandler, Handler, HandlerExt, IntoResponse, Method, Next, Request, Response, Result, + Transform, }; use crate::Route; @@ -190,6 +191,14 @@ impl Resources { { self.map_handler(|handler| t.transform(handler).boxed()) } + + /// Adds a middleware for the resources. + pub fn with_handler(self, f: F) -> Self + where + F: Handler, Output = Result> + Clone, + { + self.map_handler(|handler| handler.around(f.clone()).boxed()) + } } impl IntoIterator for Resources { diff --git a/viz-router/src/router.rs b/viz-router/src/router.rs index 07baa874..4482c03e 100644 --- a/viz-router/src/router.rs +++ b/viz-router/src/router.rs @@ -176,8 +176,8 @@ mod tests { use viz_core::{ async_trait, types::{Params, RouteInfo}, - Body, Error, Handler, HandlerExt, IntoResponse, Method, Request, RequestExt, Response, - ResponseExt, Result, StatusCode, Transform, + Body, Error, Handler, HandlerExt, IntoResponse, Method, Next, Request, RequestExt, + Response, ResponseExt, Result, StatusCode, Transform, }; use crate::{any, get, Resources, Route, Router, Tree}; @@ -266,10 +266,17 @@ mod tests { Ok(Response::new(("delete".to_string() + &items).into())) } + async fn middle((req, h): Next) -> Result + where + H: Handler> + Clone, + { + h.call(req).await + } + let users = Resources::default() .named("user") .index(index) - .create(create.before(|r: Request| async { Ok(r) })) + .create(create.before(|r: Request| async { Ok(r) }).around(middle)) .show(show) .update(update) .destroy(delete)