From 7e5f220c82cda32ee4b69b469dee9772b51177f1 Mon Sep 17 00:00:00 2001 From: Afonso Lage Date: Tue, 13 Feb 2024 06:53:49 -0300 Subject: [PATCH 1/8] Added mode property to requests on wasm target --- ehttp/src/types.rs | 34 +++++++++++++++++++++++++++++++++- ehttp/src/web.rs | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index 729a851..db412c0 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -93,8 +93,31 @@ impl<'h> IntoIterator for &'h Headers { // ---------------------------------------------------------------------------- +/// 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, +} + +#[cfg(target_arch = "wasm32")] +impl From 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)] pub struct Request { /// "GET", "POST", … pub method: String, @@ -107,6 +130,10 @@ pub struct Request { /// ("Accept", "*/*"), … pub headers: Headers, + + /// Request mode used on fetch. Only available on wasm builds + #[cfg(target_arch = "wasm32")] + pub mode: Mode, } impl Request { @@ -118,6 +145,7 @@ impl Request { url: url.to_string(), body: vec![], headers: Headers::new(&[("Accept", "*/*")]), + ..Default::default() } } @@ -129,6 +157,7 @@ impl Request { url: url.to_string(), body: vec![], headers: Headers::new(&[("Accept", "*/*")]), + ..Default::default() } } @@ -143,6 +172,7 @@ impl Request { ("Accept", "*/*"), ("Content-Type", "text/plain; charset=utf-8"), ]), + ..Default::default() } } @@ -176,6 +206,7 @@ impl Request { url: url.to_string(), body: data, headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", content_type.as_str())]), + ..Default::default() } } @@ -191,6 +222,7 @@ impl Request { url: url.to_string(), body: serde_json::to_string(body)?.into_bytes(), headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", "application/json")]), + ..Default::default() }) } } diff --git a/ehttp/src/web.rs b/ehttp/src/web.rs index 94871dd..cb7d4c3 100644 --- a/ehttp/src/web.rs +++ b/ehttp/src/web.rs @@ -42,7 +42,7 @@ pub(crate) fn string_from_fetch_error(value: JsValue) -> String { pub(crate) async fn fetch_base(request: &Request) -> Result { 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; From 9d0c57e0ecf34b85e806c771d44a9b12c3278ff7 Mon Sep 17 00:00:00 2001 From: Afonso Lage Date: Tue, 13 Feb 2024 07:03:38 -0300 Subject: [PATCH 2/8] Added export to request Mode on wasm --- ehttp/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ehttp/src/lib.rs b/ehttp/src/lib.rs index 96a5796..550b96e 100644 --- a/ehttp/src/lib.rs +++ b/ehttp/src/lib.rs @@ -75,6 +75,9 @@ pub async fn fetch_async(request: Request) -> Result { 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"))] From 69eab649cf2c2ef49a43f786bc92adb663dac412 Mon Sep 17 00:00:00 2001 From: Afonso Lage Date: Tue, 13 Feb 2024 08:42:02 -0300 Subject: [PATCH 3/8] Fixed clippy warn --- ehttp/src/types.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index db412c0..423ad29 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -145,7 +145,8 @@ impl Request { url: url.to_string(), body: vec![], headers: Headers::new(&[("Accept", "*/*")]), - ..Default::default() + #[cfg(target_arch = "wasm32")] + mode: Mode::default() } } @@ -157,7 +158,8 @@ impl Request { url: url.to_string(), body: vec![], headers: Headers::new(&[("Accept", "*/*")]), - ..Default::default() + #[cfg(target_arch = "wasm32")] + mode: Mode::default() } } @@ -172,7 +174,8 @@ impl Request { ("Accept", "*/*"), ("Content-Type", "text/plain; charset=utf-8"), ]), - ..Default::default() + #[cfg(target_arch = "wasm32")] + mode: Mode::default() } } @@ -206,7 +209,8 @@ impl Request { url: url.to_string(), body: data, headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", content_type.as_str())]), - ..Default::default() + #[cfg(target_arch = "wasm32")] + mode: Mode::default() } } @@ -222,7 +226,8 @@ impl Request { url: url.to_string(), body: serde_json::to_string(body)?.into_bytes(), headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", "application/json")]), - ..Default::default() + #[cfg(target_arch = "wasm32")] + mode: Mode::default() }) } } From 05f6760ac76b2eff4c8006ed3444c1a75e72d57f Mon Sep 17 00:00:00 2001 From: Afonso Lage Date: Tue, 13 Feb 2024 08:47:36 -0300 Subject: [PATCH 4/8] Added doc comments on Request Mode --- ehttp/src/types.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index 423ad29..a311743 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -94,13 +94,20 @@ impl<'h> IntoIterator for &'h Headers { // ---------------------------------------------------------------------------- /// Determine if cross-origin requests lead to valid responses. +/// Based on [Mozilla Docs](https://developer.mozilla.org/en-US/docs/Web/API/Request/mode) #[cfg(target_arch = "wasm32")] #[derive(Default, Clone, Copy, Debug)] pub enum Mode { + /// If a request is made to another origin with this mode set, the result is an error. SameOrigin = 0, + /// The request will not include the Origin header in a request. + /// The server's response will be opaque, meaning that JavaScript code cannot access its contents NoCors = 1, + /// Includes an Origin header in the request and expects the server to respond with an + /// "Access-Control-Allow-Origin" header that indicates whether the request is allowed. #[default] Cors = 2, + /// A mode for supporting navigation Navigate = 3, } From d34971305b06147e322955149cbe574c64db547c Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 13 Feb 2024 12:49:07 +0100 Subject: [PATCH 5/8] Add some vertical spacing --- ehttp/src/types.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index a311743..f4139b4 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -100,13 +100,16 @@ impl<'h> IntoIterator for &'h Headers { pub enum Mode { /// If a request is made to another origin with this mode set, the result is an error. SameOrigin = 0, + /// The request will not include the Origin header in a request. /// The server's response will be opaque, meaning that JavaScript code cannot access its contents NoCors = 1, + /// Includes an Origin header in the request and expects the server to respond with an /// "Access-Control-Allow-Origin" header that indicates whether the request is allowed. #[default] Cors = 2, + /// A mode for supporting navigation Navigate = 3, } From b0e583edaafe76c3a00e3a1873d9f0ee8d3bb474 Mon Sep 17 00:00:00 2001 From: Afonso Lage Date: Tue, 13 Feb 2024 08:50:50 -0300 Subject: [PATCH 6/8] Formatted link --- ehttp/src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index a311743..aa16ae6 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -94,7 +94,7 @@ impl<'h> IntoIterator for &'h Headers { // ---------------------------------------------------------------------------- /// Determine if cross-origin requests lead to valid responses. -/// Based on [Mozilla Docs](https://developer.mozilla.org/en-US/docs/Web/API/Request/mode) +/// Based on #[cfg(target_arch = "wasm32")] #[derive(Default, Clone, Copy, Debug)] pub enum Mode { From be9e6ef5174a013b507dcdc64863b8381c369a69 Mon Sep 17 00:00:00 2001 From: Afonso Lage Date: Tue, 13 Feb 2024 08:52:29 -0300 Subject: [PATCH 7/8] Removed default from Request type --- ehttp/src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index 809e5dd..5b83a76 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -127,7 +127,7 @@ impl From for web_sys::RequestMode { } /// A simple HTTP request. -#[derive(Default, Clone, Debug)] +#[derive(Clone, Debug)] pub struct Request { /// "GET", "POST", … pub method: String, From 4b5f8e301544134ff1f389c93703ba7d2004f328 Mon Sep 17 00:00:00 2001 From: Afonso Lage Date: Tue, 13 Feb 2024 09:03:53 -0300 Subject: [PATCH 8/8] Fix formatting --- ehttp/src/types.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index 5b83a76..ed3febd 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -100,16 +100,16 @@ impl<'h> IntoIterator for &'h Headers { pub enum Mode { /// If a request is made to another origin with this mode set, the result is an error. SameOrigin = 0, - - /// The request will not include the Origin header in a request. + + /// The request will not include the Origin header in a request. /// The server's response will be opaque, meaning that JavaScript code cannot access its contents NoCors = 1, - - /// Includes an Origin header in the request and expects the server to respond with an + + /// Includes an Origin header in the request and expects the server to respond with an /// "Access-Control-Allow-Origin" header that indicates whether the request is allowed. #[default] Cors = 2, - + /// A mode for supporting navigation Navigate = 3, } @@ -156,7 +156,7 @@ impl Request { body: vec![], headers: Headers::new(&[("Accept", "*/*")]), #[cfg(target_arch = "wasm32")] - mode: Mode::default() + mode: Mode::default(), } } @@ -169,7 +169,7 @@ impl Request { body: vec![], headers: Headers::new(&[("Accept", "*/*")]), #[cfg(target_arch = "wasm32")] - mode: Mode::default() + mode: Mode::default(), } } @@ -185,7 +185,7 @@ impl Request { ("Content-Type", "text/plain; charset=utf-8"), ]), #[cfg(target_arch = "wasm32")] - mode: Mode::default() + mode: Mode::default(), } } @@ -220,7 +220,7 @@ impl Request { body: data, headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", content_type.as_str())]), #[cfg(target_arch = "wasm32")] - mode: Mode::default() + mode: Mode::default(), } } @@ -237,7 +237,7 @@ impl Request { body: serde_json::to_string(body)?.into_bytes(), headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", "application/json")]), #[cfg(target_arch = "wasm32")] - mode: Mode::default() + mode: Mode::default(), }) } }