Merge pull request #99 from javierEd/main

Add spin feature for `use_cookie`
This commit is contained in:
Marc-Stefan Cassola 2024-04-19 14:47:02 +01:00 committed by GitHub
commit 9507c6a6d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 18 deletions

View file

@ -29,6 +29,7 @@ lazy_static = "1"
leptos = "0.6" leptos = "0.6"
leptos_axum = { version = "0.6", optional = true } leptos_axum = { version = "0.6", optional = true }
leptos_actix = { 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 } num = { version = "0.4", optional = true }
paste = "1" paste = "1"
prost = { version = "0.12", optional = true } prost = { version = "0.12", optional = true }
@ -142,6 +143,7 @@ docs = []
math = ["num"] math = ["num"]
prost = ["base64", "dep:prost"] prost = ["base64", "dep:prost"]
serde = ["dep:serde", "serde_json"] serde = ["dep:serde", "serde_json"]
spin = ["dep:leptos-spin", "dep:http1"]
ssr = [] ssr = []
msgpack = ["dep:rmp-serde", "dep:serde"] msgpack = ["dep:rmp-serde", "dep:serde"]

View file

@ -485,9 +485,15 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
#[cfg(all(feature = "actix", feature = "axum"))] #[cfg(all(feature = "actix", feature = "axum"))]
compile_error!("You cannot enable only one of features \"actix\" and \"axum\" at the same time"); 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")] #[cfg(feature = "actix")]
const COOKIE: http0_2::HeaderName = http0_2::header::COOKIE; 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; const COOKIE: http1::HeaderName = http1::header::COOKIE;
#[cfg(feature = "actix")] #[cfg(feature = "actix")]
@ -495,7 +501,7 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
#[cfg(feature = "axum")] #[cfg(feature = "axum")]
type HeaderValue = http1::HeaderValue; type HeaderValue = http1::HeaderValue;
#[cfg(any(feature = "axum", feature = "actix"))] #[cfg(any(feature = "axum", feature = "actix", feature = "spin"))]
let headers; let headers;
#[cfg(feature = "actix")] #[cfg(feature = "actix")]
{ {
@ -506,23 +512,43 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
{ {
headers = use_context::<http1::request::Parts>().map(|parts| parts.headers); headers = use_context::<http1::request::Parts>().map(|parts| parts.headers);
} }
#[cfg(feature = "spin")]
#[cfg(all(not(feature = "axum"), not(feature = "actix")))]
{ {
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`"); headers = use_context::<leptos_spin::RequestParts>()
.map(|parts| parts.headers().clone());
}
#[cfg(all(
not(feature = "axum"),
not(feature = "actix"),
not(feature = "spin")
))]
{
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 None
} }
#[cfg(any(feature = "axum", feature = "actix"))] #[cfg(any(feature = "axum", feature = "actix"))]
headers.map(|headers| { {
headers headers.map(|headers| {
.get(COOKIE) headers
.cloned() .get(COOKIE)
.unwrap_or_else(|| HeaderValue::from_static("")) .cloned()
.to_str() .unwrap_or_else(|| HeaderValue::from_static(""))
.unwrap_or_default() .to_str()
.to_owned() .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"))] #[cfg(not(feature = "ssr"))]
None None
@ -534,21 +560,27 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
use leptos_actix::ResponseOptions; use leptos_actix::ResponseOptions;
#[cfg(feature = "axum")] #[cfg(feature = "axum")]
use leptos_axum::ResponseOptions; use leptos_axum::ResponseOptions;
#[cfg(feature = "spin")]
use leptos_spin::ResponseOptions;
#[cfg(feature = "actix")] #[cfg(feature = "actix")]
const SET_COOKIE: http0_2::HeaderName = http0_2::header::SET_COOKIE; 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; const SET_COOKIE: http1::HeaderName = http1::header::SET_COOKIE;
#[cfg(feature = "actix")] #[cfg(feature = "actix")]
type HeaderValue = http0_2::HeaderValue; type HeaderValue = http0_2::HeaderValue;
#[cfg(feature = "axum")] #[cfg(any(feature = "axum", feature = "spin"))]
type HeaderValue = http1::HeaderValue; 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; 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"))] #[cfg(any(feature = "axum", feature = "actix"))]
@ -561,6 +593,13 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
} }
} }
} }
#[cfg(feature = "spin")]
{
if let Some(response_options) = use_context::<ResponseOptions>() {
let header_value = cookie.encoded().to_string().as_bytes().to_vec();
response_options.insert_header(SET_COOKIE.as_str(), header_value);
}
}
} }
let _ = cookie; let _ = cookie;