@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
75 lines • 2.46 kB
JavaScript
// import { IHelpfullOutput, getHelpfullErrorV2 } from "@mikezimm/fps-core-v7/lib/logic/Errors/friendly";
import { getHelpfullErrorV2 } from "../../../logic/Errors/friendly";
// Combined with same function name on CreateLinks.tsx
// export function createLink(href: string, target: string, linkDesc: string, styles?: any ){
// return (
// <Link style={ styles } href={href} target={ target }>{ linkDesc }</Link>
// );
// }
/**
* This should be able to tell whether a SPO link is valid or not.
* @param url
* @param consoleLog
* @param extraMessage
* @returns
*/
export async function _LinkIsValid(url, consoleLog = true, extraMessage = '') {
//Require this is filled out.
if (!url) {
return false;
}
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
let isValid = true;
let errMessage = '';
try {
await http.send();
isValid = http.status != 404 ? true : false;
if (isValid === false && consoleLog === true) {
errMessage = `${extraMessage} Location does not seem to exist ~ 52: ${url}`;
console.log(errMessage);
}
}
catch (e) {
isValid = false;
const HelpfulErrorOutput = getHelpfullErrorV2(e, false, true, '_LinkIsValid ~ 59', false);
errMessage = HelpfulErrorOutput.returnMess;
if (consoleLog === true) {
console.log(`${extraMessage} Location does not seem to exist: ${url}`);
console.log(errMessage);
}
}
return isValid === true ? '' : 'Link is not valid';
}
/**
* This is different from _LinkIsValid in that it returns the status... ie 404, 403, etc...
* @param url
* @returns
*/
export async function _LinkStatus(url) {
//Require this is filled out.
if (!url) {
return false;
}
else if (url === 'http://linkless.header/')
return -1;
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
let isValid = true;
try {
await http.send();
isValid = http.status ? http.status : false;
}
catch (e) {
isValid = false;
}
// try {
// const response = await fetch(url, { method: "HEAD" });
// isValid= response.ok;
// } catch (error) {
// console.error(error);
// isValid = false;
// }
return isValid;
}
//# sourceMappingURL=_LinkIsValid.js.map