mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-22 04:32:13 +00:00
examples(fetch): Tidy up the fetch example
This commit is contained in:
parent
42ea38187f
commit
723ed6e856
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "fetch"
|
name = "fetch"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Andrew Chin <achin@eminence32.net>"]
|
authors = ["The wasm-bindgen Developers"]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
@ -1,21 +1,23 @@
|
|||||||
extern crate wasm_bindgen;
|
|
||||||
extern crate js_sys;
|
|
||||||
extern crate web_sys;
|
|
||||||
extern crate wasm_bindgen_futures;
|
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
|
extern crate js_sys;
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
extern crate wasm_bindgen_futures;
|
||||||
|
extern crate web_sys;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
||||||
|
use futures::{future, Future};
|
||||||
|
use js_sys::Promise;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
use js_sys::Promise;
|
|
||||||
use web_sys::{Request, RequestInit, RequestMode, Response, Window};
|
|
||||||
use wasm_bindgen_futures::JsFuture;
|
|
||||||
use futures::{future, Future};
|
|
||||||
use wasm_bindgen_futures::future_to_promise;
|
use wasm_bindgen_futures::future_to_promise;
|
||||||
|
use wasm_bindgen_futures::JsFuture;
|
||||||
|
use web_sys::{Request, RequestInit, RequestMode, Response, Window};
|
||||||
|
|
||||||
// A struct to hold some data from the github Branch API.
|
/// A struct to hold some data from the github Branch API.
|
||||||
// Note how we don't have to define every member -- serde will ignore extra data when deserializing
|
///
|
||||||
|
/// Note how we don't have to define every member -- serde will ignore extra
|
||||||
|
/// data when deserializing
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Branch {
|
pub struct Branch {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@ -42,37 +44,38 @@ pub struct Signature {
|
|||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn run() -> Promise {
|
pub fn run() -> Promise {
|
||||||
let mut request_options = RequestInit::new();
|
let mut opts = RequestInit::new();
|
||||||
request_options.method("GET");
|
opts.method("GET");
|
||||||
request_options.mode(RequestMode::Cors);
|
opts.mode(RequestMode::Cors);
|
||||||
|
|
||||||
let req = Request::new_with_str_and_init("https://api.github.com/repos/rustwasm/wasm-bindgen/branches/master", &request_options).unwrap();
|
let request = Request::new_with_str_and_init(
|
||||||
|
"https://api.github.com/repos/rustwasm/wasm-bindgen/branches/master",
|
||||||
|
&opts,
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
// the RequestInit struct will eventually support setting headers, but that's missing right now
|
request.headers()
|
||||||
req.headers().set("Accept", "application/vnd.github.v3+json").unwrap();
|
.set("Accept", "application/vnd.github.v3+json")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let req_promise = Window::fetch_with_request(&req);
|
let request_promise = Window::fetch_with_request(&request);
|
||||||
|
|
||||||
let to_return = JsFuture::from(req_promise).and_then(|resp_value| {
|
let future = JsFuture::from(request_promise)
|
||||||
// resp_value is a Response object
|
.and_then(|resp_value| {
|
||||||
assert!(resp_value.is_instance_of::<Response>());
|
// `resp_value` is a `Response` object.
|
||||||
let resp: Response = resp_value.dyn_into().unwrap();
|
assert!(resp_value.is_instance_of::<Response>());
|
||||||
|
let resp: Response = resp_value.dyn_into().unwrap();
|
||||||
|
resp.json()
|
||||||
|
}).and_then(|json_value: Promise| {
|
||||||
|
// Convert this other `Promise` into a rust `Future`.
|
||||||
|
JsFuture::from(json_value)
|
||||||
|
}).and_then(|json| {
|
||||||
|
// Use serde to parse the JSON into a struct.
|
||||||
|
let branch_info: Branch = json.into_serde().unwrap();
|
||||||
|
|
||||||
resp.json()
|
// Send the `Branch` struct back to JS as an `Object`.
|
||||||
|
future::ok(JsValue::from_serde(&branch_info).unwrap())
|
||||||
|
});
|
||||||
|
|
||||||
|
// Convert this Rust `Future` back into a JS `Promise`.
|
||||||
}).and_then(|json_value: Promise| {
|
future_to_promise(future)
|
||||||
// convert this other promise into a rust Future
|
|
||||||
JsFuture::from(json_value)
|
|
||||||
}).and_then(|json| {
|
|
||||||
// Use serde to parse this into a struct
|
|
||||||
let branch_info: Branch = json.into_serde().unwrap();
|
|
||||||
|
|
||||||
// Send the Branch struct back to javascript as an object
|
|
||||||
future::ok(JsValue::from_serde(&branch_info).unwrap())
|
|
||||||
});
|
|
||||||
|
|
||||||
// Convert this rust future back into a javascript promise.
|
|
||||||
// Return it to javascript so that it can be driven to completion.
|
|
||||||
future_to_promise(to_return)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user