1
1
//! The Request of the Fetch API.
2
2
3
- use super :: { FetchError , Method , Result } ;
3
+ use super :: { header , FetchError , Header , Headers , Method , Result } ;
4
4
use gloo_timers:: callback:: Timeout ;
5
5
use serde:: Serialize ;
6
- use std:: { borrow:: Cow , cell:: RefCell , collections :: HashMap , rc:: Rc } ;
6
+ use std:: { borrow:: Cow , cell:: RefCell , rc:: Rc } ;
7
7
use wasm_bindgen:: JsValue ;
8
8
9
9
/// Its methods configure the request, and handle the response. Many of them return the original
10
10
/// struct, and are intended to be used chained together.
11
11
#[ derive( Debug , Clone , Default ) ]
12
- pub struct Request {
13
- url : Cow < ' static , str > ,
14
- headers : Headers ,
12
+ pub struct Request < ' a > {
13
+ url : Cow < ' a , str > ,
14
+ headers : Headers < ' a > ,
15
15
method : Method ,
16
16
body : Option < JsValue > ,
17
17
cache : Option < web_sys:: RequestCache > ,
@@ -25,14 +25,14 @@ pub struct Request {
25
25
controller : RequestController ,
26
26
}
27
27
28
- impl Request {
28
+ impl < ' a > Request < ' a > {
29
29
/// Create new request based on the provided url.
30
30
///
31
31
/// To get a [`Response`](./struct.Response.html) you need to pass
32
32
/// `Request` to the [`fetch`](./fn.fetch.html) function.
33
33
///
34
34
/// [MDN reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
35
- pub fn new ( url : impl Into < Cow < ' static , str > > ) -> Self {
35
+ pub fn new ( url : impl Into < Cow < ' a , str > > ) -> Self {
36
36
Self {
37
37
url : url. into ( ) ,
38
38
..Self :: default ( )
@@ -43,14 +43,14 @@ impl Request {
43
43
#[ allow( clippy:: missing_const_for_fn) ]
44
44
/// Set headers for this request.
45
45
/// It will replace any existing headers.
46
- pub fn headers ( mut self , headers : Headers ) -> Self {
46
+ pub fn headers ( mut self , headers : Headers < ' a > ) -> Self {
47
47
self . headers = headers;
48
48
self
49
49
}
50
50
51
51
/// Set specific header.
52
- pub fn header ( mut self , name : impl Into < String > , value : impl Into < String > ) -> Self {
53
- self . headers . insert ( name . into ( ) , value . into ( ) ) ;
52
+ pub fn header ( mut self , header : Header < ' a > ) -> Self {
53
+ self . headers . set ( header ) ;
54
54
self
55
55
}
56
56
@@ -85,10 +85,8 @@ impl Request {
85
85
/// This method can fail if JSON serialization fail. It will then
86
86
/// return `FetchError::SerdeError`.
87
87
pub fn json < T : Serialize + ?Sized > ( mut self , data : & T ) -> Result < Self > {
88
- self . headers . insert (
89
- "Content-Type" . to_owned ( ) ,
90
- "application/json; charset=utf-8" . to_owned ( ) ,
91
- ) ;
88
+ self . headers
89
+ . set ( header:: content_type ( "application/json; charset=utf-8" ) ) ;
92
90
let body = serde_json:: to_string ( data) . map_err ( FetchError :: SerdeError ) ?;
93
91
self . body = Some ( body. into ( ) ) ;
94
92
Ok ( self )
@@ -97,10 +95,8 @@ impl Request {
97
95
/// Set request body to a provided string.
98
96
/// It will also set `Content-Type` header to `text/plain; charset=utf-8`.
99
97
pub fn text ( mut self , text : impl AsRef < str > ) -> Self {
100
- self . headers . insert (
101
- "Content-Type" . to_owned ( ) ,
102
- "text/plain; charset=utf-8" . to_owned ( ) ,
103
- ) ;
98
+ self . headers
99
+ . set ( header:: content_type ( "text/plain; charset=utf-8" ) ) ;
104
100
self . body = Some ( JsValue :: from ( text. as_ref ( ) ) ) ;
105
101
self
106
102
}
@@ -162,15 +158,15 @@ impl Request {
162
158
}
163
159
}
164
160
165
- impl From < Request > for web_sys:: Request {
161
+ impl From < Request < ' _ > > for web_sys:: Request {
166
162
fn from ( request : Request ) -> Self {
167
163
let mut init = web_sys:: RequestInit :: new ( ) ;
168
164
169
165
// headers
170
166
let headers = web_sys:: Headers :: new ( ) . expect ( "fetch: cannot create headers" ) ;
171
- for ( name , value ) in & request. headers {
167
+ for header in request. headers {
172
168
headers
173
- . append ( name . as_str ( ) , value . as_str ( ) )
169
+ . append ( & header . name , & header . value )
174
170
. expect ( "fetch: cannot create header" )
175
171
}
176
172
init. headers ( & headers) ;
@@ -242,10 +238,6 @@ impl From<Request> for web_sys::Request {
242
238
}
243
239
}
244
240
245
- // TODO cows?
246
- /// Request headers.
247
- pub type Headers = HashMap < String , String > ;
248
-
249
241
#[ allow( clippy:: module_name_repetitions) ]
250
242
#[ derive( Debug , Clone ) ]
251
243
/// It allows to abort request or disable request's timeout.
0 commit comments