Skip to content

Commit 8fa4e00

Browse files
committed
feat(package_info_plus): Add customVersionJson URI to getAll method
1 parent 33681cd commit 8fa4e00

File tree

7 files changed

+48
-10
lines changed

7 files changed

+48
-10
lines changed

packages/package_info_plus/package_info_plus/example/integration_test/package_info_plus_web_test.dart

+21
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ void main() {
2929
'build_signature': '',
3030
};
3131

32+
final customUri = Uri.tryParse('https://example.com/version.json');
33+
3234
late PackageInfoPlusWebPlugin plugin;
3335
late MockClient client;
3436

@@ -78,6 +80,25 @@ void main() {
7880
},
7981
);
8082

83+
testWidgets(
84+
'Get package info using custom version json uri',
85+
(tester) async {
86+
when(client.get(customUri)).thenAnswer(
87+
(_) => Future.value(
88+
http.Response(jsonEncode(VERSION_JSON), 200),
89+
),
90+
);
91+
92+
final versionMap = await plugin.getAll(customVersionJson: customUri);
93+
94+
expect(versionMap.appName, VERSION_JSON['app_name']);
95+
expect(versionMap.version, VERSION_JSON['version']);
96+
expect(versionMap.buildNumber, VERSION_JSON['build_number']);
97+
expect(versionMap.packageName, VERSION_JSON['package_name']);
98+
expect(versionMap.buildSignature, VERSION_JSON['build_signature']);
99+
},
100+
);
101+
81102
testWidgets(
82103
'Get correct versionJsonUrl for http and https',
83104
(tester) async {

packages/package_info_plus/package_info_plus/lib/package_info_plus.dart

+9-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ class PackageInfo {
3333

3434
/// Retrieves package information from the platform.
3535
/// The result is cached.
36-
static Future<PackageInfo> fromPlatform() async {
36+
///
37+
/// Web: The plugin uses the generated version.json to read the package
38+
/// information.
39+
/// By default, the package uses the browser navigator base URL.
40+
/// Optionally, a custom version JSON URI can be provided.
41+
static Future<PackageInfo> fromPlatform({Uri? customVersionJson}) async {
3742
if (_fromPlatform != null) {
3843
return _fromPlatform!;
3944
}
4045

41-
final platformData = await PackageInfoPlatform.instance.getAll();
46+
final platformData = await PackageInfoPlatform.instance.getAll(
47+
customVersionJson: customVersionJson,
48+
);
4249
_fromPlatform = PackageInfo(
4350
appName: platformData.appName,
4451
packageName: platformData.packageName,

packages/package_info_plus/package_info_plus/lib/src/package_info_plus_linux.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PackageInfoPlusLinuxPlugin extends PackageInfoPlatform {
1515
/// Returns a map with the following keys:
1616
/// appName, packageName, version, buildNumber
1717
@override
18-
Future<PackageInfoData> getAll() async {
18+
Future<PackageInfoData> getAll({Uri? customVersionJson}) async {
1919
final versionJson = await _getVersionJson();
2020
return PackageInfoData(
2121
appName: versionJson['app_name'] ?? '',

packages/package_info_plus/package_info_plus/lib/src/package_info_plus_web.dart

+14-4
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,19 @@ class PackageInfoPlusWebPlugin extends PackageInfoPlatform {
4949
}
5050

5151
@override
52-
Future<PackageInfoData> getAll() async {
52+
Future<PackageInfoData> getAll({Uri? customVersionJson}) async {
5353
final cacheBuster = DateTime.now().millisecondsSinceEpoch;
54-
final url = versionJsonUrl(web.window.document.baseURI, cacheBuster);
55-
final response = _client == null ? await get(url) : await _client.get(url);
56-
final versionMap = _getVersionMap(response);
5754

55+
Uri? url;
56+
// If user provides a custom version.json, use it
57+
if (customVersionJson != null) {
58+
url = customVersionJson;
59+
} else {
60+
url = versionJsonUrl(web.window.document.baseURI, cacheBuster);
61+
}
62+
63+
final response = await _get(url);
64+
final versionMap = _getVersionMap(response);
5865
return PackageInfoData(
5966
appName: versionMap['app_name'] ?? '',
6067
version: versionMap['version'] ?? '',
@@ -65,6 +72,9 @@ class PackageInfoPlusWebPlugin extends PackageInfoPlatform {
6572
);
6673
}
6774

75+
Future<Response> _get(Uri url) async =>
76+
_client == null ? await get(url) : await _client.get(url);
77+
6878
Map<String, dynamic> _getVersionMap(Response response) {
6979
if (response.statusCode == 200) {
7080
try {

packages/package_info_plus/package_info_plus/lib/src/package_info_plus_windows.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class PackageInfoPlusWindowsPlugin extends PackageInfoPlatform {
1818
/// Returns a map with the following keys:
1919
/// appName, packageName, version, buildNumber
2020
@override
21-
Future<PackageInfoData> getAll() {
21+
Future<PackageInfoData> getAll({Uri? customVersionJson}) {
2222
String resolvedExecutable = Platform.resolvedExecutable;
2323

2424
/// Workaround for https://github.com/dart-lang/sdk/issues/52309

packages/package_info_plus/package_info_plus_platform_interface/lib/method_channel_package_info.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const MethodChannel _channel =
99
/// An implementation of [PackageInfoPlatform] that uses method channels.
1010
class MethodChannelPackageInfo extends PackageInfoPlatform {
1111
@override
12-
Future<PackageInfoData> getAll() async {
12+
Future<PackageInfoData> getAll({Uri? customVersionJson}) async {
1313
final map = await _channel.invokeMapMethod<String, dynamic>('getAll');
1414
return PackageInfoData(
1515
appName: map!['appName'] ?? '',

packages/package_info_plus/package_info_plus_platform_interface/lib/package_info_platform_interface.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ abstract class PackageInfoPlatform extends PlatformInterface {
3030
}
3131

3232
///Returns a map with the following keys : appName,packageName,version,buildNumber
33-
Future<PackageInfoData> getAll() {
33+
Future<PackageInfoData> getAll({Uri? customVersionJson}) {
3434
throw UnimplementedError('getAll() has not been implemented.');
3535
}
3636
}

0 commit comments

Comments
 (0)