import fetch from 'node-fetch'; export class MetricsService { constructor(apiKey, apiRevision) { this.apiKey = apiKey; this.apiRevision = apiRevision; this.baseUrl = 'https://a.klaviyo.com/api'; } async getMetrics() { try { const response = await fetch(`${this.baseUrl}/metrics/`, { headers: { 'Authorization': `Klaviyo-API-Key ${this.apiKey}`, 'revision': this.apiRevision, 'Content-Type': 'application/json', 'Accept': 'application/json' } }); if (!response.ok) { const errorData = await response.json(); console.error('[MetricsService] API Error:', errorData); throw new Error(`Klaviyo API error: ${response.status} ${response.statusText}`); } const data = await response.json(); // Sort the results by name before returning if (data.data) { data.data.sort((a, b) => a.attributes.name.localeCompare(b.attributes.name)); } return data; } catch (error) { console.error('[MetricsService] Error fetching metrics:', error); throw error; } } }