Replaced ky with axios

pull/59/head
MillenniumEarl 2020-12-15 09:26:29 +01:00
parent 18cef3fed5
commit 19d8c2cdd9
1 changed files with 15 additions and 18 deletions

View File

@ -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) {
if (checkRedirect) {
const redirectUrl = await exports.getUrlRedirect(url); const redirectUrl = await exports.getUrlRedirect(url);
if (redirectUrl === url) valid = true; valid = redirectUrl === url;
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