UNPKG

better-uptime

Version:

Uptime module with advanced features

75 lines (63 loc) 2.35 kB
const fetch = require("node-fetch"); const errors = require("./errors.js"); const arr = require("./json/time_types.json"); module.exports = { Uptime: class { constructor(opt = {}) { let link = opt.url; let time = opt.time; let task = opt.callback; let time_type = opt.time_type; let then_method = opt.then if (!time_type) { time_type = "s"; } // This part throw an error if the user doesn't type a {link} if (!link) { throw new errors(1, "You must write a link.", 3,__dirname); } // This part checks {url}'s type is not equal to "string" if (typeof link != "string") { throw new errors(2, "Link parameter must be a string.", 4); } // This part checks {time}'s type is not equal to "number" if (typeof time != "number") { throw new errors(3, "Time parameter must be a number.", 4); } // This part throw an error if the user does not type a {time} if (!time) { throw new errors(4, "Please specify a time.", 4); } // This part checks {time_type} is not equal to "string" if (typeof time_type != "string") { throw new errors(5, "Time_Type parameter must be a number.", 4); } // This part checks {time_type} is a valid time_type format if (!arr.includes(time_type)) { throw new errors(6, "Wrong time format.", 4); } // This part checks {link} is a valid url function isValidURL(url) { var isValid = url.match( /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g ); return isValid !== null; } if (!isValidURL(link)) { throw new errors(7, "The link should be the url.", 3); } // This part makes uptime the {link} in every {time} setInterval(() => { fetch(link) // this part makes the link uptime if (task && typeof task == "string") { eval(task); // this part do {task} in every {time} } }, require("ms")(time + time_type)); // This part blocks node-fetch's FetchError process.on('unhandledRejection', error => { if(error.toString().includes("FetchError")) return; console.error("Unhandled promise rejection:", error); }); } } }