Skip to content

Commit ddde1e3

Browse files
committed
Feat(enhanced-http): improved interceptors
1 parent 6b44c90 commit ddde1e3

File tree

3 files changed

+75
-35
lines changed

3 files changed

+75
-35
lines changed

packages/enhanced_http/lib/enhanced_http.dart

+63-31
Original file line numberDiff line numberDiff line change
@@ -23,112 +23,144 @@ class EnhancedHttp extends StreamedRequest with Interceptor, Utils {
2323
Future<dynamic> get(String path,
2424
{Map<String, String>? headers,
2525
InterceptorOptions? interceptors,
26-
String? responseType}) async {
26+
String? responseType,
27+
Map<String, String>? options}) async {
28+
final url = Uri.parse('$baseURL$path');
2729
return await request(() async {
28-
final url = Uri.parse('$baseURL$path');
2930
if (isStream(headers, responseType)) {
30-
return await streamed('GET', url, mergeHeaders(this.headers, headers),
31+
return await streamed(
32+
'GET',
33+
url,
34+
mergeHeaders(
35+
this.headers, headers, this.interceptors!.headers?.call()),
3136
responseType: responseType);
3237
}
3338
return await http.get(
3439
url,
35-
headers: mergeHeaders(this.headers, headers),
40+
headers: mergeHeaders(
41+
this.headers, headers, this.interceptors!.headers?.call()),
3642
);
37-
}, interceptors);
43+
}, url, options, interceptors);
3844
}
3945

4046
Future<dynamic> post(String path,
4147
{dynamic payload,
4248
Map<String, String>? headers,
4349
InterceptorOptions? interceptors,
4450
List<dynamic>? files,
45-
String? responseType}) async {
51+
String? responseType,
52+
Map<String, String>? options}) async {
4653
final url = Uri.parse('$baseURL$path');
4754
return await request(() async {
4855
if (isStream(headers, responseType)) {
49-
return await streamed('POST', url, mergeHeaders(this.headers, headers),
50-
payload: payload, files: files, responseType: responseType);
56+
return await streamed(
57+
'POST',
58+
url,
59+
mergeHeaders(
60+
this.headers, headers, this.interceptors!.headers?.call()),
61+
payload: payload,
62+
files: files,
63+
responseType: responseType);
5164
}
5265
return await http.post(
5366
url,
54-
headers: mergeHeaders(this.headers, headers),
67+
headers: mergeHeaders(
68+
this.headers, headers, this.interceptors!.headers?.call()),
5569
body: payload == null ? {} : jsonEncode(payload),
5670
);
57-
}, interceptors);
71+
}, url, options, interceptors);
5872
}
5973

6074
Future<dynamic> _update(String method, String path, payload, headers,
61-
interceptors, files, responseType) async {
75+
interceptors, files, responseType, options) async {
6276
final url = Uri.parse('$baseURL$path');
6377
return await request(() async {
6478
if (isStream(headers, responseType)) {
65-
return await streamed(method, url, mergeHeaders(this.headers, headers),
66-
payload: payload, files: files, responseType: responseType);
79+
return await streamed(
80+
method,
81+
url,
82+
mergeHeaders(
83+
this.headers, headers, this.interceptors!.headers?.call()),
84+
payload: payload,
85+
files: files,
86+
responseType: responseType);
6787
} else {
6888
payload ??= {};
6989
if (method == "PUT") {
7090
return await http.put(
7191
url,
72-
headers: mergeHeaders(this.headers, headers),
92+
headers: mergeHeaders(
93+
this.headers, headers, this.interceptors!.headers?.call()),
7394
body: jsonEncode(payload),
7495
);
7596
}
7697
return await http.patch(
7798
url,
78-
headers: mergeHeaders(this.headers, headers),
99+
headers: mergeHeaders(
100+
this.headers, headers, this.interceptors!.headers?.call()),
79101
body: jsonEncode(payload),
80102
);
81103
}
82-
}, interceptors);
104+
}, url, options, interceptors);
83105
}
84106

