Skip to content

Commit

Permalink
⚡️ Support relative baseUrl on the Web platform (#2094)
Browse files Browse the repository at this point in the history
Resolves #2087.

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package
  • Loading branch information
AlexV525 committed Jan 12, 2024
1 parent 37ab8ab commit 51d0bbb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
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),
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

0 comments on commit 51d0bbb

Please sign in to comment.