├── README.md ├── app.js └── github.js /README.md: -------------------------------------------------------------------------------- 1 | # console-rep-loader-from-gh 2 | Console app for download and render repos from github 3 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const github = require("./github"); 2 | 3 | const username = process.argv[2]; 4 | 5 | github.getRepos(username, (error, repos) => { 6 | if (error) { 7 | console.error(`Ошибка: ${error.message}`); 8 | } else { 9 | repos.forEach((repo) => console.log(repo.name)); 10 | } 11 | }); 12 | -------------------------------------------------------------------------------- /github.js: -------------------------------------------------------------------------------- 1 | const https = require("https"); 2 | 3 | function getRepos(username, done) { 4 | if (!username) return done(new Error("Необходимо указать имя пользователя")); 5 | 6 | const options = { 7 | hostname: "api.github.com", 8 | path: `/users/${username}/repos`, 9 | headers: { "User-Agent": `${username}` }, 10 | }; 11 | 12 | const req = https.get(options, (res) => { 13 | res.setEncoding("utf-8"); 14 | if (res.statusCode === 200) { 15 | let body = ""; 16 | res.on("data", (data) => (body += data)); 17 | res.on("end", () => { 18 | try { 19 | const result = JSON.parse(body); 20 | done(null, result); 21 | } catch (error) { 22 | done(new Error(`Не удалось обработать данные ${error.message}`)); 23 | } 24 | }); 25 | } else { 26 | done( 27 | new Error( 28 | `Не удалось получить данные от сервера (${res.statusCode}) ${res.statusMessage}` 29 | ) 30 | ); 31 | } 32 | }); 33 | 34 | req.on("error", (error) => 35 | done(new Error(`Не удалось отправить запрос: ${error.message}`)) 36 | ); 37 | } 38 | 39 | module.exports = { 40 | getRepos, 41 | }; 42 | --------------------------------------------------------------------------------