Review Details

TypeScript / Full Review

TypeScript
Full Review
Jul 9, 2026, 5:07 PM

Code Quality Score

75/100

Summary

The code has a good structure and performs well but has some areas for improvement regarding error handling and type safety.

Issues Found

Detailed findings

3 issues

Lack of Error Handling

high

Line 2-3

The code doesn't handle potential errors from the fetch operation or JSON parsing. If the request fails or returns an invalid JSON, it will throw an unhandled promise rejection.

Suggested fix: Wrap the fetch call in a try-catch block to handle network errors appropriately.

Potential Type Safety Issues

medium

Line 4-5

The type of the response from the API is not defined. Using 'any' for projects can lead to runtime errors as you expect specific properties.

Suggested fix: Define an interface for the user object and use it to enforce type safety throughout the function.

Unnecessary Comparison to True

low

Line 6

The comparison 'user.isAdmin == true' is unnecessary. It can be simplified to 'user.isAdmin'.

Suggested fix: Change 'if (user.isAdmin == true)' to 'if (user.isAdmin)'. This improves readability.

Improved Code

Suggested rewrite

export async function getUserProfile(userId: string) {
  try {
    const response = await fetch("/api/users/" + userId);
    if (!response.ok) {
      throw new Error("Network response was not ok");
    }
    const user: User = await response.json();

    if (user.isAdmin) {
      console.log("admin user");
    }

    return {
      name: user.name,
      projects: user.projects.map((project) => project.title),
    };
  } catch (error) {
    console.error("Failed to fetch user profile:", error);
    throw error;
  }
}

interface User {
  name: string;
  isAdmin: boolean;
  projects: { title: string }[];
}