11import '_js.dart' ;
22import 'iterator.dart' as js;
33import '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
831class 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+ }
0 commit comments