Skip to main content

Fetchtastic

Represents an HTTP request configuration. It provides methods for setting headers, URL parameters, request body, and other options. It also provides convenience methods for performing common HTTP methods such as: GET, POST, PUT, DELETE, OPTIONS, PATCH, and HEAD.

Constructors

new Fetchtastic()

new Fetchtastic(baseUrl?, controller?): Fetchtastic

Creates a new instance of Fetchtastic.

Parameters

ParameterTypeDescription
baseUrl?string | URLThe base URL for the requests.
controller?AbortControllerAn optional AbortController instance for aborting the request.

Returns

Fetchtastic

Accessors

URL

get URL(): string

URL of the request, including any URL parameters.

Returns

string


body

get body(): unknown

Body of the request. It can be a Blob, an ArrayBuffer, a TypedArray, a DataView, a FormData, a URLSearchParams, a string, or a ReadableStream object. Note that a request using the GET or HEAD method cannot have a body.

Returns

unknown


headers

get headers(): Headers

Gets the request headers.

Returns

Headers


method

get method(): 
| "OPTIONS"
| "GET"
| "HEAD"
| "PUT"
| "POST"
| "DELETE"
| "PATCH"

HTTP method associated with the request.

Returns

| "OPTIONS" | "GET" | "HEAD" | "PUT" | "POST" | "DELETE" | "PATCH"


requestOptions

get requestOptions(): FetchtasticOptions

An object containing custom settings applied to the request.

Returns

FetchtasticOptions


searchParams

get searchParams(): URLSearchParams

Gets the URL search parameters.

Returns

URLSearchParams

Methods

appendHeader()

appendHeader(name, value)

appendHeader(name, value): Fetchtastic

Appends a header to the request, it uses Headers.append under the hood.

Parameters
ParameterTypeDescription
nameFetchRequestHeaderThe name of the header.
valuestringThe value of the header.
Returns

Fetchtastic

appendHeader(name, value)

appendHeader(name, value): Fetchtastic
Parameters
ParameterType
namestring
valuestring
Returns

Fetchtastic


appendSearchParam()

appendSearchParam(name, value): Fetchtastic

Appends a search parameter to the request. it uses URLSearchParams.append under the hood.

Parameters

ParameterTypeDescription
namestringThe name of the search parameter.
valuestring | number | booleanThe value of the search parameter.

Returns

Fetchtastic


arrayBuffer()

arrayBuffer(): Promise<ArrayBuffer>

Sends the fetch request and returns the response as an ArrayBuffer.

Returns

Promise<ArrayBuffer>


badRequest()

badRequest(catcher): Fetchtastic

Handles 400 bad-request HTTP responses

Parameters

ParameterType
catcherCatcherCallback

Returns

Fetchtastic


blob()

blob(): Promise<Blob>

Resolves the fetch request and returns the response as a Blob.

Returns

Promise<Blob>


controller()

controller(abortController): Fetchtastic

Registers an abort controller, in order to cancel the request if needed.

Parameters

ParameterTypeDescription
abortControllerAbortControlleran AbortController instance

Returns

Fetchtastic

https://developer.mozilla.org/en-US/docs/Web/API/AbortController


delete()

delete(body?, url?): Fetchtastic

Sets the HTTP method to DELETE and optionally sets the request URL and body.

Parameters

ParameterTypeDescription
body?unknownThe body of the DELETE request. If not provided, the existing body is used.
url?stringif provided appends to the existing URL.

Returns

Fetchtastic

A new instance with the updated DELETE request configuration.


deleteHeader()

deleteHeader(name): Fetchtastic

Deletes a header from the request.

Parameters

ParameterTypeDescription
namestringThe name of the header to delete.

Returns

Fetchtastic


deleteSearchParam()

deleteSearchParam(name): Fetchtastic

Deletes a search parameter from the request.

Parameters

ParameterTypeDescription
namestringThe name of the search parameter to deletee

Returns

Fetchtastic


forbidden()

forbidden(catcher): Fetchtastic

Handles 403 forbidden HTTP responses

Parameters

ParameterType
catcherCatcherCallback

Returns

Fetchtastic


formData()

formData(): Promise<FormData>

Resolves the fetch request and returns the response as a FormData.

Returns

Promise<FormData>


get()

get(url?): Fetchtastic

Sets the HTTP method to GET and optionally sets the request URL.

Parameters

ParameterTypeDescription
url?stringIf provided appends to the existing URL.

Returns

Fetchtastic

A new instance with the updated GET request configuration.


head(url?): Fetchtastic

Sets the HTTP method to HEAD and optionally sets the request URL.

Parameters

ParameterTypeDescription
url?stringif provided appends to the existing URL.

Returns

Fetchtastic

A new instance with the updated HEAD request configuration.


json()

json<T>(assertData?): Promise<T>

Sends the request and returns the response as a JSON object.

Type Parameters

