Use URLSearchParams to handle client side query param logic

This commit is contained in:
Clemente 2023-01-20 18:11:49 -03:00
parent e4c9109278
commit 18eecd9606
2 changed files with 45 additions and 8 deletions

View file

@ -56,7 +56,7 @@ features = [
[features]
default = []
csr = ["leptos/csr"]
hydrate = ["leptos/hydrate", "dep:url"]
hydrate = ["leptos/hydrate"]
ssr = ["leptos/ssr", "dep:url", "dep:regex"]
stable = ["leptos/stable"]

View file

@ -1,5 +1,10 @@
use crate::ParamsMap;
use std::borrow::Cow;
#[cfg(not(feature = "ssr"))]
use js_sys::{try_iter, Array, JsString};
#[cfg(not(feature = "ssr"))]
use wasm_bindgen::JsCast;
#[cfg(not(feature = "ssr"))]
use wasm_bindgen::JsValue;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Url {
@ -25,11 +30,41 @@ pub fn escape(s: &str) -> String {
js_sys::encode_uri(s).as_string().unwrap()
}
#[cfg(not(feature = "ssr"))]
impl TryFrom<&str> for Url {
type Error = String;
fn try_from(url: &str) -> Result<Self, Self::Error> {
let url = url::Url::parse(&normalize_wasm_url(url)).map_err(|e| e.to_string())?;
let fake_host = String::from("http://leptos");
let url = web_sys::Url::new_with_base(url, &fake_host).map_js_error()?;
Ok(Self {
origin: url.origin(),
pathname: url.pathname(),
search: url.search(),
search_params: ParamsMap(
try_iter(&url.search_params())
.map_js_error()?
.ok_or("Failed to use URLSearchParams as an iterator".to_string())?
.map(|value| {
let array: Array = value.map_js_error()?.dyn_into().map_js_error()?;
Ok((
array.get(0).dyn_into::<JsString>().map_js_error()?.into(),
array.get(1).dyn_into::<JsString>().map_js_error()?.into(),
))
})
.collect::<Result<linear_map::LinearMap<String, String>, Self::Error>>()?,
),
hash: url.hash(),
})
}
}
#[cfg(feature = "ssr")]
impl TryFrom<&str> for Url {
type Error = String;
fn try_from(url: &str) -> Result<Self, Self::Error> {
let url = url::Url::parse(url).map_err(|e| e.to_string())?;
Ok(Self {
origin: url.origin().unicode_serialization(),
pathname: url.path().to_string(),
@ -44,12 +79,14 @@ impl TryFrom<&str> for Url {
}
}
#[cfg(feature = "ssr")]
fn normalize_wasm_url(url: &str) -> Cow<'_, str> {
Cow::Borrowed(url)
#[cfg(not(feature = "ssr"))]
trait MapJsError<T> {
fn map_js_error(self) -> Result<T, String>;
}
#[cfg(not(feature = "ssr"))]
fn normalize_wasm_url(url: &str) -> Cow<'_, str> {
Cow::Owned(format!("http://leptos{}", url))
impl<T> MapJsError<T> for Result<T, JsValue> {
fn map_js_error(self) -> Result<T, String> {
self.map_err(|e| e.as_string().unwrap_or_default())
}
}