Skip to content

Conversation

@Sushmoy-Nandi
Copy link

Motivation

  • Prevent false “User not found” errors caused by leading/trailing whitespace or characters that need URL-encoding when querying the LeetPush API and ensure the streak endpoint surfaces 404 as a user-not-found error.

Description

  • Trim and encodeURIComponent the provided username before calling both the stats (/${username}) and streak (/userProfileCalendar/${username}) endpoints in popup/src/lib/leetpush.api.ts, and treat a 404 from the streak endpoint as "User not found".

Testing

  • No automated tests were run for this change.

Copilot AI review requested due to automatic review settings February 6, 2026 17:46
@Sushmoy-Nandi Sushmoy-Nandi changed the title Normalize and encode usernames for LeetPush API requests #1 Normalize and encode usernames for LeetPush API requests Feb 6, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves reliability of LeetPush API requests by normalizing usernames before constructing request URLs, reducing “User not found” false negatives and aligning error handling between endpoints.

Changes:

  • Trim and URL-encode usernames before calling the user stats endpoint.
  • Trim and URL-encode usernames before calling the user streak endpoint.
  • Treat a 404 from the streak endpoint as a “User not found” error.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +28 to +31
const normalizedUsername = username.trim();
const response = await fetch(
`${BASE_URL}/${encodeURIComponent(normalizedUsername)}`,
);
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After trimming, normalizedUsername can become an empty string (e.g. user enters only whitespace). In that case this will call ${BASE_URL}/ (wrong endpoint) and likely produce confusing errors. Consider validating normalizedUsername after trim() and throwing a user-facing error before issuing the request.

Copilot uses AI. Check for mistakes.
Comment on lines +46 to +49
const normalizedUsername = username.trim();
const response = await fetch(
`${BASE_URL}/userProfileCalendar/${encodeURIComponent(normalizedUsername)}`,
);
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as fetchUserStats: username.trim() can yield an empty string, which would hit /userProfileCalendar/ and return an unexpected response. Add a guard for empty normalizedUsername before calling fetch().

Copilot uses AI. Check for mistakes.
`${BASE_URL}/${encodeURIComponent(normalizedUsername)}`,
);
if (response.status === 404) throw new Error("User not found");
else if (!response.ok) throw new Error("Failed to fetch user stats");
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if after a throw is unnecessary and inconsistent with the style used elsewhere in this file (e.g. fetchDailyProblem). Consider using two standalone if statements (or early returns) to reduce nesting.

Suggested change
else if (!response.ok) throw new Error("Failed to fetch user stats");
if (!response.ok) throw new Error("Failed to fetch user stats");

Copilot uses AI. Check for mistakes.
Comment on lines +46 to +49
const normalizedUsername = username.trim();
const response = await fetch(
`${BASE_URL}/userProfileCalendar/${encodeURIComponent(normalizedUsername)}`,
);
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The username normalization/encoding logic is duplicated in both fetchUserStats and fetchUserStreak. Consider extracting a small helper (e.g. normalizeUsernameForPath) so future changes (like additional normalization rules) only need to be made in one place.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant