@bemoje/config 1.0.1 → 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.
@@ -0,0 +1,110 @@
1
+ 'use strict';
2
+
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);
10
+
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;
@@ -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,104 @@
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 };
package/lib/index.cjs ADDED
@@ -0,0 +1,112 @@
1
+ 'use strict';
2
+
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);
10
+
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;
@@ -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';
@@ -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
+ var JsonFileStrategy = class {
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,26 @@
1
+ import fs from 'fs-extra';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+ var JsonFileStrategy = class {
6
+ static {
7
+ __name(this, "JsonFileStrategy");
8
+ }
9
+ filepath;
10
+ constructor(filepath) {
11
+ this.filepath = filepath;
12
+ }
13
+ load() {
14
+ try {
15
+ return fs.readJsonSync(this.filepath);
16
+ } catch (_) {
17
+ return void 0;
18
+ }
19
+ }
20
+ save(config) {
21
+ fs.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
22
+ `);
23
+ }
24
+ };
25
+
26
+ export { JsonFileStrategy };
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+
3
+ var value = require('@sinclair/typebox/value');
4
+ var compiler = require('@sinclair/typebox/compiler');
5
+
6
+ var __defProp = Object.defineProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
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 {
31
+ static {
32
+ __name(this, "SchemaConfigStrategy");
33
+ }
34
+ schema;
35
+ constructor(schema) {
36
+ this.schema = schema;
37
+ }
38
+ isValid(config) {
39
+ return value.Value.Check(this.schema, config);
40
+ }
41
+ assertValid(config) {
42
+ assertValidSchema(this.schema, config, "Invalid config.");
43
+ }
44
+ applyDefaults(config = {}) {
45
+ const merged = value.Value.Default(this.schema, config);
46
+ this.assertValid(merged);
47
+ return merged;
48
+ }
49
+ };
50
+
51
+ 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,49 @@
1
+ import { Value } from '@sinclair/typebox/value';
2
+ import { ValueErrorIterator } from '@sinclair/typebox/compiler';
3
+
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 {
29
+ static {
30
+ __name(this, "SchemaConfigStrategy");
31
+ }
32
+ schema;
33
+ constructor(schema) {
34
+ this.schema = schema;
35
+ }
36
+ isValid(config) {
37
+ return Value.Check(this.schema, config);
38
+ }
39
+ assertValid(config) {
40
+ assertValidSchema(this.schema, config, "Invalid config.");
41
+ }
42
+ applyDefaults(config = {}) {
43
+ const merged = Value.Default(this.schema, config);
44
+ this.assertValid(merged);
45
+ return merged;
46
+ }
47
+ };
48
+
49
+ export { SchemaConfigStrategy };
package/package.json CHANGED
@@ -1,36 +1,128 @@
1
1
  {
2
2
  "name": "@bemoje/config",
3
- "version": "1.0.1",
4
- "description": "config",
5
- "type": "module",
6
- "sideEffects": false,
3
+ "version": "1.1.2",
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 };
File without changes