All files / api suggestion.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 39 40 41 42 43 44 45 46 47 48  1x             1x     4x 4x         3x   3x 1x             2x 1x     1x                     1x          
import { CodeSuggestion } from "./types/suggestion";
import { AI_SUGGESTION_ENDPOINT } from "./endpoints";
 
/**
 * Fetches a suggestion by its ID.
 * @param {string} suggestionId - The ID of the suggestion to fetch.
 * @returns {Promise<{ data?: CodeSuggestion; error?: string }>} - The response from the server or an error message.
 */
export async function getSuggestionById(
  suggestionId: string
): Promise<{ data?: CodeSuggestion; error?: string }> {
  try {
    const response = await fetch(`${AI_SUGGESTION_ENDPOINT}/${suggestionId}`, {
      method: "GET",
      headers: { "Content-Type": "application/json" },
    });
 
    const data = await response.json();
 
    if (!response.ok) {
      return {
        error:
          data.message ||
          `Failed to get suggestion data: ${response.status} ${response.statusText}`,
      };
    }
 
    if (!data.data) {
      return { error: "Invalid response: expected suggestion data" };
    }
 
    return {
      data: {
        id: data.data.id,
        createdAt: data.data.created_at,
        hasBug: data.data.has_bug,
        suggestionArray: data.data.suggestion_array,
        model: data.data.model,
        prompt: data.data.prompt,
      },
    };
  } catch (err) {
    return {
      error: err instanceof Error ? err.message : "Unknown error occurred",
    };
  }
}