bare-fetch
WHATWG Fetch implementation for Bare
bare-fetch — WHATWG Fetch implementation for Bare.
npm i bare-fetchUsage
const fetch = require('bare-fetch')
const res = await fetch('https://example.com/data')
console.log(await res.json())API
Headers
Headers.new Headers(init?: Record<string, string> | Iterable<[string, string]>)
Source
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
Source
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—nameis empty or contains characters that are not valid in a header name.INVALID_HEADER_VALUE—valuecontains a NUL, CR, or LF character.
Headers.delete(name: string): void
Source
Delete the header name.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
Headers.entries(): IterableIterator<[name: string, value: string]>
Source
Return an iterator over [name, value] pairs.
Headers.forEach(callback: (value: string, name: string, headers: Headers) => void, thisArg?: any): void
Source
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
Source
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.has(name: string): boolean
Source
Return whether the header name exists.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
Headers.keys(): IterableIterator<string>
Source
Return an iterator over header names.
Headers.set(name: string, value: string): void
Source
Set the header name to value, replacing any existing values.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
value | string | — | The value to set, replacing any existing values. |
Throws
INVALID_HEADER_NAME—nameis empty or contains characters that are not valid in a header name.INVALID_HEADER_VALUE—valuecontains a NUL, CR, or LF character.
Headers.values(): IterableIterator<string>
Source
Return an iterator over header values.
Request
Request.new Request(input: string | URL | Request, init?: RequestInit)
Source
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—inputis not a valid URL.BODY_UNUSABLE—init.bodyis aReadableStreamthat is locked or has already been consumed.
Request.headers: Headers
Source
The request headers as a Headers object.
Request.method: HTTPMethod
Source
The request method. Standard methods (GET, POST, PUT, DELETE, HEAD, OPTIONS) are uppercased automatically.
Request.RequestInit
interface RequestInit {
body?: unknown
method?: HTTPMethod
headers?: Headers
signal?: AbortSignal
agent?: HTTPAgent
}Request.signal: AbortSignal | null
Source
The abort signal associated with the request, or null.
Request.url: string
Source
The request URL as a string.
Response
Response.new Response(body: unknown, init?: ResponseInit)
Source
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—bodyis aReadableStreamthat is locked or has already been consumed.
Response.headers: Headers
Source
The response headers as a Headers object.
Response.ok: boolean
Source
Whether the status code is in the range 200-299.
Response.redirected: boolean
Source
Whether the request was redirected to a different URL.
Response.Response.error(): Response
Source
Response.Response.json(data: unknown, init?: ResponseInit): Response
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown | — | — |
init? | ResponseInit | — | — |
Response.Response.redirect(url: string | URL, status?: HTTPStatusCode): Response
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | URL | — | — |
status? | HTTPStatusCode | — | — |
Response.status: HTTPStatusCode
Source
The HTTP status code of the response.
Response.statusText: HTTPStatusMessage
Source
The HTTP status message of the response.
Response.type: ResponseType
Source
Response.url: string | null
Source
The final response URL as a string, or null if no request has been made.
Functions
fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>
Source
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—inputor a redirectLocationis not a valid URL.UNKNOWN_PROTOCOL— the URL protocol is neitherhttp:norhttps:.TOO_MANY_REDIRECTS— more than 20 redirects were followed.NETWORK_ERROR— the underlying request failed or the connection was lost.
bare-fetch/global
Types
Fetch
type Fetch = typeof fetchThe type of the fetch function, used to type the global fetch.
bare-fetch/errors
Classes
FetchError
Source
class FetchError {
code: string
}bare-fetch/body
Body
arrayBuffer(): Promise<ArrayBuffer>
Source
Consume the body and return an ArrayBuffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
body: ReadableStream
Source
The body as a ReadableStream, or null.
bodyUsed: boolean
Source
Whether the body stream has already been consumed.
buffer(): Promise<Buffer>
Source
Consume the body and return a Buffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
bytes(): Promise<Buffer>
Source
Consume the body and return a Uint8Array.
Throws
BODY_UNUSABLE— the body has already been consumed.
formData(): Promise<FormData>
Source
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.
json(): Promise<JSON>
Source
Consume the body and return a parsed JSON value.
Throws
BODY_UNUSABLE— the body has already been consumed.
text(): Promise<string>
Source
Consume the body and return a UTF-8 string.
Throws
BODY_UNUSABLE— the body has already been consumed.
bare-fetch/request
Request
Request.new Request(input: string | URL | Request, init?: RequestInit)
Source
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—inputis not a valid URL.BODY_UNUSABLE—init.bodyis aReadableStreamthat is locked or has already been consumed.
Request.headers: Headers
Source
The request headers as a Headers object.
Request.method: HTTPMethod
Source
The request method. Standard methods (GET, POST, PUT, DELETE, HEAD, OPTIONS) are uppercased automatically.
Request.signal: AbortSignal | null
Source
The abort signal associated with the request, or null.
Request.url: string
Source
The request URL as a string.
Types
RequestInit
interface RequestInit {
body?: unknown
method?: HTTPMethod
headers?: Headers
signal?: AbortSignal
agent?: HTTPAgent
}bare-fetch/response
Response
Response.new Response(body: unknown, init?: ResponseInit)
Source
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—bodyis aReadableStreamthat is locked or has already been consumed.
Response.headers: Headers
Source
The response headers as a Headers object.
Response.ok: boolean
Source
Whether the status code is in the range 200-299.
Response.redirected: boolean
Source
Whether the request was redirected to a different URL.
Response.Response.error(): Response
Source
Response.Response.json(data: unknown, init?: ResponseInit): Response
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown | — | — |
init? | ResponseInit | — | — |
Response.Response.redirect(url: string | URL, status?: HTTPStatusCode): Response
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | URL | — | — |
status? | HTTPStatusCode | — | — |
Response.status: HTTPStatusCode
Source
The HTTP status code of the response.
Response.statusText: HTTPStatusMessage
Source
The HTTP status message of the response.
Response.type: ResponseType
Source
Response.url: string | null
Source
The final response URL as a string, or null if no request has been made.
bare-fetch/headers
Headers
Headers.new Headers(init?: Record<string, string> | Iterable<[string, string]>)
Source
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
Source
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—nameis empty or contains characters that are not valid in a header name.INVALID_HEADER_VALUE—valuecontains a NUL, CR, or LF character.
Headers.delete(name: string): void
Source
Delete the header name.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
Headers.entries(): IterableIterator<[name: string, value: string]>
Source
Return an iterator over [name, value] pairs.
Headers.forEach(callback: (value: string, name: string, headers: Headers) => void, thisArg?: any): void
Source
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
Source
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.has(name: string): boolean
Source
Return whether the header name exists.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
Headers.keys(): IterableIterator<string>
Source
Return an iterator over header names.
Headers.set(name: string, value: string): void
Source
Set the header name to value, replacing any existing values.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
value | string | — | The value to set, replacing any existing values. |
Throws
INVALID_HEADER_NAME—nameis empty or contains characters that are not valid in a header name.INVALID_HEADER_VALUE—valuecontains a NUL, CR, or LF character.
Headers.values(): IterableIterator<string>
Source
Return an iterator over header values.
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.