fix(axios): move json2formdata util

feat/EE-5573/related-changes
Chaim Lev-Ari 2023-10-17 10:01:52 +02:00
parent cc605b66ee
commit e3c2fd1699
3 changed files with 33 additions and 24 deletions

View File

@ -101,3 +101,30 @@ export function isAxiosError<
>(error: unknown): error is AxiosError<ResponseType> {
return axiosOrigin.isAxiosError(error);
}
export function arrayToJson<T>(arr?: Array<T>) {
if (!arr) {
return '';
}
return JSON.stringify(arr);
}
export function json2formData(json: Record<string, unknown>) {
const formData = new FormData();
Object.entries(json).forEach(([key, value]) => {
if (typeof value === 'undefined' || value === null) {
return;
}
if (Array.isArray(value)) {
formData.append(key, arrayToJson(value));
return;
}
formData.append(key, value as string);
});
return formData;
}

View File

@ -1,11 +1,15 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import axios, {
parseAxiosError,
json2formData,
arrayToJson,
} from '@/portainer/services/axios';
import { type EnvironmentGroupId } from '@/react/portainer/environments/environment-groups/types';
import { type TagId } from '@/portainer/tags/types';
import { EdgeAsyncIntervalsValues } from '@/react/edge/components/EdgeAsyncIntervalsForm';
import { type Environment, EnvironmentCreationTypes } from '../types';
import { arrayToJson, buildUrl, json2formData } from './utils';
import { buildUrl } from './utils';
export interface EnvironmentMetadata {
groupId?: EnvironmentGroupId;

View File

@ -12,25 +12,3 @@ export function buildUrl(id?: EnvironmentId, action?: string) {
return baseUrl;
}
export function arrayToJson<T>(arr?: Array<T>) {
if (!arr) {
return '';
}
return JSON.stringify(arr);
}
export function json2formData(json: Record<string, unknown>) {
const formData = new FormData();
Object.entries(json).forEach(([key, value]) => {
if (typeof value === 'undefined' || value === null) {
return;
}
formData.append(key, value as string);
});
return formData;
}