|
1 | | -import '_js.dart'; |
2 | | -import 'iterator.dart' as js; |
3 | | -import 'iterator_wrapper.dart'; |
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. |
29 | | -@JS() |
30 | | -@staticInterop |
31 | | -class Headers { |
32 | | - /// Creates a new [Headers] object. |
33 | | - @JS('Headers') |
34 | | - external factory Headers(); |
35 | | - |
36 | | - /// Creates a new [Headers] object. |
37 | | - @JS('Headers') |
38 | | - 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); |
49 | | -} |
50 | | - |
51 | | -extension HeadersInstanceMembers on Headers { |
52 | | - /// Appends a new value onto an existing header inside a [Headers] object, |
53 | | - /// or adds the header if it does not already exist. |
54 | | - external void append(String name, String value); |
55 | | - |
56 | | - /// Deletes a header from a [Headers] object. |
57 | | - external void delete(String name); |
58 | | - |
59 | | - /// Returns an [js.Iterator] allowing to go through all key/value pairs |
60 | | - /// contained in this object. |
61 | | - @JS('entries') |
62 | | - external js.Iterator<List<String>> _entries(); |
63 | | - |
64 | | - // forEach() |
65 | | - |
66 | | - /// Returns a [String] sequence of all the values of a header within |
67 | | - /// a [Headers] object with a given name. |
68 | | - external String? get(String name); |
69 | | - |
70 | | - /// Returns a [bool] stating whether a [Headers] object contains |
71 | | - /// a certain header. |
72 | | - external bool has(String name); |
73 | | - |
74 | | - /// Returns an [js.Iterator] allowing you to go through all keys of |
75 | | - /// the key/value pairs contained in this object. |
76 | | - @JS('keys') |
77 | | - external js.Iterator<String> _keys(); |
78 | | - |
79 | | - /// Sets a new value for an existing header inside a [Headers] object, |
80 | | - /// or adds the header if it does not already exist. |
81 | | - external void set(String name, String value); |
82 | | - |
83 | | - /// Returns an [js.Iterator] allowing you to go through all values of |
84 | | - /// the key/value pairs contained in this object. |
85 | | - @JS('values') |
86 | | - external js.Iterator<String> _values(); |
87 | | - |
88 | | - /// Returns an [IteratorWrapper] allowing to go through all key/value pairs |
89 | | - /// contained in this object. |
90 | | - IteratorWrapper<List<String>> entries() => IteratorWrapper(_entries()); |
91 | | - |
92 | | - /// Returns an [IteratorWrapper] allowing you to go through all keys of the |
93 | | - /// key/value pairs contained in this object. |
94 | | - IteratorWrapper<String> keys() => IteratorWrapper(_keys()); |
95 | | - |
96 | | - /// Returns an [IteratorWrapper] allowing you to go through all values of |
97 | | - /// the key/value pairs contained in this object. |
98 | | - IteratorWrapper<String> values() => IteratorWrapper(_values()); |
99 | | -} |
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 | | -} |
| 1 | +export 'headers/headers.dart' hide createHeadersFromArray, createHeadersFromMap; |
0 commit comments