Replaced ky with axios
parent
18cef3fed5
commit
19d8c2cdd9
|
@ -5,9 +5,6 @@ const {readFileSync, writeFileSync, existsSync} = require("fs");
|
||||||
|
|
||||||
// Public modules from npm
|
// Public modules from npm
|
||||||
const axios = require("axios").default;
|
const axios = require("axios").default;
|
||||||
const ky = require("ky-universal").create({
|
|
||||||
throwHttpErrors: false,
|
|
||||||
});
|
|
||||||
const cheerio = require("cheerio");
|
const cheerio = require("cheerio");
|
||||||
const axiosCookieJarSupport = require("axios-cookiejar-support").default;
|
const axiosCookieJarSupport = require("axios-cookiejar-support").default;
|
||||||
const tough = require("tough-cookie");
|
const tough = require("tough-cookie");
|
||||||
|
@ -249,23 +246,23 @@ module.exports.isStringAValidURL = function (url) {
|
||||||
* @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.
|
||||||
* @param {String} url URL to check
|
* @param {String} url URL to check
|
||||||
* @param {Boolean} checkRedirect If true, the function will consider redirects a violation and return false
|
* @param {Boolean} [checkRedirect]
|
||||||
|
* If true, the function will consider redirects a violation and return false.
|
||||||
|
* Default: false
|
||||||
* @returns {Promise<Boolean>} true if the URL exists, false otherwise
|
* @returns {Promise<Boolean>} true if the URL exists, false otherwise
|
||||||
*/
|
*/
|
||||||
module.exports.urlExists = async function (url, checkRedirect) {
|
module.exports.urlExists = async function (url, checkRedirect = false) {
|
||||||
if (!exports.isStringAValidURL(url)) {
|
// Local variables
|
||||||
return false;
|
let valid = false;
|
||||||
}
|
|
||||||
|
|
||||||
const response = await ky.head(url);
|
if (exports.isStringAValidURL(url)) {
|
||||||
let valid = response !== undefined && !/4\d\d/.test(response.status);
|
const response = await axios.head(url);
|
||||||
|
valid = response && !/4\d\d/.test(response.status);
|
||||||
|
|
||||||
if (!valid) return false;
|
if (valid && checkRedirect) {
|
||||||
|
const redirectUrl = await exports.getUrlRedirect(url);
|
||||||
if (checkRedirect) {
|
valid = redirectUrl === url;
|
||||||
const redirectUrl = await exports.getUrlRedirect(url);
|
}
|
||||||
if (redirectUrl === url) valid = true;
|
|
||||||
else valid = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return valid;
|
return valid;
|
||||||
|
@ -278,7 +275,7 @@ module.exports.urlExists = async function (url, checkRedirect) {
|
||||||
* @returns {Promise<String>} Redirect URL or the passed URL
|
* @returns {Promise<String>} Redirect URL or the passed URL
|
||||||
*/
|
*/
|
||||||
module.exports.getUrlRedirect = async function (url) {
|
module.exports.getUrlRedirect = async function (url) {
|
||||||
const response = await ky.head(url);
|
const response = await axios.head(url);
|
||||||
return response.url;
|
return response.config.url;
|
||||||
};
|
};
|
||||||
//#endregion Utility methods
|
//#endregion Utility methods
|
Loading…
Reference in New Issue