Review Details

TypeScript / Full Review

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

Code Quality Score

75/100

Summary

The code generally works but lacks error handling and type safety. The use of 'any' for project type is not ideal, and string concatenation can be improved.

Issues Found

Detailed findings

4 issues

Lack of error handling

high

Line 2

The fetch call does not handle network errors which might lead to unhandled promise rejections or unexpected behavior if the user does not exist or the request fails.

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

Use of 'any' type

medium

Line 6

The code uses 'any' for the project type which defeats the purpose of TypeScript's type safety features.

Suggested fix: Define an interface for the Project type and use it instead of 'any'.

String concatenation

medium

Line 2

Using string concatenation for the URL can lead to errors if userId contains unexpected characters or if the base URL changes.

Suggested fix: Use template literals for the fetch URL: `fetch(`/api/users/${userId}`)`.

Redundant comparison

low

Line 4

The comparison `user.isAdmin == true` can be simplified, as `user.isAdmin` will evaluate to true if it is a truthy value.

Suggested fix: Change to `if (user.isAdmin)`.

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 = await response.json();

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

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

interface Project {
  title: string;
}