yew_extra
Extract Axum request data within Yew server functions, similar to how leptos_axum provides extraction helpers for Leptos.
Overview
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.
Features
- Request Extraction: Extract Axum request data using the
FromRequestPartstrait - State Support: Compatible with extractors that require application state
- Type-safe: Leverages Rust's type system for compile-time guarantees
- SSR Compatible: Designed for server-side rendering scenarios
- No WASM Overhead: Server-only dependencies excluded from WASM builds
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Usage
Basic Extraction
Use the extract() function to access request data in your server functions:
use extract;
use Method;
use CookieJar;
pub async
Extraction with State
For extractors that require application state, use extract_with_state():
use extract_with_state;
pub async
Setting Up the Server
On the server side, you need to provide request parts before calling server functions:
use ;
use ;
async
How It Works
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.
Supported Extractors
Any type that implements Axum's FromRequestParts trait can be extracted, including:
- HTTP Primitives:
Method,Uri,Version,HeaderMap - Cookies:
CookieJar(fromaxum_extra) - Headers:
TypedHeader<T>(fromaxum_extra) - Connection Info:
ConnectInfo<T> - Custom Extractors: Any custom type implementing
FromRequestParts
Error Handling
Extraction can fail in two ways:
- MissingParts: Request parts weren't provided (forgot to call
provide_request_parts()) - ExtractionFailed: The extractor itself failed (e.g., missing required header)
Both errors are wrapped in the ExtractError enum which implements std::error::Error.
Platform Support
This crate is designed for server-side use only. All server-specific dependencies are excluded from WASM builds to keep your client bundle small.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.