UNPKG

@revmax/agent-sdk

Version:

Official Node.js SDK for RevMax - billing, customer management, and usage tracking

150 lines 5.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RevMaxClient = void 0; const api_1 = require("./utils/api"); const logger_1 = require("./utils/logger"); const auth_1 = require("./auth"); const resources_1 = require("./resources"); const errors_1 = require("./utils/errors"); /** * Main RevMax client class */ class RevMaxClient { /** * Creates a new RevMax client instance without connecting * You must call connect() before using any API methods * * @param apiKey - API key for authentication * @param options - Client options */ constructor(apiKey, options = {}) { /** * Organization information from API key verification */ this._orgInfo = null; this.logger = new logger_1.Logger(options.logging); this.logger.info('Creating RevMaxClient...'); // Create authentication method this.auth = (0, auth_1.createAuth)(apiKey); // Create API client this.apiClient = new api_1.ApiClient(this.auth, options, this.logger); // Initialize resources (they won't work until connect is called) this.customers = new resources_1.Customers(this.apiClient, this.logger); this.usage = new resources_1.Usage(this.apiClient, this.logger); } /** * Connect to the RevMax API and verify credentials * This must be called once before using any API methods * * @returns Promise resolving to the client instance for chaining * @throws RevMaxAuthenticationError if API key is invalid * @throws RevMaxInitializationError for other initialization errors */ async connect() { try { this.logger.info('Connecting to RevMax API and verifying API key...'); const result = await this.apiClient.get('/verify'); if (!result || !result.organization || !result.organization.id) { throw new errors_1.RevMaxInitializationError('Unexpected response format from API key verification'); } // Set organization info this._orgInfo = result.organization; // Set auth as verified if the method exists if (this.auth.setVerified) { this.auth.setVerified(true); } this.logger.info('Successfully connected to RevMax API', { organizationId: this._orgInfo.id, organizationName: this._orgInfo.name, }); return this; } catch (error) { // Handle API key verification errors const statusCode = error.statusCode || (error.response && error.response.status); const errorMessage = error.message || 'Unknown error'; const requestId = error.requestId || 'unknown'; this.logger.error(`Connection failed: ${errorMessage}`, { statusCode, requestId, error, }); if (statusCode === 401) { throw new errors_1.RevMaxAuthenticationError('Invalid API key. Please check your API key and try again.', { requestId }); } throw new errors_1.RevMaxInitializationError(`Connection failed: ${errorMessage}`, { requestId, }); } } /** * Get organization information * @returns Organization information from API key verification * @throws Error if organization info is not available */ getOrganization() { if (!this._orgInfo) { throw new Error('Organization information is not available. Make sure the API key is valid and you have called connect()'); } return { ...this._orgInfo }; // Return a copy to prevent mutation } /** * Track event for a customer (shorthand method) * @param params - Event tracking parameters * @returns Tracked event data */ async trackEvent(params) { return this.usage.trackEvent(params); } /** * Re-verify the API key * Useful if you suspect the API key status might have changed * * @returns Updated organization information */ async verifyApiKey() { try { this.logger.info('Re-verifying API key'); const result = await this.apiClient.get('/verify'); if (!result || !result.organization || !result.organization.id) { throw new errors_1.RevMaxInitializationError('Unexpected response format from API key verification'); } // Update stored organization info with proper type checking this._orgInfo = result.organization; this.logger.info('API key verification successful', { organization: result.organization, }); return { ...this._orgInfo }; } catch (error) { // Handle API key verification errors const statusCode = error.statusCode || (error.response && error.response.status); const errorMessage = error.message || 'Unknown error'; const requestId = error.requestId || 'unknown'; this.logger.error(`API key verification failed: ${errorMessage}`, { statusCode, requestId, error, }); if (statusCode === 401) { throw new errors_1.RevMaxAuthenticationError('Invalid API key. Please check your API key and try again.', { requestId }); } throw error; } } /** * Get current telemetry statistics * @returns Telemetry statistics */ getTelemetryStats() { return this.apiClient.getTelemetryStats(); } /** * Reset telemetry statistics */ resetTelemetryStats() { return this.apiClient.resetTelemetryStats(); } } exports.RevMaxClient = RevMaxClient; //# sourceMappingURL=client.js.map