- Fetch Toolkit API Reference
- Classes
- Class: API
- Class:
abstract
AuthenticationProvider - Class: BasicAuthenticationProvider
- Class: BearerAuthenticationProvider
- Class: FetchConsoleLogger
- Class: FetchError
- Class: FetchGroupHandler
- Class: FetchGroupHandlerError
- Class:
abstract
FetchHandler - Class:
abstract
FetchLogger - Class: PromiseConcurrentQueue<T>
- Functions
- fetch-toolkit
- Interfaces
- Type Aliases
- Variables
- Classes
fetch-toolkit • Docs
fetch-toolkit / API
Represents an API client that makes HTTP requests to a specified base URL.
new API(
baseUrl
,authentication
?,maxParallel
?,logger
?,customDecorators
?):API
Creates an instance of the API client.
• baseUrl: string
The base URL of the API.
• authentication?: AuthenticationProvider
Optional authentication provider for the API (see AuthenticationProvider)
• maxParallel?: number
Optional maximum number of parallel requests to be handled. If not provided, no limit is set.
• logger?: FetchLogger
Optional logger for the API (see FetchLogger)
• customDecorators?: FetchDecorator
[]
Optional custom decorators for the API (see FetchDecorator)
optional
authentication:AuthenticationProvider
Optional authentication provider for the API (see AuthenticationProvider)
readonly
baseUrl:string
The base URL of the API.
optional
customDecorators:FetchDecorator
[]
Optional custom decorators for the API (see FetchDecorator)
optional
groupHandler:FetchGroupHandler
The handler for managing parallel requests (optional). See FetchGroupHandler.
optional
logger:FetchLogger
Optional logger for the API (see FetchLogger)
call<
T
>(method
,endpoint
,params
?,body
?):Promise
<T
>
Makes an HTTP request to the API. The method returns the defined return type. Please note that no validation is done on the return type.
• T
• method: HttpMethod
The HTTP method of the request.
• endpoint: string
The endpoint of the request.
• params?: any
The query parameters of the request.
• body?: unknown
The body of the request.
Promise
<T
>
A promise that resolves to the defined return.
delete<
T
>(endpoint
,params
?,body
?):Promise
<T
>
Makes a DELETE request to the API. The method returns the defined return type. Please note that no validation is done on the return type.
• T
• endpoint: string
The endpoint of the request.
• params?: any
The query parameters of the request.
• body?: unknown
The body of the request.
Promise
<T
>
A promise that resolves to the response data.
get<
T
>(endpoint
,params
?):Promise
<T
>
Makes a GET request to the API. The method returns the defined return type. Please note that no validation is done on the return type.
• T
• endpoint: string
The endpoint of the request.
• params?: any
The query parameters of the request.
Promise
<T
>
A promise that resolves to the response data.
patch<
T
>(endpoint
,body
,params
?):Promise
<T
>
Makes a PATCH request to the API. The method returns the defined return type. Please note that no validation is done on the return type.
• T
• endpoint: string
The endpoint of the request.
• body: unknown
The body of the request.
• params?: any
The query parameters of the request.
Promise
<T
>
A promise that resolves to the response data.
post<
T
>(endpoint
,body
,params
?):Promise
<T
>
Makes a POST request to the API. The method returns the defined return type. Please note that no validation is done on the return type.
• T
• endpoint: string
The endpoint of the request.
• body: unknown
The body of the request.
• params?: any
The query parameters of the request.
Promise
<T
>
A promise that resolves to the response data.
put<
T
>(endpoint
,body
,params
?):Promise
<T
>
Makes a PUT request to the API. The method returns the defined return type. Please note that no validation is done on the return type.
• T
• endpoint: string
The endpoint of the request.
• body: unknown
The body of the request.
• params?: any
The query parameters of the request.
Promise
<T
>
A promise that resolves to the response data.
fetch-toolkit • Docs
fetch-toolkit / AuthenticationProvider
Base class for authentication providers.
new AuthenticationProvider():
AuthenticationProvider
decorateRequest(
_url
,init
):Promise
<void
>
Decorate the request with the Authorization header.
• _url: string
URL to decorate. Irrelevant for this implementation
• init: RequestInit
Fetch options
Promise
<void
>
FetchDecorator
.decorateRequest
protected
abstract
getHeaderValue():string
|Promise
<string
>
Get the value of the Authorization header.
string
| Promise
<string
>
fetch-toolkit • Docs
fetch-toolkit / BasicAuthenticationProvider
Implements the Basic authentication provider. The Basic authentication provider uses a username and password to authenticate. The username and password are encoded in base64 and sent in the Authorization header.
new BasicAuthenticationProvider(
username
,password
):BasicAuthenticationProvider
Constructor.
• username: string
Username
• password: string
Password
AuthenticationProvider
.constructor
password:
string
Password
username:
string
Username
decorateRequest(
_url
,init
):Promise
<void
>
Decorate the request with the Authorization header.
• _url: string
URL to decorate. Irrelevant for this implementation
• init: RequestInit
Fetch options
Promise
<void
>
AuthenticationProvider
.decorateRequest
protected
getHeaderValue():string
Get the value of the Authorization header.
string
AuthenticationProvider
.getHeaderValue
fetch-toolkit • Docs
fetch-toolkit / BearerAuthenticationProvider
Implements the Bearer authentication provider. The Bearer authentication provider uses a token to authenticate. The token is sent in the Authorization header.
new BearerAuthenticationProvider(
token
,prefix
):BearerAuthenticationProvider
Constructor.
• token: string
| () => string
| Promise
<string
>
Token or function that returns a token
• prefix: string
= "Bearer"
Prefix for the authorization header. Default is "Bearer"
AuthenticationProvider
.constructor
prefix:
string
="Bearer"
Prefix for the authorization header. Default is "Bearer"
token:
string
| () =>string
|Promise
<string
>
Token or function that returns a token
decorateRequest(
_url
,init
):Promise
<void
>
Decorate the request with the Authorization header.
• _url: string
URL to decorate. Irrelevant for this implementation
• init: RequestInit
Fetch options
Promise
<void
>
AuthenticationProvider
.decorateRequest
protected
getHeaderValue():Promise
<string
>
Set the value of the Authorization header to the token. If the token is a function, call it to get the token. Otherwise, use the token as is. Exceptions are not handled here. If the token is a function and it throws an exception, it will be propagated.
Promise
<string
>
Value of the Authorization header
AuthenticationProvider
.getHeaderValue
fetch-toolkit • Docs
fetch-toolkit / FetchConsoleLogger
A logger that logs to the console.
new FetchConsoleLogger(
logBody
,logHeaders
):FetchConsoleLogger
• logBody: boolean
= false
• logHeaders: boolean
= false
protected
logBody:boolean
=false
Log the body of the request.
protected
logHeaders:boolean
=false
Log the headers of the request.
decorateRequest(
url
,request
):void
Decorates the request with logging.
• url: string
URL to decorate.
• request: RequestInitToolkit
Fetch options.
void
void
decorateResponse(
_url
,request
,response
):void
Decorates the response with logging.
• _url: string
URL to decorate (not used in this implementation)
• request: RequestInitToolkit
Fetch options.
• response: Response
Fetch response.
void
void
protected
logAction(logStr
):void
Logs the action to the console as debug.
• logStr: string
The string to log.
void
static
logAdditionalAction(message
,request
?):void
Helper method to log an additional action performed by the fetching process which is not a request or response.
• message: string
Message to log.
• request?: RequestInitToolkit
Fetch options.
void
FetchLogger
.logAdditionalAction
fetch-toolkit • Docs
fetch-toolkit / FetchError
Error thrown on a faulty fetch request
Error
new FetchError(
url
,status
,statusText
,responseText
?):FetchError
• url: string
• status: number
• statusText: string
• responseText?: string
Error.constructor
message:
string
Error.message
node_modules/typescript/lib/lib.es5.d.ts:1054
name:
string
Error.name
node_modules/typescript/lib/lib.es5.d.ts:1053
optional
readonly
responseText:string
optional
stack:string
Error.stack
node_modules/typescript/lib/lib.es5.d.ts:1055
readonly
status:number
readonly
statusText:string
readonly
url:string
static
optional
prepareStackTrace: (err
,stackTraces
) =>any
Optional override for formatting stack traces
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
• err: Error
• stackTraces: CallSite
[]
any
Error.prepareStackTrace
node_modules/@types/node/globals.d.ts:28
static
stackTraceLimit:number
Error.stackTraceLimit
node_modules/@types/node/globals.d.ts:30
static
captureStackTrace(targetObject
,constructorOpt
?):void
Create .stack property on a target object
• targetObject: object
• constructorOpt?: Function
void
Error.captureStackTrace
node_modules/@types/node/globals.d.ts:21
fetch-toolkit • Docs
fetch-toolkit / FetchGroupHandler
Handles fetching requests that share a context with a maximum number of parallel requests.
new FetchGroupHandler(
maxParallel
):FetchGroupHandler
Creates an instance of FetchGroupHandler.
• maxParallel: number
The maximum number of parallel requests to handle.
readonly
maxParallel:number
The maximum number of parallel requests to handle.
get
numberOfExecuting():number
Returns the number of the executing fetch requests
number
get
waitingQueueLength():number
Returns the length of the waiting queue
number
fetch(
url
,init
?):Promise
<Response
>
Fetches a resource from the specified URL.
• url: string
The URL of the resource to fetch.
• init?: RequestInitToolkit
The optional request initialization options.
Promise
<Response
>
A Promise that resolves to the Response object representing the fetched resource.
fetch-toolkit • Docs
fetch-toolkit / FetchGroupHandlerError
Represents an error that can occur in the FetchGroupHandler class.
Error
new FetchGroupHandlerError(
message
?):FetchGroupHandlerError
• message?: string
Error.constructor
node_modules/typescript/lib/lib.es5.d.ts:1059
message:
string
Error.message
node_modules/typescript/lib/lib.es5.d.ts:1054
name:
string
Error.name
node_modules/typescript/lib/lib.es5.d.ts:1053
optional
stack:string
Error.stack
node_modules/typescript/lib/lib.es5.d.ts:1055
static
optional
prepareStackTrace: (err
,stackTraces
) =>any
Optional override for formatting stack traces
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
• err: Error
• stackTraces: CallSite
[]
any
Error.prepareStackTrace
node_modules/@types/node/globals.d.ts:28
static
stackTraceLimit:number
Error.stackTraceLimit
node_modules/@types/node/globals.d.ts:30
static
captureStackTrace(targetObject
,constructorOpt
?):void
Create .stack property on a target object
• targetObject: object
• constructorOpt?: Function
void
Error.captureStackTrace
node_modules/@types/node/globals.d.ts:21
fetch-toolkit • Docs
fetch-toolkit / FetchHandler
A class that handles the call to the fetch api itself. Use it if just implementing decorators is not enough
new FetchHandler():
FetchHandler
abstract
fetch(url
,init
?):Promise
<Response
>
• url: string
• init?: RequestInit
Promise
<Response
>
fetch-toolkit • Docs
fetch-toolkit / FetchLogger
Base class for logging decorators.
new FetchLogger(
logBody
,logHeaders
):FetchLogger
Constructor.
• logBody: boolean
= false
Log the body of the request.
• logHeaders: boolean
= false
Log the headers of the request.
protected
logBody:boolean
=false
Log the body of the request.
protected
logHeaders:boolean
=false
Log the headers of the request.
decorateRequest(
url
,request
):void
Decorates the request with logging.
• url: string
URL to decorate.
• request: RequestInitToolkit
Fetch options.
void
void
FetchDecorator
.decorateRequest
decorateResponse(
_url
,request
,response
):void
Decorates the response with logging.
• _url: string
URL to decorate (not used in this implementation)
• request: RequestInitToolkit
Fetch options.
• response: Response
Fetch response.
void
void
FetchDecorator
.decorateResponse
protected
abstract
logAction(logStr
,action
):void
Logs an action. Must be implemented by the child class.
• logStr: string
The string to log.
• action: "Request"
| "Response"
| "Additional"
| "Body"
| "Header"
The type of action to log.
void
static
logAdditionalAction(message
,request
?):void
Helper method to log an additional action performed by the fetching process which is not a request or response.
• message: string
Message to log.
• request?: RequestInitToolkit
Fetch options.
void
fetch-toolkit • Docs
fetch-toolkit / PromiseConcurrentQueue
Class to handle a queue of promises that are executed concurrently
• T
new PromiseConcurrentQueue<
T
>(maxParallel
,onEventCallback
?):PromiseConcurrentQueue
<T
>
Creates an instance of the PromiseConcurrentQueue
• maxParallel: number
The maximum number of parallel promises that can be executed. If not set, no limit is set.
• onEventCallback?: OnEventCallback
Optional callback to be triggered when an event occurs
src/parallel-promise-handler.ts:67
readonly
maxParallel:number
The maximum number of parallel promises that can be executed. If not set, no limit is set.
src/parallel-promise-handler.ts:67
optional
onEventCallback:OnEventCallback
Optional callback to be triggered when an event occurs
src/parallel-promise-handler.ts:67
get
numberOfExecuting():number
Returns the number of the executing promises
number
src/parallel-promise-handler.ts:223
get
waitingQueueLength():number
Returns the length of the waiting queue
number
src/parallel-promise-handler.ts:216
push(
func
,uid
?,data
?):Promise
<T
>
Pushes a new function to the queue
• func
Function to be executed
• uid?: string
Optional unique identifier for the function. If not set, a new one is generated
• data?: unknown
Data passed to the callback function
Promise
<T
>
Returns a promise that resolves to the result of the function
src/parallel-promise-handler.ts:76
fetch-toolkit • Docs
fetch-toolkit / fetchJson
fetchJson<
T
>(url
,init
?):Promise
<T
>
Helper method to get a response as JSON. Throws a FetchError in case of faulty Status code
• T
• url: string
URL to retrievce
• init?: RequestInitToolkit
Init parameters (same as used in the native fetch command)
Promise
<T
>
A JSON object
fetch-toolkit • Docs
fetch-toolkit / fetchSetHeader
fetchSetHeader(
init
,headerName
,headerValue
,overwrite
?):RequestInit
Helper method to set a header in the fetch request. It checks if the header has the has and set methods and uses them if available. If the header does not have the has and set methods, it uses the header as a dictionary. If the header already exists, it overwrites the value if the overwrite parameter is true. If the header does not exist, it creates it.
• init: undefined
| RequestInitToolkit
Request options
• headerName: string
Header name
• headerValue: string
Header value
• overwrite?: boolean
If true, it overwrites the header value if it already exists
RequestInit
The updated init object
fetch-toolkit • Docs
- API
- AuthenticationProvider
- BasicAuthenticationProvider
- BearerAuthenticationProvider
- FetchConsoleLogger
- FetchError
- FetchGroupHandler
- FetchGroupHandlerError
- FetchHandler
- FetchLogger
- PromiseConcurrentQueue
fetch-toolkit • Docs
fetch-toolkit / FetchDecorator
Base interface for the decorating a fetch request and response
optional
decorateRequest: (url
,request
) =>void
|Promise
<void
>
• url: string
• request: RequestInitToolkit
void
| Promise
<void
>
optional
decorateResponse: (url
,request
,response
) =>void
|Promise
<void
>
• url: string
• request: RequestInitToolkit
• response: Response
void
| Promise
<void
>
fetch-toolkit • Docs
fetch-toolkit / RequestInitToolkit
Toolkit extension for the fetch options
RequestInit
optional
body:null
|BodyInit
A BodyInit object or null to set request's body.
RequestInit.body
node_modules/typescript/lib/lib.dom.d.ts:1591
optional
cache:RequestCache
A string indicating how the request will interact with the browser's cache to set request's cache.
RequestInit.cache
node_modules/typescript/lib/lib.dom.d.ts:1593
optional
credentials:RequestCredentials
A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials.
RequestInit.credentials
node_modules/typescript/lib/lib.dom.d.ts:1595
optional
decorators:FetchDecorator
[]
optional
handler:FetchHandler
optional
headers:HeadersInit
A Headers object, an object literal, or an array of two-item arrays to set request's headers.
RequestInit.headers
node_modules/typescript/lib/lib.dom.d.ts:1597
optional
integrity:string
A cryptographic hash of the resource to be fetched by request. Sets request's integrity.
RequestInit.integrity
node_modules/typescript/lib/lib.dom.d.ts:1599
optional
keepalive:boolean
A boolean to set request's keepalive.
RequestInit.keepalive
node_modules/typescript/lib/lib.dom.d.ts:1601
optional
method:string
A string to set request's method.
RequestInit.method
node_modules/typescript/lib/lib.dom.d.ts:1603
optional
mode:RequestMode
A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode.
RequestInit.mode
node_modules/typescript/lib/lib.dom.d.ts:1605
optional
redirect:RequestRedirect
A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect.
RequestInit.redirect
node_modules/typescript/lib/lib.dom.d.ts:1607
optional
referrer:string
A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer.
RequestInit.referrer
node_modules/typescript/lib/lib.dom.d.ts:1609
optional
referrerPolicy:ReferrerPolicy
A referrer policy to set request's referrerPolicy.
RequestInit.referrerPolicy
node_modules/typescript/lib/lib.dom.d.ts:1611
optional
signal:null
|AbortSignal
An AbortSignal to set request's signal.
RequestInit.signal
node_modules/typescript/lib/lib.dom.d.ts:1613
optional
uid:string
optional
window:null
Can only be null. Used to disassociate request from any Window.
RequestInit.window
node_modules/typescript/lib/lib.dom.d.ts:1615
fetch-toolkit • Docs
fetch-toolkit / HttpMethod
HttpMethod:
"GET"
|"POST"
|"PUT"
|"DELETE"
|"PATCH"
fetch-toolkit • Docs
fetch-toolkit / OnEventCallback
OnEventCallback: (
event
,id
,data
?,message
?) =>void
Callback type for the event
• event: ParallelFunctionEventType
• id: string
• data?: unknown
• message?: string
void
src/parallel-promise-handler.ts:48
fetch-toolkit • Docs
fetch-toolkit / ParallelFunctionEventType
ParallelFunctionEventType:
"enqueue"
|"start"
|"finish"
|"error"
Type of events that can be triggered
src/parallel-promise-handler.ts:43
fetch-toolkit • Docs
fetch-toolkit / HTTP_HEADER_ACCEPT
const
HTTP_HEADER_ACCEPT:"Accept"
="Accept"
The HTTP header for specifying the accepted response format.
fetch-toolkit • Docs
fetch-toolkit / HTTP_HEADER_ACCEPT_JSON
const
HTTP_HEADER_ACCEPT_JSON:"application/json"
="application/json"
The value for specifying that the response should be in JSON format.
fetch-toolkit • Docs
fetch-toolkit / HTTP_HEADER_AUTHORIZATION
const
HTTP_HEADER_AUTHORIZATION:"Authorization"
="Authorization"
The HTTP header for specifying the authorization token.
fetch-toolkit • Docs
fetch-toolkit / HTTP_HEADER_CONTENT_TYPE
const
HTTP_HEADER_CONTENT_TYPE:"Content-Type"
="Content-Type"
The HTTP header for specifying the content type of the request.