-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
36 lines (29 loc) · 1.16 KB
/
Copy pathgithub.js
File metadata and controls
36 lines (29 loc) · 1.16 KB
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
const https = require('https');
const config = require('./config.json');
function getRepos(username, done) {
if(!username) return done(new Error('Необходимо указать имя пользователя!'));
const options = {
hostname: 'api.github.com',
path: `/users/${username}/repos`,
headers: {'User-Agent': config.username}
};
const req = https.get(options, res =>{
res.setEncoding('utf-8');
if(res.statusCode !== 200) return done(new Error(`Не удалось получить данные от сервера (${res.statusCode} ${res.statusMessage})!`));
let body = '';
res.on('data', data => body = body + data);
res.on('end', () => {
try{
const result = JSON.parse(body);
done(null, result);
}
catch (error) {
done(new Error(`Не удалось обработать данные (${error.message})`));
}
});
});
req.on('error', error => done(new Error(`Не удалось отправить запрос (${error.message})!`)));
}
module.exports = {
getRepos
};