fix: fix type inference on extract() functions (#2233)

This commit is contained in:
Greg Johnston 2024-01-26 17:54:42 -05:00 committed by GitHub
parent 26d1aee9ad
commit 1b5961edaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 18 deletions

View file

@ -1390,15 +1390,13 @@ impl LeptosRoutes for &mut ServiceConfig {
/// Ok(data)
/// }
/// ```
pub async fn extract<T, CustErr>() -> Result<T, ServerFnError<CustErr>>
pub async fn extract<T>() -> Result<T, ServerFnError>
where
T: actix_web::FromRequest,
<T as FromRequest>::Error: Display,
{
let req = use_context::<HttpRequest>().ok_or_else(|| {
ServerFnError::ServerError(
"HttpRequest should have been provided via context".to_string(),
)
ServerFnError::new("HttpRequest should have been provided via context")
})?;
T::extract(&req)

View file

@ -55,10 +55,7 @@ use leptos_router::*;
use once_cell::sync::OnceCell;
use parking_lot::RwLock;
use server_fn::redirect::REDIRECT_HEADER;
use std::{
error::Error, fmt::Debug, io, pin::Pin, sync::Arc,
thread::available_parallelism,
};
use std::{fmt::Debug, io, pin::Pin, sync::Arc, thread::available_parallelism};
use tokio_util::task::LocalPoolHandle;
use tracing::Instrument;
@ -1772,13 +1769,12 @@ fn get_leptos_pool() -> LocalPoolHandle {
/// Ok(query)
/// }
/// ```
pub async fn extract<T, CustErr>() -> Result<T, ServerFnError>
pub async fn extract<T>() -> Result<T, ServerFnError>
where
T: Sized + FromRequestParts<()>,
T::Rejection: Debug,
CustErr: Error + 'static,
{
extract_with_state::<T, (), CustErr>(&()).await
extract_with_state::<T, ()>(&()).await
}
/// A helper to make it easier to use Axum extractors in server functions. This
@ -1800,18 +1796,14 @@ where
/// Ok(query)
/// }
/// ```
pub async fn extract_with_state<T, S, CustErr>(
state: &S,
) -> Result<T, ServerFnError>
pub async fn extract_with_state<T, S>(state: &S) -> Result<T, ServerFnError>
where
T: Sized + FromRequestParts<S>,
T::Rejection: Debug,
CustErr: Error + 'static,
{
let mut parts = use_context::<Parts>().ok_or_else(|| {
ServerFnError::ServerError::<CustErr>(
"should have had Parts provided by the leptos_axum integration"
.to_string(),
ServerFnError::new(
"should have had Parts provided by the leptos_axum integration",
)
})?;
T::from_request_parts(&mut parts, state)