html-express-js 4.2.0 → 4.3.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/package.json +14 -2
- package/src/index.d.ts +18 -0
- package/src/index.js +20 -7
package/package.json
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "html-express-js",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "An Express template engine to render HTML views using native JavaScript",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"engines": {
|
|
8
|
-
"node": ">=
|
|
8
|
+
"node": ">=16"
|
|
9
|
+
},
|
|
10
|
+
"devEngines": {
|
|
11
|
+
"runtime": {
|
|
12
|
+
"name": "node",
|
|
13
|
+
"version": ">=22.19",
|
|
14
|
+
"onFail": "error"
|
|
15
|
+
},
|
|
16
|
+
"packageManager": {
|
|
17
|
+
"name": "npm",
|
|
18
|
+
"version": ">=10.9.3",
|
|
19
|
+
"onFail": "error"
|
|
20
|
+
}
|
|
9
21
|
},
|
|
10
22
|
"files": [
|
|
11
23
|
"src/index.js",
|
package/src/index.d.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates HTML for the provided view and adds all includes to state object.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} filePath - The path to html file
|
|
5
|
+
* @param {import('express').Request} req - The request to build the state from
|
|
6
|
+
* @param {Record<string, any>} [data] - Data provided to the view
|
|
7
|
+
* @returns {Promise<string>} HTML with includes available (appended to state)
|
|
8
|
+
*/
|
|
9
|
+
export function buildStateViewHtml(filePath: string, req: import("express").Request, data?: Record<string, any>): Promise<string>;
|
|
1
10
|
/**
|
|
2
11
|
* Renders a JS HTML file and adds all includes to state object.
|
|
3
12
|
*
|
|
@@ -8,6 +17,15 @@
|
|
|
8
17
|
* @returns {Promise<string>} HTML with includes available (appended to state)
|
|
9
18
|
*/
|
|
10
19
|
export function renderView(filePath: string, req: import("express").Request, res: import("express").Response, data?: Record<string, any>): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Renders a JS HTML file and adds all includes to state object.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} filePath - The path to html file
|
|
24
|
+
* @param {object} data - Data to be made available in view
|
|
25
|
+
* @param {Record<string, any>} [customState]
|
|
26
|
+
* @returns {Promise<string>} HTML with includes available (appended to state)
|
|
27
|
+
*/
|
|
28
|
+
export function buildViewHtml(filePath: string, data?: object, customState?: Record<string, any>): Promise<string>;
|
|
11
29
|
/**
|
|
12
30
|
* Template literal that supports string
|
|
13
31
|
* interpolating in passed HTML.
|
package/src/index.js
CHANGED
|
@@ -54,7 +54,7 @@ let buildRequestState;
|
|
|
54
54
|
let notFoundView = `404/index`;
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
57
|
+
* Generates the HTML for a view function inside of the passed file.
|
|
58
58
|
*
|
|
59
59
|
* @private
|
|
60
60
|
* @param {string} path - The path to html file
|
|
@@ -62,7 +62,7 @@ let notFoundView = `404/index`;
|
|
|
62
62
|
* @param {object} [state] - Page-level attributes
|
|
63
63
|
* @returns {Promise<string>} HTML
|
|
64
64
|
*/
|
|
65
|
-
async function
|
|
65
|
+
async function buildView(path, data, state) {
|
|
66
66
|
const { view } = await import(path);
|
|
67
67
|
const rendered = view(data, state);
|
|
68
68
|
let html = '';
|
|
@@ -72,6 +72,19 @@ async function renderFileTemplate(path, data, state) {
|
|
|
72
72
|
return html;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Generates HTML for the provided view and adds all includes to state object.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} filePath - The path to html file
|
|
79
|
+
* @param {import('express').Request} req - The request to build the state from
|
|
80
|
+
* @param {Record<string, any>} [data] - Data provided to the view
|
|
81
|
+
* @returns {Promise<string>} HTML with includes available (appended to state)
|
|
82
|
+
*/
|
|
83
|
+
export async function buildStateViewHtml(filePath, req, data = {}) {
|
|
84
|
+
const requestState = buildRequestState ? await buildRequestState(req) : {};
|
|
85
|
+
return await buildViewHtml(filePath, data, requestState);
|
|
86
|
+
}
|
|
87
|
+
|
|
75
88
|
/**
|
|
76
89
|
* Renders a JS HTML file and adds all includes to state object.
|
|
77
90
|
*
|
|
@@ -82,8 +95,8 @@ async function renderFileTemplate(path, data, state) {
|
|
|
82
95
|
* @returns {Promise<string>} HTML with includes available (appended to state)
|
|
83
96
|
*/
|
|
84
97
|
export async function renderView(filePath, req, res, data = {}) {
|
|
85
|
-
const
|
|
86
|
-
|
|
98
|
+
const html = await buildStateViewHtml(filePath, req, data);
|
|
99
|
+
res.set('Content-Type', 'text/html');
|
|
87
100
|
res.send(html);
|
|
88
101
|
return html;
|
|
89
102
|
}
|
|
@@ -96,7 +109,7 @@ export async function renderView(filePath, req, res, data = {}) {
|
|
|
96
109
|
* @param {Record<string, any>} [customState]
|
|
97
110
|
* @returns {Promise<string>} HTML with includes available (appended to state)
|
|
98
111
|
*/
|
|
99
|
-
async function buildViewHtml(filePath, data = {}, customState = {}) {
|
|
112
|
+
export async function buildViewHtml(filePath, data = {}, customState = {}) {
|
|
100
113
|
/**
|
|
101
114
|
* @type {Record<string, any>}
|
|
102
115
|
*/
|
|
@@ -105,9 +118,9 @@ async function buildViewHtml(filePath, data = {}, customState = {}) {
|
|
|
105
118
|
const includeFilePaths = await glob(`${includesDir}/*.js`);
|
|
106
119
|
for await (const includePath of includeFilePaths) {
|
|
107
120
|
const key = basename(includePath, '.js');
|
|
108
|
-
state.includes[key] = await
|
|
121
|
+
state.includes[key] = await buildView(includePath, data, state);
|
|
109
122
|
}
|
|
110
|
-
return await
|
|
123
|
+
return await buildView(`${viewsDir}/${filePath}.js`, data, state);
|
|
111
124
|
}
|
|
112
125
|
|
|
113
126
|
/**
|