@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.
@@ -1,11 +1,80 @@
1
1
  'use strict';
2
2
 
3
- var JsonFileStrategy = require('../strategies/JsonFileStrategy');
4
- 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');
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 ConfigFile {
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 (ConfigFile.instances.has(filepath)) {
19
- return ConfigFile.instances.get(filepath);
87
+ if (_ConfigFile.instances.has(filepath)) {
88
+ return _ConfigFile.instances.get(filepath);
20
89
  } else {
21
- ConfigFile.instances.set(filepath, this);
90
+ _ConfigFile.instances.set(filepath, this);
22
91
  }
23
92
  this.schema = schema;
24
93
  this.filepath = filepath;
25
- this.data = new JsonFileStrategy.JsonFileStrategy(filepath);
26
- this.validation = new SchemaConfigStrategy.SchemaConfigStrategy(schema);
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;
@@ -1,8 +1,74 @@
1
- import { __name } from '../chunk-SHUYVCID.js';
2
- import { JsonFileStrategy } from '../strategies/JsonFileStrategy';
3
- import { SchemaConfigStrategy } from '../strategies/SchemaConfigStrategy';
1
+ import fs from 'fs-extra';
2
+ import { Value } from '@sinclair/typebox/value';
3
+ import { ValueErrorIterator } from '@sinclair/typebox/compiler';
4
4
 
5
- class ConfigFile {
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 (ConfigFile.instances.has(filepath)) {
16
- return ConfigFile.instances.get(filepath);
81
+ if (_ConfigFile.instances.has(filepath)) {
82
+ return _ConfigFile.instances.get(filepath);
17
83
  } else {
18
- ConfigFile.instances.set(filepath, this);
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 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');
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.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
- });
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
- export * from './core/ConfigFile';
2
- export * from './interfaces/ConfigDataStrategy';
3
- export * from './interfaces/ConfigValidationStrategy';
4
- export * from './strategies/JsonFileStrategy';
5
- export * from './strategies/SchemaConfigStrategy';
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
- class JsonFileStrategy {
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
- class JsonFileStrategy {
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 tschema = require('@mono/tschema');
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 SchemaConfigStrategy {
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
- tschema.assertValidSchema(this.schema, config, "Invalid config.");
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 { assertValidSchema } from '@mono/tschema';
2
+ import { ValueErrorIterator } from '@sinclair/typebox/compiler';
4
3
 
5
- class SchemaConfigStrategy {
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bemoje/config",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Configuration file management with JSON persistence and TypeBox schema validation.",
5
5
  "keywords": [
6
6
  "config",