@w0s/report-same-referrer
Version:
Send referrer error information to endpoints
87 lines • 2.79 kB
JavaScript
/**
* Validation
*
* @param options - Options
*
* @returns If validation passes, it returns true
*/
const validate = (options) => {
/* Referrer */
const { referrer } = document;
if (referrer === '') {
return false;
}
const referrerUrl = new URL(referrer);
const getUrlPart = (part) => {
switch (part) {
case undefined:
case 'origin': {
return { referrer: referrerUrl.origin, location: location.origin };
}
case 'host': {
return { referrer: referrerUrl.host, location: location.host };
}
case 'hostname': {
return { referrer: referrerUrl.hostname, location: location.hostname };
}
default:
throw new Error('An invalid value was specified for the argument `condition`.');
}
};
const { referrer: referrerPart, location: locationPart } = getUrlPart(options?.referrer?.comparePart);
if (referrerPart !== locationPart && !options?.referrer?.sames?.includes(referrerPart)) {
return false;
}
/* User agent */
if (options?.ua !== undefined) {
const ua = navigator.userAgent;
const { denys, allows } = options.ua;
if (denys?.some((deny) => deny.test(ua))) {
console.info('No referrer error report will be sent because the user agent match the deny list.');
return false;
}
if (allows !== undefined && !allows.some((allow) => allow.test(ua))) {
console.info('No referrer error report will be sent because the user agent does not match the allow list.');
return false;
}
}
return true;
};
/**
* Send referrer error information to endpoints
*
* @param options - Options
*
* @returns Fetch result
*/
export default async (options) => {
if (!validate(options.validate)) {
return undefined;
}
const { endpoint, param, contentType, headers: headersInit } = options.fetch;
const headers = new Headers(headersInit);
if (contentType !== undefined) {
headers.set('Content-Type', contentType);
}
const bodyObject = {
[param.documentURL]: location.toString(),
[param.referrer]: document.referrer,
};
let body;
if (contentType === 'application/json') {
body = JSON.stringify(bodyObject);
}
else {
body = new URLSearchParams(bodyObject);
}
const response = await fetch(endpoint, {
method: 'POST',
headers: headers,
body: body,
});
if (!response.ok) {
throw new Error(`\`${response.url}\` is ${String(response.status)} ${response.statusText}`);
}
return response;
};
//# sourceMappingURL=reportSameReferrer.js.map