Skip to content

Commit 8f3735f

Browse files
Use baseUri for requests instead of hard-coded paths (#15)
1 parent 607111d commit 8f3735f

File tree

7 files changed

+22
-12
lines changed

7 files changed

+22
-12
lines changed

src/components/App/app-wrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const messages: Record<SupportedLanguages, IntlConfig['messages']> = {
115115
},
116116
};
117117

118-
const basename = new URL(document.querySelector('base')?.href ?? '').pathname;
118+
const basename = new URL(document.baseURI ?? '').pathname;
119119

120120
/**
121121
* Layer injecting Theme, Internationalization (i18n) and other tools (snackbar, error boundary, ...)

src/services/config-notification.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import ReconnectingWebSocket, { Event } from 'reconnecting-websocket';
99
import { APP_NAME } from '../utils/config-params';
1010
import { getUrlWithToken, getWsBase } from '../utils/api-ws';
1111

12-
const PREFIX_CONFIG_NOTIFICATION_WS = `${process.env.REACT_APP_WS_GATEWAY}/config-notification`;
12+
const PREFIX_CONFIG_NOTIFICATION_WS = `${getWsBase()}/config-notification`;
1313

1414
export function connectNotificationsWsUpdateConfig(): ReconnectingWebSocket {
15-
const webSocketUrl = `${getWsBase()}${PREFIX_CONFIG_NOTIFICATION_WS}/notify?appName=${APP_NAME}`;
15+
const webSocketUrl = `${PREFIX_CONFIG_NOTIFICATION_WS}/notify?appName=${APP_NAME}`;
1616
const reconnectingWebSocket = new ReconnectingWebSocket(
1717
() => getUrlWithToken(webSocketUrl),
1818
undefined,

src/services/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import {
1111
PARAM_LANGUAGE,
1212
PARAM_THEME,
1313
} from '../utils/config-params';
14-
import { backendFetch, backendFetchJson } from '../utils/api-rest';
14+
import { backendFetch, backendFetchJson, getRestBase } from '../utils/api-rest';
1515
import { LanguageParameters } from '../utils/language';
1616

17-
const PREFIX_CONFIG_QUERIES = `${process.env.REACT_APP_API_GATEWAY}/config`;
17+
const PREFIX_CONFIG_QUERIES = `${getRestBase()}/config`;
1818

1919
// https://github.com/gridsuite/config-server/blob/main/src/main/java/org/gridsuite/config/server/dto/ParameterInfos.java
2020
export type ConfigParameter =

src/services/study.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { backendFetchJson, Token } from '../utils/api-rest';
8+
import { backendFetchJson, getRestBase, Token } from '../utils/api-rest';
99
import { getErrorMessage } from '../utils/error';
1010
import { APP_NAME } from '../utils/config-params';
1111

12-
const STUDY_URL = `${process.env.REACT_APP_API_GATEWAY}/study/v1`;
12+
const STUDY_URL = `${getRestBase()}/study/v1`;
1313

1414
//TODO delete when commons-ui will be in typescript
1515
export type ServerAbout = {

src/services/user-admin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { backendFetch, backendFetchJson } from '../utils/api-rest';
8+
import { backendFetch, backendFetchJson, getRestBase } from '../utils/api-rest';
99
import { extractUserSub, getToken, getUser } from '../utils/api';
1010
import { User } from '../utils/auth';
1111

12-
const USER_ADMIN_URL = `${process.env.REACT_APP_API_GATEWAY}/user-admin/v1`;
12+
const USER_ADMIN_URL = `${getRestBase()}/user-admin/v1`;
1313

1414
export function getUserSub(): Promise<unknown> {
1515
return extractUserSub(getUser());

src/utils/api-rest.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export interface ErrorWithStatus extends Error {
1616
export type Url = string | URL;
1717
export type InitRequest = Partial<RequestInit>;
1818

19+
export function getRestBase(): string {
20+
// We use the `baseURI` (from `<base/>` in index.html) to build the URL, which is corrected by httpd/nginx
21+
return (
22+
document.baseURI.replace(/\/+$/, '') + process.env.REACT_APP_API_GATEWAY
23+
);
24+
}
25+
1926
function handleError(response: Response): Promise<never> {
2027
return response.text().then((text: string) => {
2128
const errorName = 'HttpResponseError : ';

src/utils/api-ws.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import { getToken } from './api';
1010
export type * from './api';
1111

1212
export function getWsBase(): string {
13-
return document.baseURI
14-
.replace(/^http(s?):\/\//, 'ws$1://')
15-
.replace(/\/$/, '');
13+
// We use the `baseURI` (from `<base/>` in index.html) to build the URL, which is corrected by httpd/nginx
14+
return (
15+
document.baseURI
16+
.replace(/^http(s?):\/\//, 'ws$1://')
17+
.replace(/\/+$/, '') + process.env.REACT_APP_WS_GATEWAY
18+
);
1619
}
1720

1821
export function getUrlWithToken(baseUrl: string): string {

0 commit comments

Comments
 (0)