Route layers for route functions
pasha-vuiko opened this issue · 4 comments
First of all, thank you so much for this great library! I enjoy the code first experience with auto generated docs.
Currently for aide::axum::routing::{delete, get, patch, put, post}
and other routing functions it is impossible to set route layers.
For example this code is valid for axum::routing::{delete, get, patch, put, post}
:
let routes = Router::new()
.route(
"/",
get(expenses_handlers::find_many)
.route_layer(cache_layer.clone())
.route_layer(auth_layer.verify(vec![Roles::Admin, Roles::Customer])),
)
.route(
"/:id",
get(expenses_handlers::find_one)
.route_layer(cache_layer)
.route_layer(auth_layer.verify(vec![Roles::Admin, Roles::Customer])),
)
but with aide::axum::routing
functions there is no .route_layer()
function, only .layer()
function is available, but it is impossible to use multiple .layer()
function for a specific route. So it would be great if this code become valid:
let routes = ApiRouter::new()
.api_route(
"/",
get(expenses_handlers::find_many)
.route_layer(cache_layer.clone()) // TODO Find out how to add multiple layers
.route_layer(auth_layer.verify(vec![Roles::Admin, Roles::Customer])),
)
.api_route(
"/:id",
get(expenses_handlers::find_one)
.route_layer(cache_layer) // TODO Find out how to add multiple layers
.route_layer(auth_layer.verify(vec![Roles::Admin, Roles::Customer])),
)
But currently following errors is received:
error[E0599]: no method named `route_layer` found for struct `ApiMethodRouter` in the current scope
--> src/api/expenses/mod.rs:47:18
|
46 | / get(expenses_handlers::find_many)
47 | | .route_layer(cache_layer.clone())
| | -^^^^^^^^^^^ method not found in `ApiMethodRouter<_, _>`
| |_________________|
|
error[E0599]: no method named `route_layer` found for struct `ApiMethodRouter` in the current scope
--> src/api/expenses/mod.rs:53:18
|
52 | / get(expenses_handlers::find_one)
53 | | .route_layer(cache_layer)
| | -^^^^^^^^^^^ method not found in `ApiMethodRouter<_, _>`
| |_________________|
All you need to do is to add following method right next to layer() method in ApiMethodRouter
/// This method wraps a route_layer around the [`ApiMethodRouter`]
/// For further information see [`axum::routing::method_routing::MethodRouter::route_layer`]
pub fn route_layer<L>(self, layer: L) -> ApiMethodRouter<S, B, Infallible>
where
L: Layer<Route<B, Infallible>> + Clone + Send + 'static,
L::Service: Service<Request<B>, Error = Infallible> + Clone + Send + 'static,
<L::Service as Service<Request<B>>>::Response: IntoResponse + 'static,
<L::Service as Service<Request<B>>>::Future: Send + 'static,
{
ApiMethodRouter {
router: self.router.route_layer(layer),
operations: self.operations,
}
}
What do you think?
I've added a PR that aligns the routing interface with axum's that adds this method as well.
thank you