UNPKG

@etsoo/appscript

Version:

Applications shared TypeScript framework

67 lines (66 loc) 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExternalSettings = void 0; /** * External settings namespace */ var ExternalSettings; (function (ExternalSettings) { /** * Sub domain match regular expression */ ExternalSettings.subDomainMatch = /(?<=\/\/)[0-9a-z]+(?=\.)/i; /** * Create settings instance * @param settings Settings * @returns Result */ function create(settings, hostname) { // Default settings reading from globalThis settings ?? (settings = Reflect.get(globalThis, "settings")); if (settings) { if (typeof settings === "string") { settings = JSON.parse(settings); } if (settings != null && typeof settings === "object" && "endpoint" in settings && "webUrl" in settings) { const s = settings; if (hostname) s.hostname = hostname; return s; } } throw new Error("No external settings found"); } ExternalSettings.create = create; /** * Format the app * @param hostname Hostname * @param app App key * @param endpoint Endpoint * @returns Result */ function formatApp(hostname, app, endpoint) { return formatHost(endpoint, hostname).replace(ExternalSettings.subDomainMatch, app); } ExternalSettings.formatApp = formatApp; function formatHost(setting, hostname) { // Default hostname hostname ?? (hostname = globalThis.location.hostname); if (typeof setting === "string") { return setting.replace("{hostname}", hostname); } else { return Object.fromEntries(Object.entries(setting).map(([key, value]) => [ key, { endpoint: formatApp(hostname, key, value.endpoint), webUrl: formatApp(hostname, key, value.webUrl) } ])); } } ExternalSettings.formatHost = formatHost; })(ExternalSettings || (exports.ExternalSettings = ExternalSettings = {}));