diff --git a/leptos_macro/tests/server.rs b/leptos_macro/tests/server.rs index 052d88d67..cbbb848fc 100644 --- a/leptos_macro/tests/server.rs +++ b/leptos_macro/tests/server.rs @@ -3,7 +3,8 @@ use cfg_if::cfg_if; cfg_if! { if #[cfg(not(feature = "ssr"))] { - use leptos::{server, server_fn::Encoding, ServerFnError}; + use leptos::{server, server_fn::{codec, ServerFn}, ServerFnError}; + use std::any::TypeId; #[test] fn server_default() { @@ -11,9 +12,11 @@ cfg_if! { pub async fn my_server_action() -> Result<(), ServerFnError> { Ok(()) } - assert_eq!(MyServerAction::PREFIX, "/api"); - assert_eq!(&MyServerAction::URL[0..16], "my_server_action"); - assert_eq!(MyServerAction::ENCODING, Encoding::Url); + assert_eq!( + ::PATH.trim_end_matches(char::is_numeric), + "/api/my_server_action" + ); + assert_eq!(TypeId::of::<::InputEncoding>(), TypeId::of::()); } #[test] @@ -22,9 +25,8 @@ cfg_if! { pub async fn my_server_action() -> Result<(), ServerFnError> { Ok(()) } - assert_eq!(FooBar::PREFIX, "/foo/bar"); - assert_eq!(FooBar::URL, "my_path"); - assert_eq!(FooBar::ENCODING, Encoding::Cbor); + assert_eq!(::PATH, "/foo/bar/my_path"); + assert_eq!(TypeId::of::<::InputEncoding>(), TypeId::of::()); } #[test] @@ -33,9 +35,8 @@ cfg_if! { pub async fn my_server_action() -> Result<(), ServerFnError> { Ok(()) } - assert_eq!(FooBar::PREFIX, "/foo/bar"); - assert_eq!(FooBar::URL, "my_path"); - assert_eq!(FooBar::ENCODING, Encoding::Cbor); + assert_eq!(::PATH, "/foo/bar/my_path"); + assert_eq!(TypeId::of::<::InputEncoding>(), TypeId::of::()); } #[test] @@ -44,9 +45,8 @@ cfg_if! { pub async fn my_server_action() -> Result<(), ServerFnError> { Ok(()) } - assert_eq!(FooBar::PREFIX, "/api"); - assert_eq!(FooBar::URL, "my_path"); - assert_eq!(FooBar::ENCODING, Encoding::Url); + assert_eq!(::PATH, "/api/my_path"); + assert_eq!(TypeId::of::<::InputEncoding>(), TypeId::of::()); } #[test] @@ -55,9 +55,11 @@ cfg_if! { pub async fn my_server_action() -> Result<(), ServerFnError> { Ok(()) } - assert_eq!(FooBar::PREFIX, "/api"); - assert_eq!(&FooBar::URL[0..16], "my_server_action"); - assert_eq!(FooBar::ENCODING, Encoding::Url); + assert_eq!( + ::PATH.trim_end_matches(char::is_numeric), + "/api/my_server_action" + ); + assert_eq!(TypeId::of::<::InputEncoding>(), TypeId::of::()); } #[test] @@ -66,9 +68,8 @@ cfg_if! { pub async fn my_server_action() -> Result<(), ServerFnError> { Ok(()) } - assert_eq!(MyServerAction::PREFIX, "/foo/bar"); - assert_eq!(&MyServerAction::URL[0..16], "my_server_action"); - assert_eq!(MyServerAction::ENCODING, Encoding::Url); + assert_eq!(::PATH.trim_end_matches(char::is_numeric), "/foo/bar/my_server_action"); + assert_eq!(TypeId::of::<::InputEncoding>(), TypeId::of::()); } #[test] @@ -77,9 +78,11 @@ cfg_if! { pub async fn my_server_action() -> Result<(), ServerFnError> { Ok(()) } - assert_eq!(MyServerAction::PREFIX, "/api"); - assert_eq!(&MyServerAction::URL[0..16], "my_server_action"); - assert_eq!(MyServerAction::ENCODING, Encoding::GetJSON); + assert_eq!( + ::PATH.trim_end_matches(char::is_numeric), + "/api/my_server_action" + ); + assert_eq!(TypeId::of::<::InputEncoding>(), TypeId::of::()); } #[test] @@ -88,9 +91,8 @@ cfg_if! { pub async fn my_server_action() -> Result<(), ServerFnError> { Ok(()) } - assert_eq!(MyServerAction::PREFIX, "/api"); - assert_eq!(MyServerAction::URL, "/path/to/my/endpoint"); - assert_eq!(MyServerAction::ENCODING, Encoding::Url); + assert_eq!(::PATH, "/api/path/to/my/endpoint"); + assert_eq!(TypeId::of::<::InputEncoding>(), TypeId::of::()); } } } diff --git a/leptos_macro/tests/ui/server.stderr b/leptos_macro/tests/ui/server.stderr index 0cfa2665e..cb3614adf 100644 --- a/leptos_macro/tests/ui/server.stderr +++ b/leptos_macro/tests/ui/server.stderr @@ -4,7 +4,7 @@ error: positional argument follows keyword argument 3 | #[server(endpoint = "my_path", FooBar)] | ^^^^^^ -error: keyword argument repeated: endpoint +error: keyword argument repeated: `endpoint` --> tests/ui/server.rs:8:30 | 8 | #[server(endpoint = "first", endpoint = "second")] @@ -40,7 +40,7 @@ error: unexpected extra argument 32 | #[server(FooBar, "/foo/bar", "Cbor", "my_path", "extra")] | ^^^^^^^ -error: Encoding Not Found +error: Encoding not found. --> tests/ui/server.rs:37:21 | 37 | #[server(encoding = "wrong")] diff --git a/server_fn/Cargo.toml b/server_fn/Cargo.toml index 22f7d5951..2f8bd20fd 100644 --- a/server_fn/Cargo.toml +++ b/server_fn/Cargo.toml @@ -64,7 +64,7 @@ reqwest = { version = "0.11", default-features = false, optional = true, feature ] } [features] -default = ["url", "json"] +default = ["url", "json", "cbor"] actix = ["ssr", "dep:actix-web", "dep:send_wrapper"] axum = [ "ssr", diff --git a/server_fn_macro/src/lib.rs b/server_fn_macro/src/lib.rs index 1d26b8a92..eca952367 100644 --- a/server_fn_macro/src/lib.rs +++ b/server_fn_macro/src/lib.rs @@ -369,6 +369,15 @@ pub fn server_macro_impl( )); }; + // Remove any leading slashes, even if they exist (we'll add them below) + let fn_path = Literal::string( + fn_path + .to_string() + .trim_start_matches('\"') + .trim_start_matches('/') + .trim_end_matches('\"'), + ); + // generate path let fn_path_starts_with_slash = fn_path.to_string().starts_with("\"/"); let fn_path = if fn_path_starts_with_slash || fn_path.to_string() == "\"\"" @@ -391,6 +400,7 @@ pub fn server_macro_impl( } else { #server_fn_path::const_format::concatcp!( #prefix, + "/", #fn_path ) }