CodeCritic AI logo

CodeCritic AI

Premium AI code review workspace

AC
Review Details

TypeScript / Full Review

TypeScript
Full Review
Apr 2, 2026, 8:06 PM

Code Quality Score

85/100

Summary

The code is overall well-structured, but some improvements in error handling and readability can be made.

Issues Found

Detailed findings

3 issues

Error Handling Improvement

medium

Line 11

The error handling within the catch block logs the error but does not provide meaningful feedback in the response. Returning an empty array with a 200 status may mislead consumers regarding the request's success.

Suggested fix: Return a 500 status code along with a message indicating an internal server error, e.g., `return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });`.

Magic String Usage

low

Line 7

The `select` method uses a magic string for property names. This reduces maintainability because if a property name changes, it won't be reflected automatically here and can lead to errors.

Suggested fix: Define the fields in a separate constant or interface that can be reused.

Lack of Type Annotations

low

Line 1

The function does not specify return type annotations, which reduces type safety and clarity in TypeScript.

Suggested fix: Annotate the function with a return type, e.g., `Promise<NextResponse>`.

Improved Code

Suggested rewrite

export async function GET(): Promise<NextResponse> {
  try {
    await connectToDatabase();

    const reviews = await Review.find()
      .sort({ createdAt: -1 })
      .limit(5)
      .select("_id code language reviewType score createdAt")
      .lean();

    const recentReviews: ReviewHistoryItem[] = reviews.map((review, index) => ({
      id: String(review._id),
      name: getReviewName(review.code, index),
      language: review.language,
      reviewType: review.reviewType,
      score: review.score,
      date: formatReviewDate(review.createdAt),
      status: getReviewStatus(review.score),
    }));

    return NextResponse.json(recentReviews);
  } catch (error) {
    console.error("Recent reviews error:", error);
    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
  }
}