File tree Expand file tree Collapse file tree 2 files changed +24
-9
lines changed Expand file tree Collapse file tree 2 files changed +24
-9
lines changed Original file line number Diff line number Diff line change 4
4
5
5
import 'dart:async' ;
6
6
import 'dart:js_interop' ;
7
-
7
+ import 'package:http/src/utils.dart' show CacheOption;
8
8
import 'package:web/web.dart'
9
9
show
10
10
AbortController,
@@ -45,6 +45,10 @@ BaseClient createClient() {
45
45
class BrowserClient extends BaseClient {
46
46
final _abortController = AbortController ();
47
47
48
+ BrowserClient ([CacheOption ? cacheOption]) {
49
+ _cacheOption = cacheOption? .cacheType ?? CacheOption .defaultType.cacheType;
50
+ }
51
+
48
52
/// Whether to send credentials such as cookies or authorization headers for
49
53
/// cross-site requests.
50
54
///
@@ -53,6 +57,8 @@ class BrowserClient extends BaseClient {
53
57
54
58
bool _isClosed = false ;
55
59
60
+ String ? _cacheOption;
61
+
56
62
/// Sends an HTTP request and asynchronously returns the response.
57
63
@override
58
64
Future <StreamedResponse > send (BaseRequest request) async {
@@ -69,7 +75,7 @@ class BrowserClient extends BaseClient {
69
75
RequestInit (
70
76
method: request.method,
71
77
body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null ,
72
- cache: request.cache ! ,
78
+ cache: _cacheOption ! ,
73
79
credentials: withCredentials ? 'include' : 'same-origin' ,
74
80
headers: {
75
81
if (request.contentLength case final contentLength? )
Original file line number Diff line number Diff line change @@ -72,11 +72,20 @@ Stream<T> onDone<T>(Stream<T> stream, void Function() onDone) =>
72
72
/// Caching utilities types for using cache for any http request.
73
73
///
74
74
/// For more references, check (Caching types)[https://developer.mozilla.org/en-US/docs/Web/API/Request/cache]
75
- mixin HttpCacheOptions {
76
- static const String defaultType = 'default' ;
77
- static const String reloadType = 'reload' ;
78
- static const String noStoringType = 'no-store' ;
79
- static const String noCachingType = 'no-cache' ;
80
- static const String forceCachingType = 'force-cache' ;
81
- static const String onlyIfCachedType = 'only-if-cached' ;
75
+ enum CacheOption {
76
+ defaultType ('default' ),
77
+ reload ('reload' ),
78
+ noStore ('no_store' ),
79
+ noCache ('no_cache' ),
80
+ forceCache ('force_cache' ),
81
+ onlyIfCached ('only_if_cached' );
82
+
83
+ final String cacheType;
84
+
85
+ const CacheOption (this .cacheType);
86
+
87
+ static CacheOption fromString (String cacheType) => values.firstWhere (
88
+ (v) => v.cacheType == cacheType,
89
+ orElse: () => CacheOption .defaultType,
90
+ );
82
91
}
You can’t perform that action at this time.
0 commit comments