Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.6.2-wip

* Add `BrowserCredentialsMode` and `BrowserClient.credentialsMode` to support the `omit` browser fetch credentials mode. Deprecate `withCredentials`.

## 1.6.1-wip

* Clarified the behavior of response headers in API documentation comments.
Expand Down
78 changes: 73 additions & 5 deletions pkgs/http/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,39 @@ BaseClient createClient() {

@JS('fetch')
external JSPromise<Response> _fetch(
RequestInfo input, [
RequestInit init,
]);
RequestInfo input, [
RequestInit init,
]);

/// The browser `fetch` credentials mode used by [BrowserClient].
///
/// Controls whether the browser sends credentials such as cookies, TLS client
/// certificates, or authorization headers with a request.
///
/// See also:
/// - https://fetch.spec.whatwg.org/#requestcredentials
enum BrowserCredentialsMode {
/// Never send credentials with the request and never include credentials in
/// the response.
///
/// This corresponds to the browser `fetch` credentials mode `omit`.
omit('omit'),

/// Send credentials for same-origin requests only.
///
/// This corresponds to the browser `fetch` credentials mode `same-origin`.
sameOrigin('same-origin'),

/// Always send credentials, even for cross-origin requests.
///
/// This corresponds to the browser `fetch` credentials mode `include`.
include('include');

const BrowserCredentialsMode(this._value);

/// The value passed to the browser `fetch` `RequestInit.credentials` field.
final String _value;
}

/// A `package:web`-based HTTP client that runs in the browser and is backed by
/// [`window.fetch`](https://fetch.spec.whatwg.org/).
Expand All @@ -52,11 +82,49 @@ external JSPromise<Response> _fetch(
/// Responses are streamed but requests are not. A request will only be sent
/// once all the data is available.
class BrowserClient extends BaseClient {
/// Create a [BrowserClient].
///
/// By default, credentials are sent for same-origin requests only, which
/// matches the previous default behavior when [withCredentials] was `false`.
BrowserClient({
this.credentialsMode = BrowserCredentialsMode.sameOrigin,
});

/// The browser `fetch` credentials mode used for requests.
///
/// Defaults to [BrowserCredentialsMode.sameOrigin], which matches the
/// previous behavior when [withCredentials] was `false`.
BrowserCredentialsMode credentialsMode;

/// Whether to send credentials such as cookies or authorization headers for
/// cross-site requests.
///
/// Defaults to `false`.
bool withCredentials = false;
///
/// This property is deprecated because it can only represent two of the three
/// browser `fetch` credentials modes (`same-origin` and `include`). Use
/// [credentialsMode] instead to also support [BrowserCredentialsMode.omit].
///
/// Reading this property returns `true` only when [credentialsMode] is
/// [BrowserCredentialsMode.include].
@Deprecated('Use credentialsMode instead.')
bool get withCredentials =>
credentialsMode == BrowserCredentialsMode.include;

/// Whether to send credentials such as cookies or authorization headers for
/// cross-site requests.
///
/// Setting this to `true` sets [credentialsMode] to
/// [BrowserCredentialsMode.include].
///
/// Setting this to `false` sets [credentialsMode] to
/// [BrowserCredentialsMode.sameOrigin].
@Deprecated('Use credentialsMode instead.')
set withCredentials(bool value) {
credentialsMode = value
? BrowserCredentialsMode.include
: BrowserCredentialsMode.sameOrigin;
}

bool _isClosed = false;
final _openRequestAbortControllers = <AbortController>[];
Expand Down Expand Up @@ -85,7 +153,7 @@ class BrowserClient extends BaseClient {
RequestInit(
method: request.method,
body: bodyBytes.isNotEmpty ? bodyBytes.toJS : null,
credentials: withCredentials ? 'include' : 'same-origin',
credentials: credentialsMode._value,
headers: {
if (request.contentLength case final contentLength?)
'content-length': contentLength,
Expand Down
2 changes: 1 addition & 1 deletion pkgs/http/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: http
version: 1.6.1-wip
version: 1.6.2-wip
description: A composable, multi-platform, Future-based API for HTTP requests.
repository: https://github.com/dart-lang/http/tree/master/pkgs/http

Expand Down