-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.ts
41 lines (33 loc) · 1.31 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import MicroEvent from '../vendor/microevent';
import * as configDefaults from './config-defaults';
class Config extends MicroEvent<{change: [Record<string, any>]}> {
keys = ['apiKey', 'eventTracker', 'coreUrl', 'eventsTrackUrl'] as const;
_attrs = {...configDefaults};
set(config: Record<string, any>) {
const changedAttributes = {};
for (let key in config) {
if (key === 'appKey') key = 'apiKey';
if (this.keys.includes(key as (typeof this.keys)[number])) {
this._attrs[key] = config[key];
changedAttributes[key] = config[key];
}
}
this.trigger('change', changedAttributes);
}
get<T>(option: string): T {
if (option === 'appKey') option = 'apiKey';
return this._attrs[option];
}
shadow<T extends Record<string, any> = Record<string, any>>(
optionsObject?: T
): T & Record<(typeof this.keys)[number], string> {
const optionsWithConfig = {...optionsObject};
this.keys.forEach((key) => {
const get = () => optionsObject?.[key] || this.get(key);
Object.defineProperty(optionsWithConfig, key, {get});
});
return optionsWithConfig as T &
Record<(typeof this.keys)[number], string>;
}
}
export default Config;