use impulse_utils::prelude::*;
#[cfg(any(feature = "csr", feature = "hydrate"))]
pub fn get_host() -> CResult<String> {
let server_host = web_sys::window()
.ok_or(ClientError::from_str("Can't get browser's window parameters."))?
.document()
.ok_or(ClientError::from_str("Can't get window's document."))?
.location()
.ok_or(ClientError::from_str("Can't get document's location."))?
.host()
.map_err(|e| ClientError::from_str(format!("Can't get host: {e:?}")))?
.to_string();
Ok(server_host)
}
#[cfg(any(feature = "csr", feature = "hydrate"))]
pub fn get_protocol() -> CResult<String> {
let server_proto = web_sys::window()
.ok_or(ClientError::from_str("Can't get browser's window parameters."))?
.document()
.ok_or(ClientError::from_str("Can't get window's document."))?
.location()
.ok_or(ClientError::from_str("Can't get document's location."))?
.protocol()
.map_err(|e| ClientError::from_str(format!("Can't get protocol: {e:?}")))?
.to_string();
Ok(server_proto)
}
#[cfg(any(feature = "csr", feature = "hydrate"))]
pub fn get_path() -> CResult<String> {
let path = web_sys::window()
.ok_or(ClientError::from_str("Can't get browser's window parameters."))?
.document()
.ok_or(ClientError::from_str("Can't get window's document."))?
.location()
.ok_or(ClientError::from_str("Can't get document's location."))?
.pathname()
.map_err(|e| ClientError::from_str(format!("Can't get pathname: {e:?}")))?
.to_string();
Ok(path)
}
#[cfg(any(feature = "csr", feature = "hydrate"))]
pub fn redirect(url: impl AsRef<str>) -> CResult<()> {
web_sys::window()
.ok_or(ClientError::from_str("Can't get browser's window parameters."))?
.document()
.ok_or(ClientError::from_str("Can't get window's document."))?
.location()
.ok_or(ClientError::from_str("Can't get document's location."))?
.set_href(url.as_ref())
.map_err(|e| ClientError::from_str(format!("Can't redirect: {e:?}")))
}
#[cfg(any(feature = "csr", feature = "hydrate"))]
pub fn endpoint(api_uri: impl AsRef<str>) -> String {
format!(
"{}//{}{}",
get_protocol().unwrap(),
get_host().unwrap(),
api_uri.as_ref()
)
}
#[cfg(feature = "ssr")]
pub fn get_host() -> CResult<String> {
use leptos::context::use_context;
let ctx = use_context::<crate::ssr::RequestUrlCtx>()
.ok_or(ClientError::from_str("RequestUrlCtx not provided in SSR context"))?;
Ok(ctx.host)
}
#[cfg(feature = "ssr")]
pub fn get_protocol() -> CResult<String> {
use leptos::context::use_context;
let ctx = use_context::<crate::ssr::RequestUrlCtx>()
.ok_or(ClientError::from_str("RequestUrlCtx not provided in SSR context"))?;
Ok(format!("{}:", ctx.scheme))
}
#[cfg(feature = "ssr")]
pub fn get_path() -> CResult<String> {
use leptos::context::use_context;
let ctx = use_context::<crate::ssr::RequestUrlCtx>()
.ok_or(ClientError::from_str("RequestUrlCtx not provided in SSR context"))?;
Ok(ctx.path)
}
#[cfg(feature = "ssr")]
pub fn redirect(url: impl AsRef<str>) -> CResult<()> {
use leptos::context::use_context;
let opts = use_context::<crate::ssr::LeptosResponseOptions>().ok_or(ClientError::from_str(
"LeptosResponseOptions not provided in SSR context",
))?;
opts.set_redirect(url.as_ref().to_string());
Ok(())
}
#[cfg(feature = "ssr")]
pub fn endpoint(api_uri: impl AsRef<str>) -> String {
format!(
"{}//{}{}",
get_protocol().unwrap_or_else(|_| "http:".to_string()),
get_host().unwrap_or_default(),
api_uri.as_ref()
)
}