@bemoje/config 1.0.0 → 1.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/README.md +83 -0
- package/index.mjs +1 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# @bemoje/config
|
|
2
|
+
|
|
3
|
+
Configuration file management with JSON persistence and TypeBox schema validation.
|
|
4
|
+
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/api/esm.html)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @bemoje/config
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- Schema-validated configuration with TypeBox
|
|
17
|
+
- Automatic default value merging
|
|
18
|
+
- JSON file persistence strategy
|
|
19
|
+
- Singleton pattern per filepath
|
|
20
|
+
- Strategy-based architecture for extensibility
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Basic Configuration
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { ConfigFile } from '@bemoje/config'
|
|
28
|
+
import { Type } from '@sinclair/typebox'
|
|
29
|
+
|
|
30
|
+
const schema = Type.Object({
|
|
31
|
+
port: Type.Number({ default: 3000 }),
|
|
32
|
+
host: Type.String({ default: 'localhost' }),
|
|
33
|
+
debug: Type.Boolean({ default: false }),
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const config = new ConfigFile(schema, './config.json')
|
|
37
|
+
|
|
38
|
+
// Load config (creates file with defaults if it doesn't exist)
|
|
39
|
+
const data = config.load()
|
|
40
|
+
// => { port: 3000, host: 'localhost', debug: false }
|
|
41
|
+
|
|
42
|
+
// Update config
|
|
43
|
+
config.update((current) => ({ ...current, port: 8080, debug: true }))
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Custom Strategies
|
|
47
|
+
|
|
48
|
+
The architecture uses the Strategy pattern for data I/O and validation:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { JsonFileStrategy } from '@bemoje/config'
|
|
52
|
+
|
|
53
|
+
// JSON file strategy handles read/write
|
|
54
|
+
const dataStrategy = new JsonFileStrategy<MyConfig>('./config.json')
|
|
55
|
+
dataStrategy.load() // => parsed JSON or undefined
|
|
56
|
+
dataStrategy.save({ port: 3000 })
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { SchemaConfigStrategy } from '@bemoje/config'
|
|
61
|
+
import { Type } from '@sinclair/typebox'
|
|
62
|
+
|
|
63
|
+
const schema = Type.Object({ name: Type.String(), version: Type.String({ default: '1.0.0' }) })
|
|
64
|
+
|
|
65
|
+
const validation = new SchemaConfigStrategy(schema)
|
|
66
|
+
|
|
67
|
+
// Validate data
|
|
68
|
+
validation.isValid({ name: 'app', version: '2.0.0' }) // => true
|
|
69
|
+
|
|
70
|
+
// Apply defaults to partial data
|
|
71
|
+
validation.applyDefaults({ name: 'app' })
|
|
72
|
+
// => { name: 'app', version: '1.0.0' }
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## API Reference
|
|
76
|
+
|
|
77
|
+
| Export | Description |
|
|
78
|
+
| -------------------------- | -------------------------------------------------------------------------------------- |
|
|
79
|
+
| `ConfigFile` | Main config class with singleton per filepath, schema validation, and JSON persistence |
|
|
80
|
+
| `JsonFileStrategy` | Strategy for reading/writing JSON config files |
|
|
81
|
+
| `SchemaConfigStrategy` | Strategy for TypeBox schema validation with default merging |
|
|
82
|
+
| `ConfigDataStrategy` | Interface for data serialization strategies |
|
|
83
|
+
| `ConfigValidationStrategy` | Interface for validation strategies |
|
package/index.mjs
CHANGED
|
@@ -32,6 +32,7 @@ var SchemaValidationError = class extends Error {
|
|
|
32
32
|
errors;
|
|
33
33
|
constructor(errors, value, message) {
|
|
34
34
|
super(message);
|
|
35
|
+
this.name = "SchemaValidationError";
|
|
35
36
|
this.value = value;
|
|
36
37
|
this.errors = Array.isArray(errors) ? errors : errors instanceof ValueErrorIterator ? Array.from(errors) : Array.from(Value.Errors(errors, value));
|
|
37
38
|
}
|