diff --git a/api/index.js b/api/index.js index 909bd28..d951d71 100644 --- a/api/index.js +++ b/api/index.js @@ -19,6 +19,9 @@ async function fetchStats(username) { pullRequests(first: 100) { totalCount } + issues(first: 100) { + totalCount + } repositories(first: 100) { nodes { stargazers { @@ -35,42 +38,94 @@ async function fetchStats(username) { }, }); - const stats = { totalStars: 0, contributedTo: 0, name: "", totalPRs: 0 }; + const stats = { + name: "", + totalPRs: 0, + totalIssues: 0, + totalStars: 0, + contributedTo: 0, + }; if (res.data.error) return stats; const user = res.data.data.user; + + stats.name = user.name; + stats.totalIssues = user.issues.totalCount; + stats.totalPRs = user.pullRequests.totalCount; + stats.contributedTo = user.repositoriesContributedTo.totalCount; + 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; } +const renderSVG = (stats, options) => { + const { name, totalStars, totalIssues, totalPRs, contributedTo } = stats; + const { hide } = options || {}; + const height = 150 - hide.length * 20; + + const STAT_MAP = { + stars: ` + Total Stars: + ${totalStars} + `, + prs: ` + Total PRs: + ${totalPRs} + `, + issues: ` + Total Issues: + ${totalIssues} + `, + contribs: ` + Contributed to: + ${contributedTo} repos + `, + }; + + return ` + + + + + ${name}'s github stats + + ${Object.keys(STAT_MAP) + .filter((key) => !hide.includes(key)) + .map((key) => STAT_MAP[key])} + + + `; +}; + module.exports = async (req, res) => { const username = req.query.username; - let { name, totalStars, totalPRs, contributedTo } = await fetchStats( - username - ); + const hide = req.query.hide; + let { + name, + totalPRs, + totalStars, + totalIssues, + 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 - - `); + res.send( + renderSVG( + { + name, + totalStars, + totalIssues, + totalPRs, + contributedTo, + }, + { hide: JSON.parse(hide || "[]") } + ) + ); }; diff --git a/readme.md b/readme.md index 7ee00db..02788ef 100644 --- a/readme.md +++ b/readme.md @@ -12,10 +12,24 @@ change the `?username=` value to your GitHubs's username ![Anurag's github stats](https://github-readme-stats.vercel.app/api?username=anuraghazra) ``` +### Hiding certain stats + +To hide any specific stats you can pass a query parameter `?hide=` with an array of items you wanna hide. + +> Options: `&hide=["stars","prs","issues","contribs"]` + +```md +![Anurag's github stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&hide=["contribs","prs"]) +``` + ## Demo ![Anurag's github stats](https://github-readme-stats.vercel.app/api?username=anuraghazra) +- Hiding specific stats + +![Anurag's github stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&hide=["contribs","issues"]) + Contributions are welcomed! <3 Made with :heart: and javascript.