ogi-addon 1.1.0 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,83 +1,83 @@
1
- import { ConfigurationFile, ConfigurationBuilder, BooleanOption, ConfigurationOption, ConfigurationOptionType, NumberOption, StringOption, isBooleanOption, isNumberOption, isStringOption } from "./ConfigurationBuilder";
2
-
3
- interface DefiniteConfig {
4
- [key: string]: string | number | boolean;
5
- }
6
- export class Configuration {
7
- readonly storedConfigTemplate: ConfigurationFile;
8
- definiteConfig: DefiniteConfig = {};
9
- constructor(configTemplate: ConfigurationFile) {
10
- this.storedConfigTemplate = configTemplate;
11
- }
12
-
13
- updateConfig(config: DefiniteConfig, validate: boolean = true): [ boolean, { [key: string]: string } ] {
14
- this.definiteConfig = config;
15
- if (validate) {
16
- const result = this.validateConfig();
17
- return result;
18
- }
19
- return [ true, {} ];
20
- }
21
- // provides falsey or truthy value, and an error message if falsey
22
- private validateConfig(): [ boolean, { [key: string]: string } ] {
23
- const erroredKeys = new Map<string, string>();
24
- for (const key in this.storedConfigTemplate) {
25
- if (this.definiteConfig[key] === null || this.definiteConfig[key] === undefined) {
26
- console.warn('Option ' + key + ' is not defined. Using default value Value: ' + this.definiteConfig[key]);
27
- this.definiteConfig[key] = this.storedConfigTemplate[key].defaultValue as string | number | boolean;
28
- }
29
- if (this.storedConfigTemplate[key].type !== typeof this.definiteConfig[key]) {
30
- throw new Error('Option ' + key + ' is not of the correct type');
31
- }
32
-
33
- const result = this.storedConfigTemplate[key].validate(this.definiteConfig[key]);
34
- if (!result[0]) {
35
- erroredKeys.set(key, result[1]);
36
- }
37
- }
38
-
39
- for (const key in this.definiteConfig) {
40
- if (!this.storedConfigTemplate[key]) {
41
- throw new Error('Option ' + key + ' is not defined in the configuration template');
42
- }
43
- }
44
-
45
- if (erroredKeys.size > 0) {
46
- return [ false, Object.fromEntries(erroredKeys) ];
47
- }
48
-
49
- return [ true, Object.fromEntries(erroredKeys) ];
50
- }
51
-
52
- getStringValue(optionName: string): string {
53
- if (!this.definiteConfig[optionName] === null) {
54
- throw new Error('Option ' + optionName + ' is not defined');
55
- }
56
- if (typeof this.definiteConfig[optionName] !== 'string') {
57
- throw new Error('Option ' + optionName + ' is not a string');
58
- }
59
- return this.definiteConfig[optionName];
60
- }
61
-
62
- getNumberValue(optionName: string): number {
63
- if (!this.definiteConfig[optionName] === null) {
64
- throw new Error('Option ' + optionName + ' is not defined');
65
- }
66
- if (typeof this.definiteConfig[optionName] !== 'number') {
67
- throw new Error('Option ' + optionName + ' is not a number');
68
- }
69
- return this.definiteConfig[optionName];
70
- }
71
-
72
- getBooleanValue(optionName: string): boolean {
73
- if (this.definiteConfig[optionName] === null) {
74
- throw new Error('Option ' + optionName + ' is not defined');
75
- }
76
- if (typeof this.definiteConfig[optionName] !== 'boolean') {
77
- throw new Error('Option ' + optionName + ' is not a boolean');
78
- }
79
- return this.definiteConfig[optionName];
80
- }
81
- }
82
-
1
+ import { ConfigurationFile, ConfigurationBuilder, BooleanOption, ConfigurationOption, ConfigurationOptionType, NumberOption, StringOption, isBooleanOption, isNumberOption, isStringOption } from "./ConfigurationBuilder";
2
+
3
+ interface DefiniteConfig {
4
+ [key: string]: string | number | boolean;
5
+ }
6
+ export class Configuration {
7
+ readonly storedConfigTemplate: ConfigurationFile;
8
+ definiteConfig: DefiniteConfig = {};
9
+ constructor(configTemplate: ConfigurationFile) {
10
+ this.storedConfigTemplate = configTemplate;
11
+ }
12
+
13
+ updateConfig(config: DefiniteConfig, validate: boolean = true): [ boolean, { [key: string]: string } ] {
14
+ this.definiteConfig = config;
15
+ if (validate) {
16
+ const result = this.validateConfig();
17
+ return result;
18
+ }
19
+ return [ true, {} ];
20
+ }
21
+ // provides falsey or truthy value, and an error message if falsey
22
+ private validateConfig(): [ boolean, { [key: string]: string } ] {
23
+ const erroredKeys = new Map<string, string>();
24
+ for (const key in this.storedConfigTemplate) {
25
+ if (this.definiteConfig[key] === null || this.definiteConfig[key] === undefined) {
26
+ console.warn('Option ' + key + ' is not defined. Using default value Value: ' + this.definiteConfig[key]);
27
+ this.definiteConfig[key] = this.storedConfigTemplate[key].defaultValue as string | number | boolean;
28
+ }
29
+ if (this.storedConfigTemplate[key].type !== typeof this.definiteConfig[key]) {
30
+ throw new Error('Option ' + key + ' is not of the correct type');
31
+ }
32
+
33
+ const result = this.storedConfigTemplate[key].validate(this.definiteConfig[key]);
34
+ if (!result[0]) {
35
+ erroredKeys.set(key, result[1]);
36
+ }
37
+ }
38
+
39
+ for (const key in this.definiteConfig) {
40
+ if (!this.storedConfigTemplate[key]) {
41
+ throw new Error('Option ' + key + ' is not defined in the configuration template');
42
+ }
43
+ }
44
+
45
+ if (erroredKeys.size > 0) {
46
+ return [ false, Object.fromEntries(erroredKeys) ];
47
+ }
48
+
49
+ return [ true, Object.fromEntries(erroredKeys) ];
50
+ }
51
+
52
+ getStringValue(optionName: string): string {
53
+ if (!this.definiteConfig[optionName] === null) {
54
+ throw new Error('Option ' + optionName + ' is not defined');
55
+ }
56
+ if (typeof this.definiteConfig[optionName] !== 'string') {
57
+ throw new Error('Option ' + optionName + ' is not a string');
58
+ }
59
+ return this.definiteConfig[optionName];
60
+ }
61
+
62
+ getNumberValue(optionName: string): number {
63
+ if (!this.definiteConfig[optionName] === null) {
64
+ throw new Error('Option ' + optionName + ' is not defined');
65
+ }
66
+ if (typeof this.definiteConfig[optionName] !== 'number') {
67
+ throw new Error('Option ' + optionName + ' is not a number');
68
+ }
69
+ return this.definiteConfig[optionName];
70
+ }
71
+
72
+ getBooleanValue(optionName: string): boolean {
73
+ if (this.definiteConfig[optionName] === null) {
74
+ throw new Error('Option ' + optionName + ' is not defined');
75
+ }
76
+ if (typeof this.definiteConfig[optionName] !== 'boolean') {
77
+ throw new Error('Option ' + optionName + ' is not a boolean');
78
+ }
79
+ return this.definiteConfig[optionName];
80
+ }
81
+ }
82
+
83
83
  export { ConfigurationFile, ConfigurationBuilder, BooleanOption, ConfigurationOption, ConfigurationOptionType, NumberOption, StringOption, isBooleanOption, isNumberOption, isStringOption };