@eka-care/patient-ts-sdk
Version:
TypeScript SDK for Trinity Patient Profile Management System
98 lines (97 loc) • 2.79 kB
TypeScript
/**
* Core HTTP client for Trinity Profiles SDK
*/
import { SdkConfig } from './types';
/**
* HTTP method types
*/
export type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'DELETE' | 'OPTIONS';
/**
* Request options
*/
export interface RequestOptions {
method: HttpMethod;
path: string;
body?: any;
params?: Record<string, string | number | boolean>;
headers?: Record<string, string>;
}
/**
* HTTP response interface
*/
export interface HttpResponse<T = any> {
data: T;
status: number;
statusText: string;
headers: Record<string, string>;
}
/**
* Core HTTP client class
*/
export declare class HttpClient {
private readonly baseUrl;
private accessToken;
private readonly timeout;
private readonly config;
private readonly onUnauthorized?;
private refreshPromise;
constructor(config: SdkConfig);
/**
* Get current configuration (for token updates)
*/
getConfig(): SdkConfig;
/**
* Update the in-memory access token. Used by the 401 retry path and by
* external callers that want to swap the token without rebuilding the client.
*/
setAccessToken(token: string): void;
/**
* Deduped token refresh. Concurrent 401s await the same in-flight promise
* so the onUnauthorized callback fires at most once per refresh cycle.
* The callback is bounded to 10s; on timeout we resolve to null (the 401
* surfaces as AuthenticationError) and let the next 401 trigger a fresh
* attempt.
*/
private getRefreshedToken;
/**
* Make HTTP request.
*
* On HTTP 401, if an `onUnauthorized` callback was configured, the SDK
* invokes it once to obtain a fresh token and retries the request a single
* time. HTTP 403 is treated as a hard authorization failure and is NOT
* retried — `AuthorizationError` propagates directly to the caller.
*/
request<T = any>(options: RequestOptions, isRetry?: boolean): Promise<HttpResponse<T>>;
/**
* GET request
*/
get<T = any>(path: string, params?: Record<string, string | number | boolean>): Promise<HttpResponse<T>>;
/**
* POST request
*/
post<T = any>(path: string, body?: any): Promise<HttpResponse<T>>;
/**
* PATCH request
*/
patch<T = any>(path: string, body?: any): Promise<HttpResponse<T>>;
/**
* DELETE request
*/
delete<T = any>(path: string): Promise<HttpResponse<T>>;
/**
* Build complete URL with query parameters
*/
private buildUrl;
/**
* Build request headers
*/
private buildHeaders;
/**
* Create abort signal for timeout
*/
private createAbortSignal;
/**
* Extract error message from response
*/
private extractErrorMessage;
}