/
Configuration.ts
138 lines (108 loc) · 3.41 KB
/
Configuration.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { passOrThrow, isArray } from '../util';
import { Schema, isSchema } from '../schema/Schema';
import { languageIsoCodeRegex, LANGUAGE_ISO_CODE_PATTERN } from '../constants';
import {
ProtocolConfiguration,
isProtocolConfiguration,
} from '../protocol/ProtocolConfiguration';
import {
StorageConfiguration,
isStorageConfiguration,
} from '../storage/StorageConfiguration';
import * as _ from 'lodash';
export type ConfigurationSetup = {
languages?: string[];
schema?: Schema;
protocolConfiguration?: ProtocolConfiguration;
storageConfiguration?: StorageConfiguration;
};
export class Configuration {
languages: string[];
schema: Schema;
protocolConfiguration: ProtocolConfiguration;
storageConfiguration: StorageConfiguration;
constructor(setup: ConfigurationSetup = {} as ConfigurationSetup) {
const {
languages,
schema,
protocolConfiguration,
storageConfiguration,
} = setup;
this.setLanguages(languages || ['en']);
if (schema) {
this.setSchema(schema);
}
if (protocolConfiguration) {
this.setProtocolConfiguration(protocolConfiguration);
}
if (storageConfiguration) {
this.setStorageConfiguration(storageConfiguration);
}
}
setLanguages(languages): void {
passOrThrow(
isArray(languages, true),
() => 'Configuration.setLanguages() expects a list of language iso codes',
);
languages.map(language => {
passOrThrow(
languageIsoCodeRegex.test(language),
() =>
`Invalid language iso code '${language}' provided (Regex: /${LANGUAGE_ISO_CODE_PATTERN}/)`,
);
});
passOrThrow(
languages.length === _.uniq(languages).length,
() => 'Language codes should be unique in the list',
);
this.languages = languages;
}
getLanguages(): string[] {
return this.languages;
}
getDefaultLanguage(): string {
return this.getLanguages()[0];
}
setSchema(schema): void {
passOrThrow(isSchema(schema), () => 'Configuration expects a valid schema');
this.schema = schema;
}
getSchema(): Schema {
passOrThrow(this.schema, () => 'Configuration is missing a valid schema');
return this.schema;
}
setProtocolConfiguration(protocolConfiguration): void {
passOrThrow(
isProtocolConfiguration(protocolConfiguration),
() => 'Configuration expects a valid protocolConfiguration',
);
this.protocolConfiguration = protocolConfiguration;
protocolConfiguration.getParentConfiguration = () => this;
}
getProtocolConfiguration(): ProtocolConfiguration {
passOrThrow(
this.protocolConfiguration,
() => 'Configuration is missing a valid protocolConfiguration',
);
return this.protocolConfiguration;
}
setStorageConfiguration(storageConfiguration): void {
passOrThrow(
isStorageConfiguration(storageConfiguration),
() => 'Configuration expects a valid storageConfiguration',
);
this.storageConfiguration = storageConfiguration;
storageConfiguration.getParentConfiguration = () => this;
}
getStorageConfiguration(): StorageConfiguration {
passOrThrow(
this.storageConfiguration,
() => 'Configuration is missing a valid storageConfiguration',
);
return this.storageConfiguration;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const isConfiguration = (obj: any): boolean => {
return obj instanceof Configuration;
};