@bemoje/template 1.1.0 → 1.1.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.
@@ -2,11 +2,33 @@
2
2
 
3
3
  var typebox = require('@sinclair/typebox');
4
4
  var value = require('@sinclair/typebox/value');
5
- var tschema = require('@mono/tschema');
5
+ var compiler = require('@sinclair/typebox/compiler');
6
6
 
7
7
  var __defProp = Object.defineProperty;
8
8
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
- class Template {
9
+ var SchemaValidationError = class extends Error {
10
+ static {
11
+ __name(this, "SchemaValidationError");
12
+ }
13
+ value;
14
+ errors;
15
+ constructor(errors, value$1, message) {
16
+ super(message);
17
+ this.name = "SchemaValidationError";
18
+ this.value = value$1;
19
+ this.errors = Array.isArray(errors) ? errors : errors instanceof compiler.ValueErrorIterator ? Array.from(errors) : Array.from(value.Value.Errors(errors, value$1));
20
+ }
21
+ };
22
+ function assertValidSchema(schema, data, message) {
23
+ const isValid = value.Value.Check(schema, data);
24
+ if (!isValid) {
25
+ throw new SchemaValidationError(value.Value.Errors(schema, data), data, message);
26
+ }
27
+ }
28
+ __name(assertValidSchema, "assertValidSchema");
29
+
30
+ // src/Template/Template.ts
31
+ var Template = class {
10
32
  static {
11
33
  __name(this, "Template");
12
34
  }
@@ -40,7 +62,7 @@ class Template {
40
62
  render(data = {}) {
41
63
  const stringTemplate = this.strategy.templateToString(this.template);
42
64
  const merged = value.Value.Cast(this.optionsSchema, data);
43
- tschema.assertValidSchema(this.optionsSchema, merged, "Invalid options");
65
+ assertValidSchema(this.optionsSchema, merged, "Invalid options");
44
66
  const populated = stringTemplate.replaceAll(/{{(\w+)}}/g, (_, key) => {
45
67
  return String(merged[key]);
46
68
  });
@@ -67,6 +89,6 @@ class Template {
67
89
  }
68
90
  }
69
91
  }
70
- }
92
+ };
71
93
 
72
94
  exports.Template = Template;
@@ -1,9 +1,32 @@
1
- import { __name } from '../chunk-SHUYVCID.js';
2
1
  import { Type } from '@sinclair/typebox';
3
2
  import { Value } from '@sinclair/typebox/value';
4
- import { assertValidSchema } from '@mono/tschema';
3
+ import { ValueErrorIterator } from '@sinclair/typebox/compiler';
5
4
 
6
- class Template {
5
+ var __defProp = Object.defineProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var SchemaValidationError = class extends Error {
8
+ static {
9
+ __name(this, "SchemaValidationError");
10
+ }
11
+ value;
12
+ errors;
13
+ constructor(errors, value, message) {
14
+ super(message);
15
+ this.name = "SchemaValidationError";
16
+ this.value = value;
17
+ this.errors = Array.isArray(errors) ? errors : errors instanceof ValueErrorIterator ? Array.from(errors) : Array.from(Value.Errors(errors, value));
18
+ }
19
+ };
20
+ function assertValidSchema(schema, data, message) {
21
+ const isValid = Value.Check(schema, data);
22
+ if (!isValid) {
23
+ throw new SchemaValidationError(Value.Errors(schema, data), data, message);
24
+ }
25
+ }
26
+ __name(assertValidSchema, "assertValidSchema");
27
+
28
+ // src/Template/Template.ts
29
+ var Template = class {
7
30
  static {
8
31
  __name(this, "Template");
9
32
  }
@@ -64,6 +87,6 @@ class Template {
64
87
  }
65
88
  }
66
89
  }
67
- }
90
+ };
68
91
 
69
92
  export { Template };
package/lib/index.cjs CHANGED
@@ -1,40 +1,160 @@
1
1
  'use strict';
2
2
 
3
- var Template = require('./Template/Template');
4
- var TemplateStrategy = require('./interfaces/TemplateStrategy');
5
- var JsonFileTemplateStrategy = require('./strategies/JsonFileTemplateStrategy');
6
- var StringTemplateStrategy = require('./strategies/StringTemplateStrategy');
7
- var TextFileTemplateStrategy = require('./strategies/TextFileTemplateStrategy');
3
+ var typebox = require('@sinclair/typebox');
4
+ var value = require('@sinclair/typebox/value');
5
+ var compiler = require('@sinclair/typebox/compiler');
8
6
 
7
+ var __defProp = Object.defineProperty;
8
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
+ var SchemaValidationError = class extends Error {
10
+ static {
11
+ __name(this, "SchemaValidationError");
12
+ }
13
+ value;
14
+ errors;
15
+ constructor(errors, value$1, message) {
16
+ super(message);
17
+ this.name = "SchemaValidationError";
18
+ this.value = value$1;
19
+ this.errors = Array.isArray(errors) ? errors : errors instanceof compiler.ValueErrorIterator ? Array.from(errors) : Array.from(value.Value.Errors(errors, value$1));
20
+ }
21
+ };
22
+ function assertValidSchema(schema, data, message) {
23
+ const isValid = value.Value.Check(schema, data);
24
+ if (!isValid) {
25
+ throw new SchemaValidationError(value.Value.Errors(schema, data), data, message);
26
+ }
27
+ }
28
+ __name(assertValidSchema, "assertValidSchema");
9
29
 
30
+ // src/Template/Template.ts
31
+ var Template = class {
32
+ static {
33
+ __name(this, "Template");
34
+ }
35
+ strategy;
36
+ optionsSchema;
37
+ template;
38
+ constructor(options) {
39
+ this.strategy = options.strategy;
40
+ this.optionsSchema = options.optionsSchema ?? typebox.Type.Object({});
41
+ this.template = options.template;
42
+ this.strategy.templateSchema.default = this.template;
43
+ this.assertValidTemplate();
44
+ }
45
+ /**
46
+ * Returns the template schema with the template set as the default value.
47
+ */
48
+ createSchema() {
49
+ return this.strategy.templateSchema;
50
+ }
51
+ /**
52
+ * Renders the template with the provided data, returning the result in the template schema format.
53
+ *
54
+ * Variables in the template using `{{variableName}}` syntax are replaced with corresponding
55
+ * values from the data object. Data is validated against the options schema before rendering.
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * const result = template.render({ name: 'John', age: 30 })
60
+ * ```
61
+ */
62
+ render(data = {}) {
63
+ const stringTemplate = this.strategy.templateToString(this.template);
64
+ const merged = value.Value.Cast(this.optionsSchema, data);
65
+ assertValidSchema(this.optionsSchema, merged, "Invalid options");
66
+ const populated = stringTemplate.replaceAll(/{{(\w+)}}/g, (_, key) => {
67
+ return String(merged[key]);
68
+ });
69
+ return this.strategy.render(populated);
70
+ }
71
+ /**
72
+ * Renders the template and returns the result as a string representation.
73
+ * Convenience method that combines `render()` and `templateToString()`.
74
+ */
75
+ renderString(data = {}) {
76
+ return this.strategy.templateToString(this.render(data));
77
+ }
78
+ /**
79
+ * Validates that the template contains all variables defined in the options schema.
80
+ *
81
+ * @throws {Error} When a variable defined in the options schema is not found in the template
82
+ */
83
+ assertValidTemplate() {
84
+ const string = this.strategy.templateToString(this.template);
85
+ const variables = Object.keys(this.optionsSchema.properties);
86
+ for (const variable of variables) {
87
+ if (!string.includes(`{{${variable}}}`)) {
88
+ throw new Error(`Template does not include variable: ${variable}`);
89
+ }
90
+ }
91
+ }
92
+ };
93
+ var JsonFileTemplateStrategy = class {
94
+ static {
95
+ __name(this, "JsonFileTemplateStrategy");
96
+ }
97
+ templateSchema;
98
+ /**
99
+ * Creates a JSON file template strategy with optional schema validation.
100
+ *
101
+ * @param templateSchema - TypeBox object schema for template validation.
102
+ * Defaults to empty object schema if not provided.
103
+ */
104
+ constructor(templateSchema) {
105
+ this.templateSchema = templateSchema ?? typebox.Type.Object({});
106
+ }
107
+ /**
108
+ * Converts a template object to pretty-printed JSON string.
109
+ */
110
+ templateToString(template) {
111
+ return JSON.stringify(template, null, 2);
112
+ }
113
+ /**
114
+ * Parses JSON string back to typed object conforming to schema.
115
+ */
116
+ render(populated) {
117
+ return JSON.parse(populated);
118
+ }
119
+ };
120
+ var StringTemplateStrategy = class {
121
+ static {
122
+ __name(this, "StringTemplateStrategy");
123
+ }
124
+ templateSchema = typebox.Type.String();
125
+ /**
126
+ * Returns the template string as-is (pass-through).
127
+ */
128
+ templateToString(template) {
129
+ return template;
130
+ }
131
+ /**
132
+ * Returns the populated string as-is (pass-through).
133
+ */
134
+ render(populated) {
135
+ return populated;
136
+ }
137
+ };
138
+ var TextFileTemplateStrategy = class {
139
+ static {
140
+ __name(this, "TextFileTemplateStrategy");
141
+ }
142
+ templateSchema = typebox.Type.Array(typebox.Type.String());
143
+ /**
144
+ * Joins string array into newline-separated text.
145
+ */
146
+ templateToString(template) {
147
+ return template.join("\n");
148
+ }
149
+ /**
150
+ * Splits text content into string array by newlines.
151
+ */
152
+ render(populated) {
153
+ return populated.split("\n");
154
+ }
155
+ };
10
156
 
