1
- const axios = require ( "axios" ) ;
2
- const { renderError, kFormatter } = require ( "../utils" ) ;
3
- require ( "dotenv" ) . config ( ) ;
4
-
5
- async function fetchStats ( username ) {
6
- if ( ! username ) throw Error ( "Invalid username" ) ;
7
-
8
- const res = await axios ( {
9
- url : "https://api.github.com/graphql" ,
10
- method : "post" ,
11
- headers : {
12
- Authorization : `bearer ${ process . env . GITHUB_TOKEN } ` ,
13
- } ,
14
- data : {
15
- query : `
16
- query userInfo($login: String!) {
17
- user(login: $login) {
18
- name
19
- repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
20
- totalCount
21
- }
22
- contributionsCollection {
23
- totalCommitContributions
24
- }
25
- pullRequests(first: 100) {
26
- totalCount
27
- }
28
- issues(first: 100) {
29
- totalCount
30
- }
31
- repositories(first: 100) {
32
- nodes {
33
- stargazers {
34
- totalCount
35
- }
36
- }
37
- }
38
- }
39
- }
40
- ` ,
41
- variables : {
42
- login : username ,
43
- } ,
44
- } ,
45
- } ) ;
46
-
47
- const stats = {
48
- name : "" ,
49
- totalPRs : 0 ,
50
- totalCommits : 0 ,
51
- totalIssues : 0 ,
52
- totalStars : 0 ,
53
- contributedTo : 0 ,
54
- } ;
55
-
56
- if ( res . data . errors ) {
57
- console . log ( res . data . errors ) ;
58
- throw Error ( "Could not fetch user" ) ;
59
- }
60
-
61
- const user = res . data . data . user ;
62
-
63
- stats . name = user . name ;
64
- stats . totalIssues = user . issues . totalCount ;
65
- stats . totalCommits = user . contributionsCollection . totalCommitContributions ;
66
- stats . totalPRs = user . pullRequests . totalCount ;
67
- stats . contributedTo = user . repositoriesContributedTo . totalCount ;
68
-
69
- stats . totalStars = user . repositories . nodes . reduce ( ( prev , curr ) => {
70
- return prev + curr . stargazers . totalCount ;
71
- } , 0 ) ;
72
-
73
- return stats ;
74
- }
75
-
76
- const createTextNode = ( icon , label , value , lheight ) => {
77
- const classname = icon === "★" && "star-icon" ;
78
- return `
79
- <tspan x="25" dy="${ lheight } " class="stat bold">
80
- <tspan class="icon ${ classname } " fill="#4C71F2">${ icon } </tspan> ${ label } :</tspan>
81
- <tspan x="155" dy="0" class="stat">${ kFormatter ( value ) } </tspan>
82
- ` ;
83
- } ;
84
-
85
- const renderSVG = ( stats , options ) => {
86
- const {
87
- name,
88
- totalStars,
89
- totalCommits,
90
- totalIssues,
91
- totalPRs,
92
- contributedTo,
93
- } = stats ;
94
- const { hide, show_icons, hide_border, line_height } = options || { } ;
95
-
96
- const lheight = line_height || 25 ;
97
-
98
- const STAT_MAP = {
99
- stars : createTextNode ( "★" , "Total Stars" , totalStars , lheight ) ,
100
- commits : createTextNode ( "🕗" , "Total Commits" , totalCommits , lheight ) ,
101
- prs : createTextNode ( "🔀" , "Total PRs" , totalPRs , lheight ) ,
102
- issues : createTextNode ( "ⓘ" , "Total Issues" , totalIssues , lheight ) ,
103
- contribs : createTextNode ( "📕" , "Contributed to" , contributedTo , lheight ) ,
104
- } ;
105
-
106
- const statItems = Object . keys ( STAT_MAP )
107
- . filter ( ( key ) => ! hide . includes ( key ) )
108
- . map ( ( key ) => STAT_MAP [ key ] ) ;
109
-
110
- const height = 45 + ( statItems . length + 1 ) * lheight ;
111
-
112
- return `
113
- <svg width="495" height="${ height } " viewBox="0 0 495 ${ height } " fill="none" xmlns="http://www.w3.org/2000/svg">
114
- <style>
115
- .header { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; fill: #2F80ED }
116
- .stat { font: 600 14px 'Segoe UI', Ubuntu, Sans-Serif; fill: #333 }
117
- .star-icon { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; }
118
- .bold { font-weight: 700 }
119
- .icon {
120
- display: none;
121
- ${ ! ! show_icons && "display: block" }
122
- }
123
- </style>
124
- ${
125
- ! hide_border &&
126
- `<rect x="0.5" y="0.5" width="494" height="99%" rx="4.5" fill="#FFFEFE" stroke="#E4E2E2"/>`
127
- }
128
-
129
- <text x="25" y="35" class="header">${ name } 's GitHub Stats</text>
130
- <text y="45">
131
- ${ statItems }
132
- </text>
133
- </svg>
134
- ` ;
135
- } ;
1
+ const { renderError } = require ( "../src/utils" ) ;
2
+ const fetchStats = require ( "../src/fetchStats" ) ;
3
+ const renderStatsCard = require ( "../src/renderStatsCard" ) ;
136
4
137
5
module . exports = async ( req , res ) => {
138
6
const { username, hide, hide_border, show_icons, line_height } = req . query ;
@@ -146,7 +14,7 @@ module.exports = async (req, res) => {
146
14
}
147
15
148
16
res . send (
149
- renderSVG ( stats , {
17
+ renderStatsCard ( stats , {
150
18
hide : JSON . parse ( hide || "[]" ) ,
151
19
show_icons,
152
20
hide_border,
0 commit comments