Total Complexity | 7 |
Complexity/F | 1.4 |
Lines of Code | 36 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 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 |