diff --git a/Cargo.toml b/Cargo.toml index edf08ba..37108b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,10 +31,18 @@ tokenizers = { version = "0.15.0", features = ["unstable_wasm"], default-feature serde = {version = "1.0.193", features = ["derive"]} serde_json = "1.0.108" js-sys = "0.3.64" +wasm-bindgen-futures = "0.4.39" +anyhow = "1.0" [dependencies.web-sys] features = [ - 'console' + 'console', + 'Headers', + 'Request', + 'RequestInit', + 'RequestMode', + 'Response', + 'Window', ] version = "0.3.64" diff --git a/tests/web.rs b/tests/web.rs index de5c1da..7a2041e 100644 --- a/tests/web.rs +++ b/tests/web.rs @@ -3,11 +3,43 @@ #![cfg(target_arch = "wasm32")] extern crate wasm_bindgen_test; +use std::println; + +use wasm_bindgen::{prelude::*, JsValue}; +use wasm_bindgen_futures::JsFuture; use wasm_bindgen_test::*; +use web_sys::console; +use web_sys::{Request, RequestInit, RequestMode, Response}; wasm_bindgen_test_configure!(run_in_browser); #[wasm_bindgen_test] -fn pass() { +async fn pass() -> Result<(), JsValue> { + let mut opts = RequestInit::new(); + opts.method("GET"); + opts.mode(RequestMode::Cors); + + let url = "https://api.github.com/repos/text-yoga/transformers-wasm/branches/main"; + + let request = Request::new_with_str_and_init(&url, &opts)?; + + request + .headers() + .set("Accept", "application/vnd.github.v3+json")?; + + let window = web_sys::window().unwrap(); + let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?; + + // `resp_value` is a `Response` object. + assert!(resp_value.is_instance_of::()); + let resp: Response = resp_value.dyn_into().unwrap(); + + // Convert this other `Promise` into a rust `Future`. + let json = JsFuture::from(resp.json()?).await?; + + let json_str = js_sys::JSON::stringify(&json)?; + + console::log_2(&"Logging arbitrary values looks like".into(), &json_str); assert_eq!(1 + 1, 2); + Ok(()) }