@@ -29,6 +29,23 @@ BaseClient createClient() {
29
29
return BrowserClient ();
30
30
}
31
31
32
+ /// Caching mode used by the [BrowserClient] .
33
+ ///
34
+ /// Sets the request cache value of the browser Fetch API.
35
+ /// [`Request.cache`] (https://developer.mozilla.org/en-US/docs/Web/API/Request/cache) property.
36
+ enum CacheMode {
37
+ defaultType ('default' ),
38
+ reload ('reload' ),
39
+ noStore ('no-store' ),
40
+ noCache ('no-cache' ),
41
+ forceCache ('force-cache' ),
42
+ onlyIfCached ('only-if-cached' );
43
+
44
+ final String cacheType;
45
+
46
+ const CacheMode (this .cacheType);
47
+ }
48
+
32
49
/// A `package:web` -based HTTP client that runs in the browser and is backed by
33
50
/// [`window.fetch`] (https://fetch.spec.whatwg.org/).
34
51
///
@@ -44,7 +61,7 @@ BaseClient createClient() {
44
61
class BrowserClient extends BaseClient {
45
62
final _abortController = AbortController ();
46
63
47
- final String ? _cacheMode;
64
+ final CacheMode _cacheMode;
48
65
49
66
/// Create a new browser-based HTTP Client.
50
67
///
@@ -53,12 +70,10 @@ class BrowserClient extends BaseClient {
53
70
///
54
71
/// For example:
55
72
/// ```dart
56
- /// final client = BrowserClient(cacheMode: ' reload' );
73
+ /// final client = BrowserClient(cacheMode: CacheMode. reload);
57
74
/// ```
58
- ///
59
- /// Defaults to `default` .
60
- /// https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
61
- BrowserClient ([String ? cacheMode]) : _cacheMode = cacheMode ?? 'default' ;
75
+ BrowserClient ({CacheMode cacheMode = CacheMode .defaultType})
76
+ : _cacheMode = cacheMode;
62
77
63
78
/// Whether to send credentials such as cookies or authorization headers for
64
79
/// cross-site requests.
@@ -84,7 +99,7 @@ class BrowserClient extends BaseClient {
84
99
RequestInit (
85
100
method: request.method,
86
101
body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null ,
87
- cache: _cacheMode! ,
102
+ cache: _cacheMode.cacheType ,
88
103
credentials: withCredentials ? 'include' : 'same-origin' ,
89
104
headers: {
90
105
if (request.contentLength case final contentLength? )
@@ -199,18 +214,4 @@ Stream<List<int>> _readBody(BaseRequest request, Response response) async* {
199
214
@JS ()
200
215
extension type _IterableHeaders ._(JSObject _) implements JSObject {
201
216
external void forEach (JSFunction fn);
202
- }
203
-
204
- /// Caching modes for any http request in a browser.
205
- enum CacheMode {
206
- defaultType ('default' ),
207
- reload ('reload' ),
208
- noStore ('no-store' ),
209
- noCache ('no-cache' ),
210
- forceCache ('force-cache' ),
211
- onlyIfCached ('only-if-cached' );
212
-
213
- final String cacheType;
214
-
215
- const CacheMode (this .cacheType);
216
- }
217
+ }
0 commit comments