Skip to content

Commit

Permalink
more bodyAsMap helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Aug 16, 2024
1 parent 02203ab commit a3ab7e7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion http/lib/http_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export 'src/http_utils.dart'
TekartikHttpRequestExtension,
TekartikHttpResponseExtension,
TekartikStreamedResponseExtension,
TekartikTkHttpRequestExtension;
TekartikTkHttpRequestExtension,
HttpClientResponseExtension;
export 'src/utils.dart'
show
httpDataAsMapOrNull,
Expand Down
11 changes: 11 additions & 0 deletions http/lib/src/http_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object?> get bodyAsMap => bodyAsMapOrNull!;

/// Get the body as a map or null.
Map<String, Object?>? get bodyAsMapOrNull {
return httpDataAsMapOrNull(body);
}
}
4 changes: 4 additions & 0 deletions http/test/http_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}

0 comments on commit a3ab7e7

Please sign in to comment.