diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 6f59215..db749a2 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" crate-type = ["cdylib", "rlib"] [dependencies] -leptos = { version = "0.8.15", features = ["csr"] } +leptos = { version = "0.8.15", features = ["csr", "msgpack"] } leptos_router = { version = "0.8.11" } console_error_panic_hook = "0.1" diff --git a/shared/Cargo.toml b/shared/Cargo.toml index ff9d038..0dd77ac 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -12,7 +12,7 @@ bytes = "1" http = "1" # Leptos 0.8.7 -leptos = { version = "0.8.7", features = ["nightly"] } +leptos = { version = "0.8.15", features = ["nightly", "msgpack"] } leptos_router = { version = "0.8.7", features = ["nightly"] } leptos_axum = { version = "0.8.7", optional = true } axum = { version = "0.8", features = ["macros"], optional = true } diff --git a/shared/src/codec.rs b/shared/src/codec.rs index b0c760e..47cb4d5 100644 --- a/shared/src/codec.rs +++ b/shared/src/codec.rs @@ -1,88 +1 @@ -use leptos::prelude::*; -use leptos::server_fn::codec::{Encoding, FromReq, FromRes, IntoReq, IntoRes}; -use leptos::server_fn::request::{ClientReq, Req}; -use leptos::server_fn::response::{ClientRes, Res, TryRes}; -use http::Method; -use bytes::Bytes; -use serde::{de::DeserializeOwned, Serialize}; -use std::future::Future; - -pub struct MessagePack; - -impl leptos::server_fn::ContentType for MessagePack { - const CONTENT_TYPE: &'static str = "application/msgpack"; -} - -impl Encoding for MessagePack { - const METHOD: Method = Method::POST; -} - -#[cfg(any(feature = "ssr", feature = "hydrate"))] -impl IntoReq for T -where - Request: ClientReq, - T: Serialize + Send, - Error: Send, -{ - fn into_req(self, path: &str, accepts: &str) -> Result { - let data = rmp_serde::to_vec(&self) - .map_err(|e| ServerFnError::new(e.to_string()).into())?; - - // Use try_new_post_bytes which should be available on ClientReq trait - Request::try_new_post_bytes( - path, - MessagePack::CONTENT_TYPE, - accepts, - Bytes::from(data) - ) - } -} - -#[cfg(any(feature = "ssr", feature = "hydrate"))] -impl FromRes for T -where - Response: ClientRes + Send, - T: DeserializeOwned + Send, - Error: Send, -{ - fn from_res(res: Response) -> impl Future> + Send { - async move { - let data = res.try_into_bytes().await?; - rmp_serde::from_slice(&data) - .map_err(|e| ServerFnError::new(e.to_string()).into()) - } - } -} - -#[cfg(feature = "ssr")] -impl FromReq for T -where - Request: Req + Send, - T: DeserializeOwned, - Error: Send, -{ - fn from_req(req: Request) -> impl Future> + Send { - async move { - let data = req.try_into_bytes().await?; - rmp_serde::from_slice(&data) - .map_err(|e| ServerFnError::new(e.to_string()).into()) - } - } -} - -#[cfg(feature = "ssr")] -impl IntoRes for T -where - Response: TryRes + Send, - T: Serialize + Send, - Error: Send, -{ - fn into_res(self) -> impl Future> + Send { - async move { - let data = rmp_serde::to_vec(&self) - .map_err(|e| ServerFnError::new(e.to_string()).into())?; - - Response::try_from_bytes(MessagePack::CONTENT_TYPE, Bytes::from(data)) - } - } -} +pub use leptos::server_fn::codec::MsgPack; diff --git a/shared/src/server_fns/auth.rs b/shared/src/server_fns/auth.rs index 0156f55..14a5220 100644 --- a/shared/src/server_fns/auth.rs +++ b/shared/src/server_fns/auth.rs @@ -1,6 +1,6 @@ use leptos::prelude::*; use serde::{Deserialize, Serialize}; -use crate::codec::MessagePack; +use crate::codec::MsgPack; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct UserResponse { @@ -20,7 +20,7 @@ pub struct SetupStatus { pub completed: bool, } -#[server(GetSetupStatus, "/api/server_fns/GetSetupStatus", encoding = "MessagePack")] +#[server(GetSetupStatus, "/api/server_fns/GetSetupStatus", encoding = "MsgPack")] pub async fn get_setup_status() -> Result { use crate::DbContext; @@ -33,7 +33,7 @@ pub async fn get_setup_status() -> Result { }) } -#[server(Setup, "/api/server_fns/Setup", encoding = "MessagePack")] +#[server(Setup, "/api/server_fns/Setup", encoding = "MsgPack")] pub async fn setup(username: String, password: String) -> Result<(), ServerFnError> { use crate::DbContext; @@ -55,7 +55,7 @@ pub async fn setup(username: String, password: String) -> Result<(), ServerFnErr Ok(()) } -#[server(Login, "/api/server_fns/Login", encoding = "MessagePack")] +#[server(Login, "/api/server_fns/Login", encoding = "MsgPack")] pub async fn login(username: String, password: String) -> Result { use crate::DbContext; use leptos_axum::ResponseOptions; @@ -111,7 +111,7 @@ pub async fn login(username: String, password: String) -> Result Result<(), ServerFnError> { use leptos_axum::ResponseOptions; use cookie::{Cookie, SameSite}; @@ -132,7 +132,7 @@ pub async fn logout() -> Result<(), ServerFnError> { Ok(()) } -#[server(GetUser, "/api/server_fns/GetUser", encoding = "MessagePack")] +#[server(GetUser, "/api/server_fns/GetUser", encoding = "MsgPack")] pub async fn get_user() -> Result, ServerFnError> { use axum::http::HeaderMap; use leptos_axum::extract;