Normalize and encode usernames for LeetPush API requests#137
Normalize and encode usernames for LeetPush API requests#137Sushmoy-Nandi wants to merge 1 commit intoLeetPushExtension:masterfrom
Conversation
There was a problem hiding this comment.
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.
| const normalizedUsername = username.trim(); | ||
| const response = await fetch( | ||
| `${BASE_URL}/${encodeURIComponent(normalizedUsername)}`, | ||
| ); |
There was a problem hiding this comment.
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.
| const normalizedUsername = username.trim(); | ||
| const response = await fetch( | ||
| `${BASE_URL}/userProfileCalendar/${encodeURIComponent(normalizedUsername)}`, | ||
| ); |
There was a problem hiding this comment.
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().
| `${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"); |
There was a problem hiding this comment.
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.
| else if (!response.ok) throw new Error("Failed to fetch user stats"); | |
| if (!response.ok) throw new Error("Failed to fetch user stats"); |
| const normalizedUsername = username.trim(); | ||
| const response = await fetch( | ||
| `${BASE_URL}/userProfileCalendar/${encodeURIComponent(normalizedUsername)}`, | ||
| ); |
There was a problem hiding this comment.
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.
Motivation
Description
encodeURIComponentthe provided username before calling both the stats (/${username}) and streak (/userProfileCalendar/${username}) endpoints inpopup/src/lib/leetpush.api.ts, and treat a 404 from the streak endpoint as "User not found".Testing