Skip to content

Commit

Permalink
supports wasm fetch credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
xushaodong committed Mar 28, 2021
1 parent c6eb2c4 commit fd5ab44
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ features = [
"Blob",
"BlobPropertyBag",
"ServiceWorkerGlobalScope",
"RequestCredentials"
]

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions src/wasm/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ async fn fetch(req: Request) -> crate::Result<Response> {
init.mode(web_sys::RequestMode::NoCors);
}

init.credentials(req.credentials);

if let Some(body) = req.body() {
if !body.is_empty() {
init.body(Some(&body.to_js_value()?.as_ref().as_ref()));
Expand Down
52 changes: 52 additions & 0 deletions src/wasm/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::Serialize;
use serde_json;
use serde_urlencoded;
use url::Url;
use web_sys::RequestCredentials;

use super::{Body, Client, Response};
use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE};
Expand All @@ -18,6 +19,7 @@ pub struct Request {
headers: HeaderMap,
body: Option<Body>,
pub(super) cors: bool,
pub(super) credentials: RequestCredentials,
}

/// A builder to construct the properties of a `Request`.
Expand All @@ -36,6 +38,7 @@ impl Request {
headers: HeaderMap::new(),
body: None,
cors: true,
credentials: RequestCredentials::SameOrigin,
}
}

Expand Down Expand Up @@ -254,6 +257,54 @@ impl RequestBuilder {
self
}

/// Set fetch credentials to 'same-origin'
///
/// # WASM
///
/// This option is only effective with WebAssembly target.
///
/// The [request credentials][mdn] will be set to 'same-origin'.
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
pub fn fetch_credentials_same_origin(mut self) -> RequestBuilder {
if let Ok(ref mut req) = self.request {
req.credentials = RequestCredentials::SameOrigin;
}
self
}

/// Set fetch credentials to 'include'
///
/// # WASM
///
/// This option is only effective with WebAssembly target.
///
/// The [request credentials][mdn] will be set to 'include'.
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
pub fn fetch_credentials_include(mut self) -> RequestBuilder {
if let Ok(ref mut req) = self.request {
req.credentials = RequestCredentials::Include;
}
self
}

/// Set fetch credentials to 'omit'
///
/// # WASM
///
/// This option is only effective with WebAssembly target.
///
/// The [request credentials][mdn] will be set to 'omit'.
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
pub fn fetch_credentials_omit(mut self) -> RequestBuilder {
if let Ok(ref mut req) = self.request {
req.credentials = RequestCredentials::Omit;
}
self
}

/// Build a `Request`, which can be inspected, modified and executed with
/// `Client::execute()`.
pub fn build(self) -> crate::Result<Request> {
Expand Down Expand Up @@ -332,6 +383,7 @@ where
headers,
body: Some(body.into()),
cors: true,
credentials: RequestCredentials::SameOrigin,
})
}
}

0 comments on commit fd5ab44

Please sign in to comment.