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

@ -1,3 +1,4 @@
import { Box, Input, InputLabel } from '@mui/material';
import EventEmitter from 'events';
import {
ChangeEventHandler,
@ -6,18 +7,16 @@ import {
useRef,
useState,
} from 'react';
import { Box, Input, InputLabel } from '@mui/material';
import { UPLOAD_FILE_TYPES } from '../../lib/consts/UPLOAD_FILE_TYPES';
import api from '../../lib/api';
import { ProgressBar } from '../Bars';
import ContainedButton from '../ContainedButton';
import FileInfo from './FileInfo';
import MessageBox from '../MessageBox';
import { BodyText } from '../Text';
import mainAxiosInstance from '../../lib/singletons/mainAxiosInstance';
type FileUploadFormProps = {
onFileUploadComplete?: () => void;
eventEmitter?: EventEmitter;
@ -117,7 +116,7 @@ const FileUploadForm = (
const inUploadFile: InUploadFile = { fileName, progressValue: 0 };
inUploadFiles.push(inUploadFile);
mainAxiosInstance
api
.post('/file', fileFormData, {
headers: {
'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 INPUT_TYPES from '../lib/consts/INPUT_TYPES';
import api from '../lib/api';
import handleAPIError from '../lib/handleAPIError';
import mainAxiosInstance from '../lib/singletons/mainAxiosInstance';
import {
buildDomainTestBatch,
buildIPAddressTestBatch,
@ -210,7 +210,7 @@ const PrepareHostForm: FC = () => {
setMessage,
setIsSubmitting,
) => {
mainAxiosInstance
api
.put<{
hostName: string;
hostOS: string;
@ -218,21 +218,10 @@ const PrepareHostForm: FC = () => {
isConnected: boolean;
isInetConnected: boolean;
isOSRegistered: boolean;
}>(
'/command/inquire-host',
{
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),
},
)
}>('/command/inquire-host', {
ipAddress: getIdentifier?.call(null),
password: getPassphrase?.call(null),
})
.then(
({
data: {

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

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