|
1 | 1 | const express = require('express'); |
2 | 2 | const axios = require('axios'); |
| 3 | +const token = process.env.CODE_STATISTIC || ""; |
3 | 4 | const app = express(); |
4 | 5 |
|
5 | 6 | async function syncAxios(url) { |
6 | | - return (await axios.get(url)).data; |
| 7 | + return (await axios.get(url, { |
| 8 | + headers: { |
| 9 | + Accept: "application/json", |
| 10 | + Authorization: `Bearer ${token}`, |
| 11 | + } |
| 12 | + })).data; |
7 | 13 | } |
8 | 14 |
|
9 | 15 | async function getLanguage(user, repo) { |
10 | 16 | return await syncAxios(`https://api.github.com/repos/${user}/${repo}/languages`); |
11 | 17 | } |
| 18 | + |
| 19 | +async function langStatistics(queue) { |
| 20 | + const res = {}; |
| 21 | + for (const idx in queue) { |
| 22 | + const task = queue[idx]; |
| 23 | + if (task instanceof Promise) { |
| 24 | + const task_response = await task; |
| 25 | + for (const lang in task_response) { |
| 26 | + lang in res ? |
| 27 | + res[lang] += task_response[lang] : |
| 28 | + res[lang] = task_response[lang]; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return res; |
| 33 | +} |
| 34 | + |
12 | 35 | app.get('/user/:user/', async function (req, res) { |
13 | 36 | const username = req.params.user; |
14 | 37 | const response = await syncAxios(`https://api.github.com/users/${username}/repos`); |
15 | | - const statistic = Object.values(response).map(async (resp) => { |
| 38 | + const result = await langStatistics(Object.values(response).map(async (resp) => { |
16 | 39 | return await getLanguage(username, resp['name']); |
17 | | - }); |
18 | | - res.send(statistic); |
| 40 | + })); |
| 41 | + res.send(result); |
19 | 42 | }) |
20 | 43 |
|
21 | 44 | app.get('/repo/:user/:repo/', function (req, res) { |
|
0 commit comments