@vtex/api
Version:
VTEX I/O API client
41 lines (40 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.inferTargetProduct = exports.getCanonicalAndAlternateAddresses = void 0;
const buildAddressPath = (basePath, localizationKey) => {
if (basePath === '') {
return `/${localizationKey}`;
}
if (localizationKey === '') {
return `/${basePath}`;
}
return `/${basePath}/${localizationKey}`;
};
const getCanonicalAndAlternateAddresses = (lmBinding) => {
let canonicalBaseAddress = '';
const alternateBaseAddresses = [];
for (const addressEntry of lmBinding.Addresses) {
const localizationKeys = Object.keys(addressEntry.Localization);
for (const localizationKey of localizationKeys) {
alternateBaseAddresses.push(addressEntry.Host + buildAddressPath(addressEntry.BasePath, localizationKey));
}
if (addressEntry.IsCanonical) {
// The canonical address is built from the shortest localization key.
const [shortestLocalizationKey = ''] = [...localizationKeys].sort((a, b) => a.length - b.length);
canonicalBaseAddress = addressEntry.Host + buildAddressPath(addressEntry.BasePath, shortestLocalizationKey);
}
}
// The canonical address must not also appear in the alternates list.
const canonicalIndexInAlternates = alternateBaseAddresses.indexOf(canonicalBaseAddress);
if (canonicalIndexInAlternates !== -1) {
alternateBaseAddresses.splice(canonicalIndexInAlternates, 1);
}
return { canonicalBaseAddress, alternateBaseAddresses };
};
exports.getCanonicalAndAlternateAddresses = getCanonicalAndAlternateAddresses;
const inferTargetProduct = (canonicalBaseAddress, alternateBaseAddresses) => {
const isAdmin = canonicalBaseAddress.endsWith('myvtex.com/admin') ||
alternateBaseAddresses.some(address => address.endsWith('myvtex.com/admin'));
return isAdmin ? 'vtex-admin' : 'vtex-storefront';
};
exports.inferTargetProduct = inferTargetProduct;