@bemoje/template 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +131 -0
- package/lib/Template/Template.cjs +72 -0
- package/lib/Template/Template.d.cts +67 -0
- package/lib/Template/Template.d.ts +67 -0
- package/lib/Template/Template.js +69 -0
- package/lib/index.cjs +40 -0
- package/lib/index.d.cts +6 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +5 -0
- package/lib/interfaces/TemplateStrategy.cjs +2 -0
- package/lib/interfaces/TemplateStrategy.d.cts +30 -0
- package/lib/interfaces/TemplateStrategy.d.ts +30 -0
- package/lib/interfaces/TemplateStrategy.js +1 -0
- package/lib/strategies/JsonFileTemplateStrategy.cjs +35 -0
- package/lib/strategies/JsonFileTemplateStrategy.d.cts +32 -0
- package/lib/strategies/JsonFileTemplateStrategy.d.ts +32 -0
- package/lib/strategies/JsonFileTemplateStrategy.js +32 -0
- package/lib/strategies/StringTemplateStrategy.cjs +26 -0
- package/lib/strategies/StringTemplateStrategy.d.cts +23 -0
- package/lib/strategies/StringTemplateStrategy.d.ts +23 -0
- package/lib/strategies/StringTemplateStrategy.js +23 -0
- package/lib/strategies/TextFileTemplateStrategy.cjs +26 -0
- package/lib/strategies/TextFileTemplateStrategy.d.cts +23 -0
- package/lib/strategies/TextFileTemplateStrategy.d.ts +23 -0
- package/lib/strategies/TextFileTemplateStrategy.js +23 -0
- package/package.json +110 -19
- package/index.d.ts +0 -159
- package/index.mjs +0 -154
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# @mono/template
|
|
2
|
+
|
|
3
|
+
A TypeScript template engine with schema validation and pluggable rendering strategies.
|
|
4
|
+
|
|
5
|
+
## Template Strategies
|
|
6
|
+
|
|
7
|
+
### StringTemplateStrategy
|
|
8
|
+
|
|
9
|
+
For simple string templates with pass-through behavior:
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { StringTemplateStrategy } from '@mono/template'
|
|
13
|
+
|
|
14
|
+
const strategy = new StringTemplateStrategy()
|
|
15
|
+
const template = new Template({
|
|
16
|
+
strategy,
|
|
17
|
+
optionsSchema: Type.Object({ user: Type.String({ default: 'Anonymous' }) }),
|
|
18
|
+
template: 'Welcome {{user}}!',
|
|
19
|
+
})
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### JsonFileTemplateStrategy
|
|
23
|
+
|
|
24
|
+
For structured JSON templates with pretty-printing:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { JsonFileTemplateStrategy } from '@mono/template'
|
|
28
|
+
|
|
29
|
+
const strategy = new JsonFileTemplateStrategy(
|
|
30
|
+
Type.Object({ name: Type.String(), version: Type.String(), author: Type.String() })
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
const template = new Template({
|
|
34
|
+
strategy,
|
|
35
|
+
optionsSchema: Type.Object({
|
|
36
|
+
packageName: Type.String(),
|
|
37
|
+
version: Type.String({ default: '1.0.0' }),
|
|
38
|
+
author: Type.String({ default: 'Unknown' }),
|
|
39
|
+
}),
|
|
40
|
+
template: { name: '{{packageName}}', version: '{{version}}', author: '{{author}}' },
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const result = template.render({ packageName: 'my-package' })
|
|
44
|
+
// Returns formatted JSON object
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### TextFileTemplateStrategy
|
|
48
|
+
|
|
49
|
+
For multi-line text templates (arrays of strings):
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { TextFileTemplateStrategy } from '@mono/template'
|
|
53
|
+
|
|
54
|
+
const strategy = new TextFileTemplateStrategy()
|
|
55
|
+
const template = new Template({
|
|
56
|
+
strategy,
|
|
57
|
+
optionsSchema: Type.Object({ className: Type.String(), author: Type.String({ default: 'Developer' }) }),
|
|
58
|
+
template: [
|
|
59
|
+
'/**',
|
|
60
|
+
' * {{className}} class',
|
|
61
|
+
' * @author {{author}}',
|
|
62
|
+
' */',
|
|
63
|
+
'export class {{className}} {',
|
|
64
|
+
' // Implementation here',
|
|
65
|
+
'}',
|
|
66
|
+
],
|
|
67
|
+
})
|
|
68
|
+
```
|
|
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
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var typebox = require('@sinclair/typebox');
|
|
4
|
+
var value = require('@sinclair/typebox/value');
|
|
5
|
+
var tschema = require('@mono/tschema');
|
|
6
|
+
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
class Template {
|
|
10
|
+
static {
|
|
11
|
+
__name(this, "Template");
|
|
12
|
+
}
|
|
13
|
+
strategy;
|
|
14
|
+
optionsSchema;
|
|
15
|
+
template;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.strategy = options.strategy;
|
|
18
|
+
this.optionsSchema = options.optionsSchema ?? typebox.Type.Object({});
|
|
19
|
+
this.template = options.template;
|
|
20
|
+
this.strategy.templateSchema.default = this.template;
|
|
21
|
+
this.assertValidTemplate();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Returns the template schema with the template set as the default value.
|
|
25
|
+
*/
|
|
26
|
+
createSchema() {
|
|
27
|
+
return this.strategy.templateSchema;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Renders the template with the provided data, returning the result in the template schema format.
|
|
31
|
+
*
|
|
32
|
+
* Variables in the template using `{{variableName}}` syntax are replaced with corresponding
|
|
33
|
+
* values from the data object. Data is validated against the options schema before rendering.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const result = template.render({ name: 'John', age: 30 })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
render(data = {}) {
|
|
41
|
+
const stringTemplate = this.strategy.templateToString(this.template);
|
|
42
|
+
const merged = value.Value.Cast(this.optionsSchema, data);
|
|
43
|
+
tschema.assertValidSchema(this.optionsSchema, merged, "Invalid options");
|
|
44
|
+
const populated = stringTemplate.replaceAll(/{{(\w+)}}/g, (_, key) => {
|
|
45
|
+
return String(merged[key]);
|
|
46
|
+
});
|
|
47
|
+
return this.strategy.render(populated);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Renders the template and returns the result as a string representation.
|
|
51
|
+
* Convenience method that combines `render()` and `templateToString()`.
|
|
52
|
+
*/
|
|
53
|
+
renderString(data = {}) {
|
|
54
|
+
return this.strategy.templateToString(this.render(data));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Validates that the template contains all variables defined in the options schema.
|
|
58
|
+
*
|
|
59
|
+
* @throws {Error} When a variable defined in the options schema is not found in the template
|
|
60
|
+
*/
|
|
61
|
+
assertValidTemplate() {
|
|
62
|
+
const string = this.strategy.templateToString(this.template);
|
|
63
|
+
const variables = Object.keys(this.optionsSchema.properties);
|
|
64
|
+
for (const variable of variables) {
|
|
65
|
+
if (!string.includes(`{{${variable}}}`)) {
|
|
66
|
+
throw new Error(`Template does not include variable: ${variable}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
exports.Template = Template;
|
|
@@ -0,0 +1,67 @@
|
|
|
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 };
|
|
@@ -0,0 +1,67 @@
|
|
|
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 };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { __name } from '../chunk-SHUYVCID.js';
|
|
2
|
+
import { Type } from '@sinclair/typebox';
|
|
3
|
+
import { Value } from '@sinclair/typebox/value';
|
|
4
|
+
import { assertValidSchema } from '@mono/tschema';
|
|
5
|
+
|
|
6
|
+
class Template {
|
|
7
|
+
static {
|
|
8
|
+
__name(this, "Template");
|
|
9
|
+
}
|
|
10
|
+
strategy;
|
|
11
|
+
optionsSchema;
|
|
12
|
+
template;
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.strategy = options.strategy;
|
|
15
|
+
this.optionsSchema = options.optionsSchema ?? Type.Object({});
|
|
16
|
+
this.template = options.template;
|
|
17
|
+
this.strategy.templateSchema.default = this.template;
|
|
18
|
+
this.assertValidTemplate();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns the template schema with the template set as the default value.
|
|
22
|
+
*/
|
|
23
|
+
createSchema() {
|
|
24
|
+
return this.strategy.templateSchema;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Renders the template with the provided data, returning the result in the template schema format.
|
|
28
|
+
*
|
|
29
|
+
* Variables in the template using `{{variableName}}` syntax are replaced with corresponding
|
|
30
|
+
* values from the data object. Data is validated against the options schema before rendering.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const result = template.render({ name: 'John', age: 30 })
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
render(data = {}) {
|
|
38
|
+
const stringTemplate = this.strategy.templateToString(this.template);
|
|
39
|
+
const merged = Value.Cast(this.optionsSchema, data);
|
|
40
|
+
assertValidSchema(this.optionsSchema, merged, "Invalid options");
|
|
41
|
+
const populated = stringTemplate.replaceAll(/{{(\w+)}}/g, (_, key) => {
|
|
42
|
+
return String(merged[key]);
|
|
43
|
+
});
|
|
44
|
+
return this.strategy.render(populated);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Renders the template and returns the result as a string representation.
|
|
48
|
+
* Convenience method that combines `render()` and `templateToString()`.
|
|
49
|
+
*/
|
|
50
|
+
renderString(data = {}) {
|
|
51
|
+
return this.strategy.templateToString(this.render(data));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Validates that the template contains all variables defined in the options schema.
|
|
55
|
+
*
|
|
56
|
+
* @throws {Error} When a variable defined in the options schema is not found in the template
|
|
57
|
+
*/
|
|
58
|
+
assertValidTemplate() {
|
|
59
|
+
const string = this.strategy.templateToString(this.template);
|
|
60
|
+
const variables = Object.keys(this.optionsSchema.properties);
|
|
61
|
+
for (const variable of variables) {
|
|
62
|
+
if (!string.includes(`{{${variable}}}`)) {
|
|
63
|
+
throw new Error(`Template does not include variable: ${variable}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { Template };
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
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');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
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
|
+
});
|
package/lib/index.d.cts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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';
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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 };
|
|
@@ -0,0 +1,30 @@
|
|
|
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 };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
class JsonFileTemplateStrategy {
|
|
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;
|
|
@@ -0,0 +1,32 @@
|
|
|
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 };
|
|
@@ -0,0 +1,32 @@
|
|
|
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 };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { __name } from '../chunk-SHUYVCID.js';
|
|
2
|
+
import { Type } from '@sinclair/typebox';
|
|
3
|
+
|
|
4
|
+
class JsonFileTemplateStrategy {
|
|
5
|
+
static {
|
|
6
|
+
__name(this, "JsonFileTemplateStrategy");
|
|
7
|
+
}
|
|
8
|
+
templateSchema;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a JSON file template strategy with optional schema validation.
|
|
11
|
+
*
|
|
12
|
+
* @param templateSchema - TypeBox object schema for template validation.
|
|
13
|
+
* Defaults to empty object schema if not provided.
|
|
14
|
+
*/
|
|
15
|
+
constructor(templateSchema) {
|
|
16
|
+
this.templateSchema = templateSchema ?? Type.Object({});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Converts a template object to pretty-printed JSON string.
|
|
20
|
+
*/
|
|
21
|
+
templateToString(template) {
|
|
22
|
+
return JSON.stringify(template, null, 2);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Parses JSON string back to typed object conforming to schema.
|
|
26
|
+
*/
|
|
27
|
+
render(populated) {
|
|
28
|
+
return JSON.parse(populated);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { JsonFileTemplateStrategy };
|