import 'dart:js_interop'; import 'dart:typed_data'; import 'chat_upload.dart'; @JS('fetch') external JSPromise<_Response> _fetch(JSString input, _Init init); extension type _Init._(JSObject _) implements JSObject { external factory _Init({ JSString method, JSAny headers, JSAny body, JSString credentials, }); } extension type _Response._(JSObject _) implements JSObject { external int get status; external JSPromise text(); } /// The browser keeps the login cookie; `same-origin` sends it only to the /// page's own server. Future postChatUpload( Uri uri, { required String name, required String mime, required Uint8List bytes, }) async { final response = await _fetch( uri.toString().toJS, _Init( method: 'POST'.toJS, headers: { chatUploadNameHeader: Uri.encodeComponent(name), 'Content-Type': mime, }.jsify()!, body: bytes.toJS, credentials: 'same-origin'.toJS, ), ).toDart; final body = (await response.text().toDart).toDart; return (status: response.status, body: body); }