@fairmint/canton-node-sdk
Version:
Canton Node SDK
190 lines • 7.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleBaseClient = exports.BaseClient = void 0;
const AuthenticationManager_1 = require("./auth/AuthenticationManager");
const HttpClient_1 = require("./http/HttpClient");
const errors_1 = require("./errors");
const EnvLoader_1 = require("./config/EnvLoader");
const FileLogger_1 = require("./logging/FileLogger");
/** Abstract base class providing common functionality for all API clients */
class BaseClient {
constructor(apiType, config) {
this.apiType = apiType;
// If no config provided, use default configuration with EnvLoader and FileLogger
this.clientConfig =
config ||
(() => {
const defaultConfig = EnvLoader_1.EnvLoader.getConfig(apiType);
defaultConfig.logger = new FileLogger_1.FileLogger();
return defaultConfig;
})();
// Validate that the required API configuration is present
if (!this.clientConfig.apis || !this.clientConfig.apis[apiType]) {
throw new errors_1.ConfigurationError(`API configuration not found for ${apiType}`);
}
// Build provider configuration from the provided config
this.config = {
providerName: this.clientConfig.provider
? `${this.clientConfig.provider}_${this.clientConfig.network}`
: this.clientConfig.network,
authUrl: this.clientConfig.authUrl || '',
apis: {
[apiType]: this.clientConfig.apis[apiType],
},
};
// Initialize authentication manager
const apiConfig = this.config.apis[this.apiType];
if (!apiConfig) {
throw new errors_1.ConfigurationError(`API configuration not found for ${this.apiType}`);
}
this.authManager = new AuthenticationManager_1.AuthenticationManager(this.config.authUrl, apiConfig.auth, this.clientConfig.logger);
// Initialize HTTP client with logger
this.httpClient = new HttpClient_1.HttpClient(this.clientConfig.logger);
}
async authenticate() {
const token = await this.authManager.authenticate();
if (token) {
this.httpClient.setBearerToken(token);
}
return token;
}
async makeGetRequest(url, config = {}) {
// Ensure we have a valid token if authentication is required
if (config.includeBearerToken) {
await this.authenticate();
}
return this.httpClient.makeGetRequest(url, config);
}
async makePostRequest(url, data, config = {}) {
// Ensure we have a valid token if authentication is required
if (config.includeBearerToken) {
await this.authenticate();
}
return this.httpClient.makePostRequest(url, data, config);
}
async makeDeleteRequest(url, config = {}) {
// Ensure we have a valid token if authentication is required
if (config.includeBearerToken) {
await this.authenticate();
}
return this.httpClient.makeDeleteRequest(url, config);
}
async makePatchRequest(url, data, config = {}) {
// Ensure we have a valid token if authentication is required
if (config.includeBearerToken) {
await this.authenticate();
}
return this.httpClient.makePatchRequest(url, data, config);
}
getLogger() {
return this.clientConfig.logger;
}
getApiUrl() {
const apiConfig = this.config.apis[this.apiType];
return apiConfig?.apiUrl || '';
}
getPartyId() {
// Use provided configuration first, fall back to API config
const apiConfig = this.config.apis[this.apiType];
if (apiConfig?.partyId) {
return apiConfig.partyId;
}
return this.clientConfig.partyId || '';
}
getUserId() {
// Use provided configuration first, fall back to API config
const apiConfig = this.config.apis[this.apiType];
if (apiConfig?.userId) {
return apiConfig.userId;
}
return this.clientConfig.userId;
}
getManagedParties() {
// For now, always use environment variables for managed parties
// as this is not typically provided in the API config
return this.clientConfig.managedParties || [];
}
buildPartyList(additionalParties = []) {
const managedParties = this.getManagedParties();
const partyId = this.getPartyId();
const partyList = [...additionalParties, ...managedParties];
if (partyId && !partyList.includes(partyId)) {
partyList.push(partyId);
}
return [...new Set(partyList)];
}
getNetwork() {
return this.clientConfig.network;
}
getProvider() {
return this.clientConfig.provider;
}
getProviderName() {
return this.config.providerName;
}
getAuthUrl() {
return this.config.authUrl;
}
getApiType() {
return this.apiType;
}
}
exports.BaseClient = BaseClient;
/** Simplified base class for APIs that don't require authentication or provider configuration */
class SimpleBaseClient {
constructor(apiType, config) {
this.apiType = apiType;
// If no config provided, use default configuration with EnvLoader and FileLogger
this.clientConfig =
config ||
(() => {
const defaultConfig = EnvLoader_1.EnvLoader.getConfig(apiType);
defaultConfig.logger = new FileLogger_1.FileLogger();
return defaultConfig;
})();
// Validate that the required API configuration is present
if (!this.clientConfig.apis || !this.clientConfig.apis[apiType]) {
throw new errors_1.ConfigurationError(`API configuration not found for ${apiType}`);
}
// Get the API config
const apiConfig = this.clientConfig.apis[apiType];
if (!apiConfig) {
throw new errors_1.ConfigurationError(`API configuration not found for ${this.apiType}`);
}
// For Lighthouse API, we expect LighthouseApiConfig
if (apiType === 'LIGHTHOUSE_API') {
this.apiConfig = apiConfig;
}
else {
throw new errors_1.ConfigurationError(`Invalid API type for SimpleBaseClient: ${apiType}`);
}
// Initialize HTTP client with logger
this.httpClient = new HttpClient_1.HttpClient(this.clientConfig.logger);
}
async makeGetRequest(url, config = {}) {
return this.httpClient.makeGetRequest(url, config);
}
async makePostRequest(url, data, config = {}) {
return this.httpClient.makePostRequest(url, data, config);
}
async makeDeleteRequest(url, config = {}) {
return this.httpClient.makeDeleteRequest(url, config);
}
async makePatchRequest(url, data, config = {}) {
return this.httpClient.makePatchRequest(url, data, config);
}
getApiUrl() {
return this.apiConfig.apiUrl;
}
getPartyId() {
return this.clientConfig.partyId || '';
}
getNetwork() {
return this.clientConfig.network;
}
getApiType() {
return this.apiType;
}
}
exports.SimpleBaseClient = SimpleBaseClient;
//# sourceMappingURL=BaseClient.js.map