From 337c5f1d9985d58a257be10d5d5e42bb96fe2145 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 23 Sep 2023 10:49:53 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=F0=9F=94=8A=20Extract=20`debugLog`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/adapters/browser_adapter.dart | 31 ++++++------------- dio/lib/src/compute/compute_io.dart | 5 ++- .../src/interceptors/imply_content_type.dart | 27 +++++----------- dio/lib/src/utils.dart | 19 ++++++++++++ 4 files changed, 37 insertions(+), 45 deletions(-) diff --git a/dio/lib/src/adapters/browser_adapter.dart b/dio/lib/src/adapters/browser_adapter.dart index eab026f17..399113c76 100644 --- a/dio/lib/src/adapters/browser_adapter.dart +++ b/dio/lib/src/adapters/browser_adapter.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'dart:convert'; import 'dart:html'; import 'dart:typed_data'; -import 'dart:developer' as dev; import 'package:meta/meta.dart'; @@ -10,13 +9,7 @@ import '../adapter.dart'; import '../dio_exception.dart'; import '../headers.dart'; import '../options.dart'; - -// For the web platform, an inline `bool.fromEnvironment` translates to -// `core.bool.fromEnvironment` instead of correctly being replaced by the -// constant value found in the environment at build time. -// -// See https://github.com/flutter/flutter/issues/51186. -const _kReleaseMode = bool.fromEnvironment('dart.vm.product'); +import '../utils.dart'; HttpClientAdapter createAdapter() => BrowserHttpClientAdapter(); @@ -161,21 +154,17 @@ class BrowserHttpClientAdapter implements HttpClientAdapter { } }); } - } else if (!_kReleaseMode) { + } else { if (options.sendTimeout != null) { - dev.log( + debugLog( 'sendTimeout cannot be used without a request body to send', - level: 900, - name: '🔔 Dio', - stackTrace: StackTrace.current, + StackTrace.current, ); } if (options.onSendProgress != null) { - dev.log( + debugLog( 'onSendProgress cannot be used without a request body to send', - level: 900, - name: '🔔 Dio', - stackTrace: StackTrace.current, + StackTrace.current, ); } } @@ -262,13 +251,11 @@ class BrowserHttpClientAdapter implements HttpClientAdapter { }); if (requestStream != null) { - if (!_kReleaseMode && options.method == 'GET') { - dev.log( + if (options.method == 'GET') { + debugLog( 'GET request with a body data are not support on the ' 'web platform. Use POST/PUT instead.', - level: 900, - name: '🔔 Dio', - stackTrace: StackTrace.current, + StackTrace.current, ); } final completer = Completer(); diff --git a/dio/lib/src/compute/compute_io.dart b/dio/lib/src/compute/compute_io.dart index 71b4d5158..f45646bfe 100644 --- a/dio/lib/src/compute/compute_io.dart +++ b/dio/lib/src/compute/compute_io.dart @@ -22,17 +22,16 @@ import 'dart:async'; import 'dart:developer'; import 'dart:isolate'; +import '../utils.dart' show kReleaseMode; import 'compute.dart' as c; -const _kReleaseMode = bool.fromEnvironment('dart.vm.product'); - /// The dart:io implementation of [c.compute]. Future compute( c.ComputeCallback callback, Q message, { String? debugLabel, }) async { - debugLabel ??= _kReleaseMode ? 'compute' : callback.toString(); + debugLabel ??= kReleaseMode ? 'compute' : callback.toString(); final Flow flow = Flow.begin(); Timeline.startSync('$debugLabel: start', flow: flow); diff --git a/dio/lib/src/interceptors/imply_content_type.dart b/dio/lib/src/interceptors/imply_content_type.dart index a26015abc..6664d323d 100644 --- a/dio/lib/src/interceptors/imply_content_type.dart +++ b/dio/lib/src/interceptors/imply_content_type.dart @@ -1,16 +1,8 @@ -import 'dart:developer' as dev; - import '../dio_mixin.dart'; import '../form_data.dart'; import '../headers.dart'; import '../options.dart'; - -// For the web platform, an inline `bool.fromEnvironment` translates to -// `core.bool.fromEnvironment` instead of correctly being replaced by the -// constant value found in the environment at build time. -// -// See https://github.com/flutter/flutter/issues/51186. -const _kReleaseMode = bool.fromEnvironment('dart.vm.product'); +import '../utils.dart'; /// {@template dio.interceptors.ImplyContentTypeInterceptor} /// The default `content-type` for requests will be implied by the @@ -34,17 +26,12 @@ class ImplyContentTypeInterceptor extends Interceptor { } else if (data is List || data is Map || data is String) { contentType = Headers.jsonContentType; } else { - // Do not log in the release mode. - if (!_kReleaseMode) { - dev.log( - '${data.runtimeType} cannot be used ' - 'to imply a default content-type, ' - 'please set a proper content-type in the request.', - level: 900, - name: '🔔 Dio', - stackTrace: StackTrace.current, - ); - } + debugLog( + '${data.runtimeType} cannot be used ' + 'to imply a default content-type, ' + 'please set a proper content-type in the request.', + StackTrace.current, + ); contentType = null; } options.contentType = contentType; diff --git a/dio/lib/src/utils.dart b/dio/lib/src/utils.dart index 586b5384d..ac6a49524 100644 --- a/dio/lib/src/utils.dart +++ b/dio/lib/src/utils.dart @@ -1,10 +1,18 @@ import 'dart:async'; import 'dart:collection'; import 'dart:convert'; +import 'dart:developer' as dev; import 'options.dart'; import 'parameter.dart'; +// For the web platform, an inline `bool.fromEnvironment` translates to +// `core.bool.fromEnvironment` instead of correctly being replaced by the +// constant value found in the environment at build time. +// +// See https://github.com/flutter/flutter/issues/51186. +const kReleaseMode = bool.fromEnvironment('dart.vm.product'); + /// Pipes all data and errors from [stream] into [sink]. Completes [Future] once /// [stream] is done. Unlike [store], [sink] remains open after [stream] is /// done. @@ -133,3 +141,14 @@ Map caseInsensitiveKeyMap([Map? value]) { if (value != null && value.isNotEmpty) map.addAll(value); return map; } + +void debugLog(String message, StackTrace stackTrace) { + if (!kReleaseMode) { + dev.log( + message, + level: 900, + name: '🔔 Dio', + stackTrace: stackTrace, + ); + } +} From 9a63c3736f5b8664ed0438ad28b2220f20bb3242 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 23 Sep 2023 10:53:09 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Type=20improvement=20f?= =?UTF-8?q?or=20`encodeMap`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/utils.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dio/lib/src/utils.dart b/dio/lib/src/utils.dart index ac6a49524..e505b6e84 100644 --- a/dio/lib/src/utils.dart +++ b/dio/lib/src/utils.dart @@ -38,7 +38,7 @@ Encoding encodingForCharset(String? charset, [Encoding fallback = latin1]) { typedef DioEncodeHandler = String? Function(String key, Object? value); String encodeMap( - data, + Object data, DioEncodeHandler handler, { bool encode = true, bool isQuery = false, @@ -51,7 +51,9 @@ String encodeMap( // When [encode] is false, for example for [FormData], nothing is encoded. final leftBracket = isQuery || !encode ? '[' : '%5B'; final rightBracket = isQuery || !encode ? ']' : '%5D'; - final encodeComponent = encode ? Uri.encodeQueryComponent : (e) => e; + + final String Function(String) encodeComponent = + encode ? Uri.encodeQueryComponent : (e) => e; Object? maybeEncode(Object? value) { if (!isQuery || value == null || value is! String) { return value; @@ -93,7 +95,7 @@ String encodeMap( } else if (sub is Map) { sub.forEach((k, v) { if (path == '') { - urlEncode(maybeEncode(v), '${encodeComponent(k)}'); + urlEncode(maybeEncode(v), encodeComponent(k)); } else { urlEncode( maybeEncode(v), From 124d3cfe76070aa380ffd8dbe6ba599e1fb21f33 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 23 Sep 2023 11:12:23 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=94=8A=20Raise=20warning=20for=20`Map?= =?UTF-8?q?`=20datas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/transformers/sync_transformer.dart | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/dio/lib/src/transformers/sync_transformer.dart b/dio/lib/src/transformers/sync_transformer.dart index f2b666299..93366cbf0 100644 --- a/dio/lib/src/transformers/sync_transformer.dart +++ b/dio/lib/src/transformers/sync_transformer.dart @@ -6,6 +6,7 @@ import '../adapter.dart'; import '../headers.dart'; import '../options.dart'; import '../transformer.dart'; +import '../utils.dart'; @Deprecated('Use BackgroundTransformer instead') typedef DefaultTransformer = SyncTransformer; @@ -33,8 +34,18 @@ class SyncTransformer extends Transformer { final dynamic data = options.data ?? ''; if (data is! String && Transformer.isJsonMimeType(options.contentType)) { return jsonEncodeCallback(data); - } else if (data is Map) { - return Transformer.urlEncodeMap(data, options.listFormat); + } else if (data is Map) { + if (data is Map) { + return Transformer.urlEncodeMap(data, options.listFormat); + } + debugLog( + 'The data is a type of Map (${data.runtimeType}), ' + 'but the transformer can only encode Map.\n' + 'If you are writing maps using `{}`, ' + 'consider writing `{}`.', + StackTrace.current, + ); + return data.toString(); } else { return data.toString(); } From 7115b8d98b045789ffb68f7e01f082e4971b1998 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 23 Sep 2023 11:12:59 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Type=20promote?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/transformers/sync_transformer.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dio/lib/src/transformers/sync_transformer.dart b/dio/lib/src/transformers/sync_transformer.dart index 93366cbf0..db8af0ce9 100644 --- a/dio/lib/src/transformers/sync_transformer.dart +++ b/dio/lib/src/transformers/sync_transformer.dart @@ -31,7 +31,7 @@ class SyncTransformer extends Transformer { @override Future transformRequest(RequestOptions options) async { - final dynamic data = options.data ?? ''; + final Object data = options.data ?? ''; if (data is! String && Transformer.isJsonMimeType(options.contentType)) { return jsonEncodeCallback(data); } else if (data is Map) { From e73ed40fbff712f63190a7585df54809b4f7547f Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 23 Sep 2023 16:05:39 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=92=A1=20TODO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/utils.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/dio/lib/src/utils.dart b/dio/lib/src/utils.dart index e505b6e84..0073bd7b0 100644 --- a/dio/lib/src/utils.dart +++ b/dio/lib/src/utils.dart @@ -144,6 +144,7 @@ Map caseInsensitiveKeyMap([Map? value]) { return map; } +// TODO(Alex): Provide a configurable property on the Dio class once https://github.com/cfug/dio/discussions/1982 has made some progress. void debugLog(String message, StackTrace stackTrace) { if (!kReleaseMode) { dev.log( From 028f0f536be936e3b3a1d4f4b2a4804bf8dd472d Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 23 Sep 2023 16:07:38 +0800 Subject: [PATCH 6/7] Update sync_transformer.dart Signed-off-by: Alex Li --- dio/lib/src/transformers/sync_transformer.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dio/lib/src/transformers/sync_transformer.dart b/dio/lib/src/transformers/sync_transformer.dart index db8af0ce9..54da8ccfb 100644 --- a/dio/lib/src/transformers/sync_transformer.dart +++ b/dio/lib/src/transformers/sync_transformer.dart @@ -39,8 +39,8 @@ class SyncTransformer extends Transformer { return Transformer.urlEncodeMap(data, options.listFormat); } debugLog( - 'The data is a type of Map (${data.runtimeType}), ' - 'but the transformer can only encode Map.\n' + 'The data is a type of `Map` (${data.runtimeType}), ' + 'but the transformer can only encode `Map`.\n' 'If you are writing maps using `{}`, ' 'consider writing `{}`.', StackTrace.current, From 8ae7ab26632961fead22277efa7e4a4fa95ce0e6 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 26 Sep 2023 06:21:53 +0800 Subject: [PATCH 7/7] Update CHANGELOG.md Signed-off-by: Alex Li --- dio/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index 9ed27486d..0c275cbd5 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -5,7 +5,7 @@ See the [Migration Guide][] for the complete breaking changes list.** ## Unreleased -*None.* +- Raise warning for `Map`s other than `Map` when encoding request data. ## 5.3.3