Refac update appsettings endpoint to flatten request body json (#788)
This commit is contained in:
parent
867ed999ee
commit
6d27b69799
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue