@bemoje/config 0.0.1
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/index.d.ts +53 -0
- package/index.mjs +103 -0
- package/package.json +36 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { TSchema, Static } from '@sinclair/typebox';
|
|
2
|
+
|
|
3
|
+
interface ConfigDataStrategy<T> {
|
|
4
|
+
load(): T | undefined;
|
|
5
|
+
save(content: T): void;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface ConfigValidationStrategy<T> {
|
|
9
|
+
isValid(content: unknown): content is T;
|
|
10
|
+
assertValid(content: unknown): asserts content is T;
|
|
11
|
+
applyDefaults(content?: Partial<T>): T;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Configuration file manager that provides schema validation and file I/O using the Strategy pattern.
|
|
16
|
+
*/
|
|
17
|
+
declare class ConfigFile<Schema extends TSchema> {
|
|
18
|
+
protected static instances: Map<string, ConfigFile<TSchema>>;
|
|
19
|
+
protected readonly data: ConfigDataStrategy<Static<Schema>>;
|
|
20
|
+
protected readonly validation: ConfigValidationStrategy<Static<Schema>>;
|
|
21
|
+
readonly filepath: string;
|
|
22
|
+
readonly schema: Schema;
|
|
23
|
+
constructor(schema: Schema, filepath: string);
|
|
24
|
+
load(): (Schema & {
|
|
25
|
+
params: [];
|
|
26
|
+
})["static"];
|
|
27
|
+
update(cb: (config: Static<Schema>) => Static<Schema>): (Schema & {
|
|
28
|
+
params: [];
|
|
29
|
+
})["static"];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Strategy for loading and saving configuration data as JSON files.
|
|
34
|
+
*/
|
|
35
|
+
declare class JsonFileStrategy<T> implements ConfigDataStrategy<T> {
|
|
36
|
+
readonly filepath: string;
|
|
37
|
+
constructor(filepath: string);
|
|
38
|
+
load(): T | undefined;
|
|
39
|
+
save(config: T): void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Strategy for validating configuration data against a TypeBox schema and applying default values.
|
|
44
|
+
*/
|
|
45
|
+
declare class SchemaConfigStrategy<Schema extends TSchema> implements ConfigValidationStrategy<Static<Schema>> {
|
|
46
|
+
readonly schema: Schema;
|
|
47
|
+
constructor(schema: Schema);
|
|
48
|
+
isValid(config: unknown): config is Static<Schema>;
|
|
49
|
+
assertValid(config: unknown): asserts config is Static<Schema>;
|
|
50
|
+
applyDefaults(config?: Partial<Static<Schema>>): Static<Schema>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { type ConfigDataStrategy, ConfigFile, type ConfigValidationStrategy, JsonFileStrategy, SchemaConfigStrategy };
|
package/index.mjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
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.value = value;
|
|
36
|
+
this.errors = Array.isArray(errors) ? errors : errors instanceof ValueErrorIterator ? Array.from(errors) : Array.from(Value.Errors(errors, value));
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
function assertValidSchema(schema, data, message) {
|
|
40
|
+
const isValid = Value.Check(schema, data);
|
|
41
|
+
if (!isValid) {
|
|
42
|
+
throw new SchemaValidationError(Value.Errors(schema, data), data, message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
__name(assertValidSchema, "assertValidSchema");
|
|
46
|
+
|
|
47
|
+
// src/strategies/SchemaConfigStrategy.ts
|
|
48
|
+
var SchemaConfigStrategy = class {
|
|
49
|
+
static {
|
|
50
|
+
__name(this, "SchemaConfigStrategy");
|
|
51
|
+
}
|
|
52
|
+
schema;
|
|
53
|
+
constructor(schema) {
|
|
54
|
+
this.schema = schema;
|
|
55
|
+
}
|
|
56
|
+
isValid(config) {
|
|
57
|
+
return Value.Check(this.schema, config);
|
|
58
|
+
}
|
|
59
|
+
assertValid(config) {
|
|
60
|
+
assertValidSchema(this.schema, config, "Invalid config.");
|
|
61
|
+
}
|
|
62
|
+
applyDefaults(config = {}) {
|
|
63
|
+
const merged = Value.Default(this.schema, config);
|
|
64
|
+
this.assertValid(merged);
|
|
65
|
+
return merged;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// src/core/ConfigFile.ts
|
|
70
|
+
var ConfigFile = class _ConfigFile {
|
|
71
|
+
static {
|
|
72
|
+
__name(this, "ConfigFile");
|
|
73
|
+
}
|
|
74
|
+
static instances = /* @__PURE__ */ new Map();
|
|
75
|
+
data;
|
|
76
|
+
validation;
|
|
77
|
+
filepath;
|
|
78
|
+
schema;
|
|
79
|
+
constructor(schema, filepath) {
|
|
80
|
+
if (_ConfigFile.instances.has(filepath)) {
|
|
81
|
+
return _ConfigFile.instances.get(filepath);
|
|
82
|
+
} else {
|
|
83
|
+
_ConfigFile.instances.set(filepath, this);
|
|
84
|
+
}
|
|
85
|
+
this.schema = schema;
|
|
86
|
+
this.filepath = filepath;
|
|
87
|
+
this.data = new JsonFileStrategy(filepath);
|
|
88
|
+
this.validation = new SchemaConfigStrategy(schema);
|
|
89
|
+
}
|
|
90
|
+
load() {
|
|
91
|
+
const config = this.data.load();
|
|
92
|
+
const merged = this.validation.applyDefaults(config);
|
|
93
|
+
this.data.save(merged);
|
|
94
|
+
return merged;
|
|
95
|
+
}
|
|
96
|
+
update(cb) {
|
|
97
|
+
const merged = this.validation.applyDefaults(cb(this.load()));
|
|
98
|
+
this.data.save(merged);
|
|
99
|
+
return merged;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export { ConfigFile, JsonFileStrategy, SchemaConfigStrategy };
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bemoje/config",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "config",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"keywords": [
|
|
8
|
+
"config"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"import": "./index.mjs",
|
|
14
|
+
"default": "./index.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@sinclair/typebox": "^0.34.37",
|
|
19
|
+
"fs-extra": "^11.3.0",
|
|
20
|
+
"@types/fs-extra": "^11.0.4"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": {
|
|
27
|
+
"name": "Benjamin Møller Jensen",
|
|
28
|
+
"email": "bemoje@bemoje.net",
|
|
29
|
+
"url": "https://github.com/bemoje/"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/bemoje/mono.git",
|
|
34
|
+
"directory": "libs/config"
|
|
35
|
+
}
|
|
36
|
+
}
|