diff --git a/packages/static/CHANGELOG.md b/packages/static/CHANGELOG.md index 5ac811ad..28047922 100644 --- a/packages/static/CHANGELOG.md +++ b/packages/static/CHANGELOG.md @@ -3,6 +3,8 @@ ## 7.0.0 * Require Dart >= 2.17 +* [Breaking] Updated `CacheAccessLevel` to enhanced Enum. Changed `PUBLIC` to `public` and `PRIVATE` to `private`. +* [Breaking] Removed `accessLevelToString` as the functionality is provided by enhanced Enum ## 6.0.0 diff --git a/packages/static/lib/src/cache.dart b/packages/static/lib/src/cache.dart index df160f66..7ceb62f7 100644 --- a/packages/static/lib/src/cache.dart +++ b/packages/static/lib/src/cache.dart @@ -5,25 +5,13 @@ import 'package:file/file.dart'; import 'package:logging/logging.dart'; import 'virtual_directory.dart'; -/// Returns a string representation of the given [CacheAccessLevel]. -String accessLevelToString(CacheAccessLevel accessLevel) { - switch (accessLevel) { - case CacheAccessLevel.PRIVATE: - return 'private'; - case CacheAccessLevel.PUBLIC: - return 'public'; - default: - throw ArgumentError('Unrecognized cache access level: $accessLevel'); - } -} - /// A `VirtualDirectory` that also sets `Cache-Control` headers. class CachingVirtualDirectory extends VirtualDirectory { final _log = Logger('CachingVirtualDirectory'); final Map _etags = {}; - /// Either `PUBLIC` or `PRIVATE`. + /// Either `public` or `private`. final CacheAccessLevel accessLevel; /// If `true`, responses will always have `private, max-age=0` as their `Cache-Control` header. @@ -41,7 +29,7 @@ class CachingVirtualDirectory extends VirtualDirectory { final int maxAge; CachingVirtualDirectory(Angel app, FileSystem fileSystem, - {this.accessLevel = CacheAccessLevel.PUBLIC, + {this.accessLevel = CacheAccessLevel.public, Directory? source, bool debug = false, Iterable indexFileNames = const ['index.html'], @@ -181,7 +169,7 @@ class CachingVirtualDirectory extends VirtualDirectory { void setCachedHeaders( DateTime modified, RequestContext req, ResponseContext res) { - var privacy = accessLevelToString(accessLevel); + var privacy = accessLevel.level; res.headers ..['cache-control'] = '$privacy, max-age=$maxAge' @@ -194,5 +182,10 @@ class CachingVirtualDirectory extends VirtualDirectory { } } -// TODO: Refactor -enum CacheAccessLevel { PUBLIC, PRIVATE } +enum CacheAccessLevel { + public('public'), + private('private'); + + const CacheAccessLevel(this.level); + final String level; +} diff --git a/packages/static/test/cache_test.dart b/packages/static/test/cache_test.dart index 208b37ba..feaaca21 100644 --- a/packages/static/test/cache_test.dart +++ b/packages/static/test/cache_test.dart @@ -5,7 +5,6 @@ import 'package:angel3_static/angel3_static.dart'; import 'package:file/local.dart'; import 'package:http/http.dart' show Client; import 'package:logging/logging.dart'; -import 'package:matcher/matcher.dart'; import 'package:test/test.dart'; void main() {