Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CORS Mode property to Request on web #52

Merged
merged 9 commits into from
Feb 13, 2024
3 changes: 3 additions & 0 deletions ehttp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ pub async fn fetch_async(request: Request) -> Result<Response> {
mod types;
pub use types::{Error, Headers, PartialResponse, Request, Response, Result};

#[cfg(target_arch = "wasm32")]
pub use types::Mode;

#[cfg(not(target_arch = "wasm32"))]
mod native;
#[cfg(not(target_arch = "wasm32"))]
Expand Down
34 changes: 33 additions & 1 deletion ehttp/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,31 @@

// ----------------------------------------------------------------------------

/// Determine if cross-origin requests lead to valid responses.
#[cfg(target_arch = "wasm32")]
#[derive(Default, Clone, Copy, Debug)]
pub enum Mode {
SameOrigin = 0,
NoCors = 1,
#[default]
Cors = 2,
Navigate = 3,
afonsolage marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(target_arch = "wasm32")]
impl From<Mode> for web_sys::RequestMode {
fn from(mode: Mode) -> Self {
match mode {
Mode::SameOrigin => web_sys::RequestMode::SameOrigin,
Mode::NoCors => web_sys::RequestMode::NoCors,
Mode::Cors => web_sys::RequestMode::Cors,
Mode::Navigate => web_sys::RequestMode::Navigate,
}
}
}

/// A simple HTTP request.
#[derive(Clone, Debug)]
#[derive(Default, Clone, Debug)]
afonsolage marked this conversation as resolved.
Show resolved Hide resolved
pub struct Request {
/// "GET", "POST", …
pub method: String,
Expand All @@ -107,6 +130,10 @@

/// ("Accept", "*/*"), …
pub headers: Headers,

/// Request mode used on fetch. Only available on wasm builds
#[cfg(target_arch = "wasm32")]
pub mode: Mode,
}

impl Request {
Expand All @@ -118,6 +145,7 @@
url: url.to_string(),
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
..Default::default()

Check failure on line 148 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

struct update has no effect, all the fields in the struct have already been specified
}
}

Expand All @@ -129,6 +157,7 @@
url: url.to_string(),
body: vec![],
headers: Headers::new(&[("Accept", "*/*")]),
..Default::default()

Check failure on line 160 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

struct update has no effect, all the fields in the struct have already been specified
}
}

Expand All @@ -143,6 +172,7 @@
("Accept", "*/*"),
("Content-Type", "text/plain; charset=utf-8"),
]),
..Default::default()

Check failure on line 175 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

struct update has no effect, all the fields in the struct have already been specified
}
}

Expand Down Expand Up @@ -176,6 +206,7 @@
url: url.to_string(),
body: data,
headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", content_type.as_str())]),
..Default::default()

Check failure on line 209 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

struct update has no effect, all the fields in the struct have already been specified
}
}

Expand All @@ -191,6 +222,7 @@
url: url.to_string(),
body: serde_json::to_string(body)?.into_bytes(),
headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", "application/json")]),
..Default::default()

Check failure on line 225 in ehttp/src/types.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

struct update has no effect, all the fields in the struct have already been specified
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion ehttp/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) fn string_from_fetch_error(value: JsValue) -> String {
pub(crate) async fn fetch_base(request: &Request) -> Result<web_sys::Response, JsValue> {
let mut opts = web_sys::RequestInit::new();
opts.method(&request.method);
opts.mode(web_sys::RequestMode::Cors);
opts.mode(request.mode.into());

if !request.body.is_empty() {
let body_bytes: &[u8] = &request.body;
Expand Down
Loading