@bemoje/config 1.0.0 → 1.1.0

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 ADDED
@@ -0,0 +1,83 @@
1
+ # @bemoje/config
2
+
3
+ Configuration file management with JSON persistence and TypeBox schema validation.
4
+
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5-blue)](https://www.typescriptlang.org/)
6
+ [![Module](https://img.shields.io/badge/Module-ESM-yellow)](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 |
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ var JsonFileStrategy = require('../strategies/JsonFileStrategy');
4
+ var SchemaConfigStrategy = require('../strategies/SchemaConfigStrategy');
5
+
6
+ var __defProp = Object.defineProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ class ConfigFile {
9
+ static {
10
+ __name(this, "ConfigFile");
11
+ }
12
+ static instances = /* @__PURE__ */ new Map();
13
+ data;
14
+ validation;
15
+ filepath;
16
+ schema;
17
+ constructor(schema, filepath) {
18
+ if (ConfigFile.instances.has(filepath)) {
19
+ return ConfigFile.instances.get(filepath);
20
+ } else {
21
+ ConfigFile.instances.set(filepath, this);
22
+ }
23
+ this.schema = schema;
24
+ this.filepath = filepath;
25
+ this.data = new JsonFileStrategy.JsonFileStrategy(filepath);
26
+ this.validation = new SchemaConfigStrategy.SchemaConfigStrategy(schema);
27
+ }
28
+ load() {
29
+ const config = this.data.load();
30
+ const merged = this.validation.applyDefaults(config);
31
+ this.data.save(merged);
32
+ return merged;
33
+ }
34
+ update(cb) {
35
+ const merged = this.validation.applyDefaults(cb(this.load()));
36
+ this.data.save(merged);
37
+ return merged;
38
+ }
39
+ }
40
+
41
+ exports.ConfigFile = ConfigFile;
@@ -0,0 +1,23 @@
1
+ import { ConfigDataStrategy } from '../interfaces/ConfigDataStrategy.cjs';
2
+ import { ConfigValidationStrategy } from '../interfaces/ConfigValidationStrategy.cjs';
3
+ import { TSchema, Static } from '@sinclair/typebox';
4
+
5
+ /**
6
+ * Configuration file manager that provides schema validation and file I/O using the Strategy pattern.
7
+ */
8
+ declare class ConfigFile<Schema extends TSchema> {
9
+ protected static instances: Map<string, ConfigFile<TSchema>>;
10
+ protected readonly data: ConfigDataStrategy<Static<Schema>>;
11
+ protected readonly validation: ConfigValidationStrategy<Static<Schema>>;
12
+ readonly filepath: string;
13
+ readonly schema: Schema;
14
+ constructor(schema: Schema, filepath: string);
15
+ load(): (Schema & {
16
+ params: [];
17
+ })["static"];
18
+ update(cb: (config: Static<Schema>) => Static<Schema>): (Schema & {
19
+ params: [];
20
+ })["static"];
21
+ }
22
+
23
+ export { ConfigFile };
@@ -0,0 +1,23 @@
1
+ import { ConfigDataStrategy } from '../interfaces/ConfigDataStrategy.js';
2
+ import { ConfigValidationStrategy } from '../interfaces/ConfigValidationStrategy.js';
3
+ import { TSchema, Static } from '@sinclair/typebox';
4
+
5
+ /**
6
+ * Configuration file manager that provides schema validation and file I/O using the Strategy pattern.
7
+ */
8
+ declare class ConfigFile<Schema extends TSchema> {
9
+ protected static instances: Map<string, ConfigFile<TSchema>>;
10
+ protected readonly data: ConfigDataStrategy<Static<Schema>>;
11
+ protected readonly validation: ConfigValidationStrategy<Static<Schema>>;
12
+ readonly filepath: string;
13
+ readonly schema: Schema;
14
+ constructor(schema: Schema, filepath: string);
15
+ load(): (Schema & {
16
+ params: [];
17
+ })["static"];
18
+ update(cb: (config: Static<Schema>) => Static<Schema>): (Schema & {
19
+ params: [];
20
+ })["static"];
21
+ }
22
+
23
+ export { ConfigFile };
@@ -0,0 +1,38 @@
1
+ import { __name } from '../chunk-SHUYVCID.js';
2
+ import { JsonFileStrategy } from '../strategies/JsonFileStrategy';
3
+ import { SchemaConfigStrategy } from '../strategies/SchemaConfigStrategy';
4
+
5
+ class ConfigFile {
6
+ static {
7
+ __name(this, "ConfigFile");
8
+ }
9
+ static instances = /* @__PURE__ */ new Map();
10
+ data;
11
+ validation;
12
+ filepath;
13
+ schema;
14
+ constructor(schema, filepath) {
15
+ if (ConfigFile.instances.has(filepath)) {
16
+ return ConfigFile.instances.get(filepath);
17
+ } else {
18
+ ConfigFile.instances.set(filepath, this);
19
+ }
20
+ this.schema = schema;
21
+ this.filepath = filepath;
22
+ this.data = new JsonFileStrategy(filepath);
23
+ this.validation = new SchemaConfigStrategy(schema);
24
+ }
25
+ load() {
26
+ const config = this.data.load();
27
+ const merged = this.validation.applyDefaults(config);
28
+ this.data.save(merged);
29
+ return merged;
30
+ }
31
+ update(cb) {
32
+ const merged = this.validation.applyDefaults(cb(this.load()));
33
+ this.data.save(merged);
34
+ return merged;
35
+ }
36
+ }
37
+
38
+ export { ConfigFile };
package/lib/index.cjs ADDED
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var ConfigFile = require('./core/ConfigFile');
4
+ var ConfigDataStrategy = require('./interfaces/ConfigDataStrategy');
5
+ var ConfigValidationStrategy = require('./interfaces/ConfigValidationStrategy');
6
+ var JsonFileStrategy = require('./strategies/JsonFileStrategy');
7
+ var SchemaConfigStrategy = require('./strategies/SchemaConfigStrategy');
8
+
9
+
10
+
11
+ Object.keys(ConfigFile).forEach(function (k) {
12
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
13
+ enumerable: true,
14
+ get: function () { return ConfigFile[k]; }
15
+ });
16
+ });
17
+ Object.keys(ConfigDataStrategy).forEach(function (k) {
18
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
19
+ enumerable: true,
20
+ get: function () { return ConfigDataStrategy[k]; }
21
+ });
22
+ });
23
+ Object.keys(ConfigValidationStrategy).forEach(function (k) {
24
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
25
+ enumerable: true,
26
+ get: function () { return ConfigValidationStrategy[k]; }
27
+ });
28
+ });
29
+ Object.keys(JsonFileStrategy).forEach(function (k) {
30
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
31
+ enumerable: true,
32
+ get: function () { return JsonFileStrategy[k]; }
33
+ });
34
+ });
35
+ Object.keys(SchemaConfigStrategy).forEach(function (k) {
36
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
37
+ enumerable: true,
38
+ get: function () { return SchemaConfigStrategy[k]; }
39
+ });
40
+ });
@@ -0,0 +1,6 @@
1
+ export { ConfigFile } from './core/ConfigFile.cjs';
2
+ export { ConfigDataStrategy } from './interfaces/ConfigDataStrategy.cjs';
3
+ export { ConfigValidationStrategy } from './interfaces/ConfigValidationStrategy.cjs';
4
+ export { JsonFileStrategy } from './strategies/JsonFileStrategy.cjs';
5
+ export { SchemaConfigStrategy } from './strategies/SchemaConfigStrategy.cjs';
6
+ import '@sinclair/typebox';
package/lib/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export { ConfigFile } from './core/ConfigFile.js';
2
+ export { ConfigDataStrategy } from './interfaces/ConfigDataStrategy.js';
3
+ export { ConfigValidationStrategy } from './interfaces/ConfigValidationStrategy.js';
4
+ export { JsonFileStrategy } from './strategies/JsonFileStrategy.js';
5
+ export { SchemaConfigStrategy } from './strategies/SchemaConfigStrategy.js';
6
+ import '@sinclair/typebox';
package/lib/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './core/ConfigFile';
2
+ export * from './interfaces/ConfigDataStrategy';
3
+ export * from './interfaces/ConfigValidationStrategy';
4
+ export * from './strategies/JsonFileStrategy';
5
+ export * from './strategies/SchemaConfigStrategy';
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,6 @@
1
+ interface ConfigDataStrategy<T> {
2
+ load(): T | undefined;
3
+ save(content: T): void;
4
+ }
5
+
6
+ export type { ConfigDataStrategy };
@@ -0,0 +1,6 @@
1
+ interface ConfigDataStrategy<T> {
2
+ load(): T | undefined;
3
+ save(content: T): void;
4
+ }
5
+
6
+ export type { ConfigDataStrategy };
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,7 @@
1
+ interface ConfigValidationStrategy<T> {
2
+ isValid(content: unknown): content is T;
3
+ assertValid(content: unknown): asserts content is T;
4
+ applyDefaults(content?: Partial<T>): T;
5
+ }
6
+
7
+ export type { ConfigValidationStrategy };
@@ -0,0 +1,7 @@
1
+ interface ConfigValidationStrategy<T> {
2
+ isValid(content: unknown): content is T;
3
+ assertValid(content: unknown): asserts content is T;
4
+ applyDefaults(content?: Partial<T>): T;
5
+ }
6
+
7
+ export type { ConfigValidationStrategy };
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs-extra');
4
+
5
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
+
7
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
8
+
9
+ var __defProp = Object.defineProperty;
10
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
11
+ class JsonFileStrategy {
12
+ static {
13
+ __name(this, "JsonFileStrategy");
14
+ }
15
+ filepath;
16
+ constructor(filepath) {
17
+ this.filepath = filepath;
18
+ }
19
+ load() {
20
+ try {
21
+ return fs__default.default.readJsonSync(this.filepath);
22
+ } catch (_) {
23
+ return void 0;
24
+ }
25
+ }
26
+ save(config) {
27
+ fs__default.default.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
28
+ `);
29
+ }
30
+ }
31
+
32
+ exports.JsonFileStrategy = JsonFileStrategy;
@@ -0,0 +1,13 @@
1
+ import { ConfigDataStrategy } from '../interfaces/ConfigDataStrategy.cjs';
2
+
3
+ /**
4
+ * Strategy for loading and saving configuration data as JSON files.
5
+ */
6
+ declare class JsonFileStrategy<T> implements ConfigDataStrategy<T> {
7
+ readonly filepath: string;
8
+ constructor(filepath: string);
9
+ load(): T | undefined;
10
+ save(config: T): void;
11
+ }
12
+
13
+ export { JsonFileStrategy };
@@ -0,0 +1,13 @@
1
+ import { ConfigDataStrategy } from '../interfaces/ConfigDataStrategy.js';
2
+
3
+ /**
4
+ * Strategy for loading and saving configuration data as JSON files.
5
+ */
6
+ declare class JsonFileStrategy<T> implements ConfigDataStrategy<T> {
7
+ readonly filepath: string;
8
+ constructor(filepath: string);
9
+ load(): T | undefined;
10
+ save(config: T): void;
11
+ }
12
+
13
+ export { JsonFileStrategy };
@@ -0,0 +1,25 @@
1
+ import { __name } from '../chunk-SHUYVCID.js';
2
+ import fs from 'fs-extra';
3
+
4
+ class JsonFileStrategy {
5
+ static {
6
+ __name(this, "JsonFileStrategy");
7
+ }
8
+ filepath;
9
+ constructor(filepath) {
10
+ this.filepath = filepath;
11
+ }
12
+ load() {
13
+ try {
14
+ return fs.readJsonSync(this.filepath);
15
+ } catch (_) {
16
+ return void 0;
17
+ }
18
+ }
19
+ save(config) {
20
+ fs.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
21
+ `);
22
+ }
23
+ }
24
+
25
+ export { JsonFileStrategy };
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ var value = require('@sinclair/typebox/value');
4
+ var tschema = require('@mono/tschema');
5
+
6
+ var __defProp = Object.defineProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ class SchemaConfigStrategy {
9
+ static {
10
+ __name(this, "SchemaConfigStrategy");
11
+ }
12
+ schema;
13
+ constructor(schema) {
14
+ this.schema = schema;
15
+ }
16
+ isValid(config) {
17
+ return value.Value.Check(this.schema, config);
18
+ }
19
+ assertValid(config) {
20
+ tschema.assertValidSchema(this.schema, config, "Invalid config.");
21
+ }
22
+ applyDefaults(config = {}) {
23
+ const merged = value.Value.Default(this.schema, config);
24
+ this.assertValid(merged);
25
+ return merged;
26
+ }
27
+ }
28
+
29
+ exports.SchemaConfigStrategy = SchemaConfigStrategy;
@@ -0,0 +1,15 @@
1
+ import { ConfigValidationStrategy } from '../interfaces/ConfigValidationStrategy.cjs';
2
+ import { TSchema, Static } from '@sinclair/typebox';
3
+
4
+ /**
5
+ * Strategy for validating configuration data against a TypeBox schema and applying default values.
6
+ */
7
+ declare class SchemaConfigStrategy<Schema extends TSchema> implements ConfigValidationStrategy<Static<Schema>> {
8
+ readonly schema: Schema;
9
+ constructor(schema: Schema);
10
+ isValid(config: unknown): config is Static<Schema>;
11
+ assertValid(config: unknown): asserts config is Static<Schema>;
12
+ applyDefaults(config?: Partial<Static<Schema>>): Static<Schema>;
13
+ }
14
+
15
+ export { SchemaConfigStrategy };
@@ -0,0 +1,15 @@
1
+ import { ConfigValidationStrategy } from '../interfaces/ConfigValidationStrategy.js';
2
+ import { TSchema, Static } from '@sinclair/typebox';
3
+
4
+ /**
5
+ * Strategy for validating configuration data against a TypeBox schema and applying default values.
6
+ */
7
+ declare class SchemaConfigStrategy<Schema extends TSchema> implements ConfigValidationStrategy<Static<Schema>> {
8
+ readonly schema: Schema;
9
+ constructor(schema: Schema);
10
+ isValid(config: unknown): config is Static<Schema>;
11
+ assertValid(config: unknown): asserts config is Static<Schema>;
12
+ applyDefaults(config?: Partial<Static<Schema>>): Static<Schema>;
13
+ }
14
+
15
+ export { SchemaConfigStrategy };
@@ -0,0 +1,26 @@
1
+ import { __name } from '../chunk-SHUYVCID.js';
2
+ import { Value } from '@sinclair/typebox/value';
3
+ import { assertValidSchema } from '@mono/tschema';
4
+
5
+ class SchemaConfigStrategy {
6
+ static {
7
+ __name(this, "SchemaConfigStrategy");
8
+ }
9
+ schema;
10
+ constructor(schema) {
11
+ this.schema = schema;
12
+ }
13
+ isValid(config) {
14
+ return Value.Check(this.schema, config);
15
+ }
16
+ assertValid(config) {
17
+ assertValidSchema(this.schema, config, "Invalid config.");
18
+ }
19
+ applyDefaults(config = {}) {
20
+ const merged = Value.Default(this.schema, config);
21
+ this.assertValid(merged);
22
+ return merged;
23
+ }
24
+ }
25
+
26
+ export { SchemaConfigStrategy };
package/package.json CHANGED
@@ -1,36 +1,128 @@
1
1
  {
2
2
  "name": "@bemoje/config",
3
- "version": "1.0.0",
4
- "description": "config",
5
- "type": "module",
6
- "sideEffects": false,
3
+ "version": "1.1.0",
4
+ "description": "Configuration file management with JSON persistence and TypeBox schema validation.",
7
5
  "keywords": [
8
- "config"
6
+ "config",
7
+ "configuration",
8
+ "json",
9
+ "persistence",
10
+ "typebox",
11
+ "schema",
12
+ "validation",
13
+ "utilities",
14
+ "typescript"
9
15
  ],
10
- "exports": {
11
- ".": {
12
- "types": "./index.d.ts",
13
- "import": "./index.mjs",
14
- "default": "./index.mjs"
15
- }
16
- },
17
16
  "dependencies": {
18
- "@sinclair/typebox": "^0.34.37",
19
- "fs-extra": "^11.3.0",
17
+ "@sinclair/typebox": "^0.34.48",
18
+ "fs-extra": "^11.3.4",
20
19
  "@types/fs-extra": "^11.0.4"
21
20
  },
21
+ "type": "module",
22
+ "sideEffects": false,
22
23
  "publishConfig": {
23
24
  "access": "public"
24
25
  },
25
- "license": "MIT",
26
- "author": {
27
- "name": "Benjamin Møller Jensen",
28
- "email": "bemoje@bemoje.net",
29
- "url": "https://github.com/bemoje/"
26
+ "main": "./lib/index.cjs",
27
+ "module": "./lib/index.js",
28
+ "types": "./lib/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "import": {
32
+ "types": "./lib/index.d.ts",
33
+ "default": "./lib/index.js"
34
+ },
35
+ "require": {
36
+ "types": "./lib/index.d.cts",
37
+ "default": "./lib/index.cjs"
38
+ }
39
+ },
40
+ "./core/ConfigFile": {
41
+ "import": {
42
+ "types": "./lib/core/ConfigFile.d.ts",
43
+ "default": "./lib/core/ConfigFile.js"
44
+ },
45
+ "require": {
46
+ "types": "./lib/core/ConfigFile.d.cts",
47
+ "default": "./lib/core/ConfigFile.cjs"
48
+ }
49
+ },
50
+ "./interfaces/ConfigDataStrategy": {
51
+ "import": {
52
+ "types": "./lib/interfaces/ConfigDataStrategy.d.ts",
53
+ "default": "./lib/interfaces/ConfigDataStrategy.js"
54
+ },
55
+ "require": {
56
+ "types": "./lib/interfaces/ConfigDataStrategy.d.cts",
57
+ "default": "./lib/interfaces/ConfigDataStrategy.cjs"
58
+ }
59
+ },
60
+ "./interfaces/ConfigValidationStrategy": {
61
+ "import": {
62
+ "types": "./lib/interfaces/ConfigValidationStrategy.d.ts",
63
+ "default": "./lib/interfaces/ConfigValidationStrategy.js"
64
+ },
65
+ "require": {
66
+ "types": "./lib/interfaces/ConfigValidationStrategy.d.cts",
67
+ "default": "./lib/interfaces/ConfigValidationStrategy.cjs"
68
+ }
69
+ },
70
+ "./strategies/JsonFileStrategy": {
71
+ "import": {
72
+ "types": "./lib/strategies/JsonFileStrategy.d.ts",
73
+ "default": "./lib/strategies/JsonFileStrategy.js"
74
+ },
75
+ "require": {
76
+ "types": "./lib/strategies/JsonFileStrategy.d.cts",
77
+ "default": "./lib/strategies/JsonFileStrategy.cjs"
78
+ }
79
+ },
80
+ "./strategies/SchemaConfigStrategy": {
81
+ "import": {
82
+ "types": "./lib/strategies/SchemaConfigStrategy.d.ts",
83
+ "default": "./lib/strategies/SchemaConfigStrategy.js"
84
+ },
85
+ "require": {
86
+ "types": "./lib/strategies/SchemaConfigStrategy.d.cts",
87
+ "default": "./lib/strategies/SchemaConfigStrategy.cjs"
88
+ }
89
+ }
30
90
  },
