zz_

index.js

thoward37@gmail.com/push-test2-1791933121043 / index.js @ 4264911f Download
Ladder Summary
  • file The module exports a single **default async function `isUp(url)`** that checks whether a given website is reachable. It extracts the hostname from the provided URL, queries the `https://isitup.org/<hostname>.json` API via `got`, throws an error if the API reports an invalid domain, and otherwise returns `true` when the site’s status code indicates it is up (and `false` when it is down).
 'use strict';
-var got = require('got');
+const got = require('got');
 
-module.exports = function (url) {
- return got('http://isitup.org/' + url + '.json', {
+const statusCodes = {
+ isUp: 1,
+ isDown: 2,
+ invalidDomain: 3
+};
+
+module.exports = async url => {
+ url = url.replace(/^(?:https?:)?\/\//, '');
+ url = encodeURIComponent(url);
+
+ const {body} = await got(`https://isitup.org/${url}.json`, {
  json: true,
  headers: {
  'user-agent': 'https://github.com/sindresorhus/is-up'
  }
- }).then(function (res) {
- if (res.body.status_code === 3) {
- throw new Error('Invalid domain');
- }
-
- return res.body.status_code === 1;
  });
+
+ const statusCode = body.status_code;
+
+ if (statusCode === statusCodes.invalidDomain) {
+ throw new Error('Invalid domain');
+ }
+
+ return statusCode === statusCodes.isUp;
 };