Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
502e14b
add headers impl
Ethan-Arrowood Jun 10, 2021
b0f9a75
update headers and begin porting tests
Ethan-Arrowood Jun 10, 2021
499c1c6
add in progress test script
Ethan-Arrowood Jun 15, 2021
357ba5d
complete test migration
Ethan-Arrowood Jun 21, 2021
b34a484
add docs
Ethan-Arrowood Jun 21, 2021
f8f2059
fix ordering
Ethan-Arrowood Jun 21, 2021
b585716
lint fixes
Ethan-Arrowood Jun 21, 2021
865d422
Update doc/api/fetch.md
Ethan-Arrowood Jun 21, 2021
c96bf21
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
e7413b1
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
4d96cf4
Update test/parallel/test-headers.js
Ethan-Arrowood Jun 21, 2021
6b726a9
Update test/parallel/test-headers.js
Ethan-Arrowood Jun 21, 2021
c735d9e
use entries for iterator
Ethan-Arrowood Jun 21, 2021
173ccef
lint md
Ethan-Arrowood Jun 21, 2021
d856bd4
fix lint again
Ethan-Arrowood Jun 21, 2021
bed131e
add missing character
Ethan-Arrowood Jun 21, 2021
a87342f
Update doc/api/fetch.md
Ethan-Arrowood Jun 21, 2021
71c1aa2
Update doc/api/fetch.md
Ethan-Arrowood Jun 21, 2021
d5e3df3
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
c8d156a
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
8fdd64c
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
d66e313
fix lint and tests
Ethan-Arrowood Jun 21, 2021
1d042f0
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
0a58d93
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
a85b1c0
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jul 22, 2021
58da701
incorporate review and fix failing test
Ethan-Arrowood Jul 22, 2021
92b9519
export api
Ethan-Arrowood Jul 22, 2021
ce73c08
Merge branch 'master' into feature/fetch-headers
Ethan-Arrowood Jul 22, 2021
6f212c0
add inspect and docs
Ethan-Arrowood Jul 22, 2021
6f06698
Update lib/fetch.js
Ethan-Arrowood Jul 26, 2021
ae223ca
Update lib/fetch.js
Ethan-Arrowood Jul 26, 2021
1ef66a0
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jul 26, 2021
a9a7b4d
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jul 26, 2021
3b902a1
incorporate review changes
Ethan-Arrowood Jul 26, 2021
1568b7c
Merge branch 'master' into feature/fetch-headers
Ethan-Arrowood Jul 26, 2021
cd38842
lint fixes
Ethan-Arrowood Jul 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 241 additions & 0 deletions doc/api/fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
# Fetch

Ethan-Arrowood marked this conversation as resolved.
Show resolved Hide resolved
<!--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'
```
Loading