@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
29 lines (28 loc) • 1.09 kB
JavaScript
export function getRedirectLookupUrls(url, queryParamWhitelist) {
const incomingUrl = `${url.origin}${url.pathname}`;
const sourceUrlObject = new URL(
incomingUrl.endsWith("/") ? incomingUrl.slice(0, -1) : incomingUrl
);
if (queryParamWhitelist) {
for (const [key, value] of [...url.searchParams.entries()]) {
if (queryParamWhitelist.has(key)) {
sourceUrlObject.searchParams.append(key, value);
}
}
}
const relativeSourceUrl = decodeURI(
sourceUrlObject.toString().replace(sourceUrlObject.origin, "")
);
const absoluteSourceUrl = decodeURI(sourceUrlObject.toString());
return { relativeSourceUrl, absoluteSourceUrl };
}
export function getTargetLocation(source, target, queryParamWhitelist) {
const base = URL.canParse(target) ? target : source;
const targetUrl = new URL(target, base);
for (const [key, value] of source.searchParams.entries()) {
if (!queryParamWhitelist?.has(key) && !targetUrl.searchParams.has(key)) {
targetUrl.searchParams.append(key, value);
}
}
return decodeURI(targetUrl.toString());
}