-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
add headers impl #38986
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
add headers impl #38986
Changes from all commits
502e14b
b0f9a75
499c1c6
357ba5d
b34a484
f8f2059
b585716
865d422
c96bf21
e7413b1
4d96cf4
6b726a9
c735d9e
173ccef
d856bd4
bed131e
a87342f
71c1aa2
d5e3df3
c8d156a
8fdd64c
d66e313
1d042f0
0a58d93
a85b1c0
58da701
92b9519
ce73c08
6f212c0
6f06698
ae223ca
1ef66a0
a9a7b4d
3b902a1
1568b7c
cd38842
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 |
---|---|---|
@@ -0,0 +1,243 @@ | ||
# Fetch | ||
|
||
<!--introduced_in=REPLACEME--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
## Class: `fetch.Headers` | ||
|
||
Represents a WHATWG Fetch Spec | ||
[Headers Class](https://fetch.spec.whatwg.org/#headers-class). | ||
|
||
### `new Headers([init])` | ||
|
||
* `init` {Headers | Iterable | string\[] | string\[]\[] | Object} Initial header list to be cloned into the new instance. | ||
|
||
Per spec, other JS primordials can be passed and will fail silently. | ||
|
||
```js | ||
new Headers(); | ||
|
||
new Headers([['name', 'value']]); | ||
|
||
new Headers(['name', 'value']); | ||
|
||
const headers = new Headers({ | ||
name: 'value', | ||
}); | ||
|
||
new Headers(headers); | ||
``` | ||
|
||
### `headers.append(name, value)` | ||
|
||
* `name` {string} | ||
* `value` {string} | ||
|
||
Non-destructive operation for adding header entries. When called multiple times | ||
with the same _name_, the values will be collected in a list and returned | ||
together when retrieved using [Headers.get](#headersgetname). | ||
|
||
```js | ||
const headers = new Headers(); | ||
|
||
headers.append('undici', 'fetch'); | ||
headers.get('undici'); // -> 'fetch' | ||
|
||
headers.append('foobar', 'fuzz'); | ||
headers.append('foobar', 'buzz'); | ||
headers.get('foobar'); // -> 'fuzz, buzz' | ||
``` | ||
|
||
### `headers.delete(name)` | ||
|
||
* `name` {string} | ||
|
||
Removes a header entry. This operation is destructive and cannot be restored. | ||
Does **not** throw an error if the given _name_ does not exist. Reminder that | ||
[Headers.get](#headersgetname) will return `null` if the _name_ does not exist. | ||
|
||
```js | ||
const headers = new Headers(); | ||
|
||
headers.append('undici', 'fetch'); | ||
|
||
headers.get('undici'); // -> 'fetch' | ||
|
||
headers.delete('undici'); | ||
|
||
headers.get('undici'); // -> null | ||
``` | ||
|
||
### `headers.get(name)` | ||
|
||
* `name` {string} | ||
* Returns: {string | null} | ||
|
||
Retrieves a header entry. If the entry _name_ has multiple values, they are | ||
returned as a string joined by `','` characters. If the _name_ does not exist, | ||
this method returns `null`. | ||
|
||
```js | ||
const headers = new Headers(); | ||
|
||
headers.append('undici', 'fetch'); | ||
headers.get('undici'); // -> 'fetch' | ||
|
||
headers.append('foobar', 'fuzz'); | ||
headers.append('foobar', 'buzz'); | ||
headers.get('foobar'); // -> 'fuzz, buzz' | ||
|
||
headers.get('nodejs'); // -> null | ||
``` | ||
|
||
### `headers.has(name)` | ||
|
||
* `name` {string} | ||
* Returns: {boolean} | ||
|
||
Checks for the existence of a given entry _name_. | ||
|
||
```js | ||
const headers = new Headers(); | ||
|
||
headers.append('undici', 'fetch'); | ||
headers.has('undici'); // -> true | ||
``` | ||
|
||
### `headers.set(name, value)` | ||
|
||
* `name` {string} | ||
* `value` {string} | ||
|
||
Destructive operation that will override any existing values for the given entry | ||
_name_. For a non-destructive alternative see | ||
[Headers.append](#headersappendname-value). | ||
|
||
```js | ||
const headers = new Headers(); | ||
|
||
headers.set('foobar', 'fuzz'); | ||
headers.get('foobar'); // -> 'fuzz' | ||
|
||
headers.set('foobar', 'buzz'); | ||
headers.get('foobar'); // -> 'buzz' | ||
``` | ||
|
||
### `headers.values()` | ||
|
||
* Returns: {Iterator} | ||
|
||
Yields a list of header values combined and sorted by their respective keys. | ||
|
||
```js | ||
const headers = new Headers(); | ||
|
||
headers.set('abc', '123'); | ||
headers.set('def', '456'); | ||
headers.set('ghi', '789'); | ||
headers.append('ghi', '012'); | ||
|
||
for (const value of headers.values()) { | ||
console.log(value); | ||
} | ||
|
||
// -> '123' | ||
// -> '456' | ||
// -> '789, 012' | ||
``` | ||
|
||
### `headers.keys()` | ||
|
||
Returns: {Iterator} | ||
|
||
Yields a sorted list of header keys. | ||
|
||
```js | ||
const headers = new Headers(); | ||
|
||
headers.set('abc', '123'); | ||
headers.set('def', '456'); | ||
headers.set('ghi', '789'); | ||
headers.append('ghi', '012'); | ||
|
||
for (const name of headers.keys()) { | ||
console.log(name); | ||
} | ||
|
||
// -> 'abc' | ||
// -> 'def' | ||
// -> 'ghi' | ||
``` | ||
|
||
### `headers.forEach(callback, [thisArg])` | ||
|
||
* `callback` {Function} | ||
* `thisArg` {any} (optional) | ||
|
||
A Headers class can be iterated using `.forEach(callback, [thisArg])`. | ||
|
||
The callback function is passed three arguments, `value: string`, `key: string`, and `iterable: Headers`. | ||
|
||
Optionally a `thisArg` can be passed which will be assigned to the `this` | ||
context of callback. | ||
|
||
The headers are returned in a sorted order, and values are combined on similar | ||
keys. | ||
|
||
```js | ||
const headers = new Headers([['abc', '123']]); | ||
|
||
headers.forEach(function(value, key, headers) { | ||
console.log(key, value); | ||
}); | ||
// -> 'abc', '123' | ||
``` | ||
|
||
### `headers[Symbol.iterator]` | ||
|
||
* Returns: {Iterator} | ||
|
||
A Headers class instance is iterable. It yields each of its entries as a pair | ||
where the first value is the entry _name_ and the second value is the header | ||
_value_. They are sorted by _name_ or otherwise referred to as the header key. | ||
|
||
```js | ||
const headers = new Headers(); | ||
|
||
headers.set('abc', '123'); | ||
headers.set('def', '456'); | ||
headers.set('ghi', '789'); | ||
headers.append('ghi', '012'); | ||
|
||
for (const [name, value] of headers) { | ||
console.log(name, value); | ||
} | ||
|
||
// -> 'abc', '123' | ||
// -> 'def', '456' | ||
// -> 'ghi', '789, 012' | ||
``` | ||
|
||
### `headers.entries()` | ||
|
||
* Returns: {Iterator} | ||
|
||
Yields a list of headers sorted and combined by key. | ||
|
||
```js | ||
const headers = new Headers(); | ||
|
||
headers.set('abc', '123'); | ||
headers.set('def', '456'); | ||
headers.set('ghi', '789'); | ||
headers.append('ghi', '012'); | ||
|
||
for (const entry of headers.entries()) { | ||
console.log(entry); | ||
} | ||
|
||
// -> 'abc', '123' | ||
// -> 'def', '456' | ||
// -> 'ghi', '789, 012' | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
* [Domain](domain.md) | ||
* [Errors](errors.md) | ||
* [Events](events.md) | ||
* [Fetch](fetch.md) | ||
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 am a bit concerned that folks will misinterpret this as being a full fetch implementation... 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. What would be the best way to introduce this without implementing all of fetch? I could make a new |
||
* [File system](fs.md) | ||
* [Globals](globals.md) | ||
* [HTTP](http.md) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const { | ||
emitExperimentalWarning, | ||
} = require('internal/util'); | ||
|
||
emitExperimentalWarning('fetch/headers'); | ||
|
||
const { Headers } = require('internal/fetch/headers'); | ||
|
||
module.exports = { Headers }; |
Uh oh!
There was an error while loading. Please reload this page.