Type ParameterDefault type
Tunknown

Parameters

ParameterTypeDescription
assertData?DataAssertionFn<T>Optional. A function to assert and transform the response data.

Returns

Promise<T>


notFound()

notFound(catcher): Fetchtastic

Handles 404 not-found HTTP responses

Parameters

ParameterType
catcherCatcherCallback

Returns

Fetchtastic


onError()

onError(status, catcher): Fetchtastic

Registers an given error handler for a specific status code.

Parameters

ParameterTypeDescription
statusnumberHTTP status code
catcherCatcherCallbackon-error callback function

Returns

Fetchtastic


options()

options(body?, url?): Fetchtastic

Sets the HTTP method to OPTIONS and optionally sets the request URL and body.

Parameters

ParameterTypeDescription
body?unknownThe body of the OPTIONS request. If not provided, the existing body is used.
url?stringif provided appends to the existing URL.

Returns

Fetchtastic

A new instance with the updated OPTIONS request configuration.


patch()

patch(body?, url?): Fetchtastic

Sets the HTTP method to PATCH and optionally sets the request URL and body.

Parameters

ParameterTypeDescription
body?unknownThe body of the PATCH request. If not provided, the existing body is used.
url?stringif provided appends to the existing URL.

Returns

Fetchtastic

A new instance with the updated PATCH request configuration.


post()

post(body?, url?): Fetchtastic

Sets the HTTP method to POST and optionally sets the request URL and body.

Parameters

ParameterTypeDescription
body?unknownThe body of the POST request. If not provided, the existing body is used.
url?stringAppends to the existing URL.

Returns

Fetchtastic

A new instance with the updated POST request configuration.


put()

put(body?, url?): Fetchtastic

Sets the HTTP method to PUT and optionally sets the request URL and body.

Parameters

ParameterTypeDescription
body?unknownThe body of the PUT request. If not provided, the existing body is used.
url?stringif provided appends to the existing URL.

Returns

Fetchtastic

A new instance with the updated PUT request configuration.


resolve()

resolve(): Promise<Response>

Resolves the fetch request and returns the Response

Returns

Promise<Response>

Throws

ResponseError if the fetch request fails.


serverError()

serverError(catcher): Fetchtastic

Handles 500 internal-server-error HTTP responses

Parameters

ParameterType
catcherCatcherCallback

Returns

Fetchtastic


setBody()

setBody(body): Fetchtastic

Sets the body of the request. It can be a Blob, an ArrayBuffer, a TypedArray, a DataView, a FormData, a URLSearchParams, a string, or a ReadableStream object. Note that a request using the GET or HEAD method cannot have a body.

Parameters

ParameterTypeDescription
bodyunknownThe body data.

Returns

Fetchtastic


setHeaders()

setHeaders(headers?, replace?): Fetchtastic

Sets the headers of the request, it uses Headers.set behind the scenes.

Parameters

ParameterTypeDefault valueDescription
headers?HeadersInitundefined-
replace?booleanfalseSpecifies whether to replace the existing headers (default: false).

Returns

Fetchtastic

https://developer.mozilla.org/en-US/docs/Web/API/Headers/set


setOptions()

setOptions(options, replace): Fetchtastic

Sets any custom settings that you want to apply to the request.

Parameters

ParameterTypeDefault valueDescription
optionsFetchtasticOptionsundefinedThe options to set.
replacebooleanfalseSpecifies whether to replace the existing options (default: false).

Returns

Fetchtastic


setSearchParams()

setSearchParams(data, replace): Fetchtastic

Sets the search parameters for the request.

Parameters

ParameterTypeDefault valueDescription
dataSearchParamInputundefinedThe URL parameters to set.
replacebooleanfalseSpecifies whether to replace the existing search parameters (default: false).

Returns

Fetchtastic


text()

text(): Promise<string>

Sends the fetch request and resolve the response as plain text.

Returns

Promise<string>


timeout()

timeout(catcher): Fetchtastic

Handles 408 request-timeout HTTP responses

Parameters

ParameterType
catcherCatcherCallback

Returns

Fetchtastic


unauthorized()

unauthorized(catcher): Fetchtastic

Handles 401 unauthorized HTTP responses

Parameters

ParameterType
catcherCatcherCallback

Returns

Fetchtastic


url()

url(url)

url(url): Fetchtastic

Sets or modifies the URL in the request configuration.

Parameters
ParameterTypeDescription
urlURLThe new URL or a string to append to the existing URL.
Returns

Fetchtastic

A new instance with the updated URL configuration.

Example
const request = new Fetchtastic()
request.url(new URL('https://example.com'));

// Append a string to the existing URL
request.url('/path');

// Replace the existing URL with a new string
request.url('/newpath', true);

url(url, replace)

url(url, replace?): Fetchtastic
Parameters
ParameterType
urlstring
replace?boolean
Returns

Fetchtastic