85107
Future<dynamic> put(String path,
86108
{dynamic payload,
87109
Map<String, String>? headers,
88110
InterceptorOptions? interceptors,
89111
List<dynamic>? files,
90-
String? responseType}) async {
91-
return _update(
92-
"PUT", path, payload, headers, interceptors, files, responseType);
112+
String? responseType,
113+
Map<String, String>? options}) async {
114+
return _update("PUT", path, payload, headers, interceptors, files,
115+
responseType, options);
93116
}
94117

95118
Future<dynamic> patch(String path,
96119
{dynamic payload,
97120
Map<String, String>? headers,
98121
InterceptorOptions? interceptors,
99122
List<dynamic>? files,
100-
String? responseType}) async {
101-
return _update(
102-
"PATCH", path, payload, headers, interceptors, files, responseType);
123+
String? responseType,
124+
Map<String, String>? options}) async {
125+
return _update("PATCH", path, payload, headers, interceptors, files,
126+
responseType, options);
103127
}
104128

105129
Future<dynamic> delete(String path,
106130
{Map<String, String>? headers,
107131
InterceptorOptions? interceptors,
108-
String? responseType}) async {
132+
String? responseType,
133+
Map<String, String>? options}) async {
134+
final url = Uri.parse('$baseURL$path');
109135
return await request(() async {
110-
final url = Uri.parse('$baseURL$path');
111136
if (isStream(headers, responseType)) {
112137
return await streamed(
113-
'DELETE', url, mergeHeaders(this.headers, headers),
138+
'DELETE',
139+
url,
140+
mergeHeaders(
141+
this.headers, headers, this.interceptors!.headers?.call()),
114142
responseType: responseType);
115143
}
116144
return await http.delete(
117145
url,
118-
headers: mergeHeaders(this.headers, headers),
146+
headers: mergeHeaders(
147+
this.headers, headers, this.interceptors!.headers?.call()),
119148
);
120-
}, interceptors);
149+
}, url, options, interceptors);
121150
}
122151

123152
Future<dynamic> head(String path,
124153
{Map<String, String>? headers,
125154
InterceptorOptions? interceptors,
126-
String? responseType}) async {
155+
String? responseType,
156+
Map<String, String>? options}) async {
157+
final url = Uri.parse('$baseURL$path');
127158
return await request(() async {
128159
return await http.head(
129-
Uri.parse('$baseURL$path'),
130-
headers: mergeHeaders(this.headers, headers),
160+
url,
161+
headers: mergeHeaders(
162+
this.headers, headers, this.interceptors!.headers?.call()),
131163
);
132-
}, interceptors);
164+
}, url, options, interceptors);
133165
}
134166
}

packages/enhanced_http/lib/interceptor.dart

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:enhanced_http/http_error.dart';
44
class InterceptorOptions {
55
Function(dynamic res)? response;
66
Function(dynamic e)? error;
7+
Function()? headers;
78

89
InterceptorOptions({this.response, this.error});
910
}
@@ -14,7 +15,11 @@ class Interceptor {
1415
String? defaultErrorMessage;
1516

1617
Future<dynamic> request(
17-
Function request, InterceptorOptions? _interceptors) async {
18+
Function request,
19+
Uri url,
20+
Map<String, String>? options,
21+
InterceptorOptions? _interceptors,
22+
) async {
1823
_interceptors = InterceptorOptions(
1924
response: interceptors?.response ?? _interceptors?.response,
2025
error: interceptors?.error ?? _interceptors?.error);
@@ -31,9 +36,11 @@ class Interceptor {
3136
}
3237
}
3338
final decoded = {
39+
"url": url,
3440
"status": response.statusCode,
3541
"data": data,
36-
"headers": response.headers
42+
"headers": response.headers,
43+
"options": options,
3744
};
3845
if (response.statusCode >= 200 && response.statusCode <= 299) {
3946
if (_interceptors.response != null) {

packages/enhanced_http/lib/utils.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class Utils {
22
Map<String, String> mergeHeaders(
3-
Map<String, String>? h1, Map<String, String>? h2) =>
4-
{...?h1, ...?h2};
3+
Map<String, String>? h1, Map<String, String>? h2,
4+
[Map<String, String>? h3]) =>
5+
{...?h1, ...?h2, ...?h3};
56

67
bool isStream(Map<String, String>? headers, [String? responseType = "json"]) {
78
if ([headers?["Content-Type"], headers?["content-type"]]

0 commit comments

Comments
 (0)