bare-fetch
WHATWG Fetch implementation for Bare
v3.5.0bare-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]>)
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
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
Delete the header name.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
Headers.entries(): IterableIterator<[name: string, value: string]>
Return an iterator over [name, value] pairs.
Headers.forEach(callback: (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
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[]
Return each Set-Cookie header value separately. Unlike get('set-cookie'), this does not combine the values with commas.
Headers.has(name: string): boolean
Return whether the header name exists.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
Headers.keys(): IterableIterator<string>
Return an iterator over header names.
Headers.set(name: 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—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>
Return an iterator over header values.
Request
Request.new Request(input: 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—inputis not a valid URL.BODY_UNUSABLE—init.bodyis aReadableStreamthat is locked or has already been consumed.INVALID_REDIRECT—init.redirectis not'follow','error', or'manual'.INVALID_CREDENTIALS—init.credentialsis not'omit','same-origin', or'include'.
Request.arrayBuffer(): Promise<ArrayBuffer>
Consume the body and return an ArrayBuffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
Request.body: ReadableStream
The body as a ReadableStream, or null.
Request.bodyUsed: boolean
Whether the body stream has already been consumed.
Request.buffer(): Promise<Buffer>
Consume the body and return a Buffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
Request.bytes(): Promise<Buffer>
Consume the body and return a Uint8Array.
Throws
BODY_UNUSABLE— the body has already been consumed.
Request.credentials: 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>
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
The request headers as a Headers object.
Request.json(): Promise<JSON>
Consume the body and return a parsed JSON value.
Throws
BODY_UNUSABLE— the body has already been consumed.
Request.method: HTTPMethod
The request method. Standard methods (GET, POST, PUT, DELETE, HEAD, OPTIONS) are uppercased automatically.
Request.redirect: RequestRedirect
How fetch handles redirects for this request.
Request.RequestCredentials
type RequestCredentials = 'omit' | 'same-origin' | 'include'Request.RequestInit
interface RequestInit {
body?: unknown
method?: HTTPMethod
headers?: Headers
signal?: AbortSignal
redirect?: RequestRedirect
credentials?: RequestCredentials
agent?: HTTPAgent
}Request.RequestRedirect
type RequestRedirect = 'error' | 'follow' | 'manual'Request.signal: AbortSignal | null
The abort signal associated with the request, or null.
Request.text(): Promise<string>
Consume the body and return a UTF-8 string.
Throws
BODY_UNUSABLE— the body has already been consumed.
Request.url: string
The request URL as a string.
Response
Response.new Response(body: 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—bodyis aReadableStreamthat is locked or has already been consumed.
Response.arrayBuffer(): Promise<ArrayBuffer>
Consume the body and return an ArrayBuffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
Response.body: ReadableStream
The body as a ReadableStream, or null.
Response.bodyUsed: boolean
Whether the body stream has already been consumed.
Response.buffer(): Promise<Buffer>
Consume the body and return a Buffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
Response.bytes(): Promise<Buffer>
Consume the body and return a Uint8Array.
Throws
BODY_UNUSABLE— the body has already been consumed.
Response.formData(): Promise<FormData>
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
The response headers as a Headers object.
Response.ok: boolean
Whether the status code is in the range 200-299.
Response.redirected: boolean
Whether the request was redirected to a different URL.
Response.error(): Response
Response.json(data: 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
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | URL | — | — |
status? | HTTPStatusCode | — | — |
Response.status: HTTPStatusCode
The HTTP status code of the response.
Response.statusText: HTTPStatusMessage
The HTTP status message of the response.
Response.text(): Promise<string>
Consume the body and return a UTF-8 string.
Throws
BODY_UNUSABLE— the body has already been consumed.
Response.type: ResponseType
Response.url: string | null
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>
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.
As of v3.4.1, when init.redirect is 'follow', the default, redirects are followed as the Fetch Standard specifies. A 301 or 302 response to a POST, and a 303 response to any method other than GET or HEAD, make the next request a GET with no body, and drop the Content-Encoding, Content-Language, Content-Length, Content-Location, and Content-Type headers. Any other redirect sends the body again, which fails with NETWORK_ERROR when the body is a stream. A redirect to another origin drops the Authorization, Cookie, Host, and Proxy-Authorization headers. Before v3.4.1, every redirect resent the original method, body, and headers, and a redirect to another origin dropped only Authorization.
As of v3.4.1, the response body is decoded when Content-Encoding lists gzip, x-gzip, or deflate, including several of them applied in turn, and is left as received when it lists any other coding. Before v3.4.1, only a single gzip or deflate was decoded, and a deflate body came out wrong.
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.TypeError—init.redirectis'error'and a redirect response is received.NETWORK_ERROR— the underlying request failed or the connection was lost, or, as of v3.4.1, a redirect would have to send a streamed body again.
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
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
Body
Body.arrayBuffer(): Promise<ArrayBuffer>
Consume the body and return an ArrayBuffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
Body.body: ReadableStream
The body as a ReadableStream, or null.
Body.bodyUsed: boolean
Whether the body stream has already been consumed.
Body.buffer(): Promise<Buffer>
Consume the body and return a Buffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
Body.bytes(): Promise<Buffer>
Consume the body and return a Uint8Array.
Throws
BODY_UNUSABLE— the body has already been consumed.
Body.formData(): Promise<FormData>
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>
Consume the body and return a parsed JSON value.
Throws
BODY_UNUSABLE— the body has already been consumed.
Body.text(): Promise<string>
Consume the body and return a UTF-8 string.
Throws
BODY_UNUSABLE— the body has already been consumed.
bare-fetch/request
Request
request.Request.new Request(input: 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—inputis not a valid URL.BODY_UNUSABLE—init.bodyis aReadableStreamthat is locked or has already been consumed.INVALID_REDIRECT—init.redirectis not'follow','error', or'manual'.INVALID_CREDENTIALS—init.credentialsis not'omit','same-origin', or'include'.
request.Request.arrayBuffer(): Promise<ArrayBuffer>
Consume the body and return an ArrayBuffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
request.Request.body: ReadableStream
The body as a ReadableStream, or null.
request.Request.bodyUsed: boolean
Whether the body stream has already been consumed.
request.Request.buffer(): Promise<Buffer>
Consume the body and return a Buffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
request.Request.bytes(): Promise<Buffer>
Consume the body and return a Uint8Array.
Throws
BODY_UNUSABLE— the body has already been consumed.
request.Request.credentials: RequestCredentials
Credential mode metadata. The module has no cookie jar and does not add or remove cookies based on this value.
request.Request.formData(): Promise<FormData>
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.Request.headers: Headers
The request headers as a Headers object.
request.Request.json(): Promise<JSON>
Consume the body and return a parsed JSON value.
Throws
BODY_UNUSABLE— the body has already been consumed.
request.Request.method: HTTPMethod
The request method. Standard methods (GET, POST, PUT, DELETE, HEAD, OPTIONS) are uppercased automatically.
request.Request.redirect: RequestRedirect
How fetch handles redirects for this request.
request.Request.RequestCredentials
type RequestCredentials = 'omit' | 'same-origin' | 'include'request.Request.RequestRedirect
type RequestRedirect = 'error' | 'follow' | 'manual'request.Request.signal: AbortSignal | null
The abort signal associated with the request, or null.
request.Request.text(): Promise<string>
Consume the body and return a UTF-8 string.
Throws
BODY_UNUSABLE— the body has already been consumed.
request.Request.url: string
The request URL as a string.
Types
RequestInit
interface RequestInit {
body?: unknown
method?: HTTPMethod
headers?: Headers
signal?: AbortSignal
redirect?: RequestRedirect
credentials?: RequestCredentials
agent?: HTTPAgent
}bare-fetch/response
Response
response.Response.new Response(body: 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—bodyis aReadableStreamthat is locked or has already been consumed.
response.Response.arrayBuffer(): Promise<ArrayBuffer>
Consume the body and return an ArrayBuffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
response.Response.body: ReadableStream
The body as a ReadableStream, or null.
response.Response.bodyUsed: boolean
Whether the body stream has already been consumed.
response.Response.buffer(): Promise<Buffer>
Consume the body and return a Buffer.
Throws
BODY_UNUSABLE— the body has already been consumed.
response.Response.bytes(): Promise<Buffer>
Consume the body and return a Uint8Array.
Throws
BODY_UNUSABLE— the body has already been consumed.
response.Response.formData(): Promise<FormData>
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.Response.headers: Headers
The response headers as a Headers object.
response.Response.ok: boolean
Whether the status code is in the range 200-299.
response.Response.redirected: boolean
Whether the request was redirected to a different URL.
response.Response.error(): Response
response.Response.json(data: 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.Response.redirect(url: string | URL, status?: HTTPStatusCode): Response
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | URL | — | — |
status? | HTTPStatusCode | — | — |
response.Response.status: HTTPStatusCode
The HTTP status code of the response.
response.Response.statusText: HTTPStatusMessage
The HTTP status message of the response.
response.Response.text(): Promise<string>
Consume the body and return a UTF-8 string.
Throws
BODY_UNUSABLE— the body has already been consumed.
response.Response.type: ResponseType
response.Response.url: string | null
The final response URL as a string, or null if no request has been made.
Types
ResponseInit
interface ResponseInit {
status?: HTTPStatusCode
statusText?: HTTPStatusMessage
headers?: Headers
}bare-fetch/headers
Headers
headers.Headers.new Headers(init?: Record<string, string> | Iterable<[string, 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.Headers.append(name: 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—nameis empty or contains characters that are not valid in a header name.INVALID_HEADER_VALUE—valuecontains a NUL, CR, or LF character.
headers.Headers.delete(name: string): void
Delete the header name.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
headers.Headers.entries(): IterableIterator<[name: string, value: string]>
Return an iterator over [name, value] pairs.
headers.Headers.forEach(callback: (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.Headers.get(name: 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.Headers.getSetCookie(): string[]
Return each Set-Cookie header value separately. Unlike get('set-cookie'), this does not combine the values with commas.
headers.Headers.has(name: string): boolean
Return whether the header name exists.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The header name. |
headers.Headers.keys(): IterableIterator<string>
Return an iterator over header names.
headers.Headers.set(name: 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—nameis empty or contains characters that are not valid in a header name.INVALID_HEADER_VALUE—valuecontains a NUL, CR, or LF character.
headers.Headers.values(): IterableIterator<string>
Return an iterator over header values.
See also
- Builds on
bare-form-data,bare-http1,bare-https,bare-mime,bare-performance,bare-stream,bare-url, andbare-zlib. - The
fetch.Request,fetch.Response, andfetch.Headersconstructors are also attached to the function. - It's pure JavaScript, built on
bare-http1andbare-https. - Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.
Last updated on