diff --git a/Cargo.toml b/Cargo.toml index 0f7bfd01..7389559b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ lazy_static = "1" leptos = "0.6" leptos_axum = { version = "0.6", optional = true } leptos_actix = { version = "0.6", optional = true } +leptos-spin = { version = "0.1", optional = true } num = { version = "0.4", optional = true } paste = "1" prost = { version = "0.12", optional = true } @@ -142,6 +143,7 @@ docs = [] math = ["num"] prost = ["base64", "dep:prost"] serde = ["dep:serde", "serde_json"] +spin = ["dep:leptos-spin", "dep:http1"] ssr = [] msgpack = ["dep:rmp-serde", "dep:serde"] diff --git a/src/use_cookie.rs b/src/use_cookie.rs index 9d06a552..dff82acd 100644 --- a/src/use_cookie.rs +++ b/src/use_cookie.rs @@ -485,9 +485,15 @@ impl Default for UseCookieOptions { #[cfg(all(feature = "actix", feature = "axum"))] compile_error!("You cannot enable only one of features \"actix\" and \"axum\" at the same time"); + #[cfg(all(feature = "actix", feature = "spin"))] + compile_error!("You cannot enable only one of features \"actix\" and \"spin\" at the same time"); + + #[cfg(all(feature = "axum", feature = "spin"))] + compile_error!("You cannot enable only one of features \"axum\" and \"spin\" at the same time"); + #[cfg(feature = "actix")] const COOKIE: http0_2::HeaderName = http0_2::header::COOKIE; - #[cfg(feature = "axum")] + #[cfg(any(feature = "axum", feature = "spin"))] const COOKIE: http1::HeaderName = http1::header::COOKIE; #[cfg(feature = "actix")] @@ -495,7 +501,7 @@ impl Default for UseCookieOptions { #[cfg(feature = "axum")] type HeaderValue = http1::HeaderValue; - #[cfg(any(feature = "axum", feature = "actix"))] + #[cfg(any(feature = "axum", feature = "actix", feature = "spin"))] let headers; #[cfg(feature = "actix")] { @@ -506,23 +512,43 @@ impl Default for UseCookieOptions { { headers = use_context::().map(|parts| parts.headers); } + #[cfg(feature = "spin")] + { + headers = use_context::() + .map(|parts| parts.headers().clone()); + } - #[cfg(all(not(feature = "axum"), not(feature = "actix")))] + #[cfg(all( + not(feature = "axum"), + not(feature = "actix"), + not(feature = "spin") + ))] { - leptos::logging::warn!("If you're using use_cookie without the feature `axum` or `actix` enabled, you should provide the option `ssr_cookies_header_getter`"); + leptos::logging::warn!("If you're using use_cookie without the feature `axum`, `actix` or `spin` enabled, you should provide the option `ssr_cookies_header_getter`"); None } #[cfg(any(feature = "axum", feature = "actix"))] - headers.map(|headers| { - headers - .get(COOKIE) - .cloned() - .unwrap_or_else(|| HeaderValue::from_static("")) - .to_str() - .unwrap_or_default() - .to_owned() - }) + { + headers.map(|headers| { + headers + .get(COOKIE) + .cloned() + .unwrap_or_else(|| HeaderValue::from_static("")) + .to_str() + .unwrap_or_default() + .to_owned() + }) + } + #[cfg(feature = "spin")] + { + headers.and_then(|headers| { + headers + .iter() + .find(|(key, _)| **key == COOKIE) + .and_then(|(_, value)| String::from_utf8(value.to_vec()).ok()) + }) + } } #[cfg(not(feature = "ssr"))] None @@ -534,21 +560,27 @@ impl Default for UseCookieOptions { use leptos_actix::ResponseOptions; #[cfg(feature = "axum")] use leptos_axum::ResponseOptions; + #[cfg(feature = "spin")] + use leptos_spin::ResponseOptions; #[cfg(feature = "actix")] const SET_COOKIE: http0_2::HeaderName = http0_2::header::SET_COOKIE; - #[cfg(feature = "axum")] + #[cfg(any(feature = "axum", feature = "spin"))] const SET_COOKIE: http1::HeaderName = http1::header::SET_COOKIE; #[cfg(feature = "actix")] type HeaderValue = http0_2::HeaderValue; - #[cfg(feature = "axum")] + #[cfg(any(feature = "axum", feature = "spin"))] type HeaderValue = http1::HeaderValue; - #[cfg(all(not(feature = "axum"), not(feature = "actix")))] + #[cfg(all( + not(feature = "axum"), + not(feature = "actix"), + not(feature = "spin") + ))] { let _ = cookie; - leptos::logging::warn!("If you're using use_cookie without the feature `axum` or `actix` enabled, you should provide the option `ssr_set_cookie`"); + leptos::logging::warn!("If you're using use_cookie without the feature `axum`, `actix` or `spin` enabled, you should provide the option `ssr_set_cookie`"); } #[cfg(any(feature = "axum", feature = "actix"))] @@ -561,6 +593,13 @@ impl Default for UseCookieOptions { } } } + #[cfg(feature = "spin")] + { + if let Some(response_options) = use_context::() { + let header_value = cookie.encoded().to_string().as_bytes().to_vec(); + response_options.insert_header(SET_COOKIE.as_str(), header_value); + } + } } let _ = cookie;