@bemoje/template 1.1.1 → 1.1.3

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 CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  A TypeScript template engine with schema validation and pluggable rendering strategies.
4
4
 
5
+ ## Exports
6
+
7
+ <!-- EXPORTS_START -->
8
+
9
+ - **JsonFileTemplateStrategy**: Template strategy for handling JSON file templates with structured object schemas. Converts structured objects to formatted JSON strings and parses JSON strings back to typed objects. Uses pretty-printing with 2-space indentation for human-readable output.
10
+ - **StringTemplateStrategy**: Template strategy for handling simple string templates. Provides pass-through behavior for string templates where the template and rendered output are both plain strings. Useful for text-based templates that don't require parsing or complex structure.
11
+ - **Template**: A generic template engine that supports variable substitution using the Strategy pattern. Validates templates and options against TypeBox schemas and renders templates with provided data. Supports mustache-style `{{variable}}` syntax for variable substitution.
12
+ - **TextFileTemplateStrategy**: Template strategy for handling multi-line text file templates. Converts arrays of strings to newline-separated text and parses text files back to string arrays by splitting on newlines. Ideal for processing configuration files, scripts, or any line-based text content.
13
+
14
+ <!-- EXPORTS_END -->
15
+
5
16
  ## Template Strategies
6
17
 
7
18
  ### StringTemplateStrategy
@@ -66,66 +77,3 @@ const template = new Template({
66
77
  ],
67
78
  })
68
79
  ```
69
-
70
- ## API Reference
71
-
72
- ### Template Class
73
-
74
- #### Constructor Options
75
-
76
- ```typescript
77
- interface TemplateOptions<TemplateSchema, OptionsSchema> {
78
- strategy: TemplateStrategy<TemplateSchema>
79
- optionsSchema?: OptionsSchema
80
- template: Static<TemplateSchema>
81
- }
82
- ```
83
-
84
- #### Methods
85
-
86
- - **`render(data?)`** - Renders template with provided data, returns typed result
87
- - **`renderString(data?)`** - Renders template and returns string representation
88
- - **`createSchema()`** - Returns the template schema with template as default
89
-
90
- ### Strategy Interface
91
-
92
- ```typescript
93
- interface TemplateStrategy<TemplateSchema extends TSchema> {
94
- readonly templateSchema: TemplateSchema
95
- templateToString(template: Static<TemplateSchema>): string
96
- render(populated: string): Static<TemplateSchema>
97
- }
98
- ```
99
-
100
- ## Validation
101
-
102
- The template engine performs validation at multiple levels:
103
-
104
- 1. **Template validation** - Ensures all variables in the options schema are present in the template
105
- 2. **Data validation** - Validates render data against the options schema using TypeBox
106
- 3. **Schema compliance** - Template structure must conform to the strategy's schema
107
-
108
- ```typescript
109
- // This will throw an error because {{name}} is missing from template
110
- const invalidTemplate = new Template({
111
- strategy: new StringTemplateStrategy(),
112
- optionsSchema: Type.Object({ name: Type.String(), age: Type.Number() }),
113
- template: 'Hello {{age}}', // Missing {{name}}!
114
- })
115
- ```
116
-
117
- ## Error Handling
118
-
119
- The library throws descriptive errors for common issues:
120
-
121
- - Missing template variables: `"Template does not include variable: variableName"`
122
- - Invalid render data: `"Invalid options"` (with TypeBox validation details)
123
- - Schema mismatches: TypeBox schema validation errors
124
-
125
- ## Use Cases
126
-
127
- - **Configuration file generation** - JSON configs, environment files
128
- - **Code generation** - Class templates, boilerplate code
129
- - **Documentation** - README templates, API docs
130
- - **Build scripts** - Package.json generation, CI/CD configs
131
- - **Email templates** - HTML/text email content
package/lib/index.d.cts CHANGED
@@ -1,6 +1,159 @@
1
- export { Template, TemplateOptions } from './Template/Template.cjs';
2
- export { TemplateStrategy } from './interfaces/TemplateStrategy.cjs';
3
- export { JsonFileTemplateStrategy } from './strategies/JsonFileTemplateStrategy.cjs';
4
- export { StringTemplateStrategy } from './strategies/StringTemplateStrategy.cjs';
5
- export { TextFileTemplateStrategy } from './strategies/TextFileTemplateStrategy.cjs';
6
- import '@sinclair/typebox';
1
+ import { TSchema, Static, TObject, TString, TArray } from '@sinclair/typebox';
2
+
3
+ /**
4
+ * Strategy interface for template processing operations.
5
+ *
6
+ * @template TemplateSchema - The schema type that defines the structure of templates,
7
+ * must extend TSchema.
8
+ */
9
+ interface TemplateStrategy<TemplateSchema extends TSchema> {
10
+ /**
11
+ * The schema definition for templates handled by this strategy.
12
+ */
13
+ readonly templateSchema: TemplateSchema;
14
+ /**
15
+ * Converts a template object to its string representation.
16
+ *
17
+ * @param template - The template object conforming to the template schema
18
+ * @returns The string representation of the template
19
+ */
20
+ templateToString(template: Static<TemplateSchema>): string;
21
+ /**
22
+ * Renders a populated template string back into a structured template object.
23
+ *
24
+ * @param populated - The populated template string
25
+ * @returns The structured template object that conforms to the template schema
26
+ */
27
+ render(populated: string): Static<TemplateSchema>;
28
+ }
29
+
30
+ interface TemplateOptions<TemplateSchema extends TSchema, OptionsSchema extends TObject> {
31
+ readonly strategy: TemplateStrategy<TemplateSchema>;
32
+ readonly optionsSchema?: OptionsSchema;
33
+ readonly template: Static<TemplateSchema>;
34
+ }
35
+ /**
36
+ * A generic template engine that supports variable substitution using the Strategy pattern.
37
+ * Validates templates and options against TypeBox schemas and renders templates with provided data.
38
+ *
39
+ * Supports mustache-style `{{variable}}` syntax for variable substitution.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const strategy = new StringTemplateStrategy()
44
+ * const optionsSchema = Type.Object({
45
+ * name: Type.String({ default: 'World' }),
46
+ * greeting: Type.String({ default: 'Hello' })
47
+ * })
48
+ *
49
+ * const template = new Template({
50
+ * strategy,
51
+ * optionsSchema,
52
+ * template: "{{greeting}} {{name}}!"
53
+ * })
54
+ *
55
+ * const result = template.render({ name: 'TypeScript' })
56
+ * // Returns: "Hello TypeScript!"
57
+ * ```
58
+ */
59
+ declare class Template<TemplateSchema extends TSchema, OptionsSchema extends TObject> {
60
+ protected readonly strategy: TemplateStrategy<TemplateSchema>;
61
+ protected readonly optionsSchema: OptionsSchema;
62
+ protected readonly template: Static<TemplateSchema>;
63
+ constructor(options: TemplateOptions<TemplateSchema, OptionsSchema>);
64
+ /**
65
+ * Returns the template schema with the template set as the default value.
66
+ */
67
+ createSchema(): TemplateSchema;
68
+ /**
69
+ * Renders the template with the provided data, returning the result in the template schema format.
70
+ *
71
+ * Variables in the template using `{{variableName}}` syntax are replaced with corresponding
72
+ * values from the data object. Data is validated against the options schema before rendering.
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * const result = template.render({ name: 'John', age: 30 })
77
+ * ```
78
+ */
79
+ render(data?: Static<OptionsSchema>): Static<TemplateSchema>;
80
+ /**
81
+ * Renders the template and returns the result as a string representation.
82
+ * Convenience method that combines `render()` and `templateToString()`.
83
+ */
84
+ renderString(data?: Static<OptionsSchema>): string;
85
+ /**
86
+ * Validates that the template contains all variables defined in the options schema.
87
+ *
88
+ * @throws {Error} When a variable defined in the options schema is not found in the template
89
+ */
90
+ protected assertValidTemplate(): void;
91
+ }
92
+
93
+ /**
94
+ * Template strategy for handling JSON file templates with structured object schemas.
95
+ *
96
+ * Converts structured objects to formatted JSON strings and parses JSON strings
97
+ * back to typed objects. Uses pretty-printing with 2-space indentation for
98
+ * human-readable output.
99
+ *
100
+ * @template Schema - Object schema type extending TObject, defaults to empty object
101
+ */
102
+ declare class JsonFileTemplateStrategy<Schema extends TObject = TObject> implements TemplateStrategy<Schema> {
103
+ readonly templateSchema: Schema;
104
+ /**
105
+ * Creates a JSON file template strategy with optional schema validation.
106
+ *
107
+ * @param templateSchema - TypeBox object schema for template validation.
108
+ * Defaults to empty object schema if not provided.
109
+ */
110
+ constructor(templateSchema?: Schema);
111
+ /**
112
+ * Converts a template object to pretty-printed JSON string.
113
+ */
114
+ templateToString(template: Static<Schema>): string;
115
+ /**
116
+ * Parses JSON string back to typed object conforming to schema.
117
+ */
118
+ render(populated: string): Static<Schema>;
119
+ }
120
+
121
+ /**
122
+ * Template strategy for handling simple string templates.
123
+ *
124
+ * Provides pass-through behavior for string templates where the template
125
+ * and rendered output are both plain strings. Useful for text-based templates
126
+ * that don't require parsing or complex structure.
127
+ */
128
+ declare class StringTemplateStrategy implements TemplateStrategy<TString> {
129
+ readonly templateSchema: TString;
130
+ /**
131
+ * Returns the template string as-is (pass-through).
132
+ */
133
+ templateToString(template: Static<TString>): string;
134
+ /**
135
+ * Returns the populated string as-is (pass-through).
136
+ */
137
+ render(populated: string): Static<TString>;
138
+ }
139
+
140
+ /**
141
+ * Template strategy for handling multi-line text file templates.
142
+ *
143
+ * Converts arrays of strings to newline-separated text and parses text files
144
+ * back to string arrays by splitting on newlines. Ideal for processing
145
+ * configuration files, scripts, or any line-based text content.
146
+ */
147
+ declare class TextFileTemplateStrategy implements TemplateStrategy<TArray<TString>> {
148
+ readonly templateSchema: TArray<TString>;
149
+ /**
150
+ * Joins string array into newline-separated text.
151
+ */
152
+ templateToString(template: Static<TArray<TString>>): string;
153
+ /**
154
+ * Splits text content into string array by newlines.
155
+ */
156
+ render(populated: string): Static<TArray<TString>>;
157
+ }
158
+
159
+ export { JsonFileTemplateStrategy, StringTemplateStrategy, Template, type TemplateOptions, type TemplateStrategy, TextFileTemplateStrategy };
package/lib/index.d.ts CHANGED
@@ -1,6 +1,159 @@
1
- export { Template, TemplateOptions } from './Template/Template.js';
2
- export { TemplateStrategy } from './interfaces/TemplateStrategy.js';
3
- export { JsonFileTemplateStrategy } from './strategies/JsonFileTemplateStrategy.js';
4
- export { StringTemplateStrategy } from './strategies/StringTemplateStrategy.js';
5
- export { TextFileTemplateStrategy } from './strategies/TextFileTemplateStrategy.js';
6
- import '@sinclair/typebox';
1
+ import { TSchema, Static, TObject, TString, TArray } from '@sinclair/typebox';
2
+
3
+ /**
4
+ * Strategy interface for template processing operations.
5
+ *
6
+ * @template TemplateSchema - The schema type that defines the structure of templates,
7
+ * must extend TSchema.
8
+ */
9
+ interface TemplateStrategy<TemplateSchema extends TSchema> {
10
+ /**
11
+ * The schema definition for templates handled by this strategy.
12
+ */
13
+ readonly templateSchema: TemplateSchema;
14
+ /**
15
+ * Converts a template object to its string representation.
16
+ *
17
+ * @param template - The template object conforming to the template schema
18
+ * @returns The string representation of the template
19
+ */
20
+ templateToString(template: Static<TemplateSchema>): string;
21
+ /**
22
+ * Renders a populated template string back into a structured template object.
23
+ *
24
+ * @param populated - The populated template string
25
+ * @returns The structured template object that conforms to the template schema
26
+ */
27
+ render(populated: string): Static<TemplateSchema>;
28
+ }
29
+
30
+ interface TemplateOptions<TemplateSchema extends TSchema, OptionsSchema extends TObject> {
31
+ readonly strategy: TemplateStrategy<TemplateSchema>;
32
+ readonly optionsSchema?: OptionsSchema;
33
+ readonly template: Static<TemplateSchema>;
34
+ }
35
+ /**
36
+ * A generic template engine that supports variable substitution using the Strategy pattern.
37
+ * Validates templates and options against TypeBox schemas and renders templates with provided data.
38
+ *
39
+ * Supports mustache-style `{{variable}}` syntax for variable substitution.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const strategy = new StringTemplateStrategy()
44
+ * const optionsSchema = Type.Object({
45
+ * name: Type.String({ default: 'World' }),
46
+ * greeting: Type.String({ default: 'Hello' })
47
+ * })
48
+ *
49
+ * const template = new Template({
50
+ * strategy,
51
+ * optionsSchema,
52
+ * template: "{{greeting}} {{name}}!"
53
+ * })
54
+ *
55
+ * const result = template.render({ name: 'TypeScript' })
56
+ * // Returns: "Hello TypeScript!"
57
+ * ```
58
+ */
59
+ declare class Template<TemplateSchema extends TSchema, OptionsSchema extends TObject> {
60
+ protected readonly strategy: TemplateStrategy<TemplateSchema>;
61
+ protected readonly optionsSchema: OptionsSchema;
62
+ protected readonly template: Static<TemplateSchema>;
63
+ constructor(options: TemplateOptions<TemplateSchema, OptionsSchema>);
64
+ /**
65
+ * Returns the template schema with the template set as the default value.
66
+ */
67
+ createSchema(): TemplateSchema;
68
+ /**
69
+ * Renders the template with the provided data, returning the result in the template schema format.
70
+ *
71
+ * Variables in the template using `{{variableName}}` syntax are replaced with corresponding
72
+ * values from the data object. Data is validated against the options schema before rendering.
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * const result = template.render({ name: 'John', age: 30 })
77
+ * ```
78
+ */
79
+ render(data?: Static<OptionsSchema>): Static<TemplateSchema>;
80
+ /**
81
+ * Renders the template and returns the result as a string representation.
82
+ * Convenience method that combines `render()` and `templateToString()`.
83
+ */
84
+ renderString(data?: Static<OptionsSchema>): string;
85
+ /**
86
+ * Validates that the template contains all variables defined in the options schema.
87
+ *
88
+ * @throws {Error} When a variable defined in the options schema is not found in the template
89
+ */
90
+ protected assertValidTemplate(): void;
91
+ }
92
+
93
+ /**
94
+ * Template strategy for handling JSON file templates with structured object schemas.
95
+ *
96
+ * Converts structured objects to formatted JSON strings and parses JSON strings
97
+ * back to typed objects. Uses pretty-printing with 2-space indentation for
98
+ * human-readable output.
99
+ *
100
+ * @template Schema - Object schema type extending TObject, defaults to empty object
101
+ */
102
+ declare class JsonFileTemplateStrategy<Schema extends TObject = TObject> implements TemplateStrategy<Schema> {
103
+ readonly templateSchema: Schema;
104
+ /**
105
+ * Creates a JSON file template strategy with optional schema validation.
106
+ *
107
+ * @param templateSchema - TypeBox object schema for template validation.
108
+ * Defaults to empty object schema if not provided.
109
+ */
110
+ constructor(templateSchema?: Schema);
111
+ /**
112
+ * Converts a template object to pretty-printed JSON string.
113
+ */
114
+ templateToString(template: Static<Schema>): string;
115
+ /**
116
+ * Parses JSON string back to typed object conforming to schema.
117
+ */
118
+ render(populated: string): Static<Schema>;
119
+ }
120
+
121
+ /**
122
+ * Template strategy for handling simple string templates.
123
+ *
124
+ * Provides pass-through behavior for string templates where the template
125
+ * and rendered output are both plain strings. Useful for text-based templates
126
+ * that don't require parsing or complex structure.
127
+ */
128
+ declare class StringTemplateStrategy implements TemplateStrategy<TString> {
129
+ readonly templateSchema: TString;
130
+ /**
131
+ * Returns the template string as-is (pass-through).
132
+ */
133
+ templateToString(template: Static<TString>): string;
134
+ /**
135
+ * Returns the populated string as-is (pass-through).
136
+ */
137
+ render(populated: string): Static<TString>;
138
+ }
139
+
140
+ /**
141
+ * Template strategy for handling multi-line text file templates.
142
+ *
143
+ * Converts arrays of strings to newline-separated text and parses text files
144
+ * back to string arrays by splitting on newlines. Ideal for processing
145
+ * configuration files, scripts, or any line-based text content.
146
+ */
147
+ declare class TextFileTemplateStrategy implements TemplateStrategy<TArray<TString>> {
148
+ readonly templateSchema: TArray<TString>;
149
+ /**
150
+ * Joins string array into newline-separated text.
151
+ */
152
+ templateToString(template: Static<TArray<TString>>): string;
153
+ /**
154
+ * Splits text content into string array by newlines.
155
+ */
156
+ render(populated: string): Static<TArray<TString>>;
157
+ }
158
+
159
+ export { JsonFileTemplateStrategy, StringTemplateStrategy, Template, type TemplateOptions, type TemplateStrategy, TextFileTemplateStrategy };
package/package.json CHANGED
@@ -1,7 +1,15 @@
1
1
  {
2
2
  "name": "@bemoje/template",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "A TypeScript template engine with schema validation and pluggable rendering strategies.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "dependencies": {
11
+ "@sinclair/typebox": "^0.34.48"
12
+ },
5
13
  "keywords": [
6
14
  "template",
7
15
  "template-engine",
@@ -12,17 +20,15 @@
12
20
  "utilities",
13
21
  "typescript"
14
22
  ],
15
- "dependencies": {
16
- "@sinclair/typebox": "^0.34.48"
17
- },
18
- "type": "module",
19
- "sideEffects": false,
20
- "publishConfig": {
21
- "access": "public"
22
- },
23
23
  "main": "./lib/index.cjs",
24
24
  "module": "./lib/index.js",
25
25
  "types": "./lib/index.d.ts",
26
+ "files": [
27
+ "./lib/index.js",
28
+ "./lib/index.d.ts",
29
+ "./lib/index.cjs",
30
+ "./lib/index.d.cts"
31
+ ],
26
32
  "exports": {
27
33
  ".": {
28
34
  "import": {
@@ -34,83 +40,17 @@
34
40
  "default": "./lib/index.cjs"
35
41
  }
36
42
  },
37
- "./Template/Template": {
38
- "import": {
39
- "types": "./lib/Template/Template.d.ts",
40
- "default": "./lib/Template/Template.js"
41
- },
42
- "require": {
43
- "types": "./lib/Template/Template.d.cts",
44
- "default": "./lib/Template/Template.cjs"
45
- }
46
- },
47
- "./interfaces/TemplateStrategy": {
43
+ "./*": {
48
44
  "import": {
49
- "types": "./lib/interfaces/TemplateStrategy.d.ts",
50
- "default": "./lib/interfaces/TemplateStrategy.js"
51
- },
52
- "require": {
53
- "types": "./lib/interfaces/TemplateStrategy.d.cts",
54
- "default": "./lib/interfaces/TemplateStrategy.cjs"
55
- }
56
- },
57
- "./strategies/JsonFileTemplateStrategy": {
58
- "import": {
59
- "types": "./lib/strategies/JsonFileTemplateStrategy.d.ts",
60
- "default": "./lib/strategies/JsonFileTemplateStrategy.js"
61
- },
62
- "require": {
63
- "types": "./lib/strategies/JsonFileTemplateStrategy.d.cts",
64
- "default": "./lib/strategies/JsonFileTemplateStrategy.cjs"
65
- }
66
- },
67
- "./strategies/StringTemplateStrategy": {
68
- "import": {
69
- "types": "./lib/strategies/StringTemplateStrategy.d.ts",
70
- "default": "./lib/strategies/StringTemplateStrategy.js"
71
- },
72
- "require": {
73
- "types": "./lib/strategies/StringTemplateStrategy.d.cts",
74
- "default": "./lib/strategies/StringTemplateStrategy.cjs"
75
- }
76
- },
77
- "./strategies/TextFileTemplateStrategy": {
78
- "import": {
79
- "types": "./lib/strategies/TextFileTemplateStrategy.d.ts",
80
- "default": "./lib/strategies/TextFileTemplateStrategy.js"
45
+ "types": "./lib/index.d.ts",
46
+ "default": "./lib/index.js"
81
47
  },
82
48
  "require": {
83
- "types": "./lib/strategies/TextFileTemplateStrategy.d.cts",
84
- "default": "./lib/strategies/TextFileTemplateStrategy.cjs"
49
+ "types": "./lib/index.d.cts",
50
+ "default": "./lib/index.cjs"
85
51
  }
86
52
  }
87
53
  },
88
- "files": [
89
- "./lib/index.js",
90
- "./lib/index.d.ts",
91
- "./lib/index.cjs",
92
- "./lib/index.d.cts",
93
- "./lib/Template/Template.js",
94
- "./lib/Template/Template.d.ts",
95
- "./lib/Template/Template.cjs",
96
- "./lib/Template/Template.d.cts",
97
- "./lib/interfaces/TemplateStrategy.js",
98
- "./lib/interfaces/TemplateStrategy.d.ts",
99
- "./lib/interfaces/TemplateStrategy.cjs",
100
- "./lib/interfaces/TemplateStrategy.d.cts",
101
- "./lib/strategies/JsonFileTemplateStrategy.js",
102
- "./lib/strategies/JsonFileTemplateStrategy.d.ts",
103
- "./lib/strategies/JsonFileTemplateStrategy.cjs",
104
- "./lib/strategies/JsonFileTemplateStrategy.d.cts",
105
- "./lib/strategies/StringTemplateStrategy.js",
106
- "./lib/strategies/StringTemplateStrategy.d.ts",
107
- "./lib/strategies/StringTemplateStrategy.cjs",
108
- "./lib/strategies/StringTemplateStrategy.d.cts",
109
- "./lib/strategies/TextFileTemplateStrategy.js",
110
- "./lib/strategies/TextFileTemplateStrategy.d.ts",
111
- "./lib/strategies/TextFileTemplateStrategy.cjs",
112
- "./lib/strategies/TextFileTemplateStrategy.d.cts"
113
- ],
114
54
  "repository": {
115
55
  "type": "git",
116
56
  "url": "git+https://github.com/bemoje/mono.git",
@@ -1,94 +0,0 @@
1
- 'use strict';
2
-
3
- var typebox = require('@sinclair/typebox');
4
- var value = require('@sinclair/typebox/value');
5
- var compiler = require('@sinclair/typebox/compiler');
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");
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
-
94
- exports.Template = Template;
@@ -1,67 +0,0 @@
1
- import { TSchema, TObject, Static } from '@sinclair/typebox';
2
- import { TemplateStrategy } from '../interfaces/TemplateStrategy.cjs';
3
-
4
- interface TemplateOptions<TemplateSchema extends TSchema, OptionsSchema extends TObject> {
5
- readonly strategy: TemplateStrategy<TemplateSchema>;
6
- readonly optionsSchema?: OptionsSchema;
7
- readonly template: Static<TemplateSchema>;
8
- }
9
- /**
10
- * A generic template engine that supports variable substitution using the Strategy pattern.
11
- * Validates templates and options against TypeBox schemas and renders templates with provided data.
12
- *
13
- * Supports mustache-style `{{variable}}` syntax for variable substitution.
14
- *
15
- * @example
16
- * ```typescript
17
- * const strategy = new StringTemplateStrategy()
18
- * const optionsSchema = Type.Object({
19
- * name: Type.String({ default: 'World' }),
20
- * greeting: Type.String({ default: 'Hello' })
21
- * })
22
- *
23
- * const template = new Template({
24
- * strategy,
25
- * optionsSchema,
26
- * template: "{{greeting}} {{name}}!"
27
- * })
28
- *
29
- * const result = template.render({ name: 'TypeScript' })
30
- * // Returns: "Hello TypeScript!"
31
- * ```
32
- */
33
- declare class Template<TemplateSchema extends TSchema, OptionsSchema extends TObject> {
34
- protected readonly strategy: TemplateStrategy<TemplateSchema>;
35
- protected readonly optionsSchema: OptionsSchema;
36
- protected readonly template: Static<TemplateSchema>;
37
- constructor(options: TemplateOptions<TemplateSchema, OptionsSchema>);
38
- /**
39
- * Returns the template schema with the template set as the default value.
40
- */
41
- createSchema(): TemplateSchema;
42
- /**
43
- * Renders the template with the provided data, returning the result in the template schema format.
44
- *
45
- * Variables in the template using `{{variableName}}` syntax are replaced with corresponding
46
- * values from the data object. Data is validated against the options schema before rendering.
47
- *
48
- * @example
49
- * ```typescript
50
- * const result = template.render({ name: 'John', age: 30 })
51
- * ```
52
- */
53
- render(data?: Static<OptionsSchema>): Static<TemplateSchema>;
54
- /**
55
- * Renders the template and returns the result as a string representation.
56
- * Convenience method that combines `render()` and `templateToString()`.
57
- */
58
- renderString(data?: Static<OptionsSchema>): string;
59
- /**
60
- * Validates that the template contains all variables defined in the options schema.
61
- *
62
- * @throws {Error} When a variable defined in the options schema is not found in the template
63
- */
64
- protected assertValidTemplate(): void;
65
- }
66
-
67
- export { Template, type TemplateOptions };
@@ -1,67 +0,0 @@
1
- import { TSchema, TObject, Static } from '@sinclair/typebox';
2
- import { TemplateStrategy } from '../interfaces/TemplateStrategy.js';
3
-
4
- interface TemplateOptions<TemplateSchema extends TSchema, OptionsSchema extends TObject> {
5
- readonly strategy: TemplateStrategy<TemplateSchema>;
6
- readonly optionsSchema?: OptionsSchema;
7
- readonly template: Static<TemplateSchema>;
8
- }
9
- /**
10
- * A generic template engine that supports variable substitution using the Strategy pattern.
11
- * Validates templates and options against TypeBox schemas and renders templates with provided data.
12
- *
13
- * Supports mustache-style `{{variable}}` syntax for variable substitution.
14
- *
15
- * @example
16
- * ```typescript
17
- * const strategy = new StringTemplateStrategy()
18
- * const optionsSchema = Type.Object({
19
- * name: Type.String({ default: 'World' }),
20
- * greeting: Type.String({ default: 'Hello' })
21
- * })
22
- *
23
- * const template = new Template({
24
- * strategy,
25
- * optionsSchema,
26
- * template: "{{greeting}} {{name}}!"
27
- * })
28
- *
29
- * const result = template.render({ name: 'TypeScript' })
30
- * // Returns: "Hello TypeScript!"
31
- * ```
32
- */
33
- declare class Template<TemplateSchema extends TSchema, OptionsSchema extends TObject> {
34
- protected readonly strategy: TemplateStrategy<TemplateSchema>;
35
- protected readonly optionsSchema: OptionsSchema;
36
- protected readonly template: Static<TemplateSchema>;
37
- constructor(options: TemplateOptions<TemplateSchema, OptionsSchema>);
38
- /**
39
- * Returns the template schema with the template set as the default value.
40
- */
41
- createSchema(): TemplateSchema;
42
- /**
43
- * Renders the template with the provided data, returning the result in the template schema format.
44
- *
45
- * Variables in the template using `{{variableName}}` syntax are replaced with corresponding
46
- * values from the data object. Data is validated against the options schema before rendering.
47
- *
48
- * @example
49
- * ```typescript
50
- * const result = template.render({ name: 'John', age: 30 })
51
- * ```
52
- */
53
- render(data?: Static<OptionsSchema>): Static<TemplateSchema>;
54
- /**
55
- * Renders the template and returns the result as a string representation.
56
- * Convenience method that combines `render()` and `templateToString()`.
57
- */
58
- renderString(data?: Static<OptionsSchema>): string;
59
- /**
60
- * Validates that the template contains all variables defined in the options schema.
61
- *
62
- * @throws {Error} When a variable defined in the options schema is not found in the template
63
- */
64
- protected assertValidTemplate(): void;
65
- }
66
-
67
- export { Template, type TemplateOptions };
@@ -1,92 +0,0 @@
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
-
92
- export { Template };
@@ -1,2 +0,0 @@
1
- 'use strict';
2
-
@@ -1,30 +0,0 @@
1
- import { TSchema, Static } from '@sinclair/typebox';
2
-
3
- /**
4
- * Strategy interface for template processing operations.
5
- *
6
- * @template TemplateSchema - The schema type that defines the structure of templates,
7
- * must extend TSchema.
8
- */
9
- interface TemplateStrategy<TemplateSchema extends TSchema> {
10
- /**
11
- * The schema definition for templates handled by this strategy.
12
- */
13
- readonly templateSchema: TemplateSchema;
14
- /**
15
- * Converts a template object to its string representation.
16
- *
17
- * @param template - The template object conforming to the template schema
18
- * @returns The string representation of the template
19
- */
20
- templateToString(template: Static<TemplateSchema>): string;
21
- /**
22
- * Renders a populated template string back into a structured template object.
23
- *
24
- * @param populated - The populated template string
25
- * @returns The structured template object that conforms to the template schema
26
- */
27
- render(populated: string): Static<TemplateSchema>;
28
- }
29
-
30
- export type { TemplateStrategy };
@@ -1,30 +0,0 @@
1
- import { TSchema, Static } from '@sinclair/typebox';
2
-
3
- /**
4
- * Strategy interface for template processing operations.
5
- *
6
- * @template TemplateSchema - The schema type that defines the structure of templates,
7
- * must extend TSchema.
8
- */
9
- interface TemplateStrategy<TemplateSchema extends TSchema> {
10
- /**
11
- * The schema definition for templates handled by this strategy.
12
- */
13
- readonly templateSchema: TemplateSchema;
14
- /**
15
- * Converts a template object to its string representation.
16
- *
17
- * @param template - The template object conforming to the template schema
18
- * @returns The string representation of the template
19
- */
20
- templateToString(template: Static<TemplateSchema>): string;
21
- /**
22
- * Renders a populated template string back into a structured template object.
23
- *
24
- * @param populated - The populated template string
25
- * @returns The structured template object that conforms to the template schema
26
- */
27
- render(populated: string): Static<TemplateSchema>;
28
- }
29
-
30
- export type { TemplateStrategy };
@@ -1 +0,0 @@
1
-
@@ -1,35 +0,0 @@
1
- 'use strict';
2
-
3
- var typebox = require('@sinclair/typebox');
4
-
5
- var __defProp = Object.defineProperty;
6
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var JsonFileTemplateStrategy = class {
8
- static {
9
- __name(this, "JsonFileTemplateStrategy");
10
- }
11
- templateSchema;
12
- /**
13
- * Creates a JSON file template strategy with optional schema validation.
14
- *
15
- * @param templateSchema - TypeBox object schema for template validation.
16
- * Defaults to empty object schema if not provided.
17
- */
18
- constructor(templateSchema) {
19
- this.templateSchema = templateSchema ?? typebox.Type.Object({});
20
- }
21
- /**
22
- * Converts a template object to pretty-printed JSON string.
23
- */
24
- templateToString(template) {
25
- return JSON.stringify(template, null, 2);
26
- }
27
- /**
28
- * Parses JSON string back to typed object conforming to schema.
29
- */
30
- render(populated) {
31
- return JSON.parse(populated);
32
- }
33
- };
34
-
35
- exports.JsonFileTemplateStrategy = JsonFileTemplateStrategy;
@@ -1,32 +0,0 @@
1
- import { TObject, Static } from '@sinclair/typebox';
2
- import { TemplateStrategy } from '../interfaces/TemplateStrategy.cjs';
3
-
4
- /**
5
- * Template strategy for handling JSON file templates with structured object schemas.
6
- *
7
- * Converts structured objects to formatted JSON strings and parses JSON strings
8
- * back to typed objects. Uses pretty-printing with 2-space indentation for
9
- * human-readable output.
10
- *
11
- * @template Schema - Object schema type extending TObject, defaults to empty object
12
- */
13
- declare class JsonFileTemplateStrategy<Schema extends TObject = TObject> implements TemplateStrategy<Schema> {
14
- readonly templateSchema: Schema;
15
- /**
16
- * Creates a JSON file template strategy with optional schema validation.
17
- *
18
- * @param templateSchema - TypeBox object schema for template validation.
19
- * Defaults to empty object schema if not provided.
20
- */
21
- constructor(templateSchema?: Schema);
22
- /**
23
- * Converts a template object to pretty-printed JSON string.
24
- */
25
- templateToString(template: Static<Schema>): string;
26
- /**
27
- * Parses JSON string back to typed object conforming to schema.
28
- */
29
- render(populated: string): Static<Schema>;
30
- }
31
-
32
- export { JsonFileTemplateStrategy };
@@ -1,32 +0,0 @@
1
- import { TObject, Static } from '@sinclair/typebox';
2
- import { TemplateStrategy } from '../interfaces/TemplateStrategy.js';
3
-
4
- /**
5
- * Template strategy for handling JSON file templates with structured object schemas.
6
- *
7
- * Converts structured objects to formatted JSON strings and parses JSON strings
8
- * back to typed objects. Uses pretty-printing with 2-space indentation for
9
- * human-readable output.
10
- *
11
- * @template Schema - Object schema type extending TObject, defaults to empty object
12
- */
13
- declare class JsonFileTemplateStrategy<Schema extends TObject = TObject> implements TemplateStrategy<Schema> {
14
- readonly templateSchema: Schema;
15
- /**
16
- * Creates a JSON file template strategy with optional schema validation.
17
- *
18
- * @param templateSchema - TypeBox object schema for template validation.
19
- * Defaults to empty object schema if not provided.
20
- */
21
- constructor(templateSchema?: Schema);
22
- /**
23
- * Converts a template object to pretty-printed JSON string.
24
- */
25
- templateToString(template: Static<Schema>): string;
26
- /**
27
- * Parses JSON string back to typed object conforming to schema.
28
- */
29
- render(populated: string): Static<Schema>;
30
- }
31
-
32
- export { JsonFileTemplateStrategy };
@@ -1,33 +0,0 @@
1
- import { Type } from '@sinclair/typebox';
2
-
3
- var __defProp = Object.defineProperty;
4
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
- var JsonFileTemplateStrategy = class {
6
- static {
7
- __name(this, "JsonFileTemplateStrategy");
8
- }
9
- templateSchema;
10
- /**
11
- * Creates a JSON file template strategy with optional schema validation.
12
- *
13
- * @param templateSchema - TypeBox object schema for template validation.
14
- * Defaults to empty object schema if not provided.
15
- */
16
- constructor(templateSchema) {
17
- this.templateSchema = templateSchema ?? Type.Object({});
18
- }
19
- /**
20
- * Converts a template object to pretty-printed JSON string.
21
- */
22
- templateToString(template) {
23
- return JSON.stringify(template, null, 2);
24
- }
25
- /**
26
- * Parses JSON string back to typed object conforming to schema.
27
- */
28
- render(populated) {
29
- return JSON.parse(populated);
30
- }
31
- };
32
-
33
- export { JsonFileTemplateStrategy };
@@ -1,26 +0,0 @@
1
- 'use strict';
2
-
3
- var typebox = require('@sinclair/typebox');
4
-
5
- var __defProp = Object.defineProperty;
6
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var StringTemplateStrategy = class {
8
- static {
9
- __name(this, "StringTemplateStrategy");
10
- }
11
- templateSchema = typebox.Type.String();
12
- /**
13
- * Returns the template string as-is (pass-through).
14
- */
15
- templateToString(template) {
16
- return template;
17
- }
18
- /**
19
- * Returns the populated string as-is (pass-through).
20
- */
21
- render(populated) {
22
- return populated;
23
- }
24
- };
25
-
26
- exports.StringTemplateStrategy = StringTemplateStrategy;
@@ -1,23 +0,0 @@
1
- import { TString, Static } from '@sinclair/typebox';
2
- import { TemplateStrategy } from '../interfaces/TemplateStrategy.cjs';
3
-
4
- /**
5
- * Template strategy for handling simple string templates.
6
- *
7
- * Provides pass-through behavior for string templates where the template
8
- * and rendered output are both plain strings. Useful for text-based templates
9
- * that don't require parsing or complex structure.
10
- */
11
- declare class StringTemplateStrategy implements TemplateStrategy<TString> {
12
- readonly templateSchema: TString;
13
- /**
14
- * Returns the template string as-is (pass-through).
15
- */
16
- templateToString(template: Static<TString>): string;
17
- /**
18
- * Returns the populated string as-is (pass-through).
19
- */
20
- render(populated: string): Static<TString>;
21
- }
22
-
23
- export { StringTemplateStrategy };
@@ -1,23 +0,0 @@
1
- import { TString, Static } from '@sinclair/typebox';
2
- import { TemplateStrategy } from '../interfaces/TemplateStrategy.js';
3
-
4
- /**
5
- * Template strategy for handling simple string templates.
6
- *
7
- * Provides pass-through behavior for string templates where the template
8
- * and rendered output are both plain strings. Useful for text-based templates
9
- * that don't require parsing or complex structure.
10
- */
11
- declare class StringTemplateStrategy implements TemplateStrategy<TString> {
12
- readonly templateSchema: TString;
13
- /**
14
- * Returns the template string as-is (pass-through).
15
- */
16
- templateToString(template: Static<TString>): string;
17
- /**
18
- * Returns the populated string as-is (pass-through).
19
- */
20
- render(populated: string): Static<TString>;
21
- }
22
-
23
- export { StringTemplateStrategy };
@@ -1,24 +0,0 @@
1
- import { Type } from '@sinclair/typebox';
2
-
3
- var __defProp = Object.defineProperty;
4
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
- var StringTemplateStrategy = class {
6
- static {
7
- __name(this, "StringTemplateStrategy");
8
- }
9
- templateSchema = Type.String();
10
- /**
11
- * Returns the template string as-is (pass-through).
12
- */
13
- templateToString(template) {
14
- return template;
15
- }
16
- /**
17
- * Returns the populated string as-is (pass-through).
18
- */
19
- render(populated) {
20
- return populated;
21
- }
22
- };
23
-
24
- export { StringTemplateStrategy };
@@ -1,26 +0,0 @@
1
- 'use strict';
2
-
3
- var typebox = require('@sinclair/typebox');
4
-
5
- var __defProp = Object.defineProperty;
6
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var TextFileTemplateStrategy = class {
8
- static {
9
- __name(this, "TextFileTemplateStrategy");
10
- }
11
- templateSchema = typebox.Type.Array(typebox.Type.String());
12
- /**
13
- * Joins string array into newline-separated text.
14
- */
15
- templateToString(template) {
16
- return template.join("\n");
17
- }
18
- /**
19
- * Splits text content into string array by newlines.
20
- */
21
- render(populated) {
22
- return populated.split("\n");
23
- }
24
- };
25
-
26
- exports.TextFileTemplateStrategy = TextFileTemplateStrategy;
@@ -1,23 +0,0 @@
1
- import { TArray, TString, Static } from '@sinclair/typebox';
2
- import { TemplateStrategy } from '../interfaces/TemplateStrategy.cjs';
3
-
4
- /**
5
- * Template strategy for handling multi-line text file templates.
6
- *
7
- * Converts arrays of strings to newline-separated text and parses text files
8
- * back to string arrays by splitting on newlines. Ideal for processing
9
- * configuration files, scripts, or any line-based text content.
10
- */
11
- declare class TextFileTemplateStrategy implements TemplateStrategy<TArray<TString>> {
12
- readonly templateSchema: TArray<TString>;
13
- /**
14
- * Joins string array into newline-separated text.
15
- */
16
- templateToString(template: Static<TArray<TString>>): string;
17
- /**
18
- * Splits text content into string array by newlines.
19
- */
20
- render(populated: string): Static<TArray<TString>>;
21
- }
22
-
23
- export { TextFileTemplateStrategy };
@@ -1,23 +0,0 @@
1
- import { TArray, TString, Static } from '@sinclair/typebox';
2
- import { TemplateStrategy } from '../interfaces/TemplateStrategy.js';
3
-
4
- /**
5
- * Template strategy for handling multi-line text file templates.
6
- *
7
- * Converts arrays of strings to newline-separated text and parses text files
8
- * back to string arrays by splitting on newlines. Ideal for processing
9
- * configuration files, scripts, or any line-based text content.
10
- */
11
- declare class TextFileTemplateStrategy implements TemplateStrategy<TArray<TString>> {
12
- readonly templateSchema: TArray<TString>;
13
- /**
14
- * Joins string array into newline-separated text.
15
- */
16
- templateToString(template: Static<TArray<TString>>): string;
17
- /**
18
- * Splits text content into string array by newlines.
19
- */
20
- render(populated: string): Static<TArray<TString>>;
21
- }
22
-
23
- export { TextFileTemplateStrategy };
@@ -1,24 +0,0 @@
1
- import { Type } from '@sinclair/typebox';
2
-
3
- var __defProp = Object.defineProperty;
4
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
- var TextFileTemplateStrategy = class {
6
- static {
7
- __name(this, "TextFileTemplateStrategy");
8
- }
9
- templateSchema = Type.Array(Type.String());
10
- /**
11
- * Joins string array into newline-separated text.
12
- */
13
- templateToString(template) {
14
- return template.join("\n");
15
- }
16
- /**
17
- * Splits text content into string array by newlines.
18
- */
19
- render(populated) {
20
- return populated.split("\n");
21
- }
22
- };
23
-
24
- export { TextFileTemplateStrategy };