@bemoje/template 0.0.1 → 1.0.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.
- package/README.md +131 -0
- package/index.mjs +2 -1
- package/package.json +1 -1
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
|
package/index.mjs
CHANGED
|
@@ -12,6 +12,7 @@ var SchemaValidationError = class extends Error {
|
|
|
12
12
|
errors;
|
|
13
13
|
constructor(errors, value, message) {
|
|
14
14
|
super(message);
|
|
15
|
+
this.name = "SchemaValidationError";
|
|
15
16
|
this.value = value;
|
|
16
17
|
this.errors = Array.isArray(errors) ? errors : errors instanceof ValueErrorIterator ? Array.from(errors) : Array.from(Value.Errors(errors, value));
|
|
17
18
|
}
|
|
@@ -60,7 +61,7 @@ var Template = class {
|
|
|
60
61
|
const stringTemplate = this.strategy.templateToString(this.template);
|
|
61
62
|
const merged = Value.Cast(this.optionsSchema, data);
|
|
62
63
|
assertValidSchema(this.optionsSchema, merged, "Invalid options");
|
|
63
|
-
const populated = stringTemplate.
|
|
64
|
+
const populated = stringTemplate.replaceAll(/{{(\w+)}}/g, (_, key) => {
|
|
64
65
|
return String(merged[key]);
|
|
65
66
|
});
|
|
66
67
|
return this.strategy.render(populated);
|