API Reference
Fetch options
All Regular fetch options are supported as is, with only body extended to support more types.
-
body
: Optional body of the request, can be an object or any other supported body type. -
query
: Query parameters to append to the URL. -
auth
: Authorization header value. -
bodySerializer
: Custom function to serialize the body object into a string. -
responseParser
: Custom function to parse the response string into an object. -
resultMode
: Mode of the result, can influence how results are handled or returned. (default: "all") -
cancelRedundantRequests
: If true, cancels previous unfinished requests to the same URL. (default: true) -
baseURL
: Base URL to be prepended to all request URLs. -
timeout
: Request timeout in milliseconds. -
defaultErrorMessage
: Default error message to use if none is provided from a response. (default: "Failed to fetch data from server!") -
throwOnError
: If true or the function returns true, throws errors instead of returning them. -
responseType
: Expected response type, affects how response is parsed. (default: "json") -
retries
: Number of retry attempts for failed requests. (default: 0) -
retryDelay
: Delay between retries in milliseconds. (default: 500) -
retryCodes
: HTTP status codes that trigger a retry. (default: [409, 425, 429, 500, 502, 503, 504]) -
retryMethods
: HTTP methods that are allowed to retry. (default: ["GET", "POST"]) -
meta
: An optional field that can contain additional information about a request, which could be helpful in differentiating between different requests in a shared interceptor. -
onRequest
: Interceptor called just before the request is made, allowing for modifications or additional operations. -
onRequestError
: Interceptor called when an error occurs during the fetch request. -
onResponse
: Interceptor called when a successful response is received from the API. -
onResponseError
: Interceptor called when an error response is received from the API.
Methods
callApi.create(options)
: Creates an instance of callApi with shared base configurations.
callApi.cancel(url: string)
: Cancels an ongoing request to the specified URL.
Utility Functions
-
isHTTPError
: Type guard for if an error is an HTTPError -
isHTTPErrorInstance
: Type guard for if an error is an instance of HTTPError. Useful for when throwAllErrors option is set to true -
toQueryString
: Converts an object to a URL query string