ApiClient class
A generic HTTP client to interact with the service API. Uses isomorphic fetch approach that works in both browser and Node.js environments without external dependencies when native fetch is available.
Constructors
| Api |
Creates a new HTTP client. |
Methods
| delete<T>(string, Request |
Makes a DELETE request. |
| get<T>(string, Request |
Makes a GET request. |
| post<T>(string, any, Request |
Makes a POST request. |
| put<T>(string, any, Request |
Makes a PUT request. |
| request |
Makes a raw HTTP request with streaming support and returns the Response object. Does not parse the response body - allows caller to handle streaming responses. |
| set |
Sets or updates the access token callback function. This allows attaching a token provider after the ApiClient has been initialized. |
| set |
Sets the callback invoked when a retry after refresh also returns 401. |
| set |
Sets or updates the refresh callback function. This allows attaching a refresh handler after the ApiClient has been initialized. |
Constructor Details
ApiClient(ApiClientConfig)
Creates a new HTTP client.
new ApiClient(config: ApiClientConfig)
Parameters
- config
- ApiClientConfig
Client configuration. A non-empty publishableKey is required.
Method Details
delete<T>(string, RequestOptions)
Makes a DELETE request.
function delete<T>(path: string, options?: RequestOptions): Promise<T>
Parameters
- path
-
string
The API endpoint path.
- options
- RequestOptions
Optional fetch request options.
Returns
Promise<T>
A promise that resolves with the response data.
get<T>(string, RequestOptions)
Makes a GET request.
function get<T>(path: string, options?: RequestOptions): Promise<T>
Parameters
- path
-
string
The API endpoint path.
- options
- RequestOptions
Optional fetch request options.
Returns
Promise<T>
A promise that resolves with the response data.
post<T>(string, any, RequestOptions)
Makes a POST request.
function post<T>(path: string, data?: any, options?: RequestOptions): Promise<T>
Parameters
- path
-
string
The API endpoint path.
- data
-
any
The request body.
- options
- RequestOptions
Optional fetch request options.
Returns
Promise<T>
A promise that resolves with the response data.
put<T>(string, any, RequestOptions)
Makes a PUT request.
function put<T>(path: string, data?: any, options?: RequestOptions): Promise<T>
Parameters
- path
-
string
The API endpoint path.
- data
-
any
The request body.
- options
- RequestOptions
Optional fetch request options.
Returns
Promise<T>
A promise that resolves with the response data.
requestRaw(string, { allowProxyPath?: boolean, body?: null | BodyInit, headers?: Record<string, string>, method?: string, signal?: AbortSignal, skipAuth?: boolean, timeout?: number })
Makes a raw HTTP request with streaming support and returns the Response object. Does not parse the response body - allows caller to handle streaming responses.
function requestRaw(path: string, options?: { allowProxyPath?: boolean, body?: null | BodyInit, headers?: Record<string, string>, method?: string, signal?: AbortSignal, skipAuth?: boolean, timeout?: number }): Promise<Response>
Parameters
- path
-
string
The API endpoint path.
- options
-
{ allowProxyPath?: boolean, body?: null | BodyInit, headers?: Record<string, string>, method?: string, signal?: AbortSignal, skipAuth?: boolean, timeout?: number }
Request options including streaming body support.
Returns
Promise<Response>
A promise that resolves with the raw Response object.
setAccessTokenCallback(() => null | string)
Sets or updates the access token callback function. This allows attaching a token provider after the ApiClient has been initialized.
function setAccessTokenCallback(callback: () => null | string)
Parameters
- callback
-
() => null | string
Function that returns the current access token or null
setAuthExhaustedCallback(() => void)
Sets the callback invoked when a retry after refresh also returns 401.
function setAuthExhaustedCallback(callback: () => void)
Parameters
- callback
-
() => void
Function called when auth is exhausted
setRefreshCallback(() => Promise<void>)
Sets or updates the refresh callback function. This allows attaching a refresh handler after the ApiClient has been initialized.
function setRefreshCallback(callback: () => Promise<void>)
Parameters
- callback
-
() => Promise<void>
Function that performs token refresh