Offload stars counter to ycl function
This commit is contained in:
@@ -1,33 +1,5 @@
|
|||||||
export default async function getStarsForUser(username: string) {
|
export default async function getStars(username: string) {
|
||||||
const userResponse = await request(`/users/${username}`);
|
const resp = await fetch(`https://user-stars.kopyl.dev/${username}`)
|
||||||
if (!userResponse || !userResponse.public_repos) {
|
const body = await resp.json()
|
||||||
throw new Error(userResponse ? userResponse.message : 'User not found.');
|
return body.stars
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user