@@ -8,7 +8,7 @@ use serde::Serialize;
8
8
#[ cfg( feature = "json" ) ]
9
9
use serde_json;
10
10
use url:: Url ;
11
- use web_sys:: RequestCredentials ;
11
+ use web_sys:: { RequestCache , RequestCredentials } ;
12
12
13
13
use super :: { Body , Client , Response } ;
14
14
use crate :: header:: { HeaderMap , HeaderName , HeaderValue , CONTENT_TYPE } ;
@@ -22,6 +22,7 @@ pub struct Request {
22
22
timeout : Option < Duration > ,
23
23
pub ( super ) cors : bool ,
24
24
pub ( super ) credentials : Option < RequestCredentials > ,
25
+ pub ( super ) cache : Option < RequestCache > ,
25
26
}
26
27
27
28
/// A builder to construct the properties of a `Request`.
@@ -42,6 +43,7 @@ impl Request {
42
43
timeout : None ,
43
44
cors : true ,
44
45
credentials : None ,
46
+ cache : None ,
45
47
}
46
48
}
47
49
@@ -122,6 +124,7 @@ impl Request {
122
124
timeout : self . timeout ,
123
125
cors : self . cors ,
124
126
credentials : self . credentials ,
127
+ cache : self . cache ,
125
128
} )
126
129
}
127
130
}
@@ -375,6 +378,102 @@ impl RequestBuilder {
375
378
self
376
379
}
377
380
381
+ /// Set fetch cache mode to 'default'.
382
+ ///
383
+ /// # WASM
384
+ ///
385
+ /// This option is only effective with WebAssembly target.
386
+ ///
387
+ /// The [request cache][mdn] will be set to 'default'.
388
+ ///
389
+ /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
390
+ pub fn fetch_cache_default ( mut self ) -> RequestBuilder {
391
+ if let Ok ( ref mut req) = self . request {
392
+ req. cache = Some ( RequestCache :: Default ) ;
393
+ }
394
+ self
395
+ }
396
+
397
+ /// Set fetch cache mode to 'no-store'.
398
+ ///
399
+ /// # WASM
400
+ ///
401
+ /// This option is only effective with WebAssembly target.
402
+ ///
403
+ /// The [request cache][mdn] will be set to 'no-store'.
404
+ ///
405
+ /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
406
+ pub fn fetch_cache_no_store ( mut self ) -> RequestBuilder {
407
+ if let Ok ( ref mut req) = self . request {
408
+ req. cache = Some ( RequestCache :: NoStore ) ;
409
+ }
410
+ self
411
+ }
412
+
413
+ /// Set fetch cache mode to 'reload'.
414
+ ///
415
+ /// # WASM
416
+ ///
417
+ /// This option is only effective with WebAssembly target.
418
+ ///
419
+ /// The [request cache][mdn] will be set to 'reload'.
420
+ ///
421
+ /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
422
+ pub fn fetch_cache_reload ( mut self ) -> RequestBuilder {
423
+ if let Ok ( ref mut req) = self . request {
424
+ req. cache = Some ( RequestCache :: Reload ) ;
425
+ }
426
+ self
427
+ }
428
+
429
+ /// Set fetch cache mode to 'no-cache'.
430
+ ///
431
+ /// # WASM
432
+ ///
433
+ /// This option is only effective with WebAssembly target.
434
+ ///
435
+ /// The [request cache][mdn] will be set to 'no-cache'.
436
+ ///
437
+ /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
438
+ pub fn fetch_cache_no_cache ( mut self ) -> RequestBuilder {
439
+ if let Ok ( ref mut req) = self . request {
440
+ req. cache = Some ( RequestCache :: NoCache ) ;
441
+ }
442
+ self
443
+ }
444
+
445
+ /// Set fetch cache mode to 'force-cache'.
446
+ ///
447
+ /// # WASM
448
+ ///
449
+ /// This option is only effective with WebAssembly target.
450
+ ///
451
+ /// The [request cache][mdn] will be set to 'force-cache'.
452
+ ///
453
+ /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
454
+ pub fn fetch_cache_force_cache ( mut self ) -> RequestBuilder {
455
+ if let Ok ( ref mut req) = self . request {
456
+ req. cache = Some ( RequestCache :: ForceCache ) ;
457
+ }
458
+ self
459
+ }
460
+
461
+ /// Set fetch cache mode to 'only-if-cached'.
462
+ ///
463
+ /// # WASM
464
+ ///
465
+ /// This option is only effective with WebAssembly target.
466
+ ///
467
+ /// The [request cache][mdn] will be set to 'only-if-cached'.
468
+ ///
469
+ /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
470
+ pub fn fetch_cache_only_if_cached ( mut self ) -> RequestBuilder {
471
+ if let Ok ( ref mut req) = self . request {
472
+ req. cache = Some ( RequestCache :: OnlyIfCached ) ;
473
+ }
474
+ self
475
+ }
476
+
378
477
/// Build a `Request`, which can be inspected, modified and executed with
379
478
/// `Client::execute()`.
380
479
pub fn build ( self ) -> crate :: Result < Request > {
@@ -493,6 +592,7 @@ where
493
592
timeout : None ,
494
593
cors : true ,
495
594
credentials : None ,
595
+ cache : None ,
496
596
} )
497
597
}
498
598
}
0 commit comments