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) {
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
.update(users)

View File

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