mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-26 22:20:19 +00:00
remove the extract macro
This commit is contained in:
parent
0636e94f37
commit
c6ddee6f21
4 changed files with 9 additions and 68 deletions
|
@ -121,23 +121,22 @@ fn app(cx: Scope) -> Element {
|
|||
}
|
||||
|
||||
#[server(GetUserName)]
|
||||
pub async fn get_user_name(
|
||||
#[extract] session: crate::auth::Session,
|
||||
) -> Result<String, ServerFnError> {
|
||||
pub async fn get_user_name() -> Result<String, ServerFnError> {
|
||||
let session: crate::auth::Session = extract().await?;
|
||||
Ok(session.0.current_user.unwrap().username.to_string())
|
||||
}
|
||||
|
||||
#[server(Login)]
|
||||
pub async fn login(#[extract] auth: crate::auth::Session) -> Result<(), ServerFnError> {
|
||||
pub async fn login() -> Result<(), ServerFnError> {
|
||||
let auth: crate::auth::Session = extract().await?;
|
||||
auth.login_user(2);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[server(Permissions)]
|
||||
pub async fn get_permissions(
|
||||
#[extract] method: axum::http::Method,
|
||||
#[extract] auth: crate::auth::Session,
|
||||
) -> Result<String, ServerFnError> {
|
||||
pub async fn get_permissions() -> Result<String, ServerFnError> {
|
||||
let method: axum::http::Method = extract().await?;
|
||||
let auth: crate::auth::Session = extract().await?;
|
||||
let current_user = auth.current_user.clone().unwrap_or_default();
|
||||
|
||||
// lets check permissions only and not worry about if they are anon or not
|
||||
|
|
|
@ -5,7 +5,6 @@ use quote::{ToTokens, __private::TokenStream as TokenStream2};
|
|||
use server_fn_macro::*;
|
||||
use syn::{
|
||||
parse::{Parse, ParseStream},
|
||||
spanned::Spanned,
|
||||
Ident, ItemFn, Token,
|
||||
};
|
||||
|
||||
|
@ -67,22 +66,6 @@ pub fn server(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
|
|||
Err(e) => return e.into(),
|
||||
};
|
||||
|
||||
// find all arguments with the #[extract] attribute
|
||||
let mut extractors: Vec<Extractor> = vec![];
|
||||
function.sig.inputs = function
|
||||
.sig
|
||||
.inputs
|
||||
.into_iter()
|
||||
.filter(|arg| {
|
||||
if let Ok(extractor) = syn::parse2(arg.clone().into_token_stream()) {
|
||||
extractors.push(extractor);
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// extract all #[middleware] attributes
|
||||
let mut middlewares: Vec<Middleware> = vec![];
|
||||
function.attrs.retain(|attr| {
|
||||
|
@ -107,7 +90,6 @@ pub fn server(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
|
|||
let mapped_body = quote::quote! {
|
||||
#(#attrs)*
|
||||
#vis #sig {
|
||||
#(#extractors)*
|
||||
#block
|
||||
}
|
||||
};
|
||||
|
@ -155,45 +137,6 @@ pub fn server(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {
|
|||
}
|
||||
}
|
||||
|
||||
struct Extractor {
|
||||
pat: syn::PatType,
|
||||
}
|
||||
|
||||
impl ToTokens for Extractor {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream2) {
|
||||
let pat = &self.pat;
|
||||
tokens.extend(quote::quote! {
|
||||
let #pat = ::dioxus_fullstack::prelude::extract_server_context().await?;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for Extractor {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
let arg: syn::FnArg = input.parse()?;
|
||||
match arg {
|
||||
syn::FnArg::Typed(mut pat_type) => {
|
||||
let mut contains_extract = false;
|
||||
pat_type.attrs.retain(|attr| {
|
||||
let is_extract = attr.path().is_ident("extract");
|
||||
if is_extract {
|
||||
contains_extract = true;
|
||||
}
|
||||
!is_extract
|
||||
});
|
||||
if !contains_extract {
|
||||
return Err(syn::Error::new(
|
||||
pat_type.span(),
|
||||
"expected an argument with the #[extract] attribute",
|
||||
));
|
||||
}
|
||||
Ok(Extractor { pat: pat_type })
|
||||
}
|
||||
_ => Err(syn::Error::new(arg.span(), "expected a typed argument")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Middleware {
|
||||
expr: syn::Expr,
|
||||
|
|
|
@ -47,8 +47,7 @@ pub mod prelude {
|
|||
pub use crate::server_context::Axum;
|
||||
#[cfg(feature = "ssr")]
|
||||
pub use crate::server_context::{
|
||||
extract_server_context, server_context, DioxusServerContext, FromServerContext,
|
||||
ProvideServerContext,
|
||||
extract, server_context, DioxusServerContext, FromServerContext, ProvideServerContext,
|
||||
};
|
||||
pub use crate::server_fn::DioxusServerFn;
|
||||
#[cfg(feature = "ssr")]
|
||||
|
|
|
@ -117,7 +117,7 @@ pub fn server_context() -> DioxusServerContext {
|
|||
/// Extract some part from the current server request.
|
||||
///
|
||||
/// This function will only provide the current server context if it is called from a server function or on the server rendering a request.
|
||||
pub async fn extract_server_context<E: FromServerContext<I>, I>() -> Result<E, E::Rejection> {
|
||||
pub async fn extract<E: FromServerContext<I>, I>() -> Result<E, E::Rejection> {
|
||||
E::from_request(&server_context()).await
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue