UNPKG

@shopware-ag/meteor-admin-sdk

Version:

The Meteor SDK for the Shopware Administration.

134 lines 5.14 kB
import has from 'lodash-es/has'; export function generateUniqueId() { return String(Date.now().toString(36) + Math.random().toString(36).substr(2)); } /* eslint-disable */ export function isObject(value) { return value !== null && typeof value === 'object'; } export function getLocationId() { const params = new URLSearchParams(window.location.search); return params.get('location-id'); } export function getWindowSrc() { const location = window.location; const urlObject = new URL(location.pathname, location.origin); return urlObject.toString(); } export function hasType(type, obj) { return isObject(obj) && obj.__type__ && obj.__type__ === type; } export function hasOwnProperty(obj, path) { return has(obj, path); } export function traverseObject(traversableObject, processor, previousKey = 'root') { for (let index in traversableObject) { const currentEntry = traversableObject[index]; processor.apply(this, [traversableObject, index, currentEntry, previousKey]); if (isObject(currentEntry)) { let pk = previousKey + '.' + index; traverseObject(currentEntry, processor, pk); } } } export function isPrimitive(value) { return value !== Object(value) || value === null || value === undefined; } /** * Removes the root prefix from a path */ export function removeRoot(path) { if (typeof path !== 'string') { return path; } return path.replace(/^root\./, ''); } export function findExtensionByBaseUrl(baseUrl) { if (typeof baseUrl !== 'string') { return undefined; } if (baseUrl === '') { return undefined; } const comparedBaseUrl = new URL(baseUrl); /* * Check if baseUrl is the same as the current window location * If so, return the dummy extension with all privileges available */ if (comparedBaseUrl.origin === window.location.origin) { return { baseUrl: comparedBaseUrl.hostname, permissions: { additional: ['*'], create: ['*'], read: ['*'], update: ['*'], delete: ['*'], }, }; } return Object.values(window._swsdk.adminExtensions) .find((ext) => { const extensionBaseUrl = new URL(ext.baseUrl); return extensionBaseUrl.hostname === comparedBaseUrl.hostname; }); } /** * Returns the technical name (registry key) of the extension matching the given base URL. * * For cross-origin extensions the lookup uses origin comparison. For same-origin extensions * (e.g. plugins served from the same host as the Admin) the origin alone is not enough to * distinguish the extension from the Admin itself. Pass the sender `Window` as `sourceWindow` * to enable a fallback that matches the window's full href against the known `baseUrl` prefixes. * * Returns undefined when no matching extension is found or when the origin is the Admin's own * origin and no `sourceWindow` is provided. */ export function findExtensionNameByBaseUrl(baseUrl, sourceWindow) { var _a; if (typeof baseUrl !== 'string' || baseUrl === '') { return undefined; } let comparedBaseUrl; try { comparedBaseUrl = new URL(baseUrl); } catch (_b) { return undefined; } if (comparedBaseUrl.origin === window.location.origin) { // Origin alone cannot resolve same-origin extensions because origin matches // the Admin itself. When the sender Window is provided, fall back to href-prefix matching. if (sourceWindow) { try { // Strip query string — the SDK appends ?location-id=... to iFrame URLs. const href = sourceWindow.location.href.split('?')[0]; // Strip any trailing slash so both 'base/' and 'base' normalise to // 'base', then accept either an exact match (baseUrl IS the file) or a // path-boundary prefix match (baseUrl is a directory). Among all // matching extensions pick the most specific one (longest baseUrl) to // handle nested base paths correctly. const match = Object.entries(window._swsdk.adminExtensions) .filter(([, ext]) => { const base = ext.baseUrl.replace(/\/$/, ''); return href === base || href.startsWith(`${base}/`); }) .sort(([, a], [, b]) => b.baseUrl.length - a.baseUrl.length)[0]; return match === null || match === void 0 ? void 0 : match[0]; } catch (_c) { // Same-origin access should never be denied; ignore defensively. } } return undefined; } return (_a = Object.entries(window._swsdk.adminExtensions).find(([, ext]) => { try { return new URL(ext.baseUrl).origin === comparedBaseUrl.origin; } catch (_a) { return false; } })) === null || _a === void 0 ? void 0 : _a[0]; } //# sourceMappingURL=utils.js.map