UNPKG

@shopware-ag/meteor-admin-sdk

Version:

The Meteor SDK for the Shopware Administration.

161 lines 7 kB
var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "lodash-es/has"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findExtensionNameByBaseUrl = exports.findExtensionByBaseUrl = exports.removeRoot = exports.isPrimitive = exports.traverseObject = exports.hasOwnProperty = exports.hasType = exports.getWindowSrc = exports.getLocationId = exports.isObject = exports.generateUniqueId = void 0; const has_1 = __importDefault(require("lodash-es/has")); function generateUniqueId() { return String(Date.now().toString(36) + Math.random().toString(36).substr(2)); } exports.generateUniqueId = generateUniqueId; /* eslint-disable */ function isObject(value) { return value !== null && typeof value === 'object'; } exports.isObject = isObject; function getLocationId() { const params = new URLSearchParams(window.location.search); return params.get('location-id'); } exports.getLocationId = getLocationId; function getWindowSrc() { const location = window.location; const urlObject = new URL(location.pathname, location.origin); return urlObject.toString(); } exports.getWindowSrc = getWindowSrc; function hasType(type, obj) { return isObject(obj) && obj.__type__ && obj.__type__ === type; } exports.hasType = hasType; function hasOwnProperty(obj, path) { return (0, has_1.default)(obj, path); } exports.hasOwnProperty = hasOwnProperty; 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); } } } exports.traverseObject = traverseObject; function isPrimitive(value) { return value !== Object(value) || value === null || value === undefined; } exports.isPrimitive = isPrimitive; /** * Removes the root prefix from a path */ function removeRoot(path) { if (typeof path !== 'string') { return path; } return path.replace(/^root\./, ''); } exports.removeRoot = removeRoot; 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; }); } exports.findExtensionByBaseUrl = findExtensionByBaseUrl; /** * 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. */ 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]; } exports.findExtensionNameByBaseUrl = findExtensionNameByBaseUrl; }); //# sourceMappingURL=utils.js.map