Reduce cyclomatic complexity

pull/59/head
MillenniumEarl 2020-12-15 09:47:40 +01:00
parent 19d8c2cdd9
commit bf71cf062c
1 changed files with 19 additions and 2 deletions

View File

@ -242,6 +242,24 @@ module.exports.isStringAValidURL = function (url) {
else return false; else return false;
}; };
/**
* @private
* Check with Axios if a URL exists.
* @param {String} url
*/
async function _axiosUrlExists(url) {
// Local variables
let valid = false;
try {
const response = await axios.head(url);
valid = response && !/4\d\d/.test(response.status);
} catch (error) {
if (error.code === "ENOTFOUND") valid = false;
else throw error;
}
return valid;
}
/** /**
* @protected * @protected
* Check if a particular URL is valid and reachable on the web. * Check if a particular URL is valid and reachable on the web.
@ -256,8 +274,7 @@ module.exports.urlExists = async function (url, checkRedirect = false) {
let valid = false; let valid = false;
if (exports.isStringAValidURL(url)) { if (exports.isStringAValidURL(url)) {
const response = await axios.head(url); valid = await _axiosUrlExists(url);
valid = response && !/4\d\d/.test(response.status);
if (valid && checkRedirect) { if (valid && checkRedirect) {
const redirectUrl = await exports.getUrlRedirect(url); const redirectUrl = await exports.getUrlRedirect(url);