91
+ "files": [
92
+ "./lib/index.js",
93
+ "./lib/index.d.ts",
94
+ "./lib/index.cjs",
95
+ "./lib/index.d.cts",
96
+ "./lib/core/ConfigFile.js",
97
+ "./lib/core/ConfigFile.d.ts",
98
+ "./lib/core/ConfigFile.cjs",
99
+ "./lib/core/ConfigFile.d.cts",
100
+ "./lib/interfaces/ConfigDataStrategy.js",
101
+ "./lib/interfaces/ConfigDataStrategy.d.ts",
102
+ "./lib/interfaces/ConfigDataStrategy.cjs",
103
+ "./lib/interfaces/ConfigDataStrategy.d.cts",
104
+ "./lib/interfaces/ConfigValidationStrategy.js",
105
+ "./lib/interfaces/ConfigValidationStrategy.d.ts",
106
+ "./lib/interfaces/ConfigValidationStrategy.cjs",
107
+ "./lib/interfaces/ConfigValidationStrategy.d.cts",
108
+ "./lib/strategies/JsonFileStrategy.js",
109
+ "./lib/strategies/JsonFileStrategy.d.ts",
110
+ "./lib/strategies/JsonFileStrategy.cjs",
111
+ "./lib/strategies/JsonFileStrategy.d.cts",
112
+ "./lib/strategies/SchemaConfigStrategy.js",
113
+ "./lib/strategies/SchemaConfigStrategy.d.ts",
114
+ "./lib/strategies/SchemaConfigStrategy.cjs",
115
+ "./lib/strategies/SchemaConfigStrategy.d.cts"
116
+ ],
31
117
  "repository": {
32
118
  "type": "git",
33
119
  "url": "git+https://github.com/bemoje/mono.git",
34
120
  "directory": "libs/config"
35
- }
121
+ },
122
+ "author": {
123
+ "name": "Benjamin Møller Jensen",
124
+ "email": "bemoje@bemoje.net",
125
+ "url": "https://github.com/bemoje/"
126
+ },
127
+ "license": "MIT"
36
128
  }
package/index.d.ts DELETED
@@ -1,53 +0,0 @@
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 DELETED
@@ -1,103 +0,0 @@
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 };