Skip to main content

ApiClient

Upgather SDK


Upgather SDK / ApiClient

Class: ApiClient

The ApiClient class provides a generic HTTP client that supports both synchronous and asynchronous API requests. It applies authentication, error handling, and request interception, and it can return responses with type safety.

Remarks​

This class uses Axios under the hood to perform HTTP requests. It provides typed methods (e.g. getTyped, postTyped, etc.) that allow the caller to receive the response body cast to the expected TypeScript type.

Constructors​

new ApiClient()​

new ApiClient(baseUrl, authStrategy, errorStrategy, logger?, timeoutMs?): ApiClient

Creates a new instance of the ApiClient.

Parameters​

baseUrl​

string

The base URL for the API.

authStrategy​

AuthStrategy

The authentication strategy to use on outgoing requests.

errorStrategy​

ErrorHandlingStrategy

The error handling strategy to use when a request fails.

logger?​

LoggerInterface

Optional logger instance for request/response logging. If not provided, uses a silent logger.

timeoutMs?​

number

Optional per-request timeout in milliseconds. When omitted, Axios applies no timeout.

Returns​

ApiClient

Remarks​

The ApiClient automatically includes a standardized User-Agent header in all requests. This header follows the format: upgather-sdk/<version> (+https://upgather-sdk-docs.prod.upgather.com) and helps identify requests from the SDK for debugging and analytics purposes.

The User-Agent header is added via a built-in interceptor that is automatically registered during construction, ensuring all outbound requests include proper identification.

Methods​

addInterceptor()​

addInterceptor(interceptor): void

Adds a request interceptor which can modify requests before they are sent.

Parameters​

interceptor​

RequestInterceptor

The RequestInterceptor to add.

Returns​

void


deleteAsync()​

deleteAsync(endpoint): Promise<ApiResponse>

Sends an asynchronous DELETE request to the specified endpoint.

Parameters​

endpoint​

string

The API endpoint.

Returns​

Promise<ApiResponse>

A promise resolving to the ApiResponse.


deleteTyped()​

deleteTyped<T>(endpoint): Promise<T>

Sends an asynchronous DELETE request to the specified endpoint and casts the response to type T.

Type Parameters​

• T

The expected type of the response body.

Parameters​

endpoint​

string

The API endpoint.

Returns​

Promise<T>

A promise resolving to the response body cast as type T.

Throws​

When the response status indicates a failure.


get()​

get(endpoint): ApiResponse

Sends a synchronous GET request to the specified endpoint.

Parameters​

endpoint​

string

The API endpoint.

Returns​

ApiResponse

The ApiResponse from the request.


getAsync()​

getAsync(endpoint): Promise<ApiResponse>

Sends an asynchronous GET request to the specified endpoint.

Parameters​

endpoint​

string

The API endpoint.

Returns​

Promise<ApiResponse>

A promise resolving to the ApiResponse.


getLogger()​

getLogger(): LoggerInterface

Gets the current logger instance.

Returns​

LoggerInterface

The current logger instance being used by the ApiClient.

Remarks​

This method allows access to the logger for debugging purposes or to pass it to other components.


getTyped()​

getTyped<T>(endpoint): Promise<T>

Sends an asynchronous GET request to the specified endpoint and casts the response body to type T.

Type Parameters​

• T

The expected type of the response body.

Parameters​

endpoint​

string

The API endpoint to query.

Returns​

Promise<T>

A promise resolving to the response body cast as type T.

Throws​

Will throw an error if the response status indicates a failure.

Example​

const article = await apiClient.getTyped<Article>('/articles/1');

post()​

post(endpoint, data): ApiResponse

Sends a synchronous POST request to the specified endpoint with the provided data.

Parameters​

endpoint​

string

The API endpoint.

data​

Json

The request payload.

Returns​

ApiResponse

The ApiResponse from the request.


postAsync()​

postAsync(endpoint, data): Promise<ApiResponse>

Sends an asynchronous POST request to the specified endpoint with the provided data.

Parameters​

endpoint​

string

The API endpoint.

data​

Json

The request payload.

Returns​

Promise<ApiResponse>

A promise resolving to the ApiResponse.


postTyped()​

postTyped<T>(endpoint, data): Promise<T>

Sends an asynchronous POST request to the specified endpoint with the provided data and casts the response to type T.

Type Parameters​

• T

The expected type of the response body.

Parameters​

endpoint​

string

The API endpoint.

data​

Json

The data to be sent as the request body.

Returns​

Promise<T>

A promise resolving to the response body cast as type T.

Throws​

When the response status indicates a failure.


putAsync()​

putAsync(endpoint, data): Promise<ApiResponse>

Sends an asynchronous PUT request to the specified endpoint with the provided data.

Parameters​

endpoint​

string

The API endpoint.

data​

Json

The request payload.

Returns​

Promise<ApiResponse>

A promise resolving to the ApiResponse.


putTyped()​

putTyped<T>(endpoint, data): Promise<T>

Sends an asynchronous PUT request to the specified endpoint with the provided data and casts the response to type T.

Type Parameters​

• T

The expected type of the response body.

Parameters​

endpoint​

string

The API endpoint.

data​

Json

The data to update.

Returns​

Promise<T>

A promise resolving to the updated resource cast as type T.

Throws​

When the response status indicates a failure.


setAuthStrategy()​

setAuthStrategy(auth): void

Updates the current authentication strategy.

Parameters​

auth​

AuthStrategy

The new AuthStrategy to use.

Returns​

void


setErrorStrategy()​

setErrorStrategy(strategy): void

Updates the current error handling strategy.

Parameters​

strategy​

ErrorHandlingStrategy

The new ErrorHandlingStrategy to use.

Returns​

void


setLogger()​

setLogger(logger): void

Sets a new logger instance.

Parameters​

logger​

LoggerInterface

The logger instance to use for logging operations.

Returns​

void

Remarks​

This method allows dynamically changing the logger after the ApiClient has been constructed. Useful for adjusting logging levels or switching between different logger implementations at runtime.


uploadAsync()​

uploadAsync<T>(endpoint, formData): Promise<T>

Sends a multipart form data POST request. Skips the content-type interceptor so Axios can set the correct multipart boundary automatically. Auth and non-content-type interceptors (logging, rate limiting) are still applied.

Type Parameters​

• T

Parameters​

endpoint​

string

The API endpoint.

formData​

FormData

The FormData to send.

Returns​

Promise<T>

A promise resolving to the response body cast as type T.

Throws​

When the response status indicates a failure.