11
- Object.keys(Template).forEach(function (k) {
12
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
13
- enumerable: true,
14
- get: function () { return Template[k]; }
15
- });
16
- });
17
- Object.keys(TemplateStrategy).forEach(function (k) {
18
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
19
- enumerable: true,
20
- get: function () { return TemplateStrategy[k]; }
21
- });
22
- });
23
- Object.keys(JsonFileTemplateStrategy).forEach(function (k) {
24
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
25
- enumerable: true,
26
- get: function () { return JsonFileTemplateStrategy[k]; }
27
- });
28
- });
29
- Object.keys(StringTemplateStrategy).forEach(function (k) {
30
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
31
- enumerable: true,
32
- get: function () { return StringTemplateStrategy[k]; }
33
- });
34
- });
35
- Object.keys(TextFileTemplateStrategy).forEach(function (k) {
36
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
37
- enumerable: true,
38
- get: function () { return TextFileTemplateStrategy[k]; }
39
- });
40
- });
157
+ exports.JsonFileTemplateStrategy = JsonFileTemplateStrategy;
158
+ exports.StringTemplateStrategy = StringTemplateStrategy;
159
+ exports.Template = Template;
160
+ exports.TextFileTemplateStrategy = TextFileTemplateStrategy;
package/lib/index.js CHANGED
@@ -1,5 +1,155 @@
1
- export * from './Template/Template';
2
- export * from './interfaces/TemplateStrategy';
3
- export * from './strategies/JsonFileTemplateStrategy';
4
- export * from './strategies/StringTemplateStrategy';
5
- export * from './strategies/TextFileTemplateStrategy';
1
+ import { Type } from '@sinclair/typebox';
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 SchemaValidationError = class extends Error {
8
+ static {
9
+ __name(this, "SchemaValidationError");
10
+ }
11
+ value;
12
+ errors;
13
+ constructor(errors, value, message) {
14
+ super(message);
15
+ this.name = "SchemaValidationError";
16
+ this.value = value;
17
+ this.errors = Array.isArray(errors) ? errors : errors instanceof ValueErrorIterator ? Array.from(errors) : Array.from(Value.Errors(errors, value));
18
+ }
19
+ };
20
+ function assertValidSchema(schema, data, message) {
21
+ const isValid = Value.Check(schema, data);
22
+ if (!isValid) {
23
+ throw new SchemaValidationError(Value.Errors(schema, data), data, message);
24
+ }
25
+ }
26
+ __name(assertValidSchema, "assertValidSchema");
27
+
28
+ // src/Template/Template.ts
29
+ var Template = class {
30
+ static {
31
+ __name(this, "Template");
32
+ }
33
+ strategy;
34
+ optionsSchema;
35
+ template;
36
+ constructor(options) {
37
+ this.strategy = options.strategy;
38
+ this.optionsSchema = options.optionsSchema ?? Type.Object({});
39
+ this.template = options.template;
40
+ this.strategy.templateSchema.default = this.template;
41
+ this.assertValidTemplate();
42
+ }
43
+ /**
44
+ * Returns the template schema with the template set as the default value.
45
+ */
46
+ createSchema() {
47
+ return this.strategy.templateSchema;
48
+ }
49
+ /**
50
+ * Renders the template with the provided data, returning the result in the template schema format.
51
+ *
52
+ * Variables in the template using `{{variableName}}` syntax are replaced with corresponding
53
+ * values from the data object. Data is validated against the options schema before rendering.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * const result = template.render({ name: 'John', age: 30 })
58
+ * ```
59
+ */
60
+ render(data = {}) {
61
+ const stringTemplate = this.strategy.templateToString(this.template);
62
+ const merged = Value.Cast(this.optionsSchema, data);
63
+ assertValidSchema(this.optionsSchema, merged, "Invalid options");
64
+ const populated = stringTemplate.replaceAll(/{{(\w+)}}/g, (_, key) => {
65
+ return String(merged[key]);
66
+ });
67
+ return this.strategy.render(populated);
68
+ }
69
+ /**
70
+ * Renders the template and returns the result as a string representation.
71
+ * Convenience method that combines `render()` and `templateToString()`.
72
+ */
73
+ renderString(data = {}) {
74
+ return this.strategy.templateToString(this.render(data));
75
+ }
76
+ /**
77
+ * Validates that the template contains all variables defined in the options schema.
78
+ *
79
+ * @throws {Error} When a variable defined in the options schema is not found in the template
80
+ */
81
+ assertValidTemplate() {
82
+ const string = this.strategy.templateToString(this.template);
83
+ const variables = Object.keys(this.optionsSchema.properties);
84
+ for (const variable of variables) {
85
+ if (!string.includes(`{{${variable}}}`)) {
86
+ throw new Error(`Template does not include variable: ${variable}`);
87
+ }
88
+ }
89
+ }
90
+ };
91
+ var JsonFileTemplateStrategy = class {
92
+ static {
93
+ __name(this, "JsonFileTemplateStrategy");
94
+ }
95
+ templateSchema;
96
+ /**
97
+ * Creates a JSON file template strategy with optional schema validation.
98
+ *
99
+ * @param templateSchema - TypeBox object schema for template validation.
100
+ * Defaults to empty object schema if not provided.
101
+ */
102
+ constructor(templateSchema) {
103
+ this.templateSchema = templateSchema ?? Type.Object({});
104
+ }
105
+ /**
106
+ * Converts a template object to pretty-printed JSON string.
107
+ */
108
+ templateToString(template) {
109
+ return JSON.stringify(template, null, 2);
110
+ }
111
+ /**
112
+ * Parses JSON string back to typed object conforming to schema.
113
+ */
114
+ render(populated) {
115
+ return JSON.parse(populated);
116
+ }
117
+ };
118
+ var StringTemplateStrategy = class {
119
+ static {
120
+ __name(this, "StringTemplateStrategy");
121
+ }
122
+ templateSchema = Type.String();
123
+ /**
124
+ * Returns the template string as-is (pass-through).
125
+ */
126
+ templateToString(template) {
127
+ return template;
128
+ }
129
+ /**
130
+ * Returns the populated string as-is (pass-through).
131
+ */
132
+ render(populated) {
133
+ return populated;
134
+ }
135
+ };
136
+ var TextFileTemplateStrategy = class {
137
+ static {
138
+ __name(this, "TextFileTemplateStrategy");
139
+ }
140
+ templateSchema = Type.Array(Type.String());
141
+ /**
142
+ * Joins string array into newline-separated text.
143
+ */
144
+ templateToString(template) {
145
+ return template.join("\n");
146
+ }
147
+ /**
148
+ * Splits text content into string array by newlines.
149
+ */
150
+ render(populated) {
151
+ return populated.split("\n");
152
+ }
153
+ };
154
+
155
+ export { JsonFileTemplateStrategy, StringTemplateStrategy, Template, TextFileTemplateStrategy };
@@ -4,7 +4,7 @@ var typebox = require('@sinclair/typebox');
4
4
 
