Skip to main content

API Reference

Axium Class

The main class for creating HTTP clients.

Constructor

// Import the function
const { Axium } = require("@nexoracle/utils"); // CJS
import { Axium } from "@nexoracle/utils"; // ESM

const axium = new Axium(defaults);

Parameters:

  • defaults (optional): Default configuration options for all requests.

Instance Methods

.request(url, options)

Make an HTTP request.

axium.request(url, options);

Parameters:

  • url: The URL to send the request to.
  • options (optional): Request configuration.

Returns:

  • Promise that resolves to a response object.

.get(url, options)

Make a GET request.

axium.get(url, options);

Parameters:

  • url: The URL to send the request to.
  • options (optional): Request configuration.

Returns:

  • Promise that resolves to a response object.

.post(url, data, options)

Make a POST request with JSON data.

axium.post(url, data, options);

Parameters:

  • url: The URL to send the request to.
  • data: The data to send in the request body (will be JSON-stringified).
  • options (optional): Request configuration.

Returns:

  • Promise that resolves to a response object.

.put(url, data, options)

Make a PUT request with JSON data.

axium.put(url, data, options);

Parameters:

  • url: The URL to send the request to.
  • data: The data to send in the request body (will be JSON-stringified).
  • options (optional): Request configuration.

Returns:

  • Promise that resolves to a response object.

.patch(url, data, options)

Make a PATCH request with JSON data.

axium.patch(url, data, options);

Parameters:

  • url: The URL to send the request to.
  • data: The data to send in the request body (will be JSON-stringified).
  • options (optional): Request configuration.

Returns:

  • Promise that resolves to a response object.

.delete(url, options)

Make a DELETE request.

axium.delete(url, options);

Parameters:

  • url: The URL to send the request to.
  • options (optional): Request configuration.

Returns:

  • Promise that resolves to a response object.

.postFormData(url, data, options)

Make a POST request with multipart form data.

axium.postFormData(url, formData, options);

Parameters:

  • url: The URL to send the request to.
  • data: FormData object to send.
  • options (optional): Request configuration.

Returns:

  • Promise that resolves to a response object.

.postUrlEncoded(url, data, options)

Make a POST request with URL-encoded form data.

axium.postUrlEncoded(url, data, options);

Parameters:

  • url: The URL to send the request to.
  • data: Object containing form data.
  • options (optional): Request configuration.

Returns:

  • Promise that resolves to a response object.

.all(requests)

Execute multiple requests concurrently.

axium.all([request1, request2, request3]);

Parameters:

  • requests: Array of Promise objects (typically from other Axium requests).

Returns:

  • Promise that resolves when all requests are complete.

.getBuffer(url, options, method)

Make a GET request and return a buffer.

axium.getBuffer(url, options);

Parameters:

  • url: The URL to send the request to.
  • options (optional): Request configuration.
  • method: The method used to make request. (default: GET)

Returns:

  • Promise that resolves to a response with data as a buffer.

.fetchJson(url, options, method)

Make a GET request and return json response.

axium.fetchJson(url, options);

Parameters:

  • url: The URL to send the request to.
  • options (optional): Request configuration.
  • method: The method used to make request. (default: GET)

Returns:

  • Promise that resolves to a response with data as a json.

.head(url, options)

Make a HEAD request.

axium.head(url, options);

Parameters:

  • url: The URL to send the request to.
  • options (optional): Request configuration.

Returns:

  • Promise that resolves to a response object (with no body, only headers and status).

.addRequestInterceptor(interceptor)

Add a request interceptor.

axium.addRequestInterceptor(interceptor);

Parameters:

  • interceptor: Function that takes request options and returns modified options.

.addResponseInterceptor(interceptor)

Add a response interceptor.

axium.addResponseInterceptor(interceptor);

Parameters:

  • interceptor: Function that takes response and returns modified response.

.setGlobalDefaults(defaults)

Set global default options.

axium.setGlobalDefaults(defaults);

Parameters:

  • defaults: Object containing default options to set.

Request Configuration

The request configuration object supports all standard fetch options plus the following Axium-specific options:

OptionTypeDescription
retriesNumberNumber of retry attempts for failed requests (default: 0)
retryDelayNumberDelay between retry attempts in milliseconds (default: 0)
timeoutNumberRequest timeout in milliseconds
paramsObjectURL query parameters
onDownloadProgressFunctionCallback for download progress tracking
onUploadProgressFunctionCallback for upload progress tracking
signalAbortSignalAbortSignal for request cancellation
responseTypeStringForce specific response parsing (arraybuffer, blob, json, text)

Response Object

Successful requests return a response object with the following properties:

PropertyTypeDescription
dataAnyParsed response data
statusNumberHTTP status code
statusTextStringHTTP status text
headersHeadersResponse headers
configObjectOriginal request configuration

Error Handling

Axium throws a FetchError object when a request fails, with the following properties:

PropertyTypeDescription
messageStringError message
statusNumberHTTP status code (if available)
responseObjectOriginal response object (if available)