const axios = require("axios"); require("dotenv").config(); async function fetchStats(username) { const res = await axios({ url: "https://api.github.com/graphql", method: "post", headers: { Authorization: `bearer ${process.env.GITHUB_TOKEN}`, }, data: { query: ` query userInfo($login: String!) { user(login: $login) { name repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) { totalCount } pullRequests(first: 100) { totalCount } repositories(first: 100) { nodes { stargazers { totalCount } } } } } `, variables: { login: username, }, }, }); const stats = { totalStars: 0, contributedTo: 0, name: "", totalPRs: 0 }; if (res.data.error) return stats; const user = res.data.data.user; stats.totalStars = user.repositories.nodes.reduce((prev, curr) => { return prev + curr.stargazers.totalCount; }, 0); stats.totalPRs = user.pullRequests.totalCount; stats.contributedTo = user.repositoriesContributedTo.totalCount; stats.name = user.name; return stats; } module.exports = async (req, res) => { const username = req.query.username; let { name, totalStars, totalPRs, contributedTo } = await fetchStats( username ); res.setHeader("Content-Type", "image/svg+xml"); res.send(` ${name}'s github stats Total Stars: ${totalStars} Total PRs: ${totalPRs} Contributed to: ${contributedTo} repos `); };