-
-
Notifications
You must be signed in to change notification settings - Fork 23
Normalize and encode usernames for LeetPush API requests #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Normalize and encode usernames for LeetPush API requests #137
Conversation
There was a problem hiding this 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.
| const normalizedUsername = username.trim(); | ||
| const response = await fetch( | ||
| `${BASE_URL}/${encodeURIComponent(normalizedUsername)}`, | ||
| ); |
Copilot
AI
Feb 6, 2026
There was a problem hiding this comment.
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.
| const normalizedUsername = username.trim(); | ||
| const response = await fetch( | ||
| `${BASE_URL}/userProfileCalendar/${encodeURIComponent(normalizedUsername)}`, | ||
| ); |
Copilot
AI
Feb 6, 2026
There was a problem hiding this comment.
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().
| `${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"); |
Copilot
AI
Feb 6, 2026
There was a problem hiding this comment.
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.
| 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)}`, | ||
| ); |
Copilot
AI
Feb 6, 2026
There was a problem hiding this comment.
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.
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