Skip to content

Commit 3a26def

Browse files
committed
Added caching options for http package
1 parent ee01325 commit 3a26def

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

pkgs/http/lib/src/base_request.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,9 @@ abstract class BaseRequest {
165165

166166
@override
167167
String toString() => '$method $url';
168-
}
168+
169+
/// [cache] property for caching the network request.
170+
///
171+
/// Defaults to `default`.
172+
String? get cache => HttpCacheUtils.defaultType;
173+
}

pkgs/http/lib/src/browser_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class BrowserClient extends BaseClient {
6969
RequestInit(
7070
method: request.method,
7171
body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null,
72+
cache: request.cache!,
7273
credentials: withCredentials ? 'include' : 'same-origin',
7374
headers: {
7475
if (request.contentLength case final contentLength?)

pkgs/http/lib/src/multipart_request.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,17 @@ class MultipartRequest extends BaseRequest {
159159
growable: false);
160160
return '$prefix${String.fromCharCodes(list)}';
161161
}
162-
}
162+
163+
@override
164+
String? get cache {
165+
if(this._cache != null){
166+
return this._cache;
167+
}
168+
else return super.cache;
169+
}
170+
171+
String? _cache;
172+
set cache(String? cacheType){
173+
this._cache = cacheType!;
174+
}
175+
}

pkgs/http/lib/src/request.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,17 @@ class Request extends BaseRequest {
181181
if (!finalized) return;
182182
throw StateError("Can't modify a finalized Request.");
183183
}
184+
185+
@override
186+
String? get cache {
187+
if(this._cache != null){
188+
return this._cache;
189+
}
190+
else return super.cache;
191+
}
192+
193+
String? _cache;
194+
set cache(String? cacheType){
195+
this._cache = cacheType!;
196+
}
184197
}

pkgs/http/lib/src/streamed_request.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,17 @@ class StreamedRequest extends BaseRequest {
5252
super.finalize();
5353
return ByteStream(_controller.stream);
5454
}
55+
56+
@override
57+
String? get cache {
58+
if(this._cache != null){
59+
return this._cache;
60+
}
61+
else return super.cache;
62+
}
63+
64+
String? _cache;
65+
set cache(String? cacheType){
66+
this._cache = cacheType!;
67+
}
5568
}

pkgs/http/lib/src/utils.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,15 @@ Stream<T> onDone<T>(Stream<T> stream, void Function() onDone) =>
6868
sink.close();
6969
onDone();
7070
}));
71+
72+
/// Caching utilities types for using cache for any http request.
73+
///
74+
/// For more references, check (Caching types)[https://developer.mozilla.org/en-US/docs/Web/API/Request/cache]
75+
mixin HttpCacheUtils{
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";
82+
}

0 commit comments

Comments
 (0)