| Crates.io | yew_extra |
| lib.rs | yew_extra |
| version | 0.3.0 |
| created_at | 2025-11-21 09:55:19.458586+00 |
| updated_at | 2025-11-21 10:30:32.494431+00 |
| description | Extract Axum request data within Yew server functions similar to how `leptos_axum` provides extraction helpers for Leptos |
| homepage | |
| repository | https://github.com/netrondev/yew_crates |
| max_upload_size | |
| id | 1943353 |
| size | 29,729 |
Extract Axum request data within Yew server functions, similar to how leptos_axum provides extraction helpers for Leptos.
yew_extra provides utilities for accessing Axum request data (headers, cookies, method, etc.) within Yew server functions when using server-side rendering. This is particularly useful when you need to access request context like cookies, headers, or custom extractors in your server functions.
FromRequestParts traitAdd this to your Cargo.toml:
cargo add yew_extra
Use the extract() function to access request data in your server functions:
use yew_extra::extract;
use axum::http::Method;
use axum_extra::extract::CookieJar;
#[yewserverhook(path = "/api/users")]
pub async fn get_users() -> Result<Vec<User>, AppError> {
// Extract the HTTP method
let method: Method = extract().await?;
// Extract cookies
let cookie_jar: CookieJar = extract().await?;
// Use the extracted data
if let Some(session) = cookie_jar.get("session_token") {
// Validate session...
}
Ok(fetch_users().await?)
}
For extractors that require application state, use extract_with_state():
use yew_extra::extract_with_state;
#[yewserverhook(path = "/api/data")]
pub async fn get_data() -> Result<Data, AppError> {
let app_state = get_app_state();
let db_pool: DbPool = extract_with_state(&app_state).await?;
Ok(fetch_data_from_db(db_pool).await?)
}
On the server side, you need to provide request parts before calling server functions:
use yew_extra::{provide_request_parts, clear_request_parts};
use axum::{body::Body, http::Request};
async fn handler(req: Request<Body>) {
let (parts, body) = req.into_parts();
// Provide the request parts to the context
provide_request_parts(parts).await;
// Execute your server function
let result = your_server_function().await;
// Clean up after completion
clear_request_parts().await;
}
yew_extra uses task-local storage to make request parts available throughout the execution of a server function. When you call provide_request_parts(), the request data is stored with a unique task ID. The extract() function then retrieves this data and uses Axum's FromRequestParts trait to extract the desired type.
This approach is similar to how leptos_axum handles request extraction, making it familiar to developers coming from the Leptos ecosystem.
Any type that implements Axum's FromRequestParts trait can be extracted, including:
Method, Uri, Version, HeaderMapCookieJar (from axum_extra)TypedHeader<T> (from axum_extra)ConnectInfo<T>FromRequestPartsExtraction can fail in two ways:
provide_request_parts())Both errors are wrapped in the ExtractError enum which implements std::error::Error.
This crate is designed for server-side use only. All server-specific dependencies are excluded from WASM builds to keep your client bundle small.
Licensed under either of:
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.