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.
Properties
URL
- The URL of the request, including any URL parameters.searchParamsJSON
- An object representing the URL parameters of the request.headersJSON
- An object representing the headers of the request.method
: The HTTP method associated with the request (GET
,POST
,PUT
, etc.).
Constructor
Creates a new instance of the Fetchtastic
class.
baseUrl
: The base URL of the request. Optional.controller
: AnAbortController
instance for aborting the request. Optional.
constructor(baseUrl?: string | URL, controller?: AbortController);
HTTP methods:
These functions when called, set a specific HTTP method to the request configuration.
Likewise, you can pass optional url
and body
arguments to these methods if needed.
get
get(url?: string): Fetchtastic
post
post(url?: string, body?: BodyInit | null | unknown): Fetchtastic
put
put(url?: string, body?: BodyInit | null | unknown): Fetchtastic
delete
delete(url?: string, body?: BodyInit | null | unknown): Fetchtastic
options
options(url?: string, body?: BodyInit | null | unknown): Fetchtastic
patch
patch(url?: string, body?: BodyInit | null | unknown): Fetchtastic
head
head(url?: string): Fetchtastic
Response methods
These functions when called, execute the HTTP request and return a promise with its response.
The primary objectives of these functions involve initiating the HTTP request with the existing configuration, with a focus on managing responses, and parsing the resulting data.
resolve
Sends the HTTP request and resolves with the Response (opens in a new tab) object representing the response.
resolve(): Promise<Response>
json
Returns a promise that resolves with the result of parsing the response body text as JSON
.
assertData
: An (optional) assertion function to validate the parsedJSON
data. If provided, the parsed data is passed to this function for validation.
json<T = unknown>(assertData?: DataAssertionFn<T>): Promise<T>
arrayBuffer
Returns a promise that resolves with an ArrayBuffer (opens in a new tab) representation of the response body.
arrayBuffer(): Promise<ArrayBuffer>
blob
Returns a promise that resolves with a Blob (opens in a new tab) representation of the response body.
blob(): Promise<Blob>
formData
Returns a promise that resolves with a FormData (opens in a new tab) representation of the response body.
formData(): Promise<FormData>
text
Returns a promise that resolves with the response data as plain text.
text(): Promise<string>
Error Catchers
Error catchers are in charge of handling Not-OK (opens in a new tab) responses,
by default Fetchtastic
will throw an error (for HTTP error codes) unless a catcher has been provided to handle it.
type ErrorCatcher = (error: HttpError, config: Fetchtastic) => void | Promise<Response | void>;
You can add as many catchers as you need for a given error code, they all will be executed if an error is caught.
onError
Sets an error-catcher function to be executed in the case of a HTTP error code response.
status
: HTTP status code of the error to be handled.catcher
: on-error callback function executed in case of error.
onError(status: number, catcher: ErrorCatcher): Fetchtastic
badRequest - 400
badRequest(catcher: ErrorCatcher): Fetchtastic
unauthorized - 401
unauthorized(catcher: ErrorCatcher): Fetchtastic
forbidden - 403
forbidden(catcher: ErrorCatcher): Fetchtastic
notFound - 404
notFound(catcher: ErrorCatcher): Fetchtastic
timeout - 408
timeout(catcher: ErrorCatcher): Fetchtastic
serverError - 500
serverError(catcher: ErrorCatcher): Fetchtastic
Request configuration:
controller
Registers an abort controller, in order to cancel the request if needed.
abortController
: A instance ofAbortController
(opens in a new tab)
controller(abortController: AbortController): Fetchtastic
headers
Sets the headers of the request, it uses Headers.set
(opens in a new tab) behind the scenes.
data
: The headers to set.replace
: Indicates whether to replace the existing headers or append to them. Default isfalse
.
headers(data?: HeadersInit, replace = false): Fetchtastic
appendHeader
Appends a header to the request, it uses Headers.append
(opens in a new tab) behind the scenes.
name
: The name of the header.value
: The value of the header.
appendHeader(name: string, value: string): Fetchtastic
deleteHeader
Deletes a header from the request.
name
: The name of the header to delete.
deleteHeader(name: string): Fetchtastic
url
Sets the URL of the request.
url
: The URL to set.replace
: Indicates whether to replace the existing URL or append to it. Default isfalse
.
url(url: string | URL, replace = false): Fetchtastic
searchParams
Sets the URL parameters of the request.
data
: The URL parameters to set.replace
: Indicates whether to replace the existing URL parameters or append to them. Default isfalse
.
searchParams(data?: SearchParamInput, replace = false): Fetchtastic
appendSearchParam
Appends a URL parameter to the request.
name
: The name of the URL search parameter.value
: The value of the URL search parameter.
appendSearchParam(name: string, value: string | number | boolean): Fetchtastic
deleteSearchParam
Deletes a URL parameter from the request.
name
: The name of the URL parameter to delete.
deleteSearchParam(name: string): Fetchtastic
body
Sets the body of the request.
body
: The body of the request.
body(body: BodyInit | null | unknown): Fetchtastic
setOptions
Sets the options of the request.
options
: The options to set.replace
: Indicates whether to replace the existing options or merge them with the new options. Default isfalse
.
setOptions(options: FetchtasticOptions, replace = false): Fetchtastic
getOptions
Gets the options object for the request.
method
: The HTTP method of the request.
Returns the options object for the request.
getOptions(method: HttpMethod): FetchOptions