From e2cdbc746fa87667e51ce4e67c5820eff34d9cdc Mon Sep 17 00:00:00 2001 From: Bruno De Simone <44624350+b4-io@users.noreply.github.com> Date: Tue, 31 Jan 2023 11:09:58 -0300 Subject: [PATCH] Add leptos_routes functions for integrations (#415) * added leptos_routes_with_context * added leptos_routes_with_handler for axum integration --- integrations/actix/src/lib.rs | 34 +++++++++++++++++++++++ integrations/axum/src/lib.rs | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/integrations/actix/src/lib.rs b/integrations/actix/src/lib.rs index e2273fd..d1811dc 100644 --- a/integrations/actix/src/lib.rs +++ b/integrations/actix/src/lib.rs @@ -646,6 +646,16 @@ pub trait LeptosRoutes { Data: 'static, Fut: Future, actix_web::Error>>, IV: IntoView + 'static; + + fn leptos_routes_with_context( + self, + options: LeptosOptions, + paths: Vec, + additional_context: impl Fn(leptos::Scope) + 'static + Clone + Send, + app_fn: impl Fn(leptos::Scope) -> IV + Clone + Send + 'static, + ) -> Self + where + IV: IntoView + 'static; } /// The default implementation of `LeptosRoutes` which takes in a list of paths, and dispatches GET requests @@ -692,4 +702,28 @@ where } router } + + fn leptos_routes_with_context( + self, + options: LeptosOptions, + paths: Vec, + additional_context: impl Fn(leptos::Scope) + 'static + Clone + Send, + app_fn: impl Fn(leptos::Scope) -> IV + Clone + Send + 'static, + ) -> Self + where + IV: IntoView + 'static, + { + let mut router = self; + for path in paths.iter() { + router = router.route( + path, + render_app_to_stream_with_context( + options.clone(), + additional_context.clone(), + app_fn.clone(), + ), + ); + } + router + } } diff --git a/integrations/axum/src/lib.rs b/integrations/axum/src/lib.rs index 776b0ad..c14b6f7 100644 --- a/integrations/axum/src/lib.rs +++ b/integrations/axum/src/lib.rs @@ -637,6 +637,21 @@ pub trait LeptosRoutes { ) -> Self where IV: IntoView + 'static; + + fn leptos_routes_with_context( + self, + options: LeptosOptions, + paths: Vec, + additional_context: impl Fn(leptos::Scope) + 'static + Clone + Send, + app_fn: impl Fn(leptos::Scope) -> IV + Clone + Send + 'static, + ) -> Self + where + IV: IntoView + 'static; + + fn leptos_routes_with_handler(self, paths: Vec, handler: H) -> Self + where + H: axum::handler::Handler, + T: 'static; } /// The default implementation of `LeptosRoutes` which takes in a list of paths, and dispatches GET requests /// to those paths to Leptos's renderer. @@ -659,4 +674,40 @@ impl LeptosRoutes for axum::Router { } router } + + fn leptos_routes_with_context( + self, + options: LeptosOptions, + paths: Vec, + additional_context: impl Fn(leptos::Scope) + 'static + Clone + Send, + app_fn: impl Fn(leptos::Scope) -> IV + Clone + Send + 'static, + ) -> Self + where + IV: IntoView + 'static, + { + let mut router = self; + for path in paths.iter() { + router = router.route( + path, + get(render_app_to_stream_with_context( + options.clone(), + additional_context.clone(), + app_fn.clone(), + )), + ); + } + router + } + + fn leptos_routes_with_handler(self, paths: Vec, handler: H) -> Self + where + H: axum::handler::Handler, + T: 'static, + { + let mut router = self; + for path in paths.iter() { + router = router.route(path, get(handler.clone())); + } + router + } }