Skip to content

Commit 5cb7445

Browse files
committed
feat!: 1.0.0-dev.3
1 parent 334d17c commit 5cb7445

15 files changed

Lines changed: 199 additions & 46 deletions

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
## 1.0.0-dev.3
2+
- **BREAKING**: Renamed extensions from `<Class>Extension` to `<Class>InstanceMembers`.
3+
- `AbortSignal`
4+
- Added `timeout` constructor-like method.
5+
- Added `abort` constructor-like method.
6+
- Added `aborted` property.
7+
- Added `reason` property.
8+
- Added `throwIfAborted` method.
9+
- `AbortController`
10+
- Added docs.
11+
- `fetch()` added docs.
12+
- `Response`
13+
- Fixed internal definition for `_formData` method.
14+
- Added docs.
15+
- `Headers`
16+
- Added docs.
17+
- Add methods `headersFromMap` and `headersFromArray` to support Dart 2.19.
18+
- `Iterator` and `IteratorResult`
19+
- Added docs.
20+
- `ReadableStreamDefaultReader` and `ReadableStreamDefaultReaderChunk`
21+
- Added docs.
22+
123
## 1.0.0-dev.2
224

325
- Downgraded `js` dependency to `^0.6.5`.

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Fetch API.
22

3+
> ⚠️ Requires Dart 2.19 or higher.
4+
35
This package provides JavaScript bindings to [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
46

57
## Features
68

79
* Full request parameters coverage
8-
* Abort requests
10+
* Support canceling requests
911
* Read response
1012
* As text
11-
* As Blob
12-
* As Stream of Uint8List
13+
* As `Blob`
14+
* As `Stream` of `Uint8List`
1315
* Support streaming of data
1416
* Get access to redirect status
15-
* Support non-200 responses
17+
* Support non-`200` responses

lib/src/abort_controller.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ import '_js.dart';
22
import 'abort_signal.dart';
33

44

5+
/// The [AbortController] interface represents a controller object that allows
6+
/// you to abort one or more Web requests as and when desired.
7+
///
8+
/// You can create a new [AbortController] object using the `AbortController()`
9+
/// constructor. Communicating with a DOM request is done using an [AbortSignal]
10+
/// object.
511
@JS()
612
@staticInterop
713
class AbortController {
814
/// Creates a new [AbortController] object instance.
915
external factory AbortController();
1016
}
1117

12-
extension AbortControllerExtension on AbortController {
13-
/// Returns an `AbortSignal` object instance, which can be used to
18+
extension AbortControllerInstanceMembers on AbortController {
19+
/// Returns an [AbortSignal] object instance, which can be used to
1420
/// communicate with, or to abort, a DOM request.
1521
external final AbortSignal signal;
1622

lib/src/abort_signal.dart

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
import '_js.dart';
2+
import 'abort_controller.dart';
23

34

5+
/// The [AbortSignal] interface represents a signal object that allows you
6+
/// to communicate with a DOM request (such as a fetch request) and abort it
7+
/// if required via an [AbortController] object.
48
@JS()
59
@staticInterop
610
class AbortSignal {
7-
external factory AbortSignal();
11+
/// Returns an [AbortSignal] instance that will automatically abort
12+
/// after a specified [time].
13+
factory AbortSignal.timeout(Duration time)
14+
=> AbortSignal._timeout(time.inMilliseconds);
15+
16+
/// Returns an [AbortSignal] instance that is already set as aborted.
17+
external static AbortSignal abort([dynamic reason]);
18+
19+
/// Returns an [AbortSignal] instance that will automatically abort
20+
/// after a specified [time] in milliseconds.
21+
@JS('timeout')
22+
external static AbortSignal _timeout(int time);
23+
}
24+
25+
26+
extension AbortSignalInstanceMembers on AbortSignal {
27+
/// A [bool] that indicates whether the request(s) the signal is
28+
/// communicating with is/are aborted (`true`) or not (`false`).
29+
external final bool aborted;
30+
31+
/// A JavaScript value providing the abort reason, once the signal has aborted.
32+
external final dynamic reason;
33+
34+
/// Throws the signal's abort [reason] if the signal has been aborted;
35+
/// otherwise it does nothing.
36+
external void throwIfAborted();
837
}

lib/src/fetch.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,25 @@ import 'response.dart';
66
@JS('fetch')
77
external Promise<Response> _fetch(dynamic resource, RequestInit? options);
88

9+
/// The global `fetch()` method starts the process of fetching a resource
10+
/// from the network, returning a promise which is fulfilled once the response
11+
/// is available.
12+
///
13+
/// The promise resolves to the [Response] object representing the response
14+
/// to your request.
15+
///
16+
/// A `fetch()` promise only rejects when a network error is encountered
17+
/// (which is usually when there's a permissions issue or similar).
18+
/// A `fetch()` promise does not reject on HTTP errors (`404`, etc.).
19+
/// Instead, a then() handler must check the [ResponseInstanceMembers.ok] and/or
20+
/// [ResponseInstanceMembers.status] properties.
21+
///
22+
/// `WindowOrWorkerGlobalScope` is implemented by both `Window` and
23+
/// `WorkerGlobalScope`, which means that the `fetch()` method is available
24+
/// in pretty much any context in which you might want to fetch resources.
25+
///
26+
/// The `fetch()` method is controlled by the `connect-src` directive of
27+
/// Content Security Policy rather than the directive of the resources
28+
/// it's retrieving.
929
Future<Response> fetch(dynamic resource, [RequestInit? options]) =>
1030
promiseToFuture(_fetch(resource, options));

lib/src/headers.dart

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,63 @@
11
import '_js.dart';
22
import 'iterator.dart' as js;
33
import 'iterator_wrapper.dart';
4-
5-
4+
import 'response.dart';
5+
6+
7+
/// The [Headers] interface of the Fetch API allows you to perform various
8+
/// actions on HTTP request and response headers. These actions include
9+
/// retrieving, setting, adding to, and removing headers from the list of
10+
/// the request's headers.
11+
///
12+
/// A [Headers] object has an associated header list, which is initially empty
13+
/// and consists of zero or more name and value pairs. You can add to this using
14+
/// methods like `append()`. In all methods of this interface, header names are
15+
/// matched by case-insensitive byte sequence.
16+
///
17+
/// For security reasons, some headers can only be controlled by the user agent.
18+
/// These headers include the forbidden header names and forbidden response
19+
/// header names.
20+
///
21+
/// A [Headers] object also has an associated guard, which takes
22+
/// a value of `immutable`, `request`, `request-no-cors`, `response`, or `none`.
23+
/// This affects whether the `set()`, `delete()`, and `append()` methods will
24+
/// mutate the header.
25+
///
26+
/// You can retrieve a [Headers] object via the `Request.headers` and
27+
/// [ResponseInstanceMembers.headers] properties, and create
28+
/// a new [Headers] object using the `Headers()` constructor.
629
@JS()
730
@staticInterop
831
class Headers {
9-
/// Creates empty [Headers].
10-
factory Headers() => Headers._();
11-
12-
/// Creates [Headers] from [Map].
13-
factory Headers.fromMap(Map<String, String> init) =>
14-
Headers._init(init.toJsObject());
15-
16-
/// Creates [Headers] from array of 2 items arrays.
17-
factory Headers.fromArray(List<List<String>> init) {
18-
final _init = JsArray<JsArray<dynamic>>();
19-
for (final header in init) {
20-
if (header.length != 2)
21-
throw Exception('Bad argument');
22-
23-
_init.add(header.toJsArray());
24-
}
25-
return Headers._init(_init);
26-
}
27-
2832
/// Creates a new [Headers] object.
2933
@JS('Headers')
30-
external factory Headers._();
34+
external factory Headers();
3135

3236
/// Creates a new [Headers] object.
3337
@JS('Headers')
3438
external factory Headers._init(dynamic init);
39+
40+
/// Warning: available only with Dart 3.0 or higher.
41+
/// Creates [Headers] from [Map].
42+
factory Headers.fromMap(Map<String, String> init) =>
43+
headersFromMap(init);
44+
45+
/// Warning: available only with Dart 3.0 or higher.
46+
/// Creates [Headers] from array of 2 items arrays.
47+
factory Headers.fromArray(List<List<String>> init) =>
48+
headersFromArray(init);
3549
}
3650

37-
extension HeadersExtension on Headers {
51+
extension HeadersInstanceMembers on Headers {
3852
/// Appends a new value onto an existing header inside a [Headers] object,
3953
/// or adds the header if it does not already exist.
4054
external void append(String name, String value);
4155

4256
/// Deletes a header from a [Headers] object.
4357
external void delete(String name);
4458

45-
/// Returns an `iterator` allowing to go through all key/value pairs contained
46-
/// in this object.
59+
/// Returns an [js.Iterator] allowing to go through all key/value pairs
60+
/// contained in this object.
4761
@JS('entries')
4862
external js.Iterator<List<String>> _entries();
4963

@@ -57,16 +71,16 @@ extension HeadersExtension on Headers {
5771
/// a certain header.
5872
external bool has(String name);
5973

60-
/// Returns an `iterator` allowing you to go through all keys of the key/value
61-
/// pairs contained in this object.
74+
/// Returns an [js.Iterator] allowing you to go through all keys of
75+
/// the key/value pairs contained in this object.
6276
@JS('keys')
6377
external js.Iterator<String> _keys();
6478

6579
/// Sets a new value for an existing header inside a [Headers] object,
6680
/// or adds the header if it does not already exist.
6781
external void set(String name, String value);
6882

69-
/// Returns an `iterator` allowing you to go through all values of
83+
/// Returns an [js.Iterator] allowing you to go through all values of
7084
/// the key/value pairs contained in this object.
7185
@JS('values')
7286
external js.Iterator<String> _values();
@@ -83,3 +97,19 @@ extension HeadersExtension on Headers {
8397
/// the key/value pairs contained in this object.
8498
IteratorWrapper<String> values() => IteratorWrapper(_values());
8599
}
100+
101+
/// Creates [Headers] from [Map].
102+
Headers headersFromMap(Map<String, String> init) =>
103+
Headers._init(init.toJsObject());
104+
105+
/// Creates [Headers] from array of 2 items arrays.
106+
Headers headersFromArray(List<List<String>> init) {
107+
final _init = JsArray<JsArray<dynamic>>();
108+
for (final header in init) {
109+
if (header.length != 2)
110+
throw Exception('Bad argument');
111+
112+
_init.add(header.toJsArray());
113+
}
114+
return Headers._init(_init);
115+
}

lib/src/iterator.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ import '_js.dart';
22
import 'iterator_result.dart';
33

44

5+
/// The iterator protocol defines a standard way to produce a sequence of values
6+
/// (either finite or infinite), and potentially a return value when all values
7+
/// have been generated.
8+
///
9+
/// All iterator protocol methods (`next()`, `return()`, and `throw()`) are
10+
/// expected to return an object implementing the [IteratorResult] interface.
511
@JS()
612
@anonymous
713
class Iterator<T> {
814
/// A function that accepts zero or one argument and returns an object
915
/// conforming to the [IteratorResult] interface.
1016
///
11-
/// If a non-object value gets returned (such as false or undefined)
12-
/// when a built-in language feature (such as for...of) is using the iterator,
13-
/// a TypeError ("iterator.next() returned a non-object value") will be thrown.
17+
/// If a non-object value gets returned (such as `false` or `undefined`)
18+
/// when a built-in language feature (such as `for...of`) is using
19+
/// the iterator, a `TypeError` (`"iterator.next() returned a non-object
20+
/// value"`) will be thrown.
1421
external IteratorResult<T> next();
1522
}

lib/src/iterator_result.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import '_js.dart';
22

33

4+
/// In practice, neither property is strictly required; if an object without
5+
/// either property is returned, it's effectively equivalent to
6+
/// `{ done: false, value: undefined }`.
7+
///
8+
/// If an iterator returns a result with `done: true`, any subsequent calls
9+
/// to `next()` are expected to return `done: true` as well, although
10+
/// this is not enforced on the language level.
411
@JS()
512
@anonymous
613
class IteratorResult<T> {

lib/src/iterator_wrapper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'dart:core' as core;
44
import 'iterator.dart' as js;
55

66

7-
/// Wrapper on top of JS iterator, providing [Iterable] and [Iterator] APIs.
7+
/// Wrapper on top of [js.Iterator], implementing [Iterable] and [Iterator] APIs.
88
class IteratorWrapper<T> extends IterableBase<T> implements core.Iterator<T> {
99
IteratorWrapper(this._iterator);
1010

lib/src/readable_stream.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import '_js.dart';
22
import 'readable_stream_default_reader.dart';
3+
import 'response.dart';
34

45

6+
/// The [ReadableStream] interface of the Streams API represents a readable
7+
/// stream of byte data. The Fetch API offers a concrete instance of
8+
/// a [ReadableStream] through the body property of a [Response] object.
59
@JS()
610
@staticInterop
711
class ReadableStream {
@@ -12,7 +16,7 @@ class ReadableStream {
1216
]);
1317
}
1418

15-
extension ReadableStreamExtension on ReadableStream {
19+
extension ReadableStreamInstanceMembers on ReadableStream {
1620
/// Returns a [bool] indicating whether or not the readable stream
1721
/// is locked to a reader.
1822
external final bool locked;

0 commit comments

Comments
 (0)