Bare Docs

bare-fetch

WHATWG Fetch implementation for Bare

Documented against v3.4.0
stable

bare-fetch — WHATWG Fetch implementation for Bare.

npm i bare-fetch

Usage

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

ParameterTypeDefaultDescription
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

ParameterTypeDefaultDescription
namestring—The header name.
valuestring—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

Delete the header name.

Parameters

ParameterTypeDefaultDescription
namestring—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

ParameterTypeDefaultDescription
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

ParameterTypeDefaultDescription
namestring—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

ParameterTypeDefaultDescription
namestring—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

ParameterTypeDefaultDescription
namestring—The header name.
valuestring | 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>

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

ParameterTypeDefaultDescription
inputstring | 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>

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

ParameterTypeDefaultDescription
bodyunknown—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>

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

ParameterTypeDefaultDescription
dataunknown——
init?ResponseInit——

Throws

  • BODY_UNUSABLE — the body has already been consumed.

Response.redirect(url: string | URL, status?: HTTPStatusCode): Response

Parameters

ParameterTypeDefaultDescription
urlstring | 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.

Parameters

ParameterTypeDefaultDescription
inputstring | 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

Types

Fetch

type Fetch = typeof fetch

The 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.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

ParameterTypeDefaultDescription
inputstring | 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>

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.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.

Types

RequestInit

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

bare-fetch/response

Response

Response.new Response(body: unknown, init?: ResponseInit)

Construct a new Response wrapping body.

Parameters

ParameterTypeDefaultDescription
bodyunknown—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>

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

ParameterTypeDefaultDescription
dataunknown——
init?ResponseInit——

Throws

  • BODY_UNUSABLE — the body has already been consumed.

Response.redirect(url: string | URL, status?: HTTPStatusCode): Response

Parameters

ParameterTypeDefaultDescription
urlstring | 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.

Types

ResponseInit

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

bare-fetch/headers

Headers

Headers.new Headers(init?: Record<string, string> | Iterable<[string, string]>)

Construct a new Headers collection, optionally initialized from init.

Parameters

ParameterTypeDefaultDescription
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

ParameterTypeDefaultDescription
namestring—The header name.
valuestring—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

Delete the header name.

Parameters

ParameterTypeDefaultDescription
namestring—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

ParameterTypeDefaultDescription
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

ParameterTypeDefaultDescription
namestring—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

ParameterTypeDefaultDescription
namestring—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

ParameterTypeDefaultDescription
namestring—The header name.
valuestring | 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>

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, 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 and bare-https.
  • Bare modules — the full bare-* catalog.
  • Bare runtime API — the runtime these modules extend.

Last updated on

Was this helpful?

On this page

Usage
API
Headers
Headers.new Headers(init?: Record<string, string> | Iterable<[string, string]>)
Headers.append(name: string, value: string): void
Headers.delete(name: string): void
Headers.entries(): IterableIterator<[name: string, value: string]>
Headers.forEach(callback: (value: string, name: string, headers: Headers) => void, thisArg?: any): void
Headers.get(name: string): string | null
Headers.getSetCookie(): string[]
Headers.has(name: string): boolean
Headers.keys(): IterableIterator<string>
Headers.set(name: string, value: string | string[]): void
Headers.values(): IterableIterator<string>
Request
Request.new Request(input: string | URL | Request, init?: RequestInit)
Request.arrayBuffer(): Promise<ArrayBuffer>
Request.body: ReadableStream
Request.bodyUsed: boolean
Request.buffer(): Promise<Buffer>
Request.bytes(): Promise<Buffer>
Request.credentials: RequestCredentials
Request.formData(): Promise<FormData>
Request.headers: Headers
Request.json(): Promise<JSON>
Request.method: HTTPMethod
Request.redirect: RequestRedirect
Request.RequestCredentials
Request.RequestInit
Request.RequestRedirect
Request.signal: AbortSignal | null
Request.text(): Promise<string>
Request.url: string
Response
Response.new Response(body: unknown, init?: ResponseInit)
Response.arrayBuffer(): Promise<ArrayBuffer>
Response.body: ReadableStream
Response.bodyUsed: boolean
Response.buffer(): Promise<Buffer>
Response.bytes(): Promise<Buffer>
Response.formData(): Promise<FormData>
Response.headers: Headers
Response.ok: boolean
Response.redirected: boolean
Response.error(): Response
Response.json(data: unknown, init?: ResponseInit): Response
Response.redirect(url: string | URL, status?: HTTPStatusCode): Response
Response.status: HTTPStatusCode
Response.statusText: HTTPStatusMessage
Response.text(): Promise<string>
Response.type: ResponseType
Response.url: string | null
Functions
fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>
bare-fetch/global
Types
Fetch
bare-fetch/errors
Classes
FetchError
bare-fetch/body
Body
Body.arrayBuffer(): Promise<ArrayBuffer>
Body.body: ReadableStream
Body.bodyUsed: boolean
Body.buffer(): Promise<Buffer>
Body.bytes(): Promise<Buffer>
Body.formData(): Promise<FormData>
Body.json(): Promise<JSON>
Body.text(): Promise<string>
bare-fetch/request
Request
Request.new Request(input: string | URL | Request, init?: RequestInit)
Request.arrayBuffer(): Promise<ArrayBuffer>
Request.body: ReadableStream
Request.bodyUsed: boolean
Request.buffer(): Promise<Buffer>
Request.bytes(): Promise<Buffer>
Request.credentials: RequestCredentials
Request.formData(): Promise<FormData>
Request.headers: Headers
Request.json(): Promise<JSON>
Request.method: HTTPMethod
Request.redirect: RequestRedirect
Request.RequestCredentials
Request.RequestRedirect
Request.signal: AbortSignal | null
Request.text(): Promise<string>
Request.url: string
Types
RequestInit
bare-fetch/response
Response
Response.new Response(body: unknown, init?: ResponseInit)
Response.arrayBuffer(): Promise<ArrayBuffer>
Response.body: ReadableStream
Response.bodyUsed: boolean
Response.buffer(): Promise<Buffer>
Response.bytes(): Promise<Buffer>
Response.formData(): Promise<FormData>
Response.headers: Headers
Response.ok: boolean
Response.redirected: boolean
Response.error(): Response
Response.json(data: unknown, init?: ResponseInit): Response
Response.redirect(url: string | URL, status?: HTTPStatusCode): Response
Response.status: HTTPStatusCode
Response.statusText: HTTPStatusMessage
Response.text(): Promise<string>
Response.type: ResponseType
Response.url: string | null
Types
ResponseInit
bare-fetch/headers
Headers
Headers.new Headers(init?: Record<string, string> | Iterable<[string, string]>)
Headers.append(name: string, value: string): void
Headers.delete(name: string): void
Headers.entries(): IterableIterator<[name: string, value: string]>
Headers.forEach(callback: (value: string, name: string, headers: Headers) => void, thisArg?: any): void
Headers.get(name: string): string | null
Headers.getSetCookie(): string[]
Headers.has(name: string): boolean
Headers.keys(): IterableIterator<string>
Headers.set(name: string, value: string | string[]): void
Headers.values(): IterableIterator<string>
See also