UNPKG

@etsoo/appscript

Version:

Applications shared TypeScript framework

64 lines (63 loc) 2.07 kB
/** * External settings namespace */ export 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 || (ExternalSettings = {}));