Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Support relative baseUrl on the Web platform #2094

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
- Fix `receiveTimeout` for streamed responses.
- Fix cancellation for streamed responses and downloads when using `IOHttpClientAdapter`.
- Fix receive progress for streamed responses and downloads when using `IOHttpClientAdapter`.
- Support relative `baseUrl` on the Web platform.

## 5.4.0

Expand Down
18 changes: 12 additions & 6 deletions dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,16 @@ typedef RequestEncoder = FutureOr<List<int>> Function(

/// The mixin class for options that provides common attributes.
mixin OptionsMixin {
/// Request base url, it can contain sub paths like: https://pub.dev/api/.
late String baseUrl;
/// Request base url, it can be multiple forms:
/// - Contains sub paths, e.g. `https://pub.dev/api/`.
/// - Relative path on Web, e.g. `api/`.
String get baseUrl => _baseUrl;
late String _baseUrl;

set baseUrl(String value) {
assert(value.isEmpty || kIsWeb || Uri.parse(value).host.isNotEmpty);
_baseUrl = value;
}

/// Common query parameters.
///
Expand Down Expand Up @@ -141,9 +149,7 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
RequestEncoder? requestEncoder,
ResponseDecoder? responseDecoder,
ListFormat? listFormat,
}) : assert(connectTimeout == null || !connectTimeout.isNegative),
kuhnroyal marked this conversation as resolved.
Show resolved Hide resolved
assert(baseUrl.isEmpty || Uri.parse(baseUrl).host.isNotEmpty),
super(
}) : super(
method: method,
receiveTimeout: receiveTimeout,
sendTimeout: sendTimeout,
Expand All @@ -161,8 +167,8 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
responseDecoder: responseDecoder,
listFormat: listFormat,
) {
this.queryParameters = queryParameters ?? {};
this.baseUrl = baseUrl;
this.queryParameters = queryParameters ?? {};
this.connectTimeout = connectTimeout;
}

Expand Down
5 changes: 5 additions & 0 deletions dio/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import 'dart:developer' as dev;
import 'options.dart';
import 'parameter.dart';

// See https://github.com/flutter/flutter/pull/112122.
const kIsWeb = bool.hasEnvironment('dart.library.js_util')
? bool.fromEnvironment('dart.library.js_util')
: identical(0, 0.0);

// 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.
Expand Down
29 changes: 17 additions & 12 deletions dio/test/options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -470,18 +470,23 @@ void main() {
});

test('option invalid base url', () {
final opt1 = 'blob:http://localhost/xyz123';
final opt2 = 'https://pub.dev';
final opt3 = 'https://';
final opt4 = 'https://loremipsum/';
final opt5 = '';
final opt6 = 'pub.dev';
expect(Uri.parse(opt1).host.isNotEmpty, false);
expect(Uri.parse(opt2).host.isNotEmpty, true);
expect(Uri.parse(opt3).host.isNotEmpty, false);
expect(Uri.parse(opt4).host.isNotEmpty, true);
expect(Uri.parse(opt5).host.isNotEmpty, false);
expect(Uri.parse(opt6).host.isNotEmpty, false);
final invalidUrls = <String>[
'blob:http://localhost/xyz123',
'https://',
'pub.dev',
];
final validUrls = <String>[
'',
'https://pub.dev',
'https://loremipsum/',
if (kIsWeb) 'api/',
];
for (final url in invalidUrls) {
expect(() => BaseOptions(baseUrl: url), throwsA(isA<AssertionError>()));
}
for (final url in validUrls) {
expect(BaseOptions(baseUrl: url), isA<BaseOptions>());
}
});

test('Throws when using invalid methods', () async {
Expand Down