Passed
Pull Request — master (#41)
by
unknown
04:25
created

src/api/config.ts   A

Complexity

Total Complexity 7
Complexity/F 1.4

Size

Lines of Code 36
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 7
mnd 2
bc 2
fnc 5
bpm 0.4
cpm 1.4
noi 0
1
/**
2
 * Return object which is a shallow merge of 'target' and 'source'. Written manually bacause:
3
 * - {...target, ...source} does not work with generic types (https://github.com/Microsoft/TypeScript/issues/22687)
4
 * - Object.assign does not work in IE <= 11
5
 */
6
function merge<T extends {}, U extends {}>(target: T, source: U): T & U {
7
    const newObj = Object.create(target);
8
    for (const key in source) {
9
        if (source.hasOwnProperty(key)) {
10
            newObj[key] = source[key];
11
        }
12
    }
13
    return newObj;
14
}
15
16
/**
17
 * Simple config class that allows to retrieve and overwrite application config set by environment in window.__config__
18
 */
19
export class AppConfig<T extends {}> {
20
    private config: T;
21
    public constructor() {
22
        // @ts-ignore
23
        this.config = window['__config__'] || {};
24
    }
25
    public get<K extends keyof T>(key: K): T[K] {
26
        return this.config[key];
27
    }
28
    public set(setConfig: Partial<T>): void {
29
        this.config = merge(this.config, setConfig);
30
    }
31
}
32
33
export const config = new AppConfig<{
34
    BASE_PATH: string;
35
}>();
36