Skip to content

Commit

Permalink
Create extra methods for passing additional context
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Dec 16, 2023
1 parent c61b435 commit 18b9ac6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
23 changes: 19 additions & 4 deletions integrations/actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ pub fn generate_route_list<IV>(
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}).0
generate_route_list_with_exclusions_and_ssg(app_fn, None).0
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
Expand All @@ -898,7 +898,7 @@ pub fn generate_route_list_with_ssg<IV>(
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg(app_fn, None, || {})
generate_route_list_with_exclusions_and_ssg(app_fn, None)
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
Expand All @@ -912,7 +912,7 @@ pub fn generate_route_list_with_exclusions<IV>(
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes, || {}).0
generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
Expand All @@ -922,13 +922,28 @@ where
pub fn generate_route_list_with_exclusions_and_ssg<IV>(
app_fn: impl Fn() -> IV + 'static + Clone,
excluded_routes: Option<Vec<String>>,
) -> (Vec<RouteListing>, StaticDataMap)
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg_and_context(app_fn, excluded_routes, || {})
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
/// create routes in Actix's App without having to use wildcard matching or fallbacks. Takes in your root app Element
/// as an argument so it can walk you app tree. This version is tailored to generated Actix compatible paths. Adding excluded_routes
/// to this function will stop `.leptos_routes()` from generating a route for it, allowing a custom handler. These need to be in Actix path format.
/// Additional context will be provided to the app Element.
pub fn generate_route_list_with_exclusions_and_ssg_and_context<IV>(
app_fn: impl Fn() -> IV + 'static + Clone,
excluded_routes: Option<Vec<String>>,
additional_context: impl Fn() + 'static + Clone,
) -> (Vec<RouteListing>, StaticDataMap)
where
IV: IntoView + 'static,
{
let (mut routes, static_data_map) =
leptos_router::generate_route_list_inner(app_fn, additional_context);
leptos_router::generate_route_list_inner_with_context(app_fn, additional_context);

// Actix's Router doesn't follow Leptos's
// Match `*` or `*someword` to replace with replace it with "/{tail.*}
Expand Down
24 changes: 20 additions & 4 deletions integrations/axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ pub fn generate_route_list<IV>(
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}).0
generate_route_list_with_exclusions_and_ssg(app_fn, None).0
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
Expand All @@ -1314,7 +1314,7 @@ pub fn generate_route_list_with_ssg<IV>(
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg(app_fn, None, || {})
generate_route_list_with_exclusions_and_ssg(app_fn, None)
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
Expand All @@ -1329,7 +1329,7 @@ pub fn generate_route_list_with_exclusions<IV>(
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes, || {}).0
generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0
}

/// TODO docs
Expand Down Expand Up @@ -1363,13 +1363,29 @@ pub async fn build_static_routes<IV>(
pub fn generate_route_list_with_exclusions_and_ssg<IV>(
app_fn: impl Fn() -> IV + 'static + Clone,
excluded_routes: Option<Vec<String>>,
) -> (Vec<RouteListing>, StaticDataMap)
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg_and_context(app_fn, excluded_routes, || {})
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
/// create routes in Axum's Router without having to use wildcard matching or fallbacks. Takes in your root app Element
/// as an argument so it can walk you app tree. This version is tailored to generate Axum compatible paths. Adding excluded_routes
/// to this function will stop `.leptos_routes()` from generating a route for it, allowing a custom handler. These need to be in Axum path format
/// Additional context will be provided to the app Element.
#[tracing::instrument(level = "trace", fields(error), skip_all)]
pub fn generate_route_list_with_exclusions_and_ssg_and_context<IV>(
app_fn: impl Fn() -> IV + 'static + Clone,
excluded_routes: Option<Vec<String>>,
additional_context: impl Fn() + 'static + Clone,
) -> (Vec<RouteListing>, StaticDataMap)
where
IV: IntoView + 'static,
{
let (routes, static_data_map) =
leptos_router::generate_route_list_inner(app_fn, additional_context);
leptos_router::generate_route_list_inner_with_context(app_fn, additional_context);
// Axum's Router defines Root routes as "/" not ""
let mut routes = routes
.into_iter()
Expand Down
22 changes: 18 additions & 4 deletions integrations/viz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ pub fn generate_route_list<IV>(
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}).0
generate_route_list_with_exclusions_and_ssg(app_fn, None).0
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
Expand All @@ -1012,7 +1012,7 @@ pub fn generate_route_list_with_ssg<IV>(
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg(app_fn, None, || {})
generate_route_list_with_exclusions_and_ssg(app_fn, None)
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
Expand All @@ -1025,7 +1025,7 @@ pub fn generate_route_list_with_exclusions<IV>(
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes, || {}).0
generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
Expand All @@ -1034,13 +1034,27 @@ where
pub fn generate_route_list_with_exclusions_and_ssg<IV>(
app_fn: impl Fn() -> IV + 'static + Clone,
excluded_routes: Option<Vec<String>>,
) -> (Vec<RouteListing>, StaticDataMap)
where
IV: IntoView + 'static,
{
generate_route_list_with_exclusions_and_ssg_and_context(app_fn, excluded_routes, || {})
}

/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
/// create routes in Viz's Router without having to use wildcard matching or fallbacks. Takes in your root app Element
/// as an argument so it can walk you app tree. This version is tailored to generate Viz compatible paths.
/// Additional context will be provided to the app Element.
pub fn generate_route_list_with_exclusions_and_ssg_and_context<IV>(
app_fn: impl Fn() -> IV + 'static + Clone,
excluded_routes: Option<Vec<String>>,
additional_context: impl Fn() + 'static + Clone,
) -> (Vec<RouteListing>, StaticDataMap)
where
IV: IntoView + 'static,
{
let (routes, static_data_map) =
leptos_router::generate_route_list_inner(app_fn, additional_context);
leptos_router::generate_route_list_inner_with_context(app_fn, additional_context);
// Viz's Router defines Root routes as "/" not ""
let mut routes = routes
.into_iter()
Expand Down
15 changes: 15 additions & 0 deletions router/src/extract_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ impl RouteListing {
/// [`viz`]: <https://docs.rs/viz/>
pub fn generate_route_list_inner<IV>(
app_fn: impl Fn() -> IV + 'static + Clone,
) -> (Vec<RouteListing>, StaticDataMap)
where
IV: IntoView + 'static,
{
generate_route_list_inner_with_context(app_fn, ||{})
}
/// Generates a list of all routes this application could possibly serve. This returns the raw routes in the leptos_router
/// format. Odds are you want `generate_route_list()` from either the [`actix`], [`axum`], or [`viz`] integrations if you want
/// to work with their router.
///
/// [`actix`]: <https://docs.rs/actix/>
/// [`axum`]: <https://docs.rs/axum/>
/// [`viz`]: <https://docs.rs/viz/>
pub fn generate_route_list_inner_with_context<IV>(
app_fn: impl Fn() -> IV + 'static + Clone,
additional_context: impl Fn() + 'static + Clone,
) -> (Vec<RouteListing>, StaticDataMap)
where
Expand Down

0 comments on commit 18b9ac6

Please sign in to comment.