Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dependencies": {
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.1",
"@gridsuite/commons-ui": "0.130.0",
"@gridsuite/commons-ui": "0.132.0",
"@hookform/resolvers": "^4.1.3",
"@mui/icons-material": "^5.18.0",
"@mui/lab": "5.0.0-alpha.175",
Expand Down
4 changes: 2 additions & 2 deletions src/services/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { backendFetchJson, getRestBase } from '../utils/api-rest';
import { getRestBase } from '../utils/api-rest';
import type { UUID } from 'node:crypto';
import { ElementAttributes } from '@gridsuite/commons-ui';
import { ElementAttributes, backendFetchJson } from '@gridsuite/commons-ui';

const EXPLORE_URL = `${getRestBase()}/explore/v1`;

Expand Down
4 changes: 2 additions & 2 deletions src/services/study.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { GridSuiteModule } from '@gridsuite/commons-ui';
import { backendFetchJson, getRestBase } from '../utils/api-rest';
import { GridSuiteModule, backendFetchJson } from '@gridsuite/commons-ui';
import { getRestBase } from '../utils/api-rest';
import { getErrorMessage } from '../utils/error';
import { APP_NAME } from '../utils/config-params';

Expand Down
9 changes: 5 additions & 4 deletions src/services/user-admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { backendFetch, backendFetchJson, getRestBase } from '../utils/api-rest';
import { backendFetch, backendFetchJson } from '@gridsuite/commons-ui';
import { getRestBase } from '../utils/api-rest';
import type { UUID } from 'node:crypto';

const USER_ADMIN_URL = `${getRestBase()}/user-admin/v1`;
Expand All @@ -23,7 +24,7 @@ export type UserInfos = UserInfosUpdate & {

export function fetchUsers(): Promise<UserInfos[]> {
console.debug(`Fetching list of users...`);
return backendFetchJson<UserInfos[]>(`${USER_ADMIN_URL}/users`, {
return backendFetchJson(`${USER_ADMIN_URL}/users`, {
headers: {
Accept: 'application/json',
//'Content-Type': 'application/json; utf-8',
Expand Down Expand Up @@ -271,7 +272,7 @@ export type Announcement = NewAnnouncement & {

export async function addAnnouncement(announcement: NewAnnouncement) {
console.debug(`Creating announcement ...`);
return backendFetchJson<Announcement>(
return backendFetchJson(
`${USER_ADMIN_URL}/announcements?startDate=${announcement.startDate}&endDate=${announcement.endDate}&severity=${announcement.severity}`,
{
method: 'put',
Expand All @@ -290,7 +291,7 @@ export async function addAnnouncement(announcement: NewAnnouncement) {
export async function fetchAnnouncementList() {
console.debug(`Fetching announcement ...`);
try {
return await backendFetchJson<Announcement[]>(`${USER_ADMIN_URL}/announcements`, { method: 'get' });
return await backendFetchJson(`${USER_ADMIN_URL}/announcements`, { method: 'get' });
} catch (reason) {
console.error('Error while fetching announcement:', reason);
throw reason;
Expand Down
59 changes: 0 additions & 59 deletions src/utils/api-rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import type { JsonValue } from 'type-fest';
import { getToken, parseError, Token } from './api';

export type { Token } from './api';

export interface ErrorWithStatus extends Error {
status?: number;
}

export type Url = string | URL;
export type InitRequest = Partial<RequestInit>;

export function getRestBase(): string {
// We use the `baseURI` (from `<base/>` in index.html) to build the URL, which is corrected by httpd/nginx
return document.baseURI.replace(/\/+$/, '') + import.meta.env.VITE_API_GATEWAY;
}

function handleError(response: Response): Promise<never> {
return response.text().then((text: string) => {
const errorName = 'HttpResponseError : ';
let error: ErrorWithStatus;
const errorJson = parseError(text);
if (errorJson?.status && errorJson?.error && errorJson?.message) {
error = new Error(
`${errorName}${errorJson.status} ${errorJson.error}, message : ${errorJson.message}`
) as ErrorWithStatus;
error.status = errorJson.status;
} else {
error = new Error(
`${errorName}${response.status} ${response.statusText}, message : ${text}`
) as ErrorWithStatus;
error.status = response.status;
}
throw error;
});
}

function prepareRequest(init?: InitRequest, token?: Token): RequestInit {
if (!(typeof init === 'undefined' || typeof init === 'object')) {
throw new TypeError(`Argument 2 of backendFetch is not an object ${typeof init}`);
}
const initCopy: RequestInit = { ...init };
initCopy.headers = new Headers(initCopy.headers || {});
const tokenCopy = token || getToken();
initCopy.headers.append('Authorization', `Bearer ${tokenCopy}`);
return initCopy;
}

function safeFetch(url: Url, initCopy?: InitRequest) {
return fetch(url, initCopy).then((response: Response) => (response.ok ? response : handleError(response)));
}

export function backendFetch(url: Url, init?: InitRequest, token?: Token): Promise<Response> {
return safeFetch(url, prepareRequest(init, token));
}

export function backendFetchText<R extends string = string>(url: Url, init?: InitRequest, token?: Token) {
return backendFetch(url, init, token).then((safeResponse) => safeResponse.text()) as Promise<R>;
}

export function backendFetchJson<R extends JsonValue>(url: Url, init?: InitRequest, token?: Token): Promise<R> {
return backendFetch(url, init, token).then((safeResponse) => safeResponse.json());
}
Loading