UNPKG

@revmax/agent-sdk

Version:

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

72 lines 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Usage = void 0; /** * Usage resource for tracking usage */ class Usage { /** * Create a new usage resource * @param client - API client * @param logger - Logger instance */ constructor(client, logger) { this.basePath = "/usage"; this.client = client; this.logger = logger; } /** * Track events for a customer - supports both single record and batch operations * @param params - Event tracking parameters (single record or batch) * @returns Tracked event data */ async trackEvent(params) { // Check if this is a single record or a batch operation if ("records" in params) { // This is a batch operation this.logger.info(`Recording batch usage for ${params.records.length} records`); // Format any Date objects in the records to ISO strings const formattedRecords = params.records.map((record) => this.formatRecord(record)); return this.client.post(`${this.basePath}/record`, { records: formattedRecords, }); } else { // This is a single record - convert it to the batch format this.logger.info("Recording usage", { agent: params.agentId, customer: params.customerExternalId, signal: params.signalName, quantity: params.quantity, }); // Format the record const formattedRecord = this.formatRecord(params); // Submit as a batch with one record const response = await this.client.post(`${this.basePath}/record`, { records: [formattedRecord], }); // If it's a batch response but only contains one record, extract and return the single result for backward compatibility if (response.results && response.results.length === 1 && response.results[0].success && response.results[0].responseData) { return response.results[0].responseData; } return response; } } /** * Format a usage record, converting Date objects to ISO strings * @param record - The usage record to format * @returns The formatted record */ formatRecord(record) { const formattedRecord = { ...record }; if (formattedRecord.usageDate instanceof Date) { formattedRecord.usageDate = formattedRecord.usageDate.toISOString(); } return formattedRecord; } } exports.Usage = Usage; //# sourceMappingURL=usage.js.map