diff --git a/Cargo.toml b/Cargo.toml index f770daf..4cf3e17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,24 +1,25 @@ [package] -name = "leptos-use" -version = "0.9.0" -edition = "2021" authors = ["Marc-Stefan Cassola"] categories = ["gui", "web-programming"] description = "Collection of essential Leptos utilities inspired by SolidJS USE / VueUse" +edition = "2021" exclude = ["examples/", "tests/"] +homepage = "https://leptos-use.rs" keywords = ["leptos", "utilities"] license = "MIT OR Apache-2.0" +name = "leptos-use" readme = "README.md" repository = "https://github.com/Synphonyte/leptos-use" -homepage = "https://leptos-use.rs" +version = "0.9.0" [dependencies] base64 = { version = "0.21", optional = true } cfg-if = "1" +cookie = { version = "0.18", features = ["percent-encode"] } default-struct-builder = "0.5" futures-util = "0.3" gloo-timers = { version = "0.3.0", features = ["futures"] } -gloo-utils = { version = "0.2.0"} +gloo-utils = { version = "0.2.0" } js-sys = "0.3" lazy_static = "1" leptos = "0.5" @@ -32,78 +33,82 @@ wasm-bindgen = "0.2.88" wasm-bindgen-futures = "0.4" [dependencies.web-sys] -version = "0.3.65" features = [ - "AddEventListenerOptions", - "BinaryType", - "Coordinates", - "CloseEvent", - "CssStyleDeclaration", - "CustomEvent", - "CustomEventInit", - "DisplayMediaStreamConstraints", - "DomRect", - "DomRectReadOnly", - "DataTransfer", - "DragEvent", - "Element", - "EventListener", - "EventListenerOptions", - "EventTarget", - "File", - "FileList", - "Geolocation", - "HtmlElement", - "HtmlLinkElement", - "HtmlStyleElement", - "IntersectionObserver", - "IntersectionObserverInit", - "IntersectionObserverEntry", - "MediaDevices", - "MediaQueryList", - "MediaStream", - "MediaStreamTrack", - "MouseEvent", - "MutationObserver", - "MutationObserverInit", - "MutationRecord", - "Navigator", - "NodeList", - "Notification", - "NotificationDirection", - "NotificationOptions", - "NotificationPermission", - "PointerEvent", - "Position", - "PositionError", - "PositionOptions", - "ResizeObserver", - "ResizeObserverBoxOptions", - "ResizeObserverEntry", - "ResizeObserverOptions", - "ResizeObserverSize", - "ScrollBehavior", - "ScrollToOptions", - "ServiceWorker", - "ServiceWorkerContainer", - "ServiceWorkerRegistration", - "ServiceWorkerState", - "Storage", - "StorageEvent", - "Touch", - "TouchEvent", - "TouchList", - "VisibilityState", - "WebSocket", - "Window", + "AddEventListenerOptions", + "BinaryType", + "Coordinates", + "CloseEvent", + "CssStyleDeclaration", + "CustomEvent", + "CustomEventInit", + "DisplayMediaStreamConstraints", + "DomRect", + "DomRectReadOnly", + "DataTransfer", + "DragEvent", + "Element", + "EventListener", + "EventListenerOptions", + "EventTarget", + "File", + "FileList", + "Geolocation", + "HtmlDocument", + "HtmlElement", + "HtmlLinkElement", + "HtmlStyleElement", + "IntersectionObserver", + "IntersectionObserverInit", + "IntersectionObserverEntry", + "MediaDevices", + "MediaQueryList", + "MediaStream", + "MediaStreamTrack", + "MouseEvent", + "MutationObserver", + "MutationObserverInit", + "MutationRecord", + "Navigator", + "NodeList", + "Notification", + "NotificationDirection", + "NotificationOptions", + "NotificationPermission", + "PointerEvent", + "Position", + "PositionError", + "PositionOptions", + "ResizeObserver", + "ResizeObserverBoxOptions", + "ResizeObserverEntry", + "ResizeObserverOptions", + "ResizeObserverSize", + "ScrollBehavior", + "ScrollToOptions", + "ServiceWorker", + "ServiceWorkerContainer", + "ServiceWorkerRegistration", + "ServiceWorkerState", + "Storage", + "StorageEvent", + "Touch", + "TouchEvent", + "TouchList", + "VisibilityState", + "WebSocket", + "Window", ] +version = "0.3" [features] +actix = ["actix-web/cookies"] +axum = ["axum-extra/cookie", "leptos_axum"] docs = [] math = ["num"] prost = ["base64", "dep:prost"] serde = ["dep:serde", "serde_json"] ssr = [] +storage = ["serde", "serde_json", "web-sys/StorageEvent"] [package.metadata.docs.rs] all-features = true diff --git a/src/lib.rs b/src/lib.rs index 2c5fdc1..443038b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ mod signal_throttled; mod use_active_element; mod use_breakpoints; mod use_color_mode; +mod use_cookie; mod use_css_var; mod use_cycle_list; mod use_debounce_fn; @@ -78,6 +79,7 @@ pub use signal_throttled::*; pub use use_active_element::*; pub use use_breakpoints::*; pub use use_color_mode::*; +pub use use_cookie::*; pub use use_css_var::*; pub use use_cycle_list::*; pub use use_debounce_fn::*; diff --git a/src/use_cookie.rs b/src/use_cookie.rs new file mode 100644 index 0000000..d1529e0 --- /dev/null +++ b/src/use_cookie.rs @@ -0,0 +1,47 @@ +use cookie::Cookie; + +pub struct UseCookie { + pub cookie: Option>, +} + +pub fn use_cookie(cookie_name: &str) -> UseCookie { + let cookies; + #[cfg(feature = "ssr")] + { + use http::HeaderValue; + use leptos::expect_context; + + let headers; + #[cfg(feature = "actix")] + { + headers = expect_context::().headers().clone(); + } + #[cfg(feature = "axum")] + { + headers = expect_context::().headers; + } + cookies = headers + .get(http::header::COOKIE) + .cloned() + .unwrap_or_else(|| HeaderValue::from_static("")) + .to_str() + .unwrap_or_default() + .to_owned(); + } + #[cfg(not(feature = "ssr"))] + { + use wasm_bindgen::JsCast; + + let js_value: wasm_bindgen::JsValue = leptos::window().document().unwrap().into(); + let document: web_sys::HtmlDocument = js_value.unchecked_into(); + cookies = document.cookie().unwrap_or_default(); + } + + let cookie = Cookie::split_parse_encoded(cookies) + .into_iter() + .filter_map(|cookie| cookie.ok()) + .find(|cookie| cookie.name() == cookie_name) + .map(|cookie| cookie.into_owned()); + + UseCookie { cookie } +}