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
use gloo_utils::iter::UncheckedIter;
use js_sys::{Array, Map};
use std::fmt;
use wasm_bindgen::{JsCast, UnwrapThrowExt};
pub struct Headers {
raw: web_sys::Headers,
}
impl Default for Headers {
fn default() -> Self {
Self::new()
}
}
impl Headers {
pub fn new() -> Self {
Self {
raw: web_sys::Headers::new().unwrap_throw(),
}
}
pub fn from_raw(raw: web_sys::Headers) -> Self {
Self { raw }
}
pub fn into_raw(self) -> web_sys::Headers {
self.raw
}
pub fn append(&self, name: &str, value: &str) {
self.raw.append(name, value).unwrap_throw()
}
pub fn delete(&self, name: &str) {
self.raw.delete(name).unwrap_throw()
}
pub fn get(&self, name: &str) -> Option<String> {
self.raw.get(name).unwrap_throw()
}
pub fn has(&self, name: &str) -> bool {
self.raw.has(name).unwrap_throw()
}
pub fn set(&self, name: &str, value: &str) {
self.raw.set(name, value).unwrap_throw()
}
pub fn entries(&self) -> impl Iterator<Item = (String, String)> {
let fake_map: &Map = self.raw.unchecked_ref();
UncheckedIter::from(fake_map.entries()).map(|entry| {
let entry: Array = entry.unchecked_into();
let key = entry.get(0);
let value = entry.get(1);
(
key.as_string().unwrap_throw(),
value.as_string().unwrap_throw(),
)
})
}
pub fn keys(&self) -> impl Iterator<Item = String> {
let fake_map: &Map = self.raw.unchecked_ref();
UncheckedIter::from(fake_map.keys()).map(|key| key.as_string().unwrap_throw())
}
pub fn values(&self) -> impl Iterator<Item = String> {
let fake_map: &Map = self.raw.unchecked_ref();
UncheckedIter::from(fake_map.values()).map(|v| v.as_string().unwrap_throw())
}
}
impl fmt::Debug for Headers {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut dbg = f.debug_struct("Headers");
for (key, value) in self.entries() {
dbg.field(&key, &value);
}
dbg.finish()
}
}