From 3e2d28dc7019910e92535a0477a6033a7cbb04b8 Mon Sep 17 00:00:00 2001 From: Tristan Chuine Date: Mon, 11 Mar 2024 17:35:20 +0100 Subject: [PATCH 1/2] use `baseUri` for requests instead of hard-coded paths --- src/components/App/app-wrapper.tsx | 2 +- src/services/config-notification.ts | 4 ++-- src/services/config.ts | 4 ++-- src/services/study.ts | 4 ++-- src/services/user-admin.ts | 4 ++-- src/utils/api-rest.ts | 6 ++++++ src/utils/api-ws.ts | 8 +++++--- 7 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/components/App/app-wrapper.tsx b/src/components/App/app-wrapper.tsx index a550978..230a159 100644 --- a/src/components/App/app-wrapper.tsx +++ b/src/components/App/app-wrapper.tsx @@ -115,7 +115,7 @@ const messages: Record = { }, }; -const basename = new URL(document.querySelector('base')?.href ?? '').pathname; +const basename = new URL(document.baseURI ?? '').pathname; /** * Layer injecting Theme, Internationalization (i18n) and other tools (snackbar, error boundary, ...) diff --git a/src/services/config-notification.ts b/src/services/config-notification.ts index 48bf662..528a93a 100644 --- a/src/services/config-notification.ts +++ b/src/services/config-notification.ts @@ -9,10 +9,10 @@ import ReconnectingWebSocket, { Event } from 'reconnecting-websocket'; import { APP_NAME } from '../utils/config-params'; import { getUrlWithToken, getWsBase } from '../utils/api-ws'; -const PREFIX_CONFIG_NOTIFICATION_WS = `${process.env.REACT_APP_WS_GATEWAY}/config-notification`; +const PREFIX_CONFIG_NOTIFICATION_WS = `${getWsBase()}/config-notification`; export function connectNotificationsWsUpdateConfig(): ReconnectingWebSocket { - const webSocketUrl = `${getWsBase()}${PREFIX_CONFIG_NOTIFICATION_WS}/notify?appName=${APP_NAME}`; + const webSocketUrl = `${PREFIX_CONFIG_NOTIFICATION_WS}/notify?appName=${APP_NAME}`; const reconnectingWebSocket = new ReconnectingWebSocket( () => getUrlWithToken(webSocketUrl), undefined, diff --git a/src/services/config.ts b/src/services/config.ts index c996a6c..094969a 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -11,10 +11,10 @@ import { PARAM_LANGUAGE, PARAM_THEME, } from '../utils/config-params'; -import { backendFetch, backendFetchJson } from '../utils/api-rest'; +import { backendFetch, backendFetchJson, getRestBase } from '../utils/api-rest'; import { LanguageParameters } from '../utils/language'; -const PREFIX_CONFIG_QUERIES = `${process.env.REACT_APP_API_GATEWAY}/config`; +const PREFIX_CONFIG_QUERIES = `${getRestBase()}/config`; // https://github.com/gridsuite/config-server/blob/main/src/main/java/org/gridsuite/config/server/dto/ParameterInfos.java export type ConfigParameter = diff --git a/src/services/study.ts b/src/services/study.ts index cfcae34..227015a 100644 --- a/src/services/study.ts +++ b/src/services/study.ts @@ -5,11 +5,11 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { backendFetchJson, Token } from '../utils/api-rest'; +import { backendFetchJson, getRestBase, Token } from '../utils/api-rest'; import { getErrorMessage } from '../utils/error'; import { APP_NAME } from '../utils/config-params'; -const STUDY_URL = `${process.env.REACT_APP_API_GATEWAY}/study/v1`; +const STUDY_URL = `${getRestBase()}/study/v1`; //TODO delete when commons-ui will be in typescript export type ServerAbout = { diff --git a/src/services/user-admin.ts b/src/services/user-admin.ts index 69ec8ee..05079d2 100644 --- a/src/services/user-admin.ts +++ b/src/services/user-admin.ts @@ -5,11 +5,11 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { backendFetch, backendFetchJson } from '../utils/api-rest'; +import { backendFetch, backendFetchJson, getRestBase } from '../utils/api-rest'; import { extractUserSub, getToken, getUser } from '../utils/api'; import { User } from '../utils/auth'; -const USER_ADMIN_URL = `${process.env.REACT_APP_API_GATEWAY}/user-admin/v1`; +const USER_ADMIN_URL = `${getRestBase()}/user-admin/v1`; export function getUserSub(): Promise { return extractUserSub(getUser()); diff --git a/src/utils/api-rest.ts b/src/utils/api-rest.ts index 9604d3c..315b56e 100644 --- a/src/utils/api-rest.ts +++ b/src/utils/api-rest.ts @@ -16,6 +16,12 @@ export interface ErrorWithStatus extends Error { export type Url = string | URL; export type InitRequest = Partial; +export function getRestBase(): string { + return ( + document.baseURI.replace(/\/+$/, '') + process.env.REACT_APP_API_GATEWAY + ); +} + function handleError(response: Response): Promise { return response.text().then((text: string) => { const errorName = 'HttpResponseError : '; diff --git a/src/utils/api-ws.ts b/src/utils/api-ws.ts index b8272eb..b19e82d 100644 --- a/src/utils/api-ws.ts +++ b/src/utils/api-ws.ts @@ -10,9 +10,11 @@ import { getToken } from './api'; export type * from './api'; export function getWsBase(): string { - return document.baseURI - .replace(/^http(s?):\/\//, 'ws$1://') - .replace(/\/$/, ''); + return ( + document.baseURI + .replace(/^http(s?):\/\//, 'ws$1://') + .replace(/\/+$/, '') + process.env.REACT_APP_WS_GATEWAY + ); } export function getUrlWithToken(baseUrl: string): string { From cfa71abf0b5deae255c1a64a58f0c52cd52ad2b0 Mon Sep 17 00:00:00 2001 From: Tristan Chuine Date: Tue, 12 Mar 2024 11:39:55 +0100 Subject: [PATCH 2/2] add comment --- src/utils/api-rest.ts | 1 + src/utils/api-ws.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/src/utils/api-rest.ts b/src/utils/api-rest.ts index 315b56e..0f20e57 100644 --- a/src/utils/api-rest.ts +++ b/src/utils/api-rest.ts @@ -17,6 +17,7 @@ export type Url = string | URL; export type InitRequest = Partial; export function getRestBase(): string { + // We use the `baseURI` (from `` in index.html) to build the URL, which is corrected by httpd/nginx return ( document.baseURI.replace(/\/+$/, '') + process.env.REACT_APP_API_GATEWAY ); diff --git a/src/utils/api-ws.ts b/src/utils/api-ws.ts index b19e82d..65985bb 100644 --- a/src/utils/api-ws.ts +++ b/src/utils/api-ws.ts @@ -10,6 +10,7 @@ import { getToken } from './api'; export type * from './api'; export function getWsBase(): string { + // We use the `baseURI` (from `` in index.html) to build the URL, which is corrected by httpd/nginx return ( document.baseURI .replace(/^http(s?):\/\//, 'ws$1://')