supports wasm fetch credentials
This commit is contained in:
		
				
					committed by
					
						 Sean McArthur
						Sean McArthur
					
				
			
			
				
	
			
			
			
						parent
						
							81c0b6685a
						
					
				
				
					commit
					1614c5ea64
				
			| @@ -162,6 +162,7 @@ features = [ | |||||||
|     "Blob", |     "Blob", | ||||||
|     "BlobPropertyBag", |     "BlobPropertyBag", | ||||||
|     "ServiceWorkerGlobalScope", |     "ServiceWorkerGlobalScope", | ||||||
|  |     "RequestCredentials" | ||||||
| ] | ] | ||||||
|  |  | ||||||
| [target.'cfg(target_arch = "wasm32")'.dev-dependencies] | [target.'cfg(target_arch = "wasm32")'.dev-dependencies] | ||||||
|   | |||||||
| @@ -207,6 +207,8 @@ async fn fetch(req: Request) -> crate::Result<Response> { | |||||||
|         init.mode(web_sys::RequestMode::NoCors); |         init.mode(web_sys::RequestMode::NoCors); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     init.credentials(req.credentials); | ||||||
|  |  | ||||||
|     if let Some(body) = req.body() { |     if let Some(body) = req.body() { | ||||||
|         if !body.is_empty() { |         if !body.is_empty() { | ||||||
|             init.body(Some(&body.to_js_value()?.as_ref().as_ref())); |             init.body(Some(&body.to_js_value()?.as_ref().as_ref())); | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ use serde::Serialize; | |||||||
| use serde_json; | use serde_json; | ||||||
| use serde_urlencoded; | use serde_urlencoded; | ||||||
| use url::Url; | use url::Url; | ||||||
|  | use web_sys::RequestCredentials; | ||||||
|  |  | ||||||
| use super::{Body, Client, Response}; | use super::{Body, Client, Response}; | ||||||
| use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE}; | use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE}; | ||||||
| @@ -18,6 +19,7 @@ pub struct Request { | |||||||
|     headers: HeaderMap, |     headers: HeaderMap, | ||||||
|     body: Option<Body>, |     body: Option<Body>, | ||||||
|     pub(super) cors: bool, |     pub(super) cors: bool, | ||||||
|  |     pub(super) credentials: RequestCredentials, | ||||||
| } | } | ||||||
|  |  | ||||||
| /// A builder to construct the properties of a `Request`. | /// A builder to construct the properties of a `Request`. | ||||||
| @@ -36,6 +38,7 @@ impl Request { | |||||||
|             headers: HeaderMap::new(), |             headers: HeaderMap::new(), | ||||||
|             body: None, |             body: None, | ||||||
|             cors: true, |             cors: true, | ||||||
|  |             credentials: RequestCredentials::SameOrigin, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -254,6 +257,54 @@ impl RequestBuilder { | |||||||
|         self |         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 |     /// Build a `Request`, which can be inspected, modified and executed with | ||||||
|     /// `Client::execute()`. |     /// `Client::execute()`. | ||||||
|     pub fn build(self) -> crate::Result<Request> { |     pub fn build(self) -> crate::Result<Request> { | ||||||
| @@ -332,6 +383,7 @@ where | |||||||
|             headers, |             headers, | ||||||
|             body: Some(body.into()), |             body: Some(body.into()), | ||||||
|             cors: true, |             cors: true, | ||||||
|  |             credentials: RequestCredentials::SameOrigin, | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user