-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.ts
43 lines (37 loc) · 1.16 KB
/
github.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { NextApiRequest, NextApiResponse } from 'next';
const GH_HEADERS = new Headers({
Authorization: `Bauer ${process.env.GITHUB_TOKEN}`
});
const starReducer = (acc: number, repo: { stargazers_count: number }) => {
const result = acc + repo.stargazers_count;
return result;
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const [userResponse, repositoriesResponse] = await Promise.all([
fetch('https://api.github.com/users/shramkoweb', {
headers: GH_HEADERS
}),
fetch('https://api.github.com/users/shramkoweb/repos?per_page=100', {
headers: GH_HEADERS
})
]);
const [user, repos] = await Promise.all([
userResponse.json(),
repositoriesResponse.json()
]);
const mineRepos = repos.filter((repo: { fork: boolean }) => !repo.fork);
const stars = mineRepos.reduce(starReducer, 0);
// With edge error we have error
// https://github.com/getsentry/sentry-javascript/issues/5667
return res.status(200).json({
stars,
followers: user.followers
});
} catch ({ message }) {
return res.status(500).json({ message });
}
}