5
5
  var __defProp = Object.defineProperty;
6
6
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- class JsonFileTemplateStrategy {
7
+ var JsonFileTemplateStrategy = class {
8
8
  static {
9
9
  __name(this, "JsonFileTemplateStrategy");
10
10
  }
@@ -30,6 +30,6 @@ class JsonFileTemplateStrategy {
30
30
  render(populated) {
31
31
  return JSON.parse(populated);
32
32
  }
33
- }
33
+ };
34
34
 
35
35
  exports.JsonFileTemplateStrategy = JsonFileTemplateStrategy;
@@ -1,7 +1,8 @@
1
- import { __name } from '../chunk-SHUYVCID.js';
2
1
  import { Type } from '@sinclair/typebox';
3
2
 
4
- class JsonFileTemplateStrategy {
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+ var JsonFileTemplateStrategy = class {
5
6
  static {
6
7
  __name(this, "JsonFileTemplateStrategy");
7
8
  }
@@ -27,6 +28,6 @@ class JsonFileTemplateStrategy {
27
28
  render(populated) {
28
29
  return JSON.parse(populated);
29
30
  }
30
- }
31
+ };
31
32
 
32
33
  export { JsonFileTemplateStrategy };
@@ -4,7 +4,7 @@ var typebox = require('@sinclair/typebox');
4
4
 
5
5
  var __defProp = Object.defineProperty;
6
6
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- class StringTemplateStrategy {
7
+ var StringTemplateStrategy = class {
8
8
  static {
9
9
  __name(this, "StringTemplateStrategy");
10
10
  }
@@ -21,6 +21,6 @@ class StringTemplateStrategy {
21
21
  render(populated) {
22
22
  return populated;
23
23
  }
24
- }
24
+ };
25
25
 
26
26
  exports.StringTemplateStrategy = StringTemplateStrategy;
@@ -1,7 +1,8 @@
1
- import { __name } from '../chunk-SHUYVCID.js';
2
1
  import { Type } from '@sinclair/typebox';
3
2
 
4
- class StringTemplateStrategy {
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+ var StringTemplateStrategy = class {
5
6
  static {
6
7
  __name(this, "StringTemplateStrategy");
7
8
  }
@@ -18,6 +19,6 @@ class StringTemplateStrategy {
18
19
  render(populated) {
19
20
  return populated;
20
21
  }
21
- }
22
+ };
22
23
 
23
24
  export { StringTemplateStrategy };
@@ -4,7 +4,7 @@ var typebox = require('@sinclair/typebox');
4
4
 
5
5
  var __defProp = Object.defineProperty;
6
6
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- class TextFileTemplateStrategy {
7
+ var TextFileTemplateStrategy = class {
8
8
  static {
9
9
  __name(this, "TextFileTemplateStrategy");
10
10
  }
@@ -21,6 +21,6 @@ class TextFileTemplateStrategy {
21
21
  render(populated) {
22
22
  return populated.split("\n");
23
23
  }
24
- }
24
+ };
25
25
 
26
26
  exports.TextFileTemplateStrategy = TextFileTemplateStrategy;
@@ -1,7 +1,8 @@
1
- import { __name } from '../chunk-SHUYVCID.js';
2
1
  import { Type } from '@sinclair/typebox';
3
2
 
4
- class TextFileTemplateStrategy {
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+ var TextFileTemplateStrategy = class {
5
6
  static {
6
7
  __name(this, "TextFileTemplateStrategy");
7
8
  }
@@ -18,6 +19,6 @@ class TextFileTemplateStrategy {
18
19
  render(populated) {
19
20
  return populated.split("\n");
20
21
  }
21
- }
22
+ };
22
23
 
23
24
  export { TextFileTemplateStrategy };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bemoje/template",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A TypeScript template engine with schema validation and pluggable rendering strategies.",
5
5
  "keywords": [
6
6
  "template",