From e4392a6781ee2c68554aa9faab6929b45239f029 Mon Sep 17 00:00:00 2001 From: Anatoly Kopyl Date: Sun, 6 Jul 2025 19:34:59 +0300 Subject: [PATCH] Offload stars counter to ycl function --- src/lib/getStars.ts | 36 ++++-------------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/src/lib/getStars.ts b/src/lib/getStars.ts index e9c0c95..20866f3 100644 --- a/src/lib/getStars.ts +++ b/src/lib/getStars.ts @@ -1,33 +1,5 @@ -export default async function getStarsForUser(username: string) { - const userResponse = await request(`/users/${username}`); - if (!userResponse || !userResponse.public_repos) { - throw new Error(userResponse ? userResponse.message : 'User not found.'); - } - - const pages = Math.ceil(userResponse.public_repos / 100); - let repos = []; - const fetchReposPromises = []; - - for (let i = 1; i <= pages; i++) { - fetchReposPromises.push(request(`/users/${username}/repos?per_page=100&page=${i}`)); - } - - const reposResponses = await Promise.all(fetchReposPromises); - repos = reposResponses.flat(); - - return starSum(repos); -} - -async function request(url) { - const headers= { - 'User-Agent': 'GitHub StarCounter', - // Authorization: `Basic ${Buffer.from(auth).toString('base64')}` - }; - - const response = await fetch(`https://api.github.com${url}`, { headers }); - return response.json() -} - -async function starSum(repos) { - return repos.reduce((acc, curr) => acc + curr.stargazers_count, 0); +export default async function getStars(username: string) { + const resp = await fetch(`https://user-stars.kopyl.dev/${username}`) + const body = await resp.json() + return body.stars }