{"size": 720, "ext": "js", "max_stars_repo_path": "src/api/makeApiCall.js", "max_stars_repo_name": "neotree/neotree-editor2", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/api/makeApiCall.js", "max_issues_repo_name": "neotree/neotree-editor2", "max_issues_repo_issues_event_min_datetime": "2019-11-20T12:55:57.000Z", "max_issues_repo_issues_event_max_datetime": "2019-11-20T12:56:07.000Z", "max_forks_repo_path": "src/api/makeApiCall.js", "max_forks_repo_name": "neotree/neotree-editor", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 28.8, "max_line_length": 74, "alphanum_fraction": 0.5652777778}
/* global fetch */
import queryString from 'query-string';

export default (url = '', opts = {}) => new Promise((resolve, reject) => {
  const { body, method: m, ...reqOpts } = opts;
  reqOpts.headers = { ...reqOpts.headers };
  const method = (m || 'GET').toUpperCase();

  if (method === 'GET') {
    url = `${url}?${queryString.stringify(body || {})}`;
  } else {
    reqOpts.headers['Content-Type'] = 'application/json';
    reqOpts.body = JSON.stringify({ ...body });
  }

  fetch(url, { method, ...reqOpts })
    .then(res => res.json())
    .then(res => {
      const error = res.error || res.errors;
      if (error) return reject(error.map ? error : [error]);
      resolve(res);
    })
    .catch(reject);
});
