allow clients to filter content api call

This commit is contained in:
Phillip Thelen
2024-01-25 16:16:49 +01:00
committed by Sabe Jones
parent ed790c1c4d
commit 39252c7828
2 changed files with 80 additions and 9 deletions

View File

@@ -5,23 +5,31 @@ import packageInfo from '../../../package.json';
export const CONTENT_CACHE_PATH = path.join(__dirname, '/../../../content_cache/');
function walkContent (obj, lang) {
function walkContent (obj, lang, removedKeys = {}) {
_.each(obj, (item, key, source) => {
if (key in removedKeys && removedKeys[key] === true) {
delete source[key];
return;
}
if (_.isPlainObject(item) || _.isArray(item)) {
walkContent(item, lang);
if (key in removedKeys && _.isPlainObject(removedKeys[key])) {
walkContent(item, lang, removedKeys[key]);
} else {
walkContent(item, lang);
}
} else if (_.isFunction(item) && item.i18nLangFunc) {
source[key] = item(lang);
}
});
}
export function localizeContentData (data, langCode) {
export function localizeContentData (data, langCode, removedKeys = {}) {
const dataClone = _.cloneDeep(data);
walkContent(dataClone, langCode);
walkContent(dataClone, langCode, removedKeys);
return dataClone;
}
export function getLocalizedContentResponse (langCode) {
const localizedContent = localizeContentData(common.content, langCode);
export function getLocalizedContentResponse (langCode, removedKeys = {}) {
const localizedContent = localizeContentData(common.content, langCode, removedKeys);
return `{"success": true, "data": ${JSON.stringify(localizedContent)}, "appVersion": "${packageInfo.version}"}`;
}