mod cache;
mod credentials;
mod mode;
mod redirect;
mod referrer_policy;
use web_sys::RequestInit;
pub use self::{
cache::Cache, credentials::Credentials, mode::Mode, redirect::Redirect,
referrer_policy::ReferrerPolicy,
};
#[derive(Debug, Clone, Default)]
pub struct FetchOptions {
pub cache: Option<Cache>,
pub credentials: Option<Credentials>,
pub integrity: Option<String>,
pub mode: Option<Mode>,
pub redirect: Option<Redirect>,
pub referrer: Option<String>,
pub referrer_policy: Option<ReferrerPolicy>,
}
impl FetchOptions {
pub fn new() -> Self {
Default::default()
}
pub fn cache(mut self, cache: Cache) -> Self {
self.cache = Some(cache);
self
}
pub fn credentials(mut self, credentials: Credentials) -> Self {
self.credentials = Some(credentials);
self
}
pub fn integrity(mut self, integrity: String) -> Self {
self.integrity = Some(integrity);
self
}
pub fn mode(mut self, mode: Mode) -> Self {
self.mode = Some(mode);
self
}
pub fn redirect(mut self, redirect: Redirect) -> Self {
self.redirect = Some(redirect);
self
}
pub fn referrer(mut self, referrer: String) -> Self {
self.referrer = Some(referrer);
self
}
pub fn referrer_policy(mut self, referrer_policy: ReferrerPolicy) -> Self {
self.referrer_policy = Some(referrer_policy);
self
}
}
impl From<FetchOptions> for RequestInit {
fn from(value: FetchOptions) -> Self {
let mut init = RequestInit::new();
if let Some(cache) = value.cache {
init.cache(cache.into());
}
if let Some(credentials) = value.credentials {
init.credentials(credentials.into());
}
if let Some(ref integrity) = value.integrity {
init.integrity(integrity);
}
if let Some(mode) = value.mode {
init.mode(mode.into());
}
if let Some(redirect) = value.redirect {
init.redirect(redirect.into());
}
if let Some(ref referrer) = value.referrer {
init.referrer(referrer);
}
if let Some(referrer_policy) = value.referrer_policy {
init.referrer_policy(referrer_policy.into());
}
init
}
}