const https = require('https');

function httpsRequest(options, cb) {
  const req = https.request(options, (res) => {
    let data = '';

    // chunk response data into data variable
    res.on('data', (d) => {
      data += d;
    });

    // when request ends, execute the callback
    res.on('end', () => {
      cb(null, data);
    });
  });

  // send request body if it exists
  req.write(options.body || '');
  req.end();

  // handle errors
  req.on('error', (err) => {
    cb(err);
  });
}

module.exports = httpsRequest;
