@@ -44,17 +44,21 @@ BaseClient createClient() {
44
44
class BrowserClient extends BaseClient {
45
45
final _abortController = AbortController ();
46
46
47
+ final String ? _cacheMode;
48
+
47
49
/// Create a new browser-based HTTP Client.
48
50
///
49
51
/// If [cacheMode] is provided then it can be used to cache the request
50
52
/// in the browser.
51
53
///
52
54
/// For example:
53
55
/// ```dart
54
- /// const mode = 'reload';
55
- /// final client = BrowserClient(cacheMode: mode);
56
+ /// final client = BrowserClient(cacheMode: 'reload');
56
57
/// ```
57
- BrowserClient ({this .cacheMode = 'default' });
58
+ ///
59
+ /// Defaults to `default` .
60
+ /// https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
61
+ BrowserClient ([String ? cacheMode]) : _cacheMode = cacheMode ?? "default" ;
58
62
59
63
/// Whether to send credentials such as cookies or authorization headers for
60
64
/// cross-site requests.
@@ -64,11 +68,6 @@ class BrowserClient extends BaseClient {
64
68
65
69
bool _isClosed = false ;
66
70
67
- /// Use different caching mode for a HTTP request.
68
- /// Defaults to `default` .
69
- // https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
70
- String cacheMode;
71
-
72
71
/// Sends an HTTP request and asynchronously returns the response.
73
72
@override
74
73
Future <StreamedResponse > send (BaseRequest request) async {
@@ -85,7 +84,7 @@ class BrowserClient extends BaseClient {
85
84
RequestInit (
86
85
method: request.method,
87
86
body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null ,
88
- cache: cacheMode ,
87
+ cache: _cacheMode ! ,
89
88
credentials: withCredentials ? 'include' : 'same-origin' ,
90
89
headers: {
91
90
if (request.contentLength case final contentLength? )
@@ -201,3 +200,17 @@ Stream<List<int>> _readBody(BaseRequest request, Response response) async* {
201
200
extension type _IterableHeaders ._(JSObject _) implements JSObject {
202
201
external void forEach (JSFunction fn);
203
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
+ }
0 commit comments