-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
Closed
Ethan-Arrowood
wants to merge
36
commits into
nodejs:master
from
Ethan-Arrowood:feature/fetch-headers
Closed
add headers impl #38986
Changes from 24 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
502e14b
add headers impl
Ethan-Arrowood b0f9a75
update headers and begin porting tests
Ethan-Arrowood 499c1c6
add in progress test script
Ethan-Arrowood 357ba5d
complete test migration
Ethan-Arrowood b34a484
add docs
Ethan-Arrowood f8f2059
fix ordering
Ethan-Arrowood b585716
lint fixes
Ethan-Arrowood 865d422
Update doc/api/fetch.md
Ethan-Arrowood c96bf21
Update lib/internal/fetch/headers.js
Ethan-Arrowood e7413b1
Update lib/internal/fetch/headers.js
Ethan-Arrowood 4d96cf4
Update test/parallel/test-headers.js
Ethan-Arrowood 6b726a9
Update test/parallel/test-headers.js
Ethan-Arrowood c735d9e
use entries for iterator
Ethan-Arrowood 173ccef
lint md
Ethan-Arrowood d856bd4
fix lint again
Ethan-Arrowood bed131e
add missing character
Ethan-Arrowood a87342f
Update doc/api/fetch.md
Ethan-Arrowood 71c1aa2
Update doc/api/fetch.md
Ethan-Arrowood d5e3df3
Update lib/internal/fetch/headers.js
Ethan-Arrowood c8d156a
Update lib/internal/fetch/headers.js
Ethan-Arrowood 8fdd64c
Update lib/internal/fetch/headers.js
Ethan-Arrowood d66e313
fix lint and tests
Ethan-Arrowood 1d042f0
Update lib/internal/fetch/headers.js
Ethan-Arrowood 0a58d93
Update lib/internal/fetch/headers.js
Ethan-Arrowood a85b1c0
Update lib/internal/fetch/headers.js
Ethan-Arrowood 58da701
incorporate review and fix failing test
Ethan-Arrowood 92b9519
export api
Ethan-Arrowood ce73c08
Merge branch 'master' into feature/fetch-headers
Ethan-Arrowood 6f212c0
add inspect and docs
Ethan-Arrowood 6f06698
Update lib/fetch.js
Ethan-Arrowood ae223ca
Update lib/fetch.js
Ethan-Arrowood 1ef66a0
Update lib/internal/fetch/headers.js
Ethan-Arrowood a9a7b4d
Update lib/internal/fetch/headers.js
Ethan-Arrowood 3b902a1
incorporate review changes
Ethan-Arrowood 1568b7c
Merge branch 'master' into feature/fetch-headers
Ethan-Arrowood cd38842
lint fixes
Ethan-Arrowood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
# 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]> | string\[] | Record<string, | ||
string>} Initial header list to be cloned into the new instance. | ||
|
||
```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} | ||
* Returns: {void} | ||
|
||
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: {IteratableIterator<string>} | ||
|
||
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: {IteratableIterator<string>} | ||
|
||
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` {(value: string, key: string, iterable: Headers) => void} | ||
* `thisArg` {any} (optional) | ||
|
||
A Headers class can be iterated using `.forEach(callback, [thisArg])`. | ||
|
||
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<\[string, string]>} | ||
|
||
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: {IteratableIterator<\[string, string]>} | ||
|
||
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' | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.