forked from felangel/mason
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.dart
112 lines (97 loc) · 3.2 KB
/
response.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
part of '_internal.dart';
/// {@template response}
/// An HTTP response.
/// {@endtemplate}
class Response {
/// Create a [Response] with a string body.
Response({
int statusCode = 200,
String? body,
Map<String, Object>? headers,
Encoding? encoding,
}) : this._(
shelf.Response(
statusCode,
body: body,
headers: headers,
encoding: encoding,
),
);
/// Create a [Response] with a stream of bytes.
Response.stream({
int statusCode = 200,
Stream<List<int>>? body,
Map<String, Object>? headers,
}) : this._(
shelf.Response(
statusCode,
body: body,
headers: headers,
),
);
/// Create a [Response] with a byte array body.
Response.bytes({
int statusCode = 200,
List<int>? body,
Map<String, Object>? headers,
}) : this._(
shelf.Response(
statusCode,
body: body,
headers: headers,
),
);
/// Create a [Response] with a json encoded body.
Response.json({
int statusCode = 200,
Object? body = const <String, dynamic>{},
Map<String, Object> headers = const <String, Object>{},
}) : this(
statusCode: statusCode,
body: body != null ? jsonEncode(body) : null,
headers: {
...headers,
if (!headers.containsKey(HttpHeaders.contentTypeHeader))
HttpHeaders.contentTypeHeader: ContentType.json.value,
},
);
Response._(this._response);
shelf.Response _response;
/// The HTTP status code of the response.
int get statusCode => _response.statusCode;
/// The HTTP headers with case-insensitive keys.
/// The returned map is unmodifiable.
Map<String, String> get headers => _response.headers;
/// Returns a [Stream] representing the body.
Stream<List<int>> bytes() => _response.read();
/// Returns a [Future] containing the body as a [String].
Future<String> body() async {
const responseBodyKey = 'dart_frog.response.body';
final bodyFromContext =
_response.context[responseBodyKey] as Completer<String>?;
if (bodyFromContext != null) return bodyFromContext.future;
final completer = Completer<String>();
try {
_response = _response.change(
context: {..._response.context, responseBodyKey: completer},
);
completer.complete(await _response.readAsString());
} catch (error, stackTrace) {
completer.completeError(error, stackTrace);
}
return completer.future;
}
/// Returns a [Future] containing the form data as a [Map].
Future<FormData> formData() {
return parseFormData(headers: headers, body: body, bytes: bytes);
}
/// Returns a [Future] containing the body text parsed as a json object.
/// This object could be anything that can be represented by json
/// e.g. a map, a list, a string, a number, a bool...
Future<dynamic> json() async => jsonDecode(await body());
/// Creates a new [Response] by copying existing values and applying specified
/// changes.
Response copyWith({Map<String, Object?>? headers, Object? body}) {
return Response._(_response.change(headers: headers, body: body));
}
}