Reduce cyclomatic complexity
parent
19d8c2cdd9
commit
bf71cf062c
|
@ -242,6 +242,24 @@ module.exports.isStringAValidURL = function (url) {
|
|||
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
|
||||
* 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;
|
||||
|
||||
if (exports.isStringAValidURL(url)) {
|
||||
const response = await axios.head(url);
|
||||
valid = response && !/4\d\d/.test(response.status);
|
||||
valid = await _axiosUrlExists(url);
|
||||
|
||||
if (valid && checkRedirect) {
|
||||
const redirectUrl = await exports.getUrlRedirect(url);
|
||||
|
|
Loading…
Reference in New Issue