# bare-fetch

<mark style="{ backgroundColor: '#7dde9a', padding: '2px 8px', borderRadius: '4px', fontSize: '0.85em', fontWeight: 500 }">
  stable
</mark>

[`bare-fetch`](https://github.com/holepunchto/bare-fetch) — WHATWG Fetch implementation for Bare.

```sh
npm i bare-fetch
```

## Usage [#usage]

```js
const fetch = require('bare-fetch')

const res = await fetch('https://example.com/data')

console.log(await res.json())
```

## API [#api]

### Headers [#headers]

#### `Headers.new Headers(init?: Record<string, string> | Iterable<[string, string]>)` [#headersnew-headersinit-recordstring-string--iterablestring-string]

Construct a new `Headers` collection, optionally initialized from `init`.

**Parameters**

| Parameter | Type                                                   | Default | Description                                                                                              |
| --------- | ------------------------------------------------------ | ------- | -------------------------------------------------------------------------------------------------------- |
| `init?`   | `Record<string, string> \| Iterable<[string, string]>` | —       | A plain object of name–value pairs, an iterable of `[name, value]` pairs, or another `Headers` instance. |

#### `Headers.append(name: string, value: string): void` [#headersappendname-string-value-string-void]

Append a value to the header `name`. If the header already exists, the value is added to the existing list.

**Parameters**

| Parameter | Type     | Default | Description                        |
| --------- | -------- | ------- | ---------------------------------- |
| `name`    | `string` | —       | The header name.                   |
| `value`   | `string` | —       | The value to append to the header. |

**Throws**

* `INVALID_HEADER_NAME` — `name` is empty or contains characters that are not valid in a
  header name.
* `INVALID_HEADER_VALUE` — `value` contains a NUL, CR, or LF character.

#### `Headers.delete(name: string): void` [#headersdeletename-string-void]

Delete the header `name`.

**Parameters**

| Parameter | Type     | Default | Description      |
| --------- | -------- | ------- | ---------------- |
| `name`    | `string` | —       | The header name. |

#### `Headers.entries(): IterableIterator<[name: string, value: string]>` [#headersentries-iterableiteratorname-string-value-string]

Return an iterator over `[name, value]` pairs.

#### `Headers.forEach(callback: (value: string, name: string, headers: Headers) => void, thisArg?: any): void` [#headersforeachcallback-value-string-name-string-headers-headers--void-thisarg-any-void]

Call `callback` for each header with the arguments `(value, name, headers)`.

**Parameters**

| Parameter  | Type                                                      | Default | Description                                           |
| ---------- | --------------------------------------------------------- | ------- | ----------------------------------------------------- |
| `callback` | `(value: string, name: string, headers: Headers) => void` | —       | Called with `(value, name, headers)` for each header. |
| `thisArg?` | `any`                                                     | —       | The value of `this` inside `callback`.                |

#### `Headers.get(name: string): string | null` [#headersgetname-string-string--null]

Get the value of the header `name` as a comma-separated string, or `null` if it does not exist.

**Parameters**

| Parameter | Type     | Default | Description      |
| --------- | -------- | ------- | ---------------- |
| `name`    | `string` | —       | The header name. |

#### `Headers.getSetCookie(): string[]` [#headersgetsetcookie-string]

Return each `Set-Cookie` header value separately. Unlike `get('set-cookie')`, this does not
combine the values with commas.

#### `Headers.has(name: string): boolean` [#headershasname-string-boolean]

Return whether the header `name` exists.

**Parameters**

| Parameter | Type     | Default | Description      |
| --------- | -------- | ------- | ---------------- |
| `name`    | `string` | —       | The header name. |

#### `Headers.keys(): IterableIterator<string>` [#headerskeys-iterableiteratorstring]

Return an iterator over header names.

#### `Headers.set(name: string, value: string | string[]): void` [#headerssetname-string-value-string--string-void]

Set the header `name` to `value`, replacing any existing values.

**Parameters**

| Parameter | Type                   | Default | Description                                      |
| --------- | ---------------------- | ------- | ------------------------------------------------ |
| `name`    | `string`               | —       | The header name.                                 |
| `value`   | `string` \| `string[]` | —       | The value to set, replacing any existing values. |

**Throws**

* `INVALID_HEADER_NAME` — `name` is empty or contains characters that are not valid in a
  header name.
* `INVALID_HEADER_VALUE` — `value` contains a NUL, CR, or LF character.

#### `Headers.values(): IterableIterator<string>` [#headersvalues-iterableiteratorstring]

Return an iterator over header values.

### Request [#request]

#### `Request.new Request(input: string | URL | Request, init?: RequestInit)` [#requestnew-requestinput-string--url--request-init-requestinit]

Construct a new `Request` from `input`, which may be a URL string, a `URL`, or another `Request`.

**Parameters**

| Parameter | Type                           | Default | Description                                                   |
| --------- | ------------------------------ | ------- | ------------------------------------------------------------- |
| `input`   | `string` \| `URL` \| `Request` | —       | The URL string, `URL`, or `Request` to base the request on.   |
| `init?`   | `RequestInit`                  | —       | Request options, identical to the ones accepted by `fetch()`. |

**Throws**

* `INVALID_URL` — `input` is not a valid URL.
* `BODY_UNUSABLE` — `init.body` is a `ReadableStream` that is locked or has already been
  consumed.
* `INVALID_REDIRECT` — `init.redirect` is not `'follow'`, `'error'`, or `'manual'`.
* `INVALID_CREDENTIALS` — `init.credentials` is not `'omit'`, `'same-origin'`, or
  `'include'`.

#### `Request.arrayBuffer(): Promise<ArrayBuffer>` [#requestarraybuffer-promisearraybuffer]

Consume the body and return an `ArrayBuffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.body: ReadableStream` [#requestbody-readablestream]

The body as a `ReadableStream`, or `null`.

#### `Request.bodyUsed: boolean` [#requestbodyused-boolean]

Whether the body stream has already been consumed.

#### `Request.buffer(): Promise<Buffer>` [#requestbuffer-promisebuffer]

Consume the body and return a `Buffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.bytes(): Promise<Buffer>` [#requestbytes-promisebuffer]

Consume the body and return a `Uint8Array`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.credentials: RequestCredentials` [#requestcredentials-requestcredentials]

Credential mode metadata. The module has no cookie jar and does not add or remove cookies
based on this value.

#### `Request.formData(): Promise<FormData>` [#requestformdata-promiseformdata]

Consume the body and return a `FormData` object. Supports `multipart/form-data` and `application/x-www-form-urlencoded` content types.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.
* `INVALID_FORM_DATA` — the content type is not form data, or the multipart boundary
  parameter is missing.

#### `Request.headers: Headers` [#requestheaders-headers]

The request headers as a `Headers` object.

#### `Request.json(): Promise<JSON>` [#requestjson-promisejson]

Consume the body and return a parsed JSON value.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.method: HTTPMethod` [#requestmethod-httpmethod]

The request method. Standard methods (`GET`, `POST`, `PUT`, `DELETE`, `HEAD`, `OPTIONS`) are
uppercased automatically.

#### `Request.redirect: RequestRedirect` [#requestredirect-requestredirect]

How fetch handles redirects for this request.

#### `Request.RequestCredentials` [#requestrequestcredentials]

```ts
type RequestCredentials = 'omit' | 'same-origin' | 'include'
```

#### `Request.RequestInit` [#requestrequestinit]

```ts
interface RequestInit {
  body?: unknown
  method?: HTTPMethod
  headers?: Headers
  signal?: AbortSignal
  redirect?: RequestRedirect
  credentials?: RequestCredentials
  agent?: HTTPAgent
}
```

#### `Request.RequestRedirect` [#requestrequestredirect]

```ts
type RequestRedirect = 'error' | 'follow' | 'manual'
```

#### `Request.signal: AbortSignal | null` [#requestsignal-abortsignal--null]

The abort signal associated with the request, or `null`.

#### `Request.text(): Promise<string>` [#requesttext-promisestring]

Consume the body and return a UTF-8 string.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.url: string` [#requesturl-string]

The request URL as a string.

### Response [#response]

#### `Response.new Response(body: unknown, init?: ResponseInit)` [#responsenew-responsebody-unknown-init-responseinit]

Construct a new `Response` wrapping `body`.

**Parameters**

| Parameter | Type           | Default | Description                                                                                     |
| --------- | -------------- | ------- | ----------------------------------------------------------------------------------------------- |
| `body`    | `unknown`      | —       | The response body, or `null` (default `null`).                                                  |
| `init?`   | `ResponseInit` | —       | Options; `status` defaults to `200`, `statusText` to `''`, and `headers` to an empty `Headers`. |

**Throws**

* `BODY_UNUSABLE` — `body` is a `ReadableStream` that is locked or has already been
  consumed.

#### `Response.arrayBuffer(): Promise<ArrayBuffer>` [#responsearraybuffer-promisearraybuffer]

Consume the body and return an `ArrayBuffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.body: ReadableStream` [#responsebody-readablestream]

The body as a `ReadableStream`, or `null`.

#### `Response.bodyUsed: boolean` [#responsebodyused-boolean]

Whether the body stream has already been consumed.

#### `Response.buffer(): Promise<Buffer>` [#responsebuffer-promisebuffer]

Consume the body and return a `Buffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.bytes(): Promise<Buffer>` [#responsebytes-promisebuffer]

Consume the body and return a `Uint8Array`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.formData(): Promise<FormData>` [#responseformdata-promiseformdata]

Consume the body and return a `FormData` object. Supports `multipart/form-data` and `application/x-www-form-urlencoded` content types.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.
* `INVALID_FORM_DATA` — the content type is not form data, or the multipart boundary
  parameter is missing.

#### `Response.headers: Headers` [#responseheaders-headers]

The response headers as a `Headers` object.

#### `Response.ok: boolean` [#responseok-boolean]

Whether the status code is in the range 200-299.

#### `Response.redirected: boolean` [#responseredirected-boolean]

Whether the request was redirected to a different URL.

#### `Response.error(): Response` [#responseerror-response]

#### `Response.json(data: unknown, init?: ResponseInit): Response` [#responsejsondata-unknown-init-responseinit-response]

Consume the body and return a parsed JSON value.

**Parameters**

| Parameter | Type           | Default | Description |
| --------- | -------------- | ------- | ----------- |
| `data`    | `unknown`      | —       | —           |
| `init?`   | `ResponseInit` | —       | —           |

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.redirect(url: string | URL, status?: HTTPStatusCode): Response` [#responseredirecturl-string--url-status-httpstatuscode-response]

**Parameters**

| Parameter | Type              | Default | Description |
| --------- | ----------------- | ------- | ----------- |
| `url`     | `string` \| `URL` | —       | —           |
| `status?` | `HTTPStatusCode`  | —       | —           |

#### `Response.status: HTTPStatusCode` [#responsestatus-httpstatuscode]

The HTTP status code of the response.

#### `Response.statusText: HTTPStatusMessage` [#responsestatustext-httpstatusmessage]

The HTTP status message of the response.

#### `Response.text(): Promise<string>` [#responsetext-promisestring]

Consume the body and return a UTF-8 string.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.type: ResponseType` [#responsetype-responsetype]

#### `Response.url: string | null` [#responseurl-string--null]

The final response URL as a string, or `null` if no request has been made.

### Functions [#functions]

#### `fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>` [#fetchinput-string--url--request-init-requestinit-promiseresponse]

Perform an HTTP or HTTPS request. `input` may be a URL string, a `URL` object, or a `Request`
object. `init` is an optional options object.

**Parameters**

| Parameter | Type                           | Default | Description                                                                                                                 |
| --------- | ------------------------------ | ------- | --------------------------------------------------------------------------------------------------------------------------- |
| `input`   | `string` \| `URL` \| `Request` | —       | The URL string, `URL`, or `Request` to fetch.                                                                               |
| `init?`   | `RequestInit`                  | —       | Request options; `body`, `signal`, and `agent` default to `null`, `method` to `'GET'`, and `headers` to an empty `Headers`. |

**Returns** `Promise<Response>` — A promise that resolves with the `Response` once the response headers arrive.

**Throws**

* `INVALID_URL` — `input` or a redirect `Location` is not a valid URL.
* `UNKNOWN_PROTOCOL` — the URL protocol is neither `http:` nor `https:`.
* `TOO_MANY_REDIRECTS` — more than 20 redirects were followed.
* `NETWORK_ERROR` — the underlying request failed or the connection was lost.

## `bare-fetch/global` [#bare-fetchglobal]

### Types [#types]

#### `Fetch` [#fetch]

```ts
type Fetch = typeof fetch
```

The type of the `fetch` function, used to type the global `fetch`.

## `bare-fetch/errors` [#bare-fetcherrors]

### Classes [#classes]

#### `FetchError` [#fetcherror]

```ts
class FetchError {
  cause: unknown
  code: string
  message: string
  name: string
  stack: string
}
```

An error produced by this module, carrying a `code` identifying the failure.

## `bare-fetch/body` [#bare-fetchbody]

### Body [#body]

#### `Body.arrayBuffer(): Promise<ArrayBuffer>` [#bodyarraybuffer-promisearraybuffer]

Consume the body and return an `ArrayBuffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Body.body: ReadableStream` [#bodybody-readablestream]

The body as a `ReadableStream`, or `null`.

#### `Body.bodyUsed: boolean` [#bodybodyused-boolean]

Whether the body stream has already been consumed.

#### `Body.buffer(): Promise<Buffer>` [#bodybuffer-promisebuffer]

Consume the body and return a `Buffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Body.bytes(): Promise<Buffer>` [#bodybytes-promisebuffer]

Consume the body and return a `Uint8Array`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Body.formData(): Promise<FormData>` [#bodyformdata-promiseformdata]

Consume the body and return a `FormData` object. Supports `multipart/form-data` and `application/x-www-form-urlencoded` content types.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.
* `INVALID_FORM_DATA` — the content type is not form data, or the multipart boundary
  parameter is missing.

#### `Body.json(): Promise<JSON>` [#bodyjson-promisejson]

Consume the body and return a parsed JSON value.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Body.text(): Promise<string>` [#bodytext-promisestring]

Consume the body and return a UTF-8 string.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

## `bare-fetch/request` [#bare-fetchrequest]

### Request [#request-1]

#### `Request.new Request(input: string | URL | Request, init?: RequestInit)` [#requestnew-requestinput-string--url--request-init-requestinit-1]

Construct a new `Request` from `input`, which may be a URL string, a `URL`, or another `Request`.

**Parameters**

| Parameter | Type                           | Default | Description                                                   |
| --------- | ------------------------------ | ------- | ------------------------------------------------------------- |
| `input`   | `string` \| `URL` \| `Request` | —       | The URL string, `URL`, or `Request` to base the request on.   |
| `init?`   | `RequestInit`                  | —       | Request options, identical to the ones accepted by `fetch()`. |

**Throws**

* `INVALID_URL` — `input` is not a valid URL.
* `BODY_UNUSABLE` — `init.body` is a `ReadableStream` that is locked or has already been
  consumed.
* `INVALID_REDIRECT` — `init.redirect` is not `'follow'`, `'error'`, or `'manual'`.
* `INVALID_CREDENTIALS` — `init.credentials` is not `'omit'`, `'same-origin'`, or
  `'include'`.

#### `Request.arrayBuffer(): Promise<ArrayBuffer>` [#requestarraybuffer-promisearraybuffer-1]

Consume the body and return an `ArrayBuffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.body: ReadableStream` [#requestbody-readablestream-1]

The body as a `ReadableStream`, or `null`.

#### `Request.bodyUsed: boolean` [#requestbodyused-boolean-1]

Whether the body stream has already been consumed.

#### `Request.buffer(): Promise<Buffer>` [#requestbuffer-promisebuffer-1]

Consume the body and return a `Buffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.bytes(): Promise<Buffer>` [#requestbytes-promisebuffer-1]

Consume the body and return a `Uint8Array`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.credentials: RequestCredentials` [#requestcredentials-requestcredentials-1]

Credential mode metadata. The module has no cookie jar and does not add or remove cookies
based on this value.

#### `Request.formData(): Promise<FormData>` [#requestformdata-promiseformdata-1]

Consume the body and return a `FormData` object. Supports `multipart/form-data` and `application/x-www-form-urlencoded` content types.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.
* `INVALID_FORM_DATA` — the content type is not form data, or the multipart boundary
  parameter is missing.

#### `Request.headers: Headers` [#requestheaders-headers-1]

The request headers as a `Headers` object.

#### `Request.json(): Promise<JSON>` [#requestjson-promisejson-1]

Consume the body and return a parsed JSON value.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.method: HTTPMethod` [#requestmethod-httpmethod-1]

The request method. Standard methods (`GET`, `POST`, `PUT`, `DELETE`, `HEAD`, `OPTIONS`) are
uppercased automatically.

#### `Request.redirect: RequestRedirect` [#requestredirect-requestredirect-1]

How fetch handles redirects for this request.

#### `Request.RequestCredentials` [#requestrequestcredentials-1]

```ts
type RequestCredentials = 'omit' | 'same-origin' | 'include'
```

#### `Request.RequestRedirect` [#requestrequestredirect-1]

```ts
type RequestRedirect = 'error' | 'follow' | 'manual'
```

#### `Request.signal: AbortSignal | null` [#requestsignal-abortsignal--null-1]

The abort signal associated with the request, or `null`.

#### `Request.text(): Promise<string>` [#requesttext-promisestring-1]

Consume the body and return a UTF-8 string.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Request.url: string` [#requesturl-string-1]

The request URL as a string.

### Types [#types-1]

#### `RequestInit` [#requestinit]

```ts
interface RequestInit {
  body?: unknown
  method?: HTTPMethod
  headers?: Headers
  signal?: AbortSignal
  redirect?: RequestRedirect
  credentials?: RequestCredentials
  agent?: HTTPAgent
}
```

## `bare-fetch/response` [#bare-fetchresponse]

### Response [#response-1]

#### `Response.new Response(body: unknown, init?: ResponseInit)` [#responsenew-responsebody-unknown-init-responseinit-1]

Construct a new `Response` wrapping `body`.

**Parameters**

| Parameter | Type           | Default | Description                                                                                     |
| --------- | -------------- | ------- | ----------------------------------------------------------------------------------------------- |
| `body`    | `unknown`      | —       | The response body, or `null` (default `null`).                                                  |
| `init?`   | `ResponseInit` | —       | Options; `status` defaults to `200`, `statusText` to `''`, and `headers` to an empty `Headers`. |

**Throws**

* `BODY_UNUSABLE` — `body` is a `ReadableStream` that is locked or has already been
  consumed.

#### `Response.arrayBuffer(): Promise<ArrayBuffer>` [#responsearraybuffer-promisearraybuffer-1]

Consume the body and return an `ArrayBuffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.body: ReadableStream` [#responsebody-readablestream-1]

The body as a `ReadableStream`, or `null`.

#### `Response.bodyUsed: boolean` [#responsebodyused-boolean-1]

Whether the body stream has already been consumed.

#### `Response.buffer(): Promise<Buffer>` [#responsebuffer-promisebuffer-1]

Consume the body and return a `Buffer`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.bytes(): Promise<Buffer>` [#responsebytes-promisebuffer-1]

Consume the body and return a `Uint8Array`.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.formData(): Promise<FormData>` [#responseformdata-promiseformdata-1]

Consume the body and return a `FormData` object. Supports `multipart/form-data` and `application/x-www-form-urlencoded` content types.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.
* `INVALID_FORM_DATA` — the content type is not form data, or the multipart boundary
  parameter is missing.

#### `Response.headers: Headers` [#responseheaders-headers-1]

The response headers as a `Headers` object.

#### `Response.ok: boolean` [#responseok-boolean-1]

Whether the status code is in the range 200-299.

#### `Response.redirected: boolean` [#responseredirected-boolean-1]

Whether the request was redirected to a different URL.

#### `Response.error(): Response` [#responseerror-response-1]

#### `Response.json(data: unknown, init?: ResponseInit): Response` [#responsejsondata-unknown-init-responseinit-response-1]

Consume the body and return a parsed JSON value.

**Parameters**

| Parameter | Type           | Default | Description |
| --------- | -------------- | ------- | ----------- |
| `data`    | `unknown`      | —       | —           |
| `init?`   | `ResponseInit` | —       | —           |

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.redirect(url: string | URL, status?: HTTPStatusCode): Response` [#responseredirecturl-string--url-status-httpstatuscode-response-1]

**Parameters**

| Parameter | Type              | Default | Description |
| --------- | ----------------- | ------- | ----------- |
| `url`     | `string` \| `URL` | —       | —           |
| `status?` | `HTTPStatusCode`  | —       | —           |

#### `Response.status: HTTPStatusCode` [#responsestatus-httpstatuscode-1]

The HTTP status code of the response.

#### `Response.statusText: HTTPStatusMessage` [#responsestatustext-httpstatusmessage-1]

The HTTP status message of the response.

#### `Response.text(): Promise<string>` [#responsetext-promisestring-1]

Consume the body and return a UTF-8 string.

**Throws**

* `BODY_UNUSABLE` — the body has already been consumed.

#### `Response.type: ResponseType` [#responsetype-responsetype-1]

#### `Response.url: string | null` [#responseurl-string--null-1]

The final response URL as a string, or `null` if no request has been made.

### Types [#types-2]

#### `ResponseInit` [#responseinit]

```ts
interface ResponseInit {
  status?: HTTPStatusCode
  statusText?: HTTPStatusMessage
  headers?: Headers
}
```

## `bare-fetch/headers` [#bare-fetchheaders]

### Headers [#headers-1]

#### `Headers.new Headers(init?: Record<string, string> | Iterable<[string, string]>)` [#headersnew-headersinit-recordstring-string--iterablestring-string-1]

Construct a new `Headers` collection, optionally initialized from `init`.

**Parameters**

| Parameter | Type                                                   | Default | Description                                                                                              |
| --------- | ------------------------------------------------------ | ------- | -------------------------------------------------------------------------------------------------------- |
| `init?`   | `Record<string, string> \| Iterable<[string, string]>` | —       | A plain object of name–value pairs, an iterable of `[name, value]` pairs, or another `Headers` instance. |

#### `Headers.append(name: string, value: string): void` [#headersappendname-string-value-string-void-1]

Append a value to the header `name`. If the header already exists, the value is added to the existing list.

**Parameters**

| Parameter | Type     | Default | Description                        |
| --------- | -------- | ------- | ---------------------------------- |
| `name`    | `string` | —       | The header name.                   |
| `value`   | `string` | —       | The value to append to the header. |

**Throws**

* `INVALID_HEADER_NAME` — `name` is empty or contains characters that are not valid in a
  header name.
* `INVALID_HEADER_VALUE` — `value` contains a NUL, CR, or LF character.

#### `Headers.delete(name: string): void` [#headersdeletename-string-void-1]

Delete the header `name`.

**Parameters**

| Parameter | Type     | Default | Description      |
| --------- | -------- | ------- | ---------------- |
| `name`    | `string` | —       | The header name. |

#### `Headers.entries(): IterableIterator<[name: string, value: string]>` [#headersentries-iterableiteratorname-string-value-string-1]

Return an iterator over `[name, value]` pairs.

#### `Headers.forEach(callback: (value: string, name: string, headers: Headers) => void, thisArg?: any): void` [#headersforeachcallback-value-string-name-string-headers-headers--void-thisarg-any-void-1]

Call `callback` for each header with the arguments `(value, name, headers)`.

**Parameters**

| Parameter  | Type                                                      | Default | Description                                           |
| ---------- | --------------------------------------------------------- | ------- | ----------------------------------------------------- |
| `callback` | `(value: string, name: string, headers: Headers) => void` | —       | Called with `(value, name, headers)` for each header. |
| `thisArg?` | `any`                                                     | —       | The value of `this` inside `callback`.                |

#### `Headers.get(name: string): string | null` [#headersgetname-string-string--null-1]

Get the value of the header `name` as a comma-separated string, or `null` if it does not exist.

**Parameters**

| Parameter | Type     | Default | Description      |
| --------- | -------- | ------- | ---------------- |
| `name`    | `string` | —       | The header name. |

#### `Headers.getSetCookie(): string[]` [#headersgetsetcookie-string-1]

Return each `Set-Cookie` header value separately. Unlike `get('set-cookie')`, this does not
combine the values with commas.

#### `Headers.has(name: string): boolean` [#headershasname-string-boolean-1]

Return whether the header `name` exists.

**Parameters**

| Parameter | Type     | Default | Description      |
| --------- | -------- | ------- | ---------------- |
| `name`    | `string` | —       | The header name. |

#### `Headers.keys(): IterableIterator<string>` [#headerskeys-iterableiteratorstring-1]

Return an iterator over header names.

#### `Headers.set(name: string, value: string | string[]): void` [#headerssetname-string-value-string--string-void-1]

Set the header `name` to `value`, replacing any existing values.

**Parameters**

| Parameter | Type                   | Default | Description                                      |
| --------- | ---------------------- | ------- | ------------------------------------------------ |
| `name`    | `string`               | —       | The header name.                                 |
| `value`   | `string` \| `string[]` | —       | The value to set, replacing any existing values. |

**Throws**

* `INVALID_HEADER_NAME` — `name` is empty or contains characters that are not valid in a
  header name.
* `INVALID_HEADER_VALUE` — `value` contains a NUL, CR, or LF character.

#### `Headers.values(): IterableIterator<string>` [#headersvalues-iterableiteratorstring-1]

Return an iterator over header values.

## See also [#see-also]

* Builds on `bare-form-data`, `bare-http1`, `bare-https`, `bare-mime`, `bare-performance`, `bare-stream`, `bare-url`, and `bare-zlib`.
* The `fetch.Request`, `fetch.Response`, and `fetch.Headers` constructors are also attached to the function.
* It's pure JavaScript, built on [`bare-http1`](/bare/reference/bare/modules) and `bare-https`.
* [Bare modules](/bare/reference/bare/modules) — the full `bare-*` catalog.
* [Bare runtime API](/bare/reference/bare/runtime) — the runtime these modules extend.