UNPKG

@fairmint/canton-node-sdk

Version:
155 lines 5.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseClient = void 0; const AuthenticationManager_1 = require("./auth/AuthenticationManager"); const EnvLoader_1 = require("./config/EnvLoader"); const errors_1 = require("./errors"); const HttpClient_1 = require("./http/HttpClient"); 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, or config missing APIs, use EnvLoader to get defaults if (!config) { // No config at all - use EnvLoader defaults const defaultConfig = EnvLoader_1.EnvLoader.getConfig(apiType); defaultConfig.logger = new FileLogger_1.FileLogger(); this.clientConfig = defaultConfig; } else if (!config.apis?.[apiType]) { // Config provided but missing API configuration - merge with EnvLoader defaults const options = { network: config.network, }; if (config.provider) { options.provider = config.provider; } const defaultConfig = EnvLoader_1.EnvLoader.getConfig(apiType, options); this.clientConfig = { ...defaultConfig, ...config, apis: { ...defaultConfig.apis, ...config.apis, }, logger: config.logger ?? new FileLogger_1.FileLogger(), }; } else { // Config fully provided this.clientConfig = config; } // Validate that the required API configuration is present if (!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; //# sourceMappingURL=BaseClient.js.map