diff --git a/packages/superset-ui-connection/src/SupersetClientClass.ts b/packages/superset-ui-connection/src/SupersetClientClass.ts index 461147c3d0..08fa0b8424 100644 --- a/packages/superset-ui-connection/src/SupersetClientClass.ts +++ b/packages/superset-ui-connection/src/SupersetClientClass.ts @@ -31,7 +31,7 @@ import { RequestConfig, ParseMethod, } from './types'; -import { DEFAULT_FETCH_RETRY_OPTIONS } from './constants'; +import { DEFAULT_FETCH_RETRY_OPTIONS, DEFAULT_BASE_URL } from './constants'; export default class SupersetClientClass { credentials: Credentials; @@ -46,7 +46,7 @@ export default class SupersetClientClass { timeout: ClientTimeout; constructor({ - baseUrl = 'http://localhost', + baseUrl = DEFAULT_BASE_URL, host, protocol, headers = {}, @@ -60,7 +60,7 @@ export default class SupersetClientClass { host || protocol ? `${protocol || 'https:'}//${host || 'localhost'}` : baseUrl, // baseUrl for API could also be relative, so we provide current location.href // as the base of baseUrl - window.location.href || 'http://localhost', + window.location.href || DEFAULT_BASE_URL, ); this.baseUrl = url.href.replace(/\/+$/, ''); // always strip trailing slash this.host = url.host; diff --git a/packages/superset-ui-connection/src/callApi/callApi.ts b/packages/superset-ui-connection/src/callApi/callApi.ts index 2f20249c1a..8e9ddf2d28 100644 --- a/packages/superset-ui-connection/src/callApi/callApi.ts +++ b/packages/superset-ui-connection/src/callApi/callApi.ts @@ -1,7 +1,13 @@ import 'whatwg-fetch'; import fetchRetry from 'fetch-retry'; import { CallApi, Payload, JsonValue } from '../types'; -import { CACHE_AVAILABLE, CACHE_KEY, HTTP_STATUS_NOT_MODIFIED, HTTP_STATUS_OK } from '../constants'; +import { + CACHE_AVAILABLE, + CACHE_KEY, + HTTP_STATUS_NOT_MODIFIED, + HTTP_STATUS_OK, + DEFAULT_BASE_URL, +} from '../constants'; function tryParsePayload(payload: Payload) { try { @@ -16,7 +22,7 @@ function tryParsePayload(payload: Payload) { */ function getFullUrl(partialUrl: string, params: CallApi['searchParams']) { if (params) { - const url = new URL(partialUrl, window.location.href); + const url = new URL(partialUrl, window.location.href || DEFAULT_BASE_URL); const search = params instanceof URLSearchParams ? params : new URLSearchParams(params); // will completely override any existing search params url.search = search.toString(); diff --git a/packages/superset-ui-connection/src/constants.ts b/packages/superset-ui-connection/src/constants.ts index e856f48898..c0c9bd01f9 100644 --- a/packages/superset-ui-connection/src/constants.ts +++ b/packages/superset-ui-connection/src/constants.ts @@ -1,5 +1,7 @@ import { FetchRetryOptions } from './types'; +export const DEFAULT_BASE_URL = 'http://localhost'; + // HTTP status codes export const HTTP_STATUS_OK = 200; export const HTTP_STATUS_NOT_MODIFIED = 304;