Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions popup/src/lib/leetpush.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export const fetchDailyProblem = async (): Promise<DailyProblemI> => {
* @returns The user stats
**/
export const fetchUserStats = async (username: string): Promise<UserStatsI> => {
const response = await fetch(`${BASE_URL}/${username}`);
const normalizedUsername = username.trim();
const response = await fetch(
`${BASE_URL}/${encodeURIComponent(normalizedUsername)}`,
);
Comment on lines +28 to +31
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.
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.

Expand All @@ -40,8 +43,12 @@ export const fetchUserStats = async (username: string): Promise<UserStatsI> => {
export const fetchUserStreak = async (
username: string,
): Promise<UserStreakI> => {
const response = await fetch(`${BASE_URL}/userProfileCalendar/${username}`);
const normalizedUsername = username.trim();
const response = await fetch(
`${BASE_URL}/userProfileCalendar/${encodeURIComponent(normalizedUsername)}`,
);
Comment on lines +46 to +49
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.
Comment on lines +46 to +49
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.
if (response.status === 404) throw new Error("User not found");
if (!response.ok) throw new Error("Failed to fetch user streak");

return await response.json();
};
};
Loading