-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[webview_flutter_web] Migrate to package:web. #6792
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
Changes from 19 commits
61b792d
5775d2e
efbad89
d622bb5
2e11ac2
ab527d9
39b12b6
5e378c4
516c357
d03c9fc
bd2d8a7
4d4f6d7
b77d3fb
a890b22
4ee87cc
87c7243
e01a712
befc2d8
ba4a2e5
da0b903
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,11 @@ | |
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:html'; | ||
import 'dart:async'; | ||
import 'dart:js_interop'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:web/web.dart' as web; | ||
|
||
/// Factory class for creating [HttpRequest] instances. | ||
class HttpRequestFactory { | ||
|
@@ -11,20 +15,16 @@ class HttpRequestFactory { | |
|
||
/// Creates and sends a URL request for the specified [url]. | ||
/// | ||
/// Returns an `Object` (so this class can be mocked by mockito), which can be | ||
/// cast as [web.Response] from `package:web`. | ||
/// | ||
/// By default `request` will perform an HTTP GET request, but a different | ||
/// method (`POST`, `PUT`, `DELETE`, etc) can be used by specifying the | ||
/// [method] parameter. (See also [HttpRequest.postFormData] for `POST` | ||
/// requests only. | ||
/// | ||
/// The Future is completed when the response is available. | ||
/// [method] parameter. | ||
/// | ||
/// If specified, `sendData` will send data in the form of a [ByteBuffer], | ||
/// [Blob], [Document], [String], or [FormData] along with the HttpRequest. | ||
/// The Future is completed when the [web.Response] is available. | ||
/// | ||
/// If specified, [responseType] sets the desired response format for the | ||
/// request. By default it is [String], but can also be 'arraybuffer', 'blob', | ||
/// 'document', 'json', or 'text'. See also [HttpRequest.responseType] | ||
/// for more information. | ||
/// If specified, `sendData` will be sent as the `body` of the fetch. | ||
/// | ||
/// The [withCredentials] parameter specified that credentials such as a cookie | ||
/// (already) set in the header or | ||
|
@@ -55,27 +55,33 @@ class HttpRequestFactory { | |
/// // Do something with the response. | ||
/// }); | ||
/// | ||
/// Note that requests for file:// URIs are only supported by Chrome extensions | ||
/// Requests for file:// URIs are only supported by Chrome extensions | ||
ditman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// with appropriate permissions in their manifest. Requests to file:// URIs | ||
/// will also never fail- the Future will always complete successfully, even | ||
/// when the file cannot be found. | ||
/// | ||
/// See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access_authentication). | ||
Future<HttpRequest> request(String url, | ||
{String? method, | ||
bool? withCredentials, | ||
String? responseType, | ||
String? mimeType, | ||
Map<String, String>? requestHeaders, | ||
dynamic sendData, | ||
void Function(ProgressEvent e)? onProgress}) { | ||
return HttpRequest.request(url, | ||
method: method, | ||
withCredentials: withCredentials, | ||
responseType: responseType, | ||
mimeType: mimeType, | ||
requestHeaders: requestHeaders, | ||
sendData: sendData, | ||
onProgress: onProgress); | ||
Future<Object> request( | ||
String url, { | ||
String method = 'GET', | ||
bool withCredentials = false, | ||
String? mimeType, | ||
Map<String, String>? requestHeaders, | ||
Uint8List? sendData, | ||
}) async { | ||
final Map<String, String> headers = <String, String>{ | ||
if (mimeType != null) 'content-type': mimeType, | ||
...?requestHeaders, | ||
}; | ||
return web.window | ||
.fetch( | ||
url.toJS, | ||
web.RequestInit( | ||
method: method, | ||
body: sendData?.toJS, | ||
credentials: withCredentials ? 'include' : 'same-origin', | ||
headers: headers.jsify()! as web.HeadersInit, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we have support for the JS Map type yet (in JS interop that is), so perhaps we should add a TODO for that? Unless we have a constructor for In my opinion we can eventually get rid of the Not sure how @ditman feels about doing that, though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (@navaronbracke I wrote the Currently The
I'm using the simplest/most legible way I've found so far of creating an object literal of a JS-interop type (a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Maybe something with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add a better constructor here if that's easier to read, but it's not going to do anything more complicated than just
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @srujzs yeah, I'd like to have a headers.jsify()! as web.HeadersInit by headers.jsifyAs<web.HeadersInit>(); (Not that it saves too much typing, though) |
||
)) | ||
.toDart; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.