From ecc863fdacc94e3f2ec1c6cccc42e730063b6b08 Mon Sep 17 00:00:00 2001 From: SnejUgal Date: Fri, 7 Aug 2020 05:09:31 +0700 Subject: [PATCH] Do not assume that `window` exists in WASM (#990) --- src/wasm/client.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/wasm/client.rs b/src/wasm/client.rs index 4f6ea1c..47059dd 100644 --- a/src/wasm/client.rs +++ b/src/wasm/client.rs @@ -1,11 +1,18 @@ use http::Method; use std::future::Future; -use wasm_bindgen::UnwrapThrowExt as _; +use wasm_bindgen::prelude::{wasm_bindgen, UnwrapThrowExt as _}; +use js_sys::Promise; use url::Url; use super::{Request, RequestBuilder, Response}; use crate::IntoUrl; +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_name = fetch)] + fn fetch_with_request(input: &web_sys::Request) -> Promise; +} + /// dox #[derive(Clone, Debug)] pub struct Client(()); @@ -159,9 +166,7 @@ async fn fetch(req: Request) -> crate::Result { .map_err(crate::error::builder)?; // Await the fetch() promise - let p = web_sys::window() - .expect("window should exist") - .fetch_with_request(&js_req); + let p = fetch_with_request(&js_req); let js_resp = super::promise::(p) .await .map_err(crate::error::request)?;