Refac update appsettings endpoint to flatten request body json (#788)

This commit is contained in:
Merlin 2024-08-23 18:35:07 +02:00 committed by GitHub
parent 867ed999ee
commit 6d27b69799
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 1 deletions

View File

@ -4,6 +4,33 @@ import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import { AppSettingsConfigType } from '../loader/loadAppsettingsConfig';
type ObjectType = Record<
string,
string | number | boolean | Record<string, string | number | boolean>
>;
function flattenObject(ob: ObjectType) {
// source: https://stackoverflow.com/a/53739792
const toReturn: ObjectType = {};
for (const i in ob) {
if (!Object.prototype.hasOwnProperty.call(ob, i)) continue;
if (typeof ob[i] == 'object' && ob[i] !== null) {
const flatObject = flattenObject(ob[i]);
for (const x in flatObject) {
if (!Object.prototype.hasOwnProperty.call(flatObject, x)) continue;
toReturn[i + '.' + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
return toReturn;
}
const updateAppsettingsConfig = async (config: AppSettingsConfigType) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
@ -15,7 +42,7 @@ const updateAppsettingsConfig = async (config: AppSettingsConfigType) => {
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify(config),
body: JSON.stringify(flattenObject(config)),
});
const appSettingsConfig = await response.json();