Made infrastructure to ensure private data is not leaked to client
This commit is contained in:
@@ -1,36 +1,109 @@
|
||||
import configData from '../config.yml';
|
||||
|
||||
export type Brand = {
|
||||
export interface Brand {
|
||||
logo?: string;
|
||||
icon?: string;
|
||||
usemask?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export type Section = {
|
||||
export interface Section extends Brand {
|
||||
title: string;
|
||||
subtitle?: string;
|
||||
} & Brand;
|
||||
}
|
||||
|
||||
export type Service = {
|
||||
export interface Service extends Section {
|
||||
url: string;
|
||||
target?: string;
|
||||
type?: string;
|
||||
|
||||
[x: string]: unknown;
|
||||
} & Section;
|
||||
}
|
||||
|
||||
export type ServiceGroup = {
|
||||
export interface ServiceGroup extends Section {
|
||||
items: Service[];
|
||||
} & Section;
|
||||
|
||||
export type Config = {
|
||||
[x: string]: unknown;
|
||||
}
|
||||
|
||||
export interface Config extends Section {
|
||||
services: ServiceGroup[];
|
||||
} & Section;
|
||||
|
||||
export const config: Config = {
|
||||
...{
|
||||
title: 'Flanders',
|
||||
services: []
|
||||
},
|
||||
...configData
|
||||
[x: string]: unknown;
|
||||
}
|
||||
|
||||
type DeepRequired<T> = T extends object
|
||||
? {
|
||||
[P in keyof T]: DeepRequired<T[P]>;
|
||||
}
|
||||
: T;
|
||||
|
||||
const requiredConfig: DeepRequired<Config> = {
|
||||
title: '',
|
||||
subtitle: '',
|
||||
services: [
|
||||
{
|
||||
title: '',
|
||||
items: [
|
||||
{
|
||||
title: '',
|
||||
url: '',
|
||||
}
|
||||
],
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
export function mergeConfig(a: Config, b: any): Config {
|
||||
return { ...a, ...b };
|
||||
}
|
||||
|
||||
export const defaultConfig: Config = {
|
||||
title: 'Flanders',
|
||||
|
||||
services: []
|
||||
};
|
||||
|
||||
|
||||
type SPOJO = Record<string,unknown>
|
||||
|
||||
function strip<Type extends SPOJO> (toStrip: Type, reference: Type): Type {
|
||||
const res: Type = {...toStrip}
|
||||
const referenceNames = Object.entries(reference).map(([key,value]) => key)
|
||||
|
||||
for ( const [key,value] of Object.entries(res) ) {
|
||||
if ( referenceNames.includes(key) == false ) {
|
||||
// remove the object
|
||||
delete res[key];
|
||||
continue
|
||||
}
|
||||
|
||||
// strips further arrays
|
||||
if ( value instanceof Array ) {
|
||||
const stripped : SPOJO = {};
|
||||
const childRef = (reference[key] as Array<SPOJO>)[0];
|
||||
stripped[key] = value.map((v: SPOJO) => strip(v,childRef));
|
||||
Object.assign(res,stripped);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( typeof value != "object") {
|
||||
continue
|
||||
}
|
||||
|
||||
// it is a child object, we strip it further
|
||||
const stripped : SPOJO = {};
|
||||
stripped[key] = strip(value as SPOJO,reference[key] as SPOJO);
|
||||
Object.assign(res,stripped);
|
||||
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export function stripPrivateFields(config: Config): Config {
|
||||
return strip(config,requiredConfig);
|
||||
}
|
||||
|
||||
|
||||
export const config: Config = mergeConfig(defaultConfig, configData);
|
||||
|
||||
export const clientConfig :Config = stripPrivateFields(config)
|
||||
|
||||
Reference in New Issue
Block a user