From a3ab7e79094cc2aecc6ee17b41798044842efda8 Mon Sep 17 00:00:00 2001 From: Alexandre Roux Date: Fri, 16 Aug 2024 12:55:24 +0200 Subject: [PATCH] more bodyAsMap helpers --- http/lib/http_utils.dart | 3 ++- http/lib/src/http_utils.dart | 11 +++++++++++ http/test/http_utils_test.dart | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/http/lib/http_utils.dart b/http/lib/http_utils.dart index ca50596..7e77d4d 100644 --- a/http/lib/http_utils.dart +++ b/http/lib/http_utils.dart @@ -4,7 +4,8 @@ export 'src/http_utils.dart' TekartikHttpRequestExtension, TekartikHttpResponseExtension, TekartikStreamedResponseExtension, - TekartikTkHttpRequestExtension; + TekartikTkHttpRequestExtension, + HttpClientResponseExtension; export 'src/utils.dart' show httpDataAsMapOrNull, diff --git a/http/lib/src/http_utils.dart b/http/lib/src/http_utils.dart index 270ab4a..e8ba5b7 100644 --- a/http/lib/src/http_utils.dart +++ b/http/lib/src/http_utils.dart @@ -94,3 +94,14 @@ extension TekartikHttpResponseExtension on http.Response { reasonPhrase: reasonPhrase); } } + +/// Common response extension. +extension HttpClientResponseExtension on HttpClientResponse { + /// Get the body as a map. + Map get bodyAsMap => bodyAsMapOrNull!; + + /// Get the body as a map or null. + Map? get bodyAsMapOrNull { + return httpDataAsMapOrNull(body); + } +} diff --git a/http/test/http_utils_test.dart b/http/test/http_utils_test.dart index 9eecc2e..5a183e8 100644 --- a/http/test/http_utils_test.dart +++ b/http/test/http_utils_test.dart @@ -39,6 +39,10 @@ void main() { HttpRequest? httpRequest; expect(httpRequest?.bodyAsMapOrNull, null); expect(httpRequest?.bodyAsMap, null); + + HttpClientResponse? clientResponse; + expect(clientResponse?.bodyAsMapOrNull, null); + expect(clientResponse?.bodyAsMap, null); }); }); }