"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = fetchPackageInfo;

var _request = _interopRequireDefault(require("request"));

var _getPkgInfo = _interopRequireDefault(require("./getPkgInfo"));

var _config = require("../../config");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Fetch the latest information of the package from npm registry
 */
function fetchPackageInfo() {
  const appName = (0, _getPkgInfo.default)().name;
  const url = `${_config.API_ENDPOINTS_NPMS_PACKAGE}/${appName}`;
  const options = {
    url,
    method: 'GET',
    // allow only 5 seconds for better UX
    timeout: 5 * 1000
  };
  return new Promise((resolve, reject) => {
    (0, _request.default)(options, (error, res) => {
      if (error) {
        reject(error);
      } else {
        try {
          const jsonRes = JSON.parse(res.body);
          resolve(jsonRes.collected.metadata);
        } catch (errorJson) {
          reject(errorJson);
        }
      }
    });
  });
}