From 71e01d5be608c3ceec34a8856b4eee2dfbed2b24 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 5 Nov 2025 14:53:51 -0300 Subject: [PATCH] use default fullstackcontext instead --- packages/fullstack-core/src/streaming.rs | 15 +++++++++++++-- packages/fullstack-macro/src/lib.rs | 15 +-------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/fullstack-core/src/streaming.rs b/packages/fullstack-core/src/streaming.rs index bb7052d67..32fc176c4 100644 --- a/packages/fullstack-core/src/streaming.rs +++ b/packages/fullstack-core/src/streaming.rs @@ -126,8 +126,19 @@ impl FullstackContext { /// The body of the request is always empty when using this method, as the body can only be consumed once in the server /// function extractors. pub async fn extract, M>() -> Result { - let this = Self::current() - .ok_or_else(|| ServerFnError::new("No FullstackContext found".to_string()))?; + let this = Self::current().unwrap_or_else(|| { + // Create a dummy context if one doesn't exist, making the function usable outside of a request context + FullstackContext::new( + axum_core::extract::Request::builder() + .method("GET") + .uri("/") + .header("X-Dummy-Header", "true") + .body(()) + .unwrap() + .into_parts() + .0, + ) + }); let parts = this.request_headers.read().clone(); let request = axum_core::extract::Request::from_parts(parts, Default::default()); diff --git a/packages/fullstack-macro/src/lib.rs b/packages/fullstack-macro/src/lib.rs index 835aab1e3..126d179e2 100644 --- a/packages/fullstack-macro/src/lib.rs +++ b/packages/fullstack-macro/src/lib.rs @@ -432,15 +432,6 @@ fn route_impl_with_route( let extracted_as_server_headers = route.extracted_as_server_headers(query_tokens.clone()); - // Generate extraction code based on whether there are server extractors - let extraction_code = if route.server_args.is_empty() { - quote! { Default::default() } - } else { - quote! { - dioxus_fullstack::FullstackContext::extract::<(#(#server_types,)*), _>().await? - } - }; - Ok(quote! { #(#fn_docs)* #route_docs @@ -564,11 +555,7 @@ fn route_impl_with_route( } // Extract the server arguments from the context if needed. - // If there are no server extractors, use Default::default() which allows - // the function to be called from backend code without HTTP context. - // If there are server extractors, extraction will fail with a clear error - // when called from backend code without HTTP context. - let (#(#server_names,)*) = #extraction_code; + let (#(#server_names,)*) = dioxus_fullstack::FullstackContext::extract::<(#(#server_types,)*), _>().await?; // Call the function directly return #fn_on_server_name #ty_generics(