All files / api stats.ts

100% Statements 11/11
66.66% Branches 4/6
100% Functions 1/1
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 392x             2x       4x 4x         3x   3x 1x             2x 1x     1x     1x        
import { LOG_ENDPOINT } from "./endpoints";
import { AIStats } from "./types/suggestion";
 
/**
 * Fetches AI usage statistics from the server.
 * @returns {Promise<{ data?: AIStats[]; error?: string }>} - The response from the server or an error message.
 */
export async function getAIUsageStats(): Promise<{
  data?: AIStats[];
  error?: string;
}> {
  try {
    const response = await fetch(`${LOG_ENDPOINT}/ai`, {
      method: "GET",
      headers: { "Content-Type": "application/json" },
    });
 
    const data = await response.json();
 
    if (!response.ok) {
      return {
        error:
          data.message ||
          `Failed to get AI usage stats: ${response.status} ${response.statusText}`,
      };
    }
 
    if (!data.data) {
      return { error: "Invalid response: expected usage data" };
    }
 
    return { data: data.data as AIStats[] };
 
  } catch (err) {
    return {
      error: err instanceof Error ? err.message : "Unknown error occurred",
    };
  }
}