feat: allow feature-name flexibility when using server functions (#1427)

This commit is contained in:
Greg Johnston 2023-07-25 06:07:52 -04:00 committed by GitHub
parent e67bc2083a
commit 5e26e84d77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 44 deletions

View File

@ -46,6 +46,7 @@ ssr = [
"leptos_macro/ssr",
"leptos_reactive/ssr",
"leptos_server/ssr",
"server_fn/ssr",
]
nightly = [
"leptos_dom/nightly",

View File

@ -35,10 +35,9 @@ trybuild = "1"
leptos = { path = "../leptos" }
[features]
default = ["ssr"]
csr = []
hydrate = []
ssr = []
ssr = ["server_fn_macro/ssr"]
nightly = ["server_fn_macro/nightly"]
tracing = []

View File

@ -10,6 +10,7 @@ readme = "../README.md"
[dependencies]
leptos_reactive = { workspace = true }
leptos_macro = { workspace = true }
server_fn = { workspace = true }
lazy_static = "1"
serde = { version = "1", features = ["derive"] }
@ -21,11 +22,11 @@ inventory = "0.3"
leptos = { path = "../leptos" }
[features]
csr = ["leptos_reactive/csr"]
csr = ["leptos_reactive/csr", "leptos_macro/csr"]
default-tls = ["server_fn/default-tls"]
hydrate = ["leptos_reactive/hydrate"]
hydrate = ["leptos_reactive/hydrate", "leptos_macro/hydrate"]
rustls = ["server_fn/rustls"]
ssr = ["leptos_reactive/ssr", "server_fn/ssr"]
ssr = ["leptos_reactive/ssr", "server_fn/ssr", "leptos_macro/ssr"]
nightly = ["leptos_reactive/nightly", "server_fn/nightly"]
[package.metadata.cargo-all-features]

View File

@ -19,3 +19,4 @@ const_format = "0.2.30"
[features]
nightly = []
ssr = []

View File

@ -217,6 +217,61 @@ pub fn server_macro_impl(
.map(|(doc, span)| quote_spanned!(*span=> #[doc = #doc]))
.collect::<TokenStream2>();
let inventory = if cfg!(feature = "ssr") {
quote! {
#server_fn_path::inventory::submit! {
#trait_obj_wrapper::from_generic_server_fn(#server_fn_path::ServerFnTraitObj::new(
#struct_name::PREFIX,
#struct_name::URL,
#struct_name::ENCODING,
<#struct_name as #server_fn_path::ServerFn<#server_ctx_path>>::call_from_bytes,
))
}
}
} else {
quote! {}
};
let call_fn = if cfg!(feature = "ssr") {
quote! {
fn call_fn(self, cx: #server_ctx_path) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self::Output, #server_fn_path::ServerFnError>>>> {
let #struct_name { #(#field_names),* } = self;
Box::pin(async move { #fn_name( #cx_fn_arg #(#field_names_2),*).await })
}
}
} else {
quote! {
fn call_fn_client(self, cx: #server_ctx_path) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self::Output, #server_fn_path::ServerFnError>>>> {
let #struct_name { #(#field_names_3),* } = self;
Box::pin(async move { #fn_name( #cx_fn_arg #(#field_names_4),*).await })
}
}
};
let func = if cfg!(feature = "ssr") {
quote! {
#docs
#vis async fn #fn_name(#(#fn_args),*) #output_arrow #return_ty {
#block
}
}
} else {
quote! {
#docs
#[allow(unused_variables)]
#vis async fn #fn_name(#(#fn_args_2),*) #output_arrow #return_ty {
#server_fn_path::call_server_fn(
&{
let prefix = #struct_name::PREFIX.to_string();
prefix + "/" + #struct_name::URL
},
#struct_name { #(#field_names_5),* },
#encoding
).await
}
}
};
Ok(quote::quote! {
#args_docs
#docs
@ -241,15 +296,7 @@ pub fn server_macro_impl(
const ENCODING: #server_fn_path::Encoding = #encoding;
}
#[cfg(feature = "ssr")]
#server_fn_path::inventory::submit! {
#trait_obj_wrapper::from_generic_server_fn(#server_fn_path::ServerFnTraitObj::new(
#struct_name::PREFIX,
#struct_name::URL,
#struct_name::ENCODING,
<#struct_name as #server_fn_path::ServerFn<#server_ctx_path>>::call_from_bytes,
))
}
#inventory
impl #server_fn_path::ServerFn<#server_ctx_path> for #struct_name {
type Output = #output_ty;
@ -266,38 +313,10 @@ pub fn server_macro_impl(
Self::ENCODING
}
#[cfg(feature = "ssr")]
fn call_fn(self, cx: #server_ctx_path) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self::Output, #server_fn_path::ServerFnError>>>> {
let #struct_name { #(#field_names),* } = self;
Box::pin(async move { #fn_name( #cx_fn_arg #(#field_names_2),*).await })
}
#[cfg(not(feature = "ssr"))]
fn call_fn_client(self, cx: #server_ctx_path) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self::Output, #server_fn_path::ServerFnError>>>> {
let #struct_name { #(#field_names_3),* } = self;
Box::pin(async move { #fn_name( #cx_fn_arg #(#field_names_4),*).await })
}
#call_fn
}
#docs
#[cfg(feature = "ssr")]
#vis async fn #fn_name(#(#fn_args),*) #output_arrow #return_ty {
#block
}
#docs
#[cfg(not(feature = "ssr"))]
#[allow(unused_variables)]
#vis async fn #fn_name(#(#fn_args_2),*) #output_arrow #return_ty {
#server_fn_path::call_server_fn(
&{
let prefix = #struct_name::PREFIX.to_string();
prefix + "/" + #struct_name::URL
},
#struct_name { #(#field_names_5),* },
#encoding
).await
}
#func
})
}