@bemoje/template 1.1.2 → 2.0.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 +11 -63
- package/package.json +13 -53
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/package.json
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/template",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A TypeScript template engine with schema validation and pluggable rendering strategies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@sinclair/typebox": "^0.34.48"
|
|
12
|
+
},
|
|
7
13
|
"keywords": [
|
|
8
14
|
"template",
|
|
9
15
|
"template-engine",
|
|
@@ -34,47 +40,7 @@
|
|
|
34
40
|
"default": "./lib/index.cjs"
|
|
35
41
|
}
|
|
36
42
|
},
|
|
37
|
-
"
|
|
38
|
-
"import": {
|
|
39
|
-
"types": "./lib/index.d.ts",
|
|
40
|
-
"default": "./lib/index.js"
|
|
41
|
-
},
|
|
42
|
-
"require": {
|
|
43
|
-
"types": "./lib/index.d.cts",
|
|
44
|
-
"default": "./lib/index.cjs"
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
"./TemplateStrategy": {
|
|
48
|
-
"import": {
|
|
49
|
-
"types": "./lib/index.d.ts",
|
|
50
|
-
"default": "./lib/index.js"
|
|
51
|
-
},
|
|
52
|
-
"require": {
|
|
53
|
-
"types": "./lib/index.d.cts",
|
|
54
|
-
"default": "./lib/index.cjs"
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
"./JsonFileTemplateStrategy": {
|
|
58
|
-
"import": {
|
|
59
|
-
"types": "./lib/index.d.ts",
|
|
60
|
-
"default": "./lib/index.js"
|
|
61
|
-
},
|
|
62
|
-
"require": {
|
|
63
|
-
"types": "./lib/index.d.cts",
|
|
64
|
-
"default": "./lib/index.cjs"
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
"./StringTemplateStrategy": {
|
|
68
|
-
"import": {
|
|
69
|
-
"types": "./lib/index.d.ts",
|
|
70
|
-
"default": "./lib/index.js"
|
|
71
|
-
},
|
|
72
|
-
"require": {
|
|
73
|
-
"types": "./lib/index.d.cts",
|
|
74
|
-
"default": "./lib/index.cjs"
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
"./TextFileTemplateStrategy": {
|
|
43
|
+
"./*": {
|
|
78
44
|
"import": {
|
|
79
45
|
"types": "./lib/index.d.ts",
|
|
80
46
|
"default": "./lib/index.js"
|
|
@@ -85,21 +51,15 @@
|
|
|
85
51
|
}
|
|
86
52
|
}
|
|
87
53
|
},
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
"access": "public"
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "git+https://github.com/bemoje/mono.git",
|
|
57
|
+
"directory": "libs/template"
|
|
93
58
|
},
|
|
94
|
-
"license": "MIT",
|
|
95
59
|
"author": {
|
|
96
60
|
"name": "Benjamin Møller Jensen",
|
|
97
61
|
"email": "bemoje@bemoje.net",
|
|
98
62
|
"url": "https://github.com/bemoje/"
|
|
99
63
|
},
|
|
100
|
-
"
|
|
101
|
-
"type": "git",
|
|
102
|
-
"url": "git+https://github.com/bemoje/mono.git",
|
|
103
|
-
"directory": "libs/template"
|
|
104
|
-
}
|
|
64
|
+
"license": "MIT"
|
|
105
65
|
}
|