After looking further I finally managed to make it work, the hint was in the docs, which lead me to this piece of code:
Example of using custom routes
In essence,
- define a custom route-handler for
leptos_routes_with_handler
which calls leptos_axum::render_app_to_stream_with_context
and provides required states there in the context
- define a server_fn_handler that calls
handle_server_fn_with_context
, yet again providing the states yet again in the context
- define some kind of extractor function that you can use from your
#[server]
function, that uses use_context::T
to extract the type from the Scope.
I personally used the approach to define a function on a state I want to extract:
impl MyState {
pub fn from_cx(cx: Scope) -> Result<MyState, ServerFnError> {
use_context::<MyState>(cx)
.ok_or_else(|| ServerFnError::ServerError("My State missing.".into()))
}
}
so I can retrieve it inside my
#[server(Something, "/api")]
pub async fn something(cx: Scope, arg: MyStruct) -> Result<String, ServerFnError> {
let my_state = MyState::from_cx(cx)?;
}
but the example code in the repo uses a simple function (see in todo.rs), and uses two substates of the AppState, the AuthSession and the SqlitePool, that are delivered via Scope.