File size: 9,161 Bytes
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
 * API Helper Utilities
 * Shared utilities for API requests across all pages
 */

export class APIHelper {
  /**
   * Get request headers with optional authorization
   * @returns {Object} Headers object
   */
  static getHeaders() {
    const token = localStorage.getItem('HF_TOKEN');
    const headers = {
      'Content-Type': 'application/json'
    };
    
    if (token && token.trim()) {
      // Check if token is expired
      if (this.isTokenExpired(token)) {
        console.warn('[APIHelper] Token expired, removing from storage');
        localStorage.removeItem('HF_TOKEN');
      } else {
        headers['Authorization'] = `Bearer ${token}`;
      }
    }
    
    return headers;
  }

  /**
   * Check if JWT token is expired
   * @param {string} token - JWT token
   * @returns {boolean} True if expired
   */
  static isTokenExpired(token) {
    try {
      // Basic JWT expiration check
      const parts = token.split('.');
      if (parts.length !== 3) return false; // Not a JWT
      
      const payload = JSON.parse(atob(parts[1]));
      if (!payload.exp) return false; // No expiration
      
      const now = Math.floor(Date.now() / 1000);
      return payload.exp < now;
    } catch (e) {
      console.warn('[APIHelper] Token validation error:', e);
      return false;
    }
  }

  /**
   * Fetch data from API with automatic error handling
   * @param {string} url - API endpoint
   * @param {Object} options - Fetch options
   * @returns {Promise<any>} Response data
   */
  static async fetchAPI(url, options = {}) {
    const headers = this.getHeaders();
    
    try {
      const response = await fetch(url, {
        ...options,
        headers: {
          ...headers,
          ...options.headers
        }
      });

      if (!response.ok) {
        throw new Error(`HTTP ${response.status}: ${response.statusText}`);
      }

      const contentType = response.headers.get('content-type');
      if (contentType && contentType.includes('application/json')) {
        return await response.json();
      }
      
      return await response.text();
    } catch (error) {
      console.error(`[APIHelper] Fetch error for ${url}:`, error);
      
      // Return fallback data instead of throwing
      return this._getFallbackData(url, error);
    }
  }

  /**
   * Get fallback data for failed API requests
   * @private
   */
  static _getFallbackData(url, error) {
    // Return appropriate fallback based on URL
    if (url.includes('/resources/summary') || url.includes('/resources')) {
      return {
        success: false,
        error: error.message,
        summary: {
          total_resources: 0,
          free_resources: 0,
          models_available: 0,
          total_api_keys: 0,
          categories: {}
        },
        fallback: true
      };
    }
    
    if (url.includes('/models/status')) {
      return {
        success: false,
        error: error.message,
        status: 'error',
        status_message: `Error: ${error.message}`,
        models_loaded: 0,
        models_failed: 0,
        hf_mode: 'unknown',
        transformers_available: false,
        fallback: true,
        timestamp: new Date().toISOString()
      };
    }
    
    if (url.includes('/models/summary') || url.includes('/models')) {
      return {
        ok: false,
        error: error.message,
        summary: {
          total_models: 0,
          loaded_models: 0,
          failed_models: 0,
          hf_mode: 'error',
          transformers_available: false
        },
        categories: {},
        health_registry: [],
        fallback: true,
        timestamp: new Date().toISOString()
      };
    }
    
    if (url.includes('/health') || url.includes('/status')) {
      return {
        status: 'offline',
        healthy: false,
        error: error.message,
        fallback: true
      };
    }
    
    // Generic fallback
    return {
      error: error.message,
      fallback: true,
      data: null
    };
  }

  /**
   * Extract array from various response formats
   * @param {any} data - API response data
   * @param {string[]} keys - Possible keys containing array data
   * @returns {Array} Extracted array or empty array
   */
  static extractArray(data, keys = ['data', 'items', 'results', 'list']) {
    // Direct array
    if (Array.isArray(data)) {
      return data;
    }

    // Check common keys
    for (const key of keys) {
      if (data && Array.isArray(data[key])) {
        return data[key];
      }
    }

    // Object values
    if (data && typeof data === 'object' && !Array.isArray(data)) {
      const values = Object.values(data);
      if (values.length > 0 && values.every(v => typeof v === 'object')) {
        return values;
      }
    }

    console.warn('[APIHelper] Could not extract array from:', data);
    return [];
  }

  /**
   * Check API health
   * @returns {Promise<Object>} Health status
   */
  static async checkHealth() {
    try {
      const controller = new AbortController();
      const timeoutId = setTimeout(() => controller.abort(), 5000);
      
      const response = await fetch('/api/health', {
        signal: controller.signal,
        cache: 'no-cache'
      });
      
      clearTimeout(timeoutId);
      
      if (response.ok) {
        const data = await response.json();
        return {
          status: 'online',
          healthy: true,
          data: data
        };
      } else {
        return {
          status: 'degraded',
          healthy: false,
          httpStatus: response.status
        };
      }
    } catch (error) {
      return {
        status: 'offline',
        healthy: false,
        error: error.message
      };
    }
  }

  /**
   * Setup periodic health monitoring
   * @param {Function} callback - Callback function with health status
   * @param {number} interval - Check interval in ms (default: 30000)
   * @returns {number} Interval ID
   */
  static monitorHealth(callback, interval = 30000) {
    // Initial check
    this.checkHealth().then(callback);
    
    // Periodic checks
    return setInterval(async () => {
      if (!document.hidden) {
        const health = await this.checkHealth();
        callback(health);
      }
    }, interval);
  }

  /**
   * Show toast notification
   * @param {string} message - Message to display
   * @param {string} type - Type: success, error, warning, info
   * @param {number} duration - Display duration in ms
   */
  static showToast(message, type = 'info', duration = 3000) {
    const colors = {
      success: '#22c55e',
      error: '#ef4444',
      warning: '#f59e0b',
      info: '#3b82f6'
    };
    
    const toast = document.createElement('div');
    toast.style.cssText = `
      position: fixed;
      top: 20px;
      right: 20px;
      padding: 12px 20px;
      border-radius: 8px;
      background: ${colors[type] || colors.info};
      color: white;
      font-weight: 500;
      z-index: 9999;
      box-shadow: 0 4px 12px rgba(0,0,0,0.3);
      animation: slideIn 0.3s ease;
    `;
    toast.textContent = message;
    
    document.body.appendChild(toast);
    setTimeout(() => {
      toast.style.animation = 'slideOut 0.3s ease';
      setTimeout(() => toast.remove(), 300);
    }, duration);
  }

  /**
   * Format number with locale
   * @param {number} num - Number to format
   * @param {Object} options - Intl.NumberFormat options
   * @returns {string} Formatted number
   */
  static formatNumber(num, options = {}) {
    return new Intl.NumberFormat('en-US', options).format(num);
  }

  /**
   * Format currency
   * @param {number} amount - Amount to format
   * @param {string} currency - Currency code (default: USD)
   * @returns {string} Formatted currency
   */
  static formatCurrency(amount, currency = 'USD') {
    return this.formatNumber(amount, {
      style: 'currency',
      currency: currency,
      minimumFractionDigits: 2,
      maximumFractionDigits: 2
    });
  }

  /**
   * Format percentage
   * @param {number} value - Value to format
   * @param {number} decimals - Decimal places
   * @returns {string} Formatted percentage
   */
  static formatPercentage(value, decimals = 2) {
    return `${value >= 0 ? '+' : ''}${value.toFixed(decimals)}%`;
  }

  /**
   * Debounce function
   * @param {Function} func - Function to debounce
   * @param {number} wait - Wait time in ms
   * @returns {Function} Debounced function
   */
  static debounce(func, wait = 300) {
    let timeout;
    return function executedFunction(...args) {
      const later = () => {
        clearTimeout(timeout);
        func(...args);
      };
      clearTimeout(timeout);
      timeout = setTimeout(later, wait);
    };
  }

  /**
   * Throttle function
   * @param {Function} func - Function to throttle
   * @param {number} limit - Time limit in ms
   * @returns {Function} Throttled function
   */
  static throttle(func, limit = 300) {
    let inThrottle;
    return function executedFunction(...args) {
      if (!inThrottle) {
        func(...args);
        inThrottle = true;
        setTimeout(() => (inThrottle = false), limit);
      }
    };
  }
}

export default APIHelper;