Skip to main content

ApiClient

Upgather SDK


Upgather SDK / ApiClient

Class: ApiClient

Defined in: lib/core/ApiClient.ts:24

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?): ApiClient

Defined in: lib/core/ApiClient.ts:54

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.

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

Defined in: lib/core/ApiClient.ts:330

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>

Defined in: lib/core/ApiClient.ts:248

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>

Defined in: lib/core/ApiClient.ts:157

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

Defined in: lib/core/ApiClient.ts:190

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>

Defined in: lib/core/ApiClient.ts:213

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

Defined in: lib/core/ApiClient.ts:355

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>

Defined in: lib/core/ApiClient.ts:89

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

Defined in: lib/core/ApiClient.ts:202

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>

Defined in: lib/core/ApiClient.ts:225

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>

Defined in: lib/core/ApiClient.ts:112

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>

Defined in: lib/core/ApiClient.ts:237

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>

Defined in: lib/core/ApiClient.ts:135

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

Defined in: lib/core/ApiClient.ts:312

Updates the current authentication strategy.

Parameters

auth

AuthStrategy

The new AuthStrategy to use.

Returns

void


setErrorStrategy()

setErrorStrategy(strategy): void

Defined in: lib/core/ApiClient.ts:321

Updates the current error handling strategy.

Parameters

strategy

ErrorHandlingStrategy

The new ErrorHandlingStrategy to use.

Returns

void


setLogger()

setLogger(logger): void

Defined in: lib/core/ApiClient.ts:343

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>

Defined in: lib/core/ApiClient.ts:263

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.