fix: fix followers amount for user with thousands of followers

This commit is contained in:
Khairul Hidayat 2024-08-10 00:55:58 +07:00
parent c983044e11
commit 9ed2984dbd
2 changed files with 19 additions and 5 deletions

View File

@ -13,7 +13,13 @@ export const fetchUserProfile = async (data: FetchUserProfileType) => {
if (!user) { if (!user) {
throw new Error("User not found!"); throw new Error("User not found!");
} }
const details = await github.getUser(user.username);
const accessToken = user.accessToken || import.meta.env.GITHUB_DEFAULT_TOKEN;
const details = await github.getUser(user.username, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
await db await db
.update(users) .update(users)

View File

@ -26,8 +26,8 @@ const selectors = {
}; };
const github = { const github = {
async getUser(username: string) { async getUser(username: string, options?: Partial<FetchOptions>) {
const response = await this.fetch(username); const response = await this.fetch(username, options);
const $ = cheerio.load(response); const $ = cheerio.load(response);
const name = $(selectors.user.name).text().trim(); const name = $(selectors.user.name).text().trim();
@ -43,13 +43,21 @@ const github = {
achievements.push({ name, image }); achievements.push({ name, image });
}); });
const user = await github.fetch(`users/${username}`, {
ghApi: true,
headers: {
accept: "application/json",
...(options?.headers || {}),
},
});
return { return {
name: name || username, name: name || username,
avatar, avatar,
username, username,
location, location,
followers, followers: user.followers || followers,
following, following: user.following || following,
achievements, achievements,
}; };
}, },