1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::collections::HashMap;
use::std::hash::BuildHasher;
use futures::{future, Future};
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures;
use web_sys;
use serde::Serialize;
use serde_json;
pub enum Method {
Get,
Head,
Post,
Put,
Delete,
Connect,
Options,
Trace,
Patch
}
impl Method {
fn as_str(&self) -> &str {
match *self {
Method::Get => "GET",
Method::Head => "HEAD",
Method::Post => "POST",
Method::Put => "PUT",
Method::Delete => "DELETE",
Method::Connect => "CONNECT",
Method::Options => "OPTIONS",
Method::Trace => "TRACE",
Method::Patch => "PATCH",
}
}
}
#[derive(Clone)]
pub struct RequestOpts {
pub payload: Option<String>,
pub headers: HashMap<String, String>,
pub credentials: HashMap<String, String>,
pub mode: web_sys::RequestMode,
}
impl RequestOpts {
pub fn new() -> Self {
Self {
payload: None,
headers: HashMap::new(),
credentials: HashMap::new(),
mode: web_sys::RequestMode::Cors,
}
}
}
pub fn fetch(
method: Method,
url: &str,
request_opts: Option<RequestOpts>,
callback: Box<Fn(JsValue)>)
{
let mut opts = web_sys::RequestInit::new();
opts.method(method.as_str());
opts.mode(web_sys::RequestMode::Cors); if let Some(o) = request_opts.clone() {
if let Some(p) = o.payload {
opts.body(Some(&JsValue::from_str(&p)));
}
opts.mode(o.mode);
}
let request = web_sys::Request::new_with_str_and_init(url, &opts.clone())
.expect("Problem with request");
if let Some(o) = request_opts {
for (name, value) in &o.headers {
request.headers().set(&name, &value).unwrap();
}
}
let window = web_sys::window().expect("Can't find window");
let request_promise = window.fetch_with_request(&request);
let f = wasm_bindgen_futures::JsFuture::from(request_promise)
.and_then(|resp_value| {
let resp: web_sys::Response = resp_value.dyn_into()
.expect("Problem casting response as Reponse.");
resp.json()
})
.and_then(|json_value| {
wasm_bindgen_futures::JsFuture::from(json_value)
})
.and_then(move |v| {
callback(v);
future::ok(JsValue::null())
});
wasm_bindgen_futures::future_to_promise(f);
}
pub fn get(
url: &str,
request_opts: Option<RequestOpts>,
callback: Box<Fn(JsValue)>)
{
fetch(Method::Get, url, request_opts, callback)
}
pub fn post<S: Serialize>(
url: &str,
payload: S,
request_opts: Option<RequestOpts>,
callback: Box<Fn(JsValue)>)
{
let serialized_payload = serde_json::to_string(&payload).expect("Problem serializing payload");
let updated_opts = match request_opts {
Some(o) => RequestOpts{payload: Some(serialized_payload), ..o},
None => {
let mut opts = RequestOpts::new();
opts.payload = Some(serialized_payload);
opts
}
};
fetch(Method::Post, url, Some(updated_opts), callback)
}