Changeset View
Changeset View
Standalone View
Standalone View
assets/src/utils/functions.js
Show First 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | closeButton = | ||||
`<button type="button" class="close" data-dismiss="alert" aria-label="Close"> | `<button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||||
<span aria-hidden="true">×</span> | <span aria-hidden="true">×</span> | ||||
</button>`; | </button>`; | ||||
extraClasses = 'alert-dismissible'; | extraClasses = 'alert-dismissible'; | ||||
} | } | ||||
return `<div class="alert alert-${type} ${extraClasses}" role="alert">${message}${closeButton}</div>`; | return `<div class="alert alert-${type} ${extraClasses}" role="alert">${message}${closeButton}</div>`; | ||||
} | } | ||||
export function isValidURL(string) { | export function validateUrl(url, allowedProtocols = []) { | ||||
let originUrl = null; | |||||
let validUrl = true; | |||||
try { | try { | ||||
new URL(string); | originUrl = new URL(url); | ||||
} catch (_) { | } catch (TypeError) { | ||||
return false; | validUrl = false; | ||||
} | } | ||||
return true; | |||||
if (validUrl && allowedProtocols) { | |||||
validUrl = ( | |||||
allowedProtocols.find(protocol => protocol === originUrl.protocol) !== undefined | |||||
); | |||||
} | |||||
return validUrl ? originUrl : null; | |||||
} | } | ||||
export async function isArchivedOrigin(originPath) { | export async function isArchivedOrigin(originPath) { | ||||
if (!isValidURL(originPath)) { | if (!validateUrl(originPath)) { | ||||
// Not a valid URL, return immediately | // Not a valid URL, return immediately | ||||
return false; | return false; | ||||
} else { | } else { | ||||
const response = await fetch(Urls.api_1_origin(originPath)); | const response = await fetch(Urls.api_1_origin(originPath)); | ||||
return response.ok && response.status === 200; // Success response represents an archived origin | return response.ok && response.status === 200; // Success response represents an archived origin | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 67 Lines • Show Last 20 Lines |