// Client configuration for the Curvegrid MultiBaas API
import {
Configuration,
AddressesApi,
AdminApi,
ChainsApi,
ContractsApi,
EventQueriesApi,
EventsApi,
HsmApi,
TxmApi,
WebhooksApi,
} from "@curvegrid/multibaas-sdk";
import { log } from "./logging";
// API client instances
let baseURL: string | null = null;
let apiKey: string | null = null;
let configuration: Configuration | null = null;
// Individual API clients
let addressesApi: AddressesApi | null = null;
let adminApi: AdminApi | null = null;
let chainsApi: ChainsApi | null = null;
let contractsApi: ContractsApi | null = null;
let eventQueriesApi: EventQueriesApi | null = null;
let eventsApi: EventsApi | null = null;
let hsmApi: HsmApi | null = null;
let txmApi: TxmApi | null = null;
let webhooksApi: WebhooksApi | null = null;
/**
* Initialize all API clients with the provided base URL and API key
* If parameters are not provided, will attempt to use environment variables BASE_URL and API_KEY
*/
export function initializeClient(
providedBaseUrl?: string,
providedKey?: string,
): void {
const envBaseUrl = process.env.BASE_URL;
const envApiKey = process.env.API_KEY;
const baseUrl = providedBaseUrl || envBaseUrl;
const key = providedKey || envApiKey;
if (!baseUrl || !key) {
throw new Error(
"Base URL and API key must be provided either as parameters or environment variables (BASE_URL, API_KEY)",
);
}
try {
baseURL = baseUrl;
apiKey = key;
// Log the configuration we're about to create
log("debug", "Creating MultiBaas configuration", {
basePath: baseUrl,
apiKeyProvided: !!key,
});
configuration = new Configuration({
basePath: new URL("/api/v0", baseUrl).toString(),
accessToken: key,
});
// Initialize all API clients
try {
addressesApi = new AddressesApi(configuration);
} catch (error) {
log("error", "Failed to initialize AddressesApi", error);
}
try {
adminApi = new AdminApi(configuration);
} catch (error) {
log("error", "Failed to initialize AdminApi", error);
}
try {
chainsApi = new ChainsApi(configuration);
} catch (error) {
log("error", "Failed to initialize ChainsApi", error);
}
try {
contractsApi = new ContractsApi(configuration);
} catch (error) {
log("error", "Failed to initialize ContractsApi", error);
}
try {
eventQueriesApi = new EventQueriesApi(configuration);
} catch (error) {
log("error", "Failed to initialize EventQueriesApi", error);
}
try {
eventsApi = new EventsApi(configuration);
} catch (error) {
log("error", "Failed to initialize EventsApi", error);
}
try {
hsmApi = new HsmApi(configuration);
} catch (error) {
log("error", "Failed to initialize HsmApi", error);
}
try {
txmApi = new TxmApi(configuration);
} catch (error) {
log("error", "Failed to initialize TxmApi", error);
}
try {
webhooksApi = new WebhooksApi(configuration);
} catch (error) {
log("error", "Failed to initialize WebhooksApi", error);
}
} catch (error) {
log("error", "Failed to initialize MultiBaas client", error);
throw error;
}
}
// Helper function to check if clients are initialized
function checkInitialized(): void {
if (!configuration) {
throw new Error(
"API clients not initialized. Call initializeClient first.",
);
}
}
// Getter functions for each API client
export function getAddressesApi(): AddressesApi {
checkInitialized();
return addressesApi!;
}
export function getAdminApi(): AdminApi {
checkInitialized();
return adminApi!;
}
export function getChainsApi(): ChainsApi {
checkInitialized();
return chainsApi!;
}
export function getContractsApi(): ContractsApi {
checkInitialized();
return contractsApi!;
}
export function getEventQueriesApi(): EventQueriesApi {
checkInitialized();
return eventQueriesApi!;
}
export function getEventsApi(): EventsApi {
checkInitialized();
return eventsApi!;
}
export function getHsmApi(): HsmApi {
checkInitialized();
return hsmApi!;
}
export function getTxmApi(): TxmApi {
checkInitialized();
return txmApi!;
}
export function getWebhooksApi(): WebhooksApi {
checkInitialized();
return webhooksApi!;
}
export function getBaseURL(): string {
if (!baseURL) {
throw new Error("Base URL not set. Call initializeClient first.");
}
return baseURL;
}
export function getApiKey(): string {
if (!apiKey) {
throw new Error("API key not set. Call initializeClient first.");
}
return apiKey;
}
export function getConfiguration(): Configuration {
checkInitialized();
return configuration!;
}