Offload stars counter to ycl function
All checks were successful
Deploy / build-and-publish (push) Successful in 5m9s
Deploy / deploy (push) Successful in 40s

This commit is contained in:
2025-07-06 19:34:59 +03:00
parent 530c3b0a93
commit e4392a6781

View File

@@ -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
}