mirror of
https://github.com/tuono-labs/tuono
synced 2026-07-29 13:52:46 -07:00
feat: allow any serializable object as response
This commit is contained in:
@@ -38,7 +38,11 @@ async fn catch_all(request: Request) -> Html<String> {
|
|||||||
|
|
||||||
let req = tuono_lib::Request::new(pathname, headers);
|
let req = tuono_lib::Request::new(pathname, headers);
|
||||||
|
|
||||||
let payload = tuono_lib::Payload::new(&req, "".to_string()).client_payload();
|
|
||||||
|
// TODO: remove unwrap
|
||||||
|
let payload = tuono_lib::Payload::new(&req, Box::new(""))
|
||||||
|
.client_payload()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let result = ssr::Js::SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(&payload)));
|
let result = ssr::Js::SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(&payload)));
|
||||||
|
|
||||||
|
|||||||
@@ -13,5 +13,6 @@ ssr_rs = "0.5.1"
|
|||||||
axum = "0.7.5"
|
axum = "0.7.5"
|
||||||
tuono_lib_macros = { path = "../tuono_lib_macros"}
|
tuono_lib_macros = { path = "../tuono_lib_macros"}
|
||||||
serde = { version = "1.0.202", features = ["derive"] }
|
serde = { version = "1.0.202", features = ["derive"] }
|
||||||
|
erased-serde = "0.4.5"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,25 @@
|
|||||||
use serde::Serialize;
|
use erased_serde::Serialize;
|
||||||
|
use serde::Serialize as SerdeSerialize;
|
||||||
|
|
||||||
use crate::request::Location;
|
use crate::request::Location;
|
||||||
use crate::Request;
|
use crate::Request;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(SerdeSerialize)]
|
||||||
/// This is the data shared to the client
|
/// This is the data shared to the client
|
||||||
pub struct Payload<'a> {
|
pub struct Payload<'a> {
|
||||||
router: Location<'a>,
|
router: Location<'a>,
|
||||||
props: String,
|
props: Box<dyn Serialize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Payload<'a> {
|
impl<'a> Payload<'a> {
|
||||||
pub fn new(req: &Request<'a>, props: String) -> Payload<'a> {
|
pub fn new(req: &Request<'a>, props: Box<dyn Serialize>) -> Payload<'a> {
|
||||||
Payload {
|
Payload {
|
||||||
router: req.location(),
|
router: req.location(),
|
||||||
props,
|
props,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn client_payload(&self) -> String {
|
pub fn client_payload(&self) -> Result<String, serde_json::Error> {
|
||||||
serde_json::to_string(&self).unwrap()
|
serde_json::to_string(&self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
use erased_serde::Serialize;
|
||||||
|
|
||||||
pub enum Response {
|
pub enum Response {
|
||||||
Redirect(String),
|
Redirect(String),
|
||||||
Props(String),
|
Props(Box<dyn Serialize>),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,17 +19,23 @@ pub fn handler_core(_args: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
|
|
||||||
let req = tuono_lib::Request::new(pathname, headers);
|
let req = tuono_lib::Request::new(pathname, headers);
|
||||||
|
|
||||||
let props = #fn_name(&req);
|
let local_response = #fn_name(&req);
|
||||||
|
|
||||||
let payload = tuono_lib::Payload::new(&req, "".to_string()).client_payload();
|
let res = match local_response{
|
||||||
|
tuono_lib::Response::Props(val) => {
|
||||||
|
|
||||||
|
// TODO: remove unwrap
|
||||||
|
let payload = tuono_lib::Payload::new(&req, val).client_payload().unwrap();
|
||||||
|
|
||||||
dbg!(&payload);
|
dbg!(&payload);
|
||||||
|
|
||||||
let res = match props {
|
tuono_lib::ssr::Js::SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(&payload)))
|
||||||
tuono_lib::Response::Props(val) => tuono_lib::ssr::Js::SSR.with(|ssr| ssr.borrow_mut().render_to_string(Some(&payload))),
|
},
|
||||||
|
/// TODO: handle here redirection and rewrite
|
||||||
_ => Ok("500 Internal server error".to_string())
|
_ => Ok("500 Internal server error".to_string())
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Ok(html) => Html(html),
|
Ok(html) => Html(html),
|
||||||
_ => Html("500 internal server error".to_string())
|
_ => Html("500 internal server error".to_string())
|
||||||
|
|||||||
Reference in New Issue
Block a user