LogoPear Docs
ReferencesBareModules

bare-fetch

WHATWG Fetch implementation for Bare

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]>)

Source

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

Source

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

Parameters

ParameterTypeDefaultDescription
namestringThe header name.
valuestringThe value to append to the header.

Throws

  • INVALID_HEADER_NAMEname is empty or contains characters that are not valid in a header name.
  • INVALID_HEADER_VALUEvalue contains a NUL, CR, or LF character.

Headers.delete(name: string): void

Source

Delete the header name.

Parameters

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

ParameterTypeDefaultDescription
callback(value: string, name: string, headers: Headers) => voidCalled with (value, name, headers) for each header.
thisArg?anyThe 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

ParameterTypeDefaultDescription
namestringThe header name.

Headers.has(name: string): boolean

Source

Return whether the header name exists.

Parameters

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

ParameterTypeDefaultDescription
namestringThe header name.
valuestringThe value to set, replacing any existing values.

Throws

  • INVALID_HEADER_NAMEname is empty or contains characters that are not valid in a header name.
  • INVALID_HEADER_VALUEvalue contains 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

ParameterTypeDefaultDescription
inputstring | URL | RequestThe URL string, URL, or Request to base the request on.
init?RequestInitRequest options, identical to the ones accepted by fetch().

Throws

  • INVALID_URLinput is not a valid URL.
  • BODY_UNUSABLEinit.body is a ReadableStream that 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
}
Source

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

ParameterTypeDefaultDescription
bodyunknownThe response body, or null (default null).
init?ResponseInitOptions; status defaults to 200, statusText to '', and headers to an empty Headers.

Throws

  • BODY_UNUSABLEbody is a ReadableStream that 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

ParameterTypeDefaultDescription
dataunknown
init?ResponseInit

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

Source

Parameters

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

ParameterTypeDefaultDescription
inputstring | URL | RequestThe URL string, URL, or Request to fetch.
init?RequestInitRequest 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_URLinput 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
Source

The 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

ParameterTypeDefaultDescription
inputstring | URL | RequestThe URL string, URL, or Request to base the request on.
init?RequestInitRequest options, identical to the ones accepted by fetch().

Throws

  • INVALID_URLinput is not a valid URL.
  • BODY_UNUSABLEinit.body is a ReadableStream that 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
}
Source

bare-fetch/response

Response

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

Source

Construct a new Response wrapping body.

Parameters

ParameterTypeDefaultDescription
bodyunknownThe response body, or null (default null).
init?ResponseInitOptions; status defaults to 200, statusText to '', and headers to an empty Headers.

Throws

  • BODY_UNUSABLEbody is a ReadableStream that 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

ParameterTypeDefaultDescription
dataunknown
init?ResponseInit

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

Source

Parameters

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

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

Source

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

Parameters

ParameterTypeDefaultDescription
namestringThe header name.
valuestringThe value to append to the header.

Throws

  • INVALID_HEADER_NAMEname is empty or contains characters that are not valid in a header name.
  • INVALID_HEADER_VALUEvalue contains a NUL, CR, or LF character.

Headers.delete(name: string): void

Source

Delete the header name.

Parameters

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

ParameterTypeDefaultDescription
callback(value: string, name: string, headers: Headers) => voidCalled with (value, name, headers) for each header.
thisArg?anyThe 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

ParameterTypeDefaultDescription
namestringThe header name.

Headers.has(name: string): boolean

Source

Return whether the header name exists.

Parameters

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

ParameterTypeDefaultDescription
namestringThe header name.
valuestringThe value to set, replacing any existing values.

Throws

  • INVALID_HEADER_NAMEname is empty or contains characters that are not valid in a header name.
  • INVALID_HEADER_VALUEvalue contains a NUL, CR, or LF character.

Headers.values(): IterableIterator<string>

Source

Return an iterator over header values.

See also

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.has(name: string): boolean
Headers.keys(): IterableIterator<string>
Headers.set(name: string, value: string): void
Headers.values(): IterableIterator<string>
Request
Request.new Request(input: string | URL | Request, init?: RequestInit)
Request.headers: Headers
Request.method: HTTPMethod
Request.RequestInit
Request.signal: AbortSignal | null
Request.url: string
Response
Response.new Response(body: unknown, init?: ResponseInit)
Response.headers: Headers
Response.ok: boolean
Response.redirected: boolean
Response.Response.error(): Response
Response.Response.json(data: unknown, init?: ResponseInit): Response
Response.Response.redirect(url: string | URL, status?: HTTPStatusCode): Response
Response.status: HTTPStatusCode
Response.statusText: HTTPStatusMessage
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
arrayBuffer(): Promise<ArrayBuffer>
body: ReadableStream
bodyUsed: boolean
buffer(): Promise<Buffer>
bytes(): Promise<Buffer>
formData(): Promise<FormData>
json(): Promise<JSON>
text(): Promise<string>
bare-fetch/request
Request
Request.new Request(input: string | URL | Request, init?: RequestInit)
Request.headers: Headers
Request.method: HTTPMethod
Request.signal: AbortSignal | null
Request.url: string
Types
RequestInit
bare-fetch/response
Response
Response.new Response(body: unknown, init?: ResponseInit)
Response.headers: Headers
Response.ok: boolean
Response.redirected: boolean
Response.Response.error(): Response
Response.Response.json(data: unknown, init?: ResponseInit): Response
Response.Response.redirect(url: string | URL, status?: HTTPStatusCode): Response
Response.status: HTTPStatusCode
Response.statusText: HTTPStatusMessage
Response.type: ResponseType
Response.url: string | null
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.has(name: string): boolean
Headers.keys(): IterableIterator<string>
Headers.set(name: string, value: string): void
Headers.values(): IterableIterator<string>
See also