import { IConfiguration } from '../../Interfaces/IConfiguration'; import { ISetContainerListRequest } from '../../Interfaces/ISetContainerListRequest'; import { ISetContainerListResponse } from '../../Interfaces/ISetContainerListResponse'; import { GetCircularReplacerToDotnet } from '../../utils/saveload'; /** * Fetch the configuration from the API * @returns {Configation} The model of the configuration for the application */ export async function FetchConfiguration(): Promise { const url = import.meta.env.VITE_API_FETCH_URL; // The test library cannot use the Fetch API // @ts-expect-error // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (window.fetch) { return await fetch(url, { method: 'POST' }) .then(async(response) => await response.json() ) as IConfiguration; } return await new Promise((resolve) => { const xhr = new XMLHttpRequest(); xhr.open('POST', url, true); xhr.onreadystatechange = function() { // Call a function when the state changes. if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { resolve(JSON.parse(this.responseText)); } }; xhr.timeout = 5000; xhr.send(); }); } export async function SetContainerList(request: ISetContainerListRequest): Promise { const url = import.meta.env.VITE_API_SET_CONTAINER_LIST_URL; const dataParsed = JSON.stringify(request, GetCircularReplacerToDotnet()); // The test library cannot use the Fetch API // @ts-expect-error // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (window.fetch) { return await fetch(url, { method: 'POST', headers: new Headers({ // eslint-disable-next-line @typescript-eslint/naming-convention 'Content-Type': 'application/json' }), body: dataParsed }) .then(async(response) => await response.json() ) as ISetContainerListResponse; } return await new Promise((resolve) => { const xhr = new XMLHttpRequest(); xhr.open('POST', url, true); xhr.onreadystatechange = function() { // Call a function when the state changes. if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { resolve(JSON.parse(this.responseText)); } }; xhr.setRequestHeader('Content-type', 'application/json'); xhr.send(dataParsed); }); }