fix(striker-ui): revise axios instance to use default data transformers

main
Tsu-ba-me 2 years ago
parent 099b2c9470
commit 8e5dccac03
  1. 18
      striker-ui/components/Files/FileEditForm.tsx
  2. 7
      striker-ui/components/Files/FileUploadForm.tsx
  3. 23
      striker-ui/components/PrepareHostForm.tsx
  4. 24
      striker-ui/components/ProvisionServerDialog.tsx
  5. 17
      striker-ui/components/StrikerInitForm.tsx
  6. 12
      striker-ui/lib/api.ts
  7. 10
      striker-ui/lib/singletons/mainAxiosInstance.ts

@ -1,3 +1,4 @@
import { Box, Checkbox, checkboxClasses } from '@mui/material';
import { AxiosResponse } from 'axios'; import { AxiosResponse } from 'axios';
import { import {
FormEventHandler, FormEventHandler,
@ -5,18 +6,17 @@ import {
useEffect, useEffect,
useState, useState,
} from 'react'; } from 'react';
import { Box, Checkbox, checkboxClasses } from '@mui/material';
import API_BASE_URL from '../../lib/consts/API_BASE_URL'; import API_BASE_URL from '../../lib/consts/API_BASE_URL';
import { GREY, RED, TEXT } from '../../lib/consts/DEFAULT_THEME'; import { GREY, RED, TEXT } from '../../lib/consts/DEFAULT_THEME';
import api from '../../lib/api';
import ConfirmDialog from '../ConfirmDialog'; import ConfirmDialog from '../ConfirmDialog';
import ContainedButton from '../ContainedButton'; import ContainedButton from '../ContainedButton';
import FileInfo from './FileInfo'; import FileInfo from './FileInfo';
import Spinner from '../Spinner'; import Spinner from '../Spinner';
import fetchJSON from '../../lib/fetchers/fetchJSON'; import fetchJSON from '../../lib/fetchers/fetchJSON';
import mainAxiosInstance from '../../lib/singletons/mainAxiosInstance';
type ReducedFileLocation = Partial< type ReducedFileLocation = Partial<
Pick<FileLocation, 'fileLocationUUID' | 'isFileLocationActive'> Pick<FileLocation, 'fileLocationUUID' | 'isFileLocationActive'>
@ -127,17 +127,9 @@ const FileEditForm = (
editRequestContent.fileLocations = changedFileLocations; editRequestContent.fileLocations = changedFileLocations;
} }
const stringEditFileRequestContent = JSON.stringify(editRequestContent); if (Object.keys(editRequestContent).length > 0) {
if (stringEditFileRequestContent !== '{}') {
reducedEditPromises.push( reducedEditPromises.push(
mainAxiosInstance.put( api.put(`/file/${fileUUID}`, editRequestContent),
`/file/${fileUUID}`,
stringEditFileRequestContent,
{
headers: { 'Content-Type': 'application/json' },
},
),
); );
} }
@ -159,7 +151,7 @@ const FileEditForm = (
const purgePromises = filesToEdit const purgePromises = filesToEdit
.filter(({ isSelected }) => isSelected) .filter(({ isSelected }) => isSelected)
.map(({ fileUUID }) => mainAxiosInstance.delete(`/file/${fileUUID}`)); .map(({ fileUUID }) => api.delete(`/file/${fileUUID}`));
Promise.all(purgePromises) Promise.all(purgePromises)
.then(() => { .then(() => {

@ -1,3 +1,4 @@
import { Box, Input, InputLabel } from '@mui/material';
import EventEmitter from 'events'; import EventEmitter from 'events';
import { import {
ChangeEventHandler, ChangeEventHandler,
@ -6,18 +7,16 @@ import {
useRef, useRef,
useState, useState,
} from 'react'; } from 'react';
import { Box, Input, InputLabel } from '@mui/material';
import { UPLOAD_FILE_TYPES } from '../../lib/consts/UPLOAD_FILE_TYPES'; import { UPLOAD_FILE_TYPES } from '../../lib/consts/UPLOAD_FILE_TYPES';
import api from '../../lib/api';
import { ProgressBar } from '../Bars'; import { ProgressBar } from '../Bars';
import ContainedButton from '../ContainedButton'; import ContainedButton from '../ContainedButton';
import FileInfo from './FileInfo'; import FileInfo from './FileInfo';
import MessageBox from '../MessageBox'; import MessageBox from '../MessageBox';
import { BodyText } from '../Text'; import { BodyText } from '../Text';
import mainAxiosInstance from '../../lib/singletons/mainAxiosInstance';
type FileUploadFormProps = { type FileUploadFormProps = {
onFileUploadComplete?: () => void; onFileUploadComplete?: () => void;
eventEmitter?: EventEmitter; eventEmitter?: EventEmitter;
@ -117,7 +116,7 @@ const FileUploadForm = (
const inUploadFile: InUploadFile = { fileName, progressValue: 0 }; const inUploadFile: InUploadFile = { fileName, progressValue: 0 };
inUploadFiles.push(inUploadFile); inUploadFiles.push(inUploadFile);
mainAxiosInstance api
.post('/file', fileFormData, { .post('/file', fileFormData, {
headers: { headers: {
'Content-Type': 'multipart/form-data', 'Content-Type': 'multipart/form-data',

@ -8,8 +8,8 @@ import { FC, useCallback, useMemo, useRef, useState } from 'react';
import { GREY } from '../lib/consts/DEFAULT_THEME'; import { GREY } from '../lib/consts/DEFAULT_THEME';
import INPUT_TYPES from '../lib/consts/INPUT_TYPES'; import INPUT_TYPES from '../lib/consts/INPUT_TYPES';
import api from '../lib/api';
import handleAPIError from '../lib/handleAPIError'; import handleAPIError from '../lib/handleAPIError';
import mainAxiosInstance from '../lib/singletons/mainAxiosInstance';
import { import {
buildDomainTestBatch, buildDomainTestBatch,
buildIPAddressTestBatch, buildIPAddressTestBatch,
@ -210,7 +210,7 @@ const PrepareHostForm: FC = () => {
setMessage, setMessage,
setIsSubmitting, setIsSubmitting,
) => { ) => {
mainAxiosInstance api
.put<{ .put<{
hostName: string; hostName: string;
hostOS: string; hostOS: string;
@ -218,21 +218,10 @@ const PrepareHostForm: FC = () => {
isConnected: boolean; isConnected: boolean;
isInetConnected: boolean; isInetConnected: boolean;
isOSRegistered: boolean; isOSRegistered: boolean;
}>( }>('/command/inquire-host', {
'/command/inquire-host', ipAddress: getIdentifier?.call(null),
{ password: getPassphrase?.call(null),
ipAddress: getIdentifier?.call(null), })
password: getPassphrase?.call(null),
},
{
transformRequest: (data, headers = {}) => {
headers['Content-Type'] = 'application/json';
return JSON.stringify(data);
},
transformResponse: (data) => JSON.parse(data),
},
)
.then( .then(
({ ({
data: { data: {

@ -13,12 +13,12 @@ import { v4 as uuidv4 } from 'uuid';
import { BLUE, RED, TEXT } from '../lib/consts/DEFAULT_THEME'; import { BLUE, RED, TEXT } from '../lib/consts/DEFAULT_THEME';
import api from '../lib/api';
import Autocomplete from './Autocomplete'; import Autocomplete from './Autocomplete';
import ConfirmDialog from './ConfirmDialog'; import ConfirmDialog from './ConfirmDialog';
import ContainedButton from './ContainedButton'; import ContainedButton from './ContainedButton';
import { dsize, dsizeToByte } from '../lib/format_data_size_wrappers'; import { dsize, dsizeToByte } from '../lib/format_data_size_wrappers';
import IconButton, { IconButtonProps } from './IconButton'; import IconButton, { IconButtonProps } from './IconButton';
import mainAxiosInstance from '../lib/singletons/mainAxiosInstance';
import MessageBox, { MessageBoxProps } from './MessageBox'; import MessageBox, { MessageBoxProps } from './MessageBox';
import OutlinedInputWithLabel from './OutlinedInputWithLabel'; import OutlinedInputWithLabel from './OutlinedInputWithLabel';
import OutlinedLabeledInputWithSelect from './OutlinedLabeledInputWithSelect'; import OutlinedLabeledInputWithSelect from './OutlinedLabeledInputWithSelect';
@ -1462,16 +1462,14 @@ const ProvisionServerDialog = ({
}; };
useEffect(() => { useEffect(() => {
mainAxiosInstance api
.get('/anvil', { .get('/anvil', {
params: { params: {
anvilUUIDs: 'all', anvilUUIDs: 'all',
isForProvisionServer: true, isForProvisionServer: true,
}, },
}) })
.then(({ data: stringData }) => { .then(({ data }) => {
const data = JSON.parse(stringData);
const { const {
anvils: ueAllAnvils, anvils: ueAllAnvils,
anvilSelectItems: ueAnvilSelectItems, anvilSelectItems: ueAnvilSelectItems,
@ -1787,7 +1785,7 @@ const ProvisionServerDialog = ({
setIsOpenProvisionConfirmDialog(false); setIsOpenProvisionConfirmDialog(false);
}} }}
onProceedAppend={() => { onProceedAppend={() => {
const requestBody = JSON.stringify({ const requestBody = {
serverName: inputServerNameValue, serverName: inputServerNameValue,
cpuCores: inputCPUCoresValue, cpuCores: inputCPUCoresValue,
memory: memory.toString(), memory: memory.toString(),
@ -1799,18 +1797,14 @@ const ProvisionServerDialog = ({
driverISOFileUUID: inputDriverISOFileUUID, driverISOFileUUID: inputDriverISOFileUUID,
anvilUUID: inputAnvilValue, anvilUUID: inputAnvilValue,
optimizeForOS: inputOptimizeForOSValue?.key, optimizeForOS: inputOptimizeForOSValue?.key,
}); };
setIsProvisionRequestInProgress(true); setIsProvisionRequestInProgress(true);
mainAxiosInstance api.post('/server', requestBody).then(() => {
.post('/server', requestBody, { setIsProvisionRequestInProgress(false);
headers: { 'Content-Type': 'application/json' }, setSuccessfulProvisionCount(successfulProvisionCount + 1);
}) });
.then(() => {
setIsProvisionRequestInProgress(false);
setSuccessfulProvisionCount(successfulProvisionCount + 1);
});
setIsOpenProvisionConfirmDialog(false); setIsOpenProvisionConfirmDialog(false);
}} }}

@ -1,6 +1,7 @@
import { Grid } from '@mui/material'; import { Grid } from '@mui/material';
import { FC, useCallback, useMemo, useRef, useState } from 'react'; import { FC, useCallback, useMemo, useRef, useState } from 'react';
import api from '../lib/api';
import ConfirmDialog from './ConfirmDialog'; import ConfirmDialog from './ConfirmDialog';
import ContainedButton from './ContainedButton'; import ContainedButton from './ContainedButton';
import FlexBox from './FlexBox'; import FlexBox from './FlexBox';
@ -8,7 +9,7 @@ import GeneralInitForm, {
GeneralInitFormForwardedRefContent, GeneralInitFormForwardedRefContent,
GeneralInitFormValues, GeneralInitFormValues,
} from './GeneralInitForm'; } from './GeneralInitForm';
import mainAxiosInstance from '../lib/singletons/mainAxiosInstance'; import handleAPIError from '../lib/handleAPIError';
import MessageBox, { Message } from './MessageBox'; import MessageBox, { Message } from './MessageBox';
import NetworkInitForm, { import NetworkInitForm, {
NetworkInitFormForwardedRefContent, NetworkInitFormForwardedRefContent,
@ -218,19 +219,15 @@ const StrikerInitForm: FC = () => {
setIsSubmittingForm(true); setIsSubmittingForm(true);
setIsOpenConfirm(false); setIsOpenConfirm(false);
mainAxiosInstance api
.put('/command/initialize-striker', JSON.stringify(requestBody), { .post('/host', requestBody)
headers: { 'Content-Type': 'application/json' },
})
.then(() => { .then(() => {
setIsSubmittingForm(false); setIsSubmittingForm(false);
}) })
.catch((reason) => { .catch((error) => {
setSubmitMessage({ const errorMessage = handleAPIError(error);
children: `Failed to submit; ${reason}`,
type: 'error',
});
setSubmitMessage(errorMessage);
setIsSubmittingForm(false); setIsSubmittingForm(false);
}); });
}} }}

@ -0,0 +1,12 @@
import axios, { Axios } from 'axios';
import API_BASE_URL from './consts/API_BASE_URL';
const api = new Axios({
baseURL: API_BASE_URL,
transformRequest: axios.defaults.transformRequest,
transformResponse: axios.defaults.transformResponse,
validateStatus: (status) => status < 400,
});
export default api;

@ -1,10 +0,0 @@
import { Axios } from 'axios';
import API_BASE_URL from '../consts/API_BASE_URL';
const mainAxiosInstance = new Axios({
baseURL: API_BASE_URL,
validateStatus: (status) => status < 400,
});
export default mainAxiosInstance;
Loading…
Cancel
Save