Add RequestBuilder::fetch_mode_no_cors()
This commit is contained in:
@@ -136,6 +136,7 @@ features = [
|
|||||||
"Headers",
|
"Headers",
|
||||||
"Request",
|
"Request",
|
||||||
"RequestInit",
|
"RequestInit",
|
||||||
|
"RequestMode",
|
||||||
"Response",
|
"Response",
|
||||||
"Window",
|
"Window",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -353,6 +353,19 @@ impl RequestBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Disable CORS on fetching the request.
|
||||||
|
///
|
||||||
|
/// # WASM
|
||||||
|
///
|
||||||
|
/// This option is only effective with WebAssembly target.
|
||||||
|
///
|
||||||
|
/// The [request mode][mdn] will be set to 'no-cors'.
|
||||||
|
///
|
||||||
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
|
||||||
|
pub fn fetch_mode_no_cors(self) -> RequestBuilder {
|
||||||
|
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> {
|
||||||
|
|||||||
@@ -120,6 +120,11 @@ async fn fetch(req: Request) -> crate::Result<Response> {
|
|||||||
}
|
}
|
||||||
init.headers(&js_headers.into());
|
init.headers(&js_headers.into());
|
||||||
|
|
||||||
|
// When req.cors is true, do nothing because the default mode is 'cors'
|
||||||
|
if !req.cors {
|
||||||
|
init.mode(web_sys::RequestMode::NoCors);
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(body) = req.body() {
|
if let Some(body) = req.body() {
|
||||||
let body_bytes: &[u8] = body.bytes();
|
let body_bytes: &[u8] = body.bytes();
|
||||||
let body_array: Uint8Array = body_bytes.into();
|
let body_array: Uint8Array = body_bytes.into();
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ pub struct Request {
|
|||||||
url: Url,
|
url: Url,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
body: Option<Body>,
|
body: Option<Body>,
|
||||||
|
pub(super) cors: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A builder to construct the properties of a `Request`.
|
/// A builder to construct the properties of a `Request`.
|
||||||
@@ -28,6 +29,7 @@ impl Request {
|
|||||||
url,
|
url,
|
||||||
headers: HeaderMap::new(),
|
headers: HeaderMap::new(),
|
||||||
body: None,
|
body: None,
|
||||||
|
cors: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,6 +121,22 @@ impl RequestBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Disable CORS on fetching the request.
|
||||||
|
///
|
||||||
|
/// # WASM
|
||||||
|
///
|
||||||
|
/// This option is only effective with WebAssembly target.
|
||||||
|
///
|
||||||
|
/// The [request mode][mdn] will be set to 'no-cors'.
|
||||||
|
///
|
||||||
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
|
||||||
|
pub fn fetch_mode_no_cors(mut self) -> RequestBuilder {
|
||||||
|
if let Ok(ref mut req) = self.request {
|
||||||
|
req.cors = false;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Constructs the Request and sends it to the target URL, returning a
|
/// Constructs the Request and sends it to the target URL, returning a
|
||||||
/// future Response.
|
/// future Response.
|
||||||
///
|
///
|
||||||
|
|||||||
Reference in New Issue
Block a user