altair-graphql-core
Version:
Several of the core logic for altair graphql client
143 lines • 5.85 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAltairConfig = exports.setAltairConfig = exports.AltairConfig = exports.isTranslateMode = void 0;
const crx_1 = require("../crx");
const is_electron_1 = __importDefault(require("../utils/is_electron"));
const options_1 = require("./options");
const urls_1 = require("./urls");
const defaults_1 = require("./defaults");
const languages_1 = require("./languages");
const parseUrl = (url) => {
try {
return new URL(url);
}
catch (e) {
return;
}
};
exports.isTranslateMode = globalThis.__ALTAIR_TRANSLATE__;
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 = defaults_1.DEFAULT_OPTIONS.ADD_QUERY_DEPTH_LIMIT;
this.tab_size = defaults_1.DEFAULT_OPTIONS.TAB_SIZE;
this.max_windows = is_electron_1.default ? 50 : 15;
this.default_language = exports.isTranslateMode
? languages_1.languagesSchema.enum.TranslationLang
: defaults_1.DEFAULT_OPTIONS.DEFAULT_LANGUAGE;
this.query_history_depth = is_electron_1.default ? 100 : 15;
this.disableLineNumbers = false;
this.defaultTheme = defaults_1.DEFAULT_OPTIONS.DEFAULT_THEME;
this.themes = defaults_1.DEFAULT_OPTIONS.THEMES;
this.isTranslateMode = exports.isTranslateMode;
this.isWebApp = globalThis.__ALTAIR_WEB_APP__;
this.cspNonce = '';
// assigning options here to get the return type
this.options = (0, options_1.getOptions)({});
this.options = (0, options_1.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 (crx_1.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 urls_1.urlMap[environment];
}
async getUrlConfigWithLocal(environment = 'production') {
// Check for local sandbox url first
const localSandboxUrl = await this.getLocalSandBoxUrl();
const urls = urls_1.urlMap[environment];
if (localSandboxUrl) {
urls.sandbox = localSandboxUrl;
}
return urls;
}
async getUrl(name, environment = 'production') {
const urlConfig = await this.getUrlConfigWithLocal(environment);
return urlConfig[name];
}
}
exports.AltairConfig = AltairConfig;
let config = new AltairConfig();
const setAltairConfig = (_config) => {
config = _config;
};
exports.setAltairConfig = setAltairConfig;
const getAltairConfig = () => {
return config;
};
exports.getAltairConfig = getAltairConfig;
globalThis.getAltairConfig = exports.getAltairConfig;
//# sourceMappingURL=index.js.map