{"size": 961, "ext": "js", "max_stars_repo_path": "api/ring.js", "max_stars_repo_name": "styfle/doorbell", "max_stars_repo_stars_event_min_datetime": "2020-04-27T01:30:34.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-06T07:51:22.000Z", "max_issues_repo_path": "api/ring.js", "max_issues_repo_name": "styfle/doorbell", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/ring.js", "max_forks_repo_name": "styfle/doorbell", "max_forks_repo_forks_event_min_datetime": "2020-07-17T18:11:38.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-31T12:44:46.000Z"}
{"max_stars_count": 10, "max_issues_count": null, "max_forks_count": 4, "avg_line_length": 30.03125, "max_line_length": 75, "alphanum_fraction": 0.6430801249}
const https = require('https');

module.exports = (req, res) => {
  if (req.method !== 'POST') {
    res.statusCode = 405;
    res.end(`Method not allowed: ${req.method}`);
    return;
  }

  const notification = `${req.query.name || 'Someone'} rang the doorbell.`;
  const accessCode = process.env.ALEXA_ACCESS_CODE;

  if (!accessCode) {
    throw new Error('Did you forget to assign ALEXA_ACCESS_CODE?');
  }

  console.log(notification);
  const url = new URL('https://api.notifymyecho.com/v1/NotifyMe');
  url.searchParams.append('notification', notification);
  url.searchParams.append('accessCode', accessCode);

  https.get(url, ({ statusCode }) => {
    const location = [200, 201, 202].includes(statusCode)
      ? '/success.html'
      : '/failure.html';
    res.statusCode = 303;
    res.setHeader('Location', location);
    res.setHeader('Content-Type', 'text/html');
    res.end(`Redirecting to <a href="${location}">${location}</a>...`);
  });
}
