diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c1bff73..56f23074 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 2.0.5 * Add `allowHttp1` to `AngelHttp2` constructors. https://github.com/angel-dart/angel/issues/108 -* +* Add `deserializeBody` and `decodeBody` to `RequestContext`. https://github.com/angel-dart/angel/issues/109 +* Add `HostnameRouter`, which allows for routing based on hostname. https://github.com/angel-dart/angel/issues/110 # 2.0.4 * Prepare for Dart SDK change to `Stream>` that are now diff --git a/lib/src/core/request_context.dart b/lib/src/core/request_context.dart index 487c16fd..1afe206b 100644 --- a/lib/src/core/request_context.dart +++ b/lib/src/core/request_context.dart @@ -221,6 +221,17 @@ abstract class RequestContext { /// Returns as `true` if the client's `Accept` header indicates that it will accept any response content type. bool get acceptsAll => _acceptsAllCache ??= accepts('*/*'); + /// Shorthand for deserializing a body. + Future deserializeBody(FutureOr Function(Map) f, + {Encoding encoding = utf8}) async { + await parseBody(encoding: encoding); + return await f(bodyAsMap); + } + + /// Shorthand for decoding a body. + Future decodeBody(Codec codec, {Encoding encoding = utf8}) => + deserializeBody(codec.decode, encoding: encoding); + /// Manually parses the request body, if it has not already been parsed. Future parseBody({Encoding encoding = utf8}) async { if (contentType == null) {