UNPKG

altair-graphql-core

Version:

Several of the core logic for altair graphql client

134 lines 5.25 kB
import { isExtension } from '../crx'; import isElectron from '../utils/is_electron'; import { getOptions } from './options'; import { urlMap } from './urls'; import { DEFAULT_OPTIONS } from './defaults'; import { languagesSchema } from './languages'; const parseUrl = (url) => { try { return new URL(url); } catch (e) { return; } }; export const isTranslateMode = globalThis.__ALTAIR_TRANSLATE__; export class AltairConfig { constructor(options = {}) { this.useLocalSandboxUrl = false; this.donation = { url: 'https://opencollective.com/altair/donate', action_count_threshold: 50, }; this.ga = 'UA-41432833-6'; this.add_query_depth_limit = DEFAULT_OPTIONS.ADD_QUERY_DEPTH_LIMIT; this.tab_size = DEFAULT_OPTIONS.TAB_SIZE; this.max_windows = isElectron ? 50 : 15; this.default_language = isTranslateMode ? languagesSchema.enum.TranslationLang : DEFAULT_OPTIONS.DEFAULT_LANGUAGE; this.query_history_depth = isElectron ? 100 : 15; this.disableLineNumbers = false; this.defaultTheme = DEFAULT_OPTIONS.DEFAULT_THEME; this.themes = DEFAULT_OPTIONS.THEMES; this.isTranslateMode = isTranslateMode; this.isWebApp = globalThis.__ALTAIR_WEB_APP__; this.cspNonce = ''; // assigning options here to get the return type this.options = getOptions({}); this.options = getOptions(options); this.options.endpointURL = globalThis.__ALTAIR_ENDPOINT_URL__ ?? this.options.endpointURL ?? ''; this.options.subscriptionsEndpoint = globalThis.__ALTAIR_SUBSCRIPTIONS_ENDPOINT__ ?? this.options.subscriptionsEndpoint ?? ''; this.options.initialQuery = globalThis.__ALTAIR_INITIAL_QUERY__ ?? this.options.initialQuery ?? ''; this.options.initialVariables = globalThis.__ALTAIR_INITIAL_VARIABLES__ ?? this.options.initialVariables ?? ''; this.options.initialHeaders = globalThis.__ALTAIR_INITIAL_HEADERS__ ?? this.options.initialHeaders ?? {}; this.options.initialPreRequestScript = globalThis.__ALTAIR_INITIAL_PRE_REQUEST_SCRIPT__ ?? this.options.initialPreRequestScript ?? ''; this.options.instanceStorageNamespace = globalThis.__ALTAIR_INSTANCE_STORAGE_NAMESPACE__ ?? this.options.instanceStorageNamespace ?? 'altair_'; this.cspNonce = this.options.cspNonce ?? ''; } getPossibleLocalSandBoxRoot() { if (isExtension) { // we only support mv3 extensions now // and mv3 extensions doesn't allow using iframe // sandbox with allow-same-origin so we have to open up // the postMessage without origin verification // This doesn't sit well with me, so for now we don't // support local sandbox for extensions. // We can revisit this later if needed. return; } // check document base url if (document.baseURI && parseUrl(document.baseURI)?.origin === globalThis.location.origin) { // add iframe-sandbox path to base url if (document.baseURI.endsWith('/')) { return new URL(document.baseURI + 'iframe-sandbox'); } else { // remove the last part of the url return new URL(document.baseURI.slice(0, document.baseURI.lastIndexOf('/') + 1) + 'iframe-sandbox'); } } } async getLocalSandBoxUrl() { if (typeof this.localSandboxUrl === 'undefined') { const localSandboxRoot = this.getPossibleLocalSandBoxRoot()?.href ?? ''; if (localSandboxRoot) { this.localSandboxUrl = localSandboxRoot + '/index.html'; const localSandboxTestUrl = localSandboxRoot + '/sandbox.png'; const res = await fetch(localSandboxTestUrl); if (res.ok) { this.useLocalSandboxUrl = true; } } } if (this.useLocalSandboxUrl) { return this.localSandboxUrl; } } getUrlConfig(environment = 'production') { return urlMap[environment]; } async getUrlConfigWithLocal(environment = 'production') { // Check for local sandbox url first const localSandboxUrl = await this.getLocalSandBoxUrl(); const urls = urlMap[environment]; if (localSandboxUrl) { urls.sandbox = localSandboxUrl; } return urls; } async getUrl(name, environment = 'production') { const urlConfig = await this.getUrlConfigWithLocal(environment); return urlConfig[name]; } } let config = new AltairConfig(); export const setAltairConfig = (_config) => { config = _config; }; export const getAltairConfig = () => { return config; }; globalThis.getAltairConfig = getAltairConfig; //# sourceMappingURL=index.js.map