31 lines
976 B
Dart
31 lines
976 B
Dart
import 'dart:js_interop';
|
|
|
|
@JS('fetch')
|
|
external JSPromise<_Response> _fetch(JSString url, _Options options);
|
|
|
|
extension type _Options._(JSObject _) implements JSObject {
|
|
external factory _Options({JSString credentials, JSString cache});
|
|
}
|
|
|
|
extension type _Response._(JSObject _) implements JSObject {
|
|
external int get status;
|
|
external JSPromise<JSString> text();
|
|
}
|
|
|
|
Future<String> fetchDiscovery() async {
|
|
final response = await _fetch(
|
|
'/api/discovery'.toJS,
|
|
_Options(credentials: 'same-origin'.toJS, cache: 'no-store'.toJS),
|
|
).toDart;
|
|
if (response.status == 401 || response.status == 403) {
|
|
throw StateError('Sign in to this server before searching the network.');
|
|
}
|
|
if (response.status == 404) {
|
|
throw StateError('Update this server to enable browser network discovery.');
|
|
}
|
|
if (response.status != 200) {
|
|
throw StateError('Network discovery failed (HTTP ${response.status}).');
|
|
}
|
|
return (await response.text().toDart).toDart;
|
|
}
|