@bemoje/config 1.1.0 → 1.1.2
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.
- package/lib/core/ConfigFile.cjs +78 -9
- package/lib/core/ConfigFile.js +74 -8
- package/lib/index.cjs +107 -35
- package/lib/index.js +104 -5
- package/lib/strategies/JsonFileStrategy.cjs +2 -2
- package/lib/strategies/JsonFileStrategy.js +4 -3
- package/lib/strategies/SchemaConfigStrategy.cjs +26 -4
- package/lib/strategies/SchemaConfigStrategy.js +27 -4
- package/package.json +1 -1
package/lib/core/ConfigFile.cjs
CHANGED
|
@@ -1,11 +1,80 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var fs = require('fs-extra');
|
|
4
|
+
var value = require('@sinclair/typebox/value');
|
|
5
|
+
var compiler = require('@sinclair/typebox/compiler');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
5
10
|
|
|
6
11
|
var __defProp = Object.defineProperty;
|
|
7
12
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
-
class
|
|
13
|
+
var JsonFileStrategy = class {
|
|
14
|
+
static {
|
|
15
|
+
__name(this, "JsonFileStrategy");
|
|
16
|
+
}
|
|
17
|
+
filepath;
|
|
18
|
+
constructor(filepath) {
|
|
19
|
+
this.filepath = filepath;
|
|
20
|
+
}
|
|
21
|
+
load() {
|
|
22
|
+
try {
|
|
23
|
+
return fs__default.default.readJsonSync(this.filepath);
|
|
24
|
+
} catch (_) {
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
save(config) {
|
|
29
|
+
fs__default.default.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
|
|
30
|
+
`);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var SchemaValidationError = class extends Error {
|
|
34
|
+
static {
|
|
35
|
+
__name(this, "SchemaValidationError");
|
|
36
|
+
}
|
|
37
|
+
value;
|
|
38
|
+
errors;
|
|
39
|
+
constructor(errors, value$1, message) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = "SchemaValidationError";
|
|
42
|
+
this.value = value$1;
|
|
43
|
+
this.errors = Array.isArray(errors) ? errors : errors instanceof compiler.ValueErrorIterator ? Array.from(errors) : Array.from(value.Value.Errors(errors, value$1));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
function assertValidSchema(schema, data, message) {
|
|
47
|
+
const isValid = value.Value.Check(schema, data);
|
|
48
|
+
if (!isValid) {
|
|
49
|
+
throw new SchemaValidationError(value.Value.Errors(schema, data), data, message);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
__name(assertValidSchema, "assertValidSchema");
|
|
53
|
+
|
|
54
|
+
// src/strategies/SchemaConfigStrategy.ts
|
|
55
|
+
var SchemaConfigStrategy = class {
|
|
56
|
+
static {
|
|
57
|
+
__name(this, "SchemaConfigStrategy");
|
|
58
|
+
}
|
|
59
|
+
schema;
|
|
60
|
+
constructor(schema) {
|
|
61
|
+
this.schema = schema;
|
|
62
|
+
}
|
|
63
|
+
isValid(config) {
|
|
64
|
+
return value.Value.Check(this.schema, config);
|
|
65
|
+
}
|
|
66
|
+
assertValid(config) {
|
|
67
|
+
assertValidSchema(this.schema, config, "Invalid config.");
|
|
68
|
+
}
|
|
69
|
+
applyDefaults(config = {}) {
|
|
70
|
+
const merged = value.Value.Default(this.schema, config);
|
|
71
|
+
this.assertValid(merged);
|
|
72
|
+
return merged;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// src/core/ConfigFile.ts
|
|
77
|
+
var ConfigFile = class _ConfigFile {
|
|
9
78
|
static {
|
|
10
79
|
__name(this, "ConfigFile");
|
|
11
80
|
}
|
|
@@ -15,15 +84,15 @@ class ConfigFile {
|
|
|
15
84
|
filepath;
|
|
16
85
|
schema;
|
|
17
86
|
constructor(schema, filepath) {
|
|
18
|
-
if (
|
|
19
|
-
return
|
|
87
|
+
if (_ConfigFile.instances.has(filepath)) {
|
|
88
|
+
return _ConfigFile.instances.get(filepath);
|
|
20
89
|
} else {
|
|
21
|
-
|
|
90
|
+
_ConfigFile.instances.set(filepath, this);
|
|
22
91
|
}
|
|
23
92
|
this.schema = schema;
|
|
24
93
|
this.filepath = filepath;
|
|
25
|
-
this.data = new JsonFileStrategy
|
|
26
|
-
this.validation = new SchemaConfigStrategy
|
|
94
|
+
this.data = new JsonFileStrategy(filepath);
|
|
95
|
+
this.validation = new SchemaConfigStrategy(schema);
|
|
27
96
|
}
|
|
28
97
|
load() {
|
|
29
98
|
const config = this.data.load();
|
|
@@ -36,6 +105,6 @@ class ConfigFile {
|
|
|
36
105
|
this.data.save(merged);
|
|
37
106
|
return merged;
|
|
38
107
|
}
|
|
39
|
-
}
|
|
108
|
+
};
|
|
40
109
|
|
|
41
110
|
exports.ConfigFile = ConfigFile;
|
package/lib/core/ConfigFile.js
CHANGED
|
@@ -1,8 +1,74 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import { Value } from '@sinclair/typebox/value';
|
|
3
|
+
import { ValueErrorIterator } from '@sinclair/typebox/compiler';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var JsonFileStrategy = class {
|
|
8
|
+
static {
|
|
9
|
+
__name(this, "JsonFileStrategy");
|
|
10
|
+
}
|
|
11
|
+
filepath;
|
|
12
|
+
constructor(filepath) {
|
|
13
|
+
this.filepath = filepath;
|
|
14
|
+
}
|
|
15
|
+
load() {
|
|
16
|
+
try {
|
|
17
|
+
return fs.readJsonSync(this.filepath);
|
|
18
|
+
} catch (_) {
|
|
19
|
+
return void 0;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
save(config) {
|
|
23
|
+
fs.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
|
|
24
|
+
`);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var SchemaValidationError = class extends Error {
|
|
28
|
+
static {
|
|
29
|
+
__name(this, "SchemaValidationError");
|
|
30
|
+
}
|
|
31
|
+
value;
|
|
32
|
+
errors;
|
|
33
|
+
constructor(errors, value, message) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "SchemaValidationError";
|
|
36
|
+
this.value = value;
|
|
37
|
+
this.errors = Array.isArray(errors) ? errors : errors instanceof ValueErrorIterator ? Array.from(errors) : Array.from(Value.Errors(errors, value));
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
function assertValidSchema(schema, data, message) {
|
|
41
|
+
const isValid = Value.Check(schema, data);
|
|
42
|
+
if (!isValid) {
|
|
43
|
+
throw new SchemaValidationError(Value.Errors(schema, data), data, message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
__name(assertValidSchema, "assertValidSchema");
|
|
47
|
+
|
|
48
|
+
// src/strategies/SchemaConfigStrategy.ts
|
|
49
|
+
var SchemaConfigStrategy = class {
|
|
50
|
+
static {
|
|
51
|
+
__name(this, "SchemaConfigStrategy");
|
|
52
|
+
}
|
|
53
|
+
schema;
|
|
54
|
+
constructor(schema) {
|
|
55
|
+
this.schema = schema;
|
|
56
|
+
}
|
|
57
|
+
isValid(config) {
|
|
58
|
+
return Value.Check(this.schema, config);
|
|
59
|
+
}
|
|
60
|
+
assertValid(config) {
|
|
61
|
+
assertValidSchema(this.schema, config, "Invalid config.");
|
|
62
|
+
}
|
|
63
|
+
applyDefaults(config = {}) {
|
|
64
|
+
const merged = Value.Default(this.schema, config);
|
|
65
|
+
this.assertValid(merged);
|
|
66
|
+
return merged;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/core/ConfigFile.ts
|
|
71
|
+
var ConfigFile = class _ConfigFile {
|
|
6
72
|
static {
|
|
7
73
|
__name(this, "ConfigFile");
|
|
8
74
|
}
|
|
@@ -12,10 +78,10 @@ class ConfigFile {
|
|
|
12
78
|
filepath;
|
|
13
79
|
schema;
|
|
14
80
|
constructor(schema, filepath) {
|
|
15
|
-
if (
|
|
16
|
-
return
|
|
81
|
+
if (_ConfigFile.instances.has(filepath)) {
|
|
82
|
+
return _ConfigFile.instances.get(filepath);
|
|
17
83
|
} else {
|
|
18
|
-
|
|
84
|
+
_ConfigFile.instances.set(filepath, this);
|
|
19
85
|
}
|
|
20
86
|
this.schema = schema;
|
|
21
87
|
this.filepath = filepath;
|
|
@@ -33,6 +99,6 @@ class ConfigFile {
|
|
|
33
99
|
this.data.save(merged);
|
|
34
100
|
return merged;
|
|
35
101
|
}
|
|
36
|
-
}
|
|
102
|
+
};
|
|
37
103
|
|
|
38
104
|
export { ConfigFile };
|
package/lib/index.cjs
CHANGED
|
@@ -1,40 +1,112 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var JsonFileStrategy = require('./strategies/JsonFileStrategy');
|
|
7
|
-
var SchemaConfigStrategy = require('./strategies/SchemaConfigStrategy');
|
|
3
|
+
var fs = require('fs-extra');
|
|
4
|
+
var value = require('@sinclair/typebox/value');
|
|
5
|
+
var compiler = require('@sinclair/typebox/compiler');
|
|
8
6
|
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
8
|
|
|
9
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
10
10
|
|
|
11
|
-
Object.
|
|
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
|
-
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
13
|
+
var JsonFileStrategy = class {
|
|
14
|
+
static {
|
|
15
|
+
__name(this, "JsonFileStrategy");
|
|
16
|
+
}
|
|
17
|
+
filepath;
|
|
18
|
+
constructor(filepath) {
|
|
19
|
+
this.filepath = filepath;
|
|
20
|
+
}
|
|
21
|
+
load() {
|
|
22
|
+
try {
|
|
23
|
+
return fs__default.default.readJsonSync(this.filepath);
|
|
24
|
+
} catch (_) {
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
save(config) {
|
|
29
|
+
fs__default.default.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
|
|
30
|
+
`);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var SchemaValidationError = class extends Error {
|
|
34
|
+
static {
|
|
35
|
+
__name(this, "SchemaValidationError");
|
|
36
|
+
}
|
|
37
|
+
value;
|
|
38
|
+
errors;
|
|
39
|
+
constructor(errors, value$1, message) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = "SchemaValidationError";
|
|
42
|
+
this.value = value$1;
|
|
43
|
+
this.errors = Array.isArray(errors) ? errors : errors instanceof compiler.ValueErrorIterator ? Array.from(errors) : Array.from(value.Value.Errors(errors, value$1));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
function assertValidSchema(schema, data, message) {
|
|
47
|
+
const isValid = value.Value.Check(schema, data);
|
|
48
|
+
if (!isValid) {
|
|
49
|
+
throw new SchemaValidationError(value.Value.Errors(schema, data), data, message);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
__name(assertValidSchema, "assertValidSchema");
|
|
53
|
+
|
|
54
|
+
// src/strategies/SchemaConfigStrategy.ts
|
|
55
|
+
var SchemaConfigStrategy = class {
|
|
56
|
+
static {
|
|
57
|
+
__name(this, "SchemaConfigStrategy");
|
|
58
|
+
}
|
|
59
|
+
schema;
|
|
60
|
+
constructor(schema) {
|
|
61
|
+
this.schema = schema;
|
|
62
|
+
}
|
|
63
|
+
isValid(config) {
|
|
64
|
+
return value.Value.Check(this.schema, config);
|
|
65
|
+
}
|
|
66
|
+
assertValid(config) {
|
|
67
|
+
assertValidSchema(this.schema, config, "Invalid config.");
|
|
68
|
+
}
|
|
69
|
+
applyDefaults(config = {}) {
|
|
70
|
+
const merged = value.Value.Default(this.schema, config);
|
|
71
|
+
this.assertValid(merged);
|
|
72
|
+
return merged;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// src/core/ConfigFile.ts
|
|
77
|
+
var ConfigFile = class _ConfigFile {
|
|
78
|
+
static {
|
|
79
|
+
__name(this, "ConfigFile");
|
|
80
|
+
}
|
|
81
|
+
static instances = /* @__PURE__ */ new Map();
|
|
82
|
+
data;
|
|
83
|
+
validation;
|
|
84
|
+
filepath;
|
|
85
|
+
schema;
|
|
86
|
+
constructor(schema, filepath) {
|
|
87
|
+
if (_ConfigFile.instances.has(filepath)) {
|
|
88
|
+
return _ConfigFile.instances.get(filepath);
|
|
89
|
+
} else {
|
|
90
|
+
_ConfigFile.instances.set(filepath, this);
|
|
91
|
+
}
|
|
92
|
+
this.schema = schema;
|
|
93
|
+
this.filepath = filepath;
|
|
94
|
+
this.data = new JsonFileStrategy(filepath);
|
|
95
|
+
this.validation = new SchemaConfigStrategy(schema);
|
|
96
|
+
}
|
|
97
|
+
load() {
|
|
98
|
+
const config = this.data.load();
|
|
99
|
+
const merged = this.validation.applyDefaults(config);
|
|
100
|
+
this.data.save(merged);
|
|
101
|
+
return merged;
|
|
102
|
+
}
|
|
103
|
+
update(cb) {
|
|
104
|
+
const merged = this.validation.applyDefaults(cb(this.load()));
|
|
105
|
+
this.data.save(merged);
|
|
106
|
+
return merged;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
exports.ConfigFile = ConfigFile;
|
|
111
|
+
exports.JsonFileStrategy = JsonFileStrategy;
|
|
112
|
+
exports.SchemaConfigStrategy = SchemaConfigStrategy;
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,104 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import { Value } from '@sinclair/typebox/value';
|
|
3
|
+
import { ValueErrorIterator } from '@sinclair/typebox/compiler';
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var JsonFileStrategy = class {
|
|
8
|
+
static {
|
|
9
|
+
__name(this, "JsonFileStrategy");
|
|
10
|
+
}
|
|
11
|
+
filepath;
|
|
12
|
+
constructor(filepath) {
|
|
13
|
+
this.filepath = filepath;
|
|
14
|
+
}
|
|
15
|
+
load() {
|
|
16
|
+
try {
|
|
17
|
+
return fs.readJsonSync(this.filepath);
|
|
18
|
+
} catch (_) {
|
|
19
|
+
return void 0;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
save(config) {
|
|
23
|
+
fs.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
|
|
24
|
+
`);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var SchemaValidationError = class extends Error {
|
|
28
|
+
static {
|
|
29
|
+
__name(this, "SchemaValidationError");
|
|
30
|
+
}
|
|
31
|
+
value;
|
|
32
|
+
errors;
|
|
33
|
+
constructor(errors, value, message) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "SchemaValidationError";
|
|
36
|
+
this.value = value;
|
|
37
|
+
this.errors = Array.isArray(errors) ? errors : errors instanceof ValueErrorIterator ? Array.from(errors) : Array.from(Value.Errors(errors, value));
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
function assertValidSchema(schema, data, message) {
|
|
41
|
+
const isValid = Value.Check(schema, data);
|
|
42
|
+
if (!isValid) {
|
|
43
|
+
throw new SchemaValidationError(Value.Errors(schema, data), data, message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
__name(assertValidSchema, "assertValidSchema");
|
|
47
|
+
|
|
48
|
+
// src/strategies/SchemaConfigStrategy.ts
|
|
49
|
+
var SchemaConfigStrategy = class {
|
|
50
|
+
static {
|
|
51
|
+
__name(this, "SchemaConfigStrategy");
|
|
52
|
+
}
|
|
53
|
+
schema;
|
|
54
|
+
constructor(schema) {
|
|
55
|
+
this.schema = schema;
|
|
56
|
+
}
|
|
57
|
+
isValid(config) {
|
|
58
|
+
return Value.Check(this.schema, config);
|
|
59
|
+
}
|
|
60
|
+
assertValid(config) {
|
|
61
|
+
assertValidSchema(this.schema, config, "Invalid config.");
|
|
62
|
+
}
|
|
63
|
+
applyDefaults(config = {}) {
|
|
64
|
+
const merged = Value.Default(this.schema, config);
|
|
65
|
+
this.assertValid(merged);
|
|
66
|
+
return merged;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/core/ConfigFile.ts
|
|
71
|
+
var ConfigFile = class _ConfigFile {
|
|
72
|
+
static {
|
|
73
|
+
__name(this, "ConfigFile");
|
|
74
|
+
}
|
|
75
|
+
static instances = /* @__PURE__ */ new Map();
|
|
76
|
+
data;
|
|
77
|
+
validation;
|
|
78
|
+
filepath;
|
|
79
|
+
schema;
|
|
80
|
+
constructor(schema, filepath) {
|
|
81
|
+
if (_ConfigFile.instances.has(filepath)) {
|
|
82
|
+
return _ConfigFile.instances.get(filepath);
|
|
83
|
+
} else {
|
|
84
|
+
_ConfigFile.instances.set(filepath, this);
|
|
85
|
+
}
|
|
86
|
+
this.schema = schema;
|
|
87
|
+
this.filepath = filepath;
|
|
88
|
+
this.data = new JsonFileStrategy(filepath);
|
|
89
|
+
this.validation = new SchemaConfigStrategy(schema);
|
|
90
|
+
}
|
|
91
|
+
load() {
|
|
92
|
+
const config = this.data.load();
|
|
93
|
+
const merged = this.validation.applyDefaults(config);
|
|
94
|
+
this.data.save(merged);
|
|
95
|
+
return merged;
|
|
96
|
+
}
|
|
97
|
+
update(cb) {
|
|
98
|
+
const merged = this.validation.applyDefaults(cb(this.load()));
|
|
99
|
+
this.data.save(merged);
|
|
100
|
+
return merged;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export { ConfigFile, JsonFileStrategy, SchemaConfigStrategy };
|
|
@@ -8,7 +8,7 @@ var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
|
8
8
|
|
|
9
9
|
var __defProp = Object.defineProperty;
|
|
10
10
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
11
|
-
|
|
11
|
+
var JsonFileStrategy = class {
|
|
12
12
|
static {
|
|
13
13
|
__name(this, "JsonFileStrategy");
|
|
14
14
|
}
|
|
@@ -27,6 +27,6 @@ class JsonFileStrategy {
|
|
|
27
27
|
fs__default.default.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
|
|
28
28
|
`);
|
|
29
29
|
}
|
|
30
|
-
}
|
|
30
|
+
};
|
|
31
31
|
|
|
32
32
|
exports.JsonFileStrategy = JsonFileStrategy;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { __name } from '../chunk-SHUYVCID.js';
|
|
2
1
|
import fs from 'fs-extra';
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
+
var JsonFileStrategy = class {
|
|
5
6
|
static {
|
|
6
7
|
__name(this, "JsonFileStrategy");
|
|
7
8
|
}
|
|
@@ -20,6 +21,6 @@ class JsonFileStrategy {
|
|
|
20
21
|
fs.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
|
|
21
22
|
`);
|
|
22
23
|
}
|
|
23
|
-
}
|
|
24
|
+
};
|
|
24
25
|
|
|
25
26
|
export { JsonFileStrategy };
|
|
@@ -1,11 +1,33 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var value = require('@sinclair/typebox/value');
|
|
4
|
-
var
|
|
4
|
+
var compiler = require('@sinclair/typebox/compiler');
|
|
5
5
|
|
|
6
6
|
var __defProp = Object.defineProperty;
|
|
7
7
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
-
class
|
|
8
|
+
var SchemaValidationError = class extends Error {
|
|
9
|
+
static {
|
|
10
|
+
__name(this, "SchemaValidationError");
|
|
11
|
+
}
|
|
12
|
+
value;
|
|
13
|
+
errors;
|
|
14
|
+
constructor(errors, value$1, message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "SchemaValidationError";
|
|
17
|
+
this.value = value$1;
|
|
18
|
+
this.errors = Array.isArray(errors) ? errors : errors instanceof compiler.ValueErrorIterator ? Array.from(errors) : Array.from(value.Value.Errors(errors, value$1));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
function assertValidSchema(schema, data, message) {
|
|
22
|
+
const isValid = value.Value.Check(schema, data);
|
|
23
|
+
if (!isValid) {
|
|
24
|
+
throw new SchemaValidationError(value.Value.Errors(schema, data), data, message);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
__name(assertValidSchema, "assertValidSchema");
|
|
28
|
+
|
|
29
|
+
// src/strategies/SchemaConfigStrategy.ts
|
|
30
|
+
var SchemaConfigStrategy = class {
|
|
9
31
|
static {
|
|
10
32
|
__name(this, "SchemaConfigStrategy");
|
|
11
33
|
}
|
|
@@ -17,13 +39,13 @@ class SchemaConfigStrategy {
|
|
|
17
39
|
return value.Value.Check(this.schema, config);
|
|
18
40
|
}
|
|
19
41
|
assertValid(config) {
|
|
20
|
-
|
|
42
|
+
assertValidSchema(this.schema, config, "Invalid config.");
|
|
21
43
|
}
|
|
22
44
|
applyDefaults(config = {}) {
|
|
23
45
|
const merged = value.Value.Default(this.schema, config);
|
|
24
46
|
this.assertValid(merged);
|
|
25
47
|
return merged;
|
|
26
48
|
}
|
|
27
|
-
}
|
|
49
|
+
};
|
|
28
50
|
|
|
29
51
|
exports.SchemaConfigStrategy = SchemaConfigStrategy;
|
|
@@ -1,8 +1,31 @@
|
|
|
1
|
-
import { __name } from '../chunk-SHUYVCID.js';
|
|
2
1
|
import { Value } from '@sinclair/typebox/value';
|
|
3
|
-
import {
|
|
2
|
+
import { ValueErrorIterator } from '@sinclair/typebox/compiler';
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var SchemaValidationError = class extends Error {
|
|
7
|
+
static {
|
|
8
|
+
__name(this, "SchemaValidationError");
|
|
9
|
+
}
|
|
10
|
+
value;
|
|
11
|
+
errors;
|
|
12
|
+
constructor(errors, value, message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "SchemaValidationError";
|
|
15
|
+
this.value = value;
|
|
16
|
+
this.errors = Array.isArray(errors) ? errors : errors instanceof ValueErrorIterator ? Array.from(errors) : Array.from(Value.Errors(errors, value));
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
function assertValidSchema(schema, data, message) {
|
|
20
|
+
const isValid = Value.Check(schema, data);
|
|
21
|
+
if (!isValid) {
|
|
22
|
+
throw new SchemaValidationError(Value.Errors(schema, data), data, message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
__name(assertValidSchema, "assertValidSchema");
|
|
26
|
+
|
|
27
|
+
// src/strategies/SchemaConfigStrategy.ts
|
|
28
|
+
var SchemaConfigStrategy = class {
|
|
6
29
|
static {
|
|
7
30
|
__name(this, "SchemaConfigStrategy");
|
|
8
31
|
}
|
|
@@ -21,6 +44,6 @@ class SchemaConfigStrategy {
|
|
|
21
44
|
this.assertValid(merged);
|
|
22
45
|
return merged;
|
|
23
46
|
}
|
|
24
|
-
}
|
|
47
|
+
};
|
|
25
48
|
|
|
26
49
|
export { SchemaConfigStrategy };
|