@scayle/storefront-core
Version:
Collection of essential utilities to work with the Storefront API
33 lines (32 loc) • 1.36 kB
JavaScript
const SANITIZATION_MASK = "********";
const regex = /(?<!^).(?!$)/g;
export const stripShopLocaleFromPath = (locale, path, splitter = "/") => path.split(splitter).filter((segment) => segment.toLowerCase() !== locale.toLowerCase()).join("/");
export const purifySensitiveValue = (value, showFirstAndLastChar = false) => {
return showFirstAndLastChar && value.length > 2 ? value.replace(regex, "*") : SANITIZATION_MASK;
};
export const purifySensitiveData = (data = {}, blacklistedKeys = ["password", "token", "code"], showFirstAndLastChar = false) => {
return Object.entries(data).reduce(
(purifiedPayload, [key, value]) => {
if (!!value && value.constructor === Object) {
purifiedPayload[key] = purifySensitiveData(value, blacklistedKeys);
return purifiedPayload;
}
const isSensitiveData = blacklistedKeys.some(
(excludedKey) => key.includes(excludedKey)
);
if (isSensitiveData && value) {
purifiedPayload[key] = purifySensitiveValue(value, showFirstAndLastChar);
} else {
purifiedPayload[key] = value;
}
return purifiedPayload;
},
{}
);
};
export const createAndPurifyHeaders = (headers) => {
const sanitizedHeaders = Object.fromEntries(
Object.entries(headers).filter(([_, value]) => value !== void 0)
);
return new Headers(sanitizedHeaders);
};