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

supports wasm fetch credentials #1227

Merged
merged 3 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
})
}
}