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

CodeCritic AI
Premium AI code review workspace
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
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.
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.
Line 1
The function does not specify return type annotations, which reduces type safety and clarity in TypeScript.
Improved Code
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 });
}
}