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 24, 2021
1 parent c6eb2c4 commit e9ec241
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 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: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ if_wasm! {
mod wasm;
mod util;

pub use self::wasm::{Body, Client, ClientBuilder, Request, RequestBuilder, Response};
pub use self::wasm::{Body, Client, ClientBuilder, Request, RequestBuilder, RequestCredentials, Response};
#[cfg(feature = "multipart")]
pub use self::wasm::multipart;
}
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
2 changes: 1 addition & 1 deletion src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod response;

pub use self::body::Body;
pub use self::client::{Client, ClientBuilder};
pub use self::request::{Request, RequestBuilder};
pub use self::request::{Request, RequestBuilder, RequestCredentials};
pub use self::response::Response;

async fn promise<T>(promise: js_sys::Promise) -> Result<T, crate::error::BoxError>
Expand Down
20 changes: 20 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;
pub 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,22 @@ impl RequestBuilder {
self
}

/// Set fetch credentials
///
/// # WASM
///
/// This option is only effective with WebAssembly target.
///
/// The [request credentials][mdn] will be set.
///
/// [mdn]: https://developer.mozilla.org/zh-CN/docs/Web/API/Request/credentials
pub fn fetch_credentials(mut self, credentials: RequestCredentials) -> RequestBuilder {
if let Ok(ref mut req) = self.request {
req.credentials = credentials;
}
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 +351,7 @@ where
headers,
body: Some(body.into()),
cors: true,
credentials: RequestCredentials::SameOrigin,
})
}
}

0 comments on commit e9ec241

Please sign in to comment.