{"size": 631, "ext": "js", "max_stars_repo_path": "JavaScript/6-fetch.js", "max_stars_repo_name": "Fodin/Promise", "max_stars_repo_stars_event_min_datetime": "2017-02-28T03:08:55.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-19T16:23:44.000Z", "max_issues_repo_path": "JavaScript/6-fetch.js", "max_issues_repo_name": "Fodin/Promise", "max_issues_repo_issues_event_min_datetime": "2019-08-20T00:03:33.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-20T21:28:11.000Z", "max_forks_repo_path": "JavaScript/6-fetch.js", "max_forks_repo_name": "Fodin/Promise", "max_forks_repo_forks_event_min_datetime": "2018-02-09T21:42:39.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-22T10:27:12.000Z"}
{"max_stars_count": 13, "max_issues_count": 3, "max_forks_count": 12, "avg_line_length": 19.71875, "max_line_length": 59, "alphanum_fraction": 0.5245641838}
'use strict';

const http = require('http');

const fetch = url => new Promise((resolve, reject) => {
  http.get(url, res => {
    const code = res.statusCode;
    if (code !== 200) {
      return reject(new Error(`HTTP status code ${code}`));
    }

    res.on('error', reject);

    const chunks = [];
    res.on('data', chunk => {
      chunks.push(chunk);
    });

    res.on('end', () => {
      const json = Buffer.concat(chunks).toString();
      try {
        const object = JSON.parse(json);
        resolve(object);
      } catch (error) {
        return reject(error);
      }
    });
  });
});

module.exports = fetch;
