@wxn0brp/falcon-frame 0.9.5 → 0.9.7
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/dist/index.d.ts +6 -6
- package/dist/index.js +1 -1
- package/dist/render.d.ts +2 -0
- package/dist/render.js +50 -3
- package/dist/types.d.ts +2 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import http from "http";
|
|
|
3
3
|
import { renderHTML } from "./render.js";
|
|
4
4
|
import { FFResponse } from "./res.js";
|
|
5
5
|
import { Router } from "./router.js";
|
|
6
|
-
import type { BeforeHandleRequest, BodyParserEntry, CombinedVars, EngineCallback, ErrorHandler, FFOpts, FFRequest, ParseBodyFunction, RouteHandler, StandardBodyParserOptions, ValidationErrorFormatter } from "./types.js";
|
|
6
|
+
import type { BeforeHandleRequest, BodyParserEntry, CombinedVars, EngineCallback, ErrorHandler, FFOpts, FFRequest, FinalHandler, ParseBodyFunction, RouteHandler, StandardBodyParserOptions, ValidationErrorFormatter } from "./types.js";
|
|
7
7
|
export declare class FalconFrame<Vars extends Record<string, any> = {}> extends Router {
|
|
8
8
|
logger: Logger;
|
|
9
9
|
bodyParsers: Record<string, BodyParserEntry>;
|
|
@@ -11,8 +11,8 @@ export declare class FalconFrame<Vars extends Record<string, any> = {}> extends
|
|
|
11
11
|
opts: FFOpts;
|
|
12
12
|
engines: Record<string, EngineCallback>;
|
|
13
13
|
_400_formatter: ValidationErrorFormatter;
|
|
14
|
-
_404:
|
|
15
|
-
_413:
|
|
14
|
+
_404: FinalHandler;
|
|
15
|
+
_413: FinalHandler;
|
|
16
16
|
_500: ErrorHandler;
|
|
17
17
|
constructor(opts?: Partial<FFOpts>);
|
|
18
18
|
addBodyParser(contentType: string, parser: ParseBodyFunction, opts?: StandardBodyParserOptions): this;
|
|
@@ -38,8 +38,8 @@ export declare class FalconFrame<Vars extends Record<string, any> = {}> extends
|
|
|
38
38
|
*/
|
|
39
39
|
l(port: number): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
40
40
|
set400Formatter(formatter: ValidationErrorFormatter): void;
|
|
41
|
-
set404(handler:
|
|
42
|
-
set413(handler:
|
|
41
|
+
set404(handler: FinalHandler): void;
|
|
42
|
+
set413(handler: FinalHandler): void;
|
|
43
43
|
set500(handler: ErrorHandler): void;
|
|
44
44
|
}
|
|
45
45
|
export default FalconFrame;
|
|
@@ -48,4 +48,4 @@ export type { OfflineRequest, OfflineResponse } from "./engine.js";
|
|
|
48
48
|
export * as Helpers from "./helpers.js";
|
|
49
49
|
export type { FFOpts as Opts } from "./types.js";
|
|
50
50
|
export { validateBody } from "./valid.js";
|
|
51
|
-
export { FFRequest, FFResponse, renderHTML, RouteHandler, Router };
|
|
51
|
+
export { FFRequest, FFResponse, FinalHandler, renderHTML, RouteHandler, Router, };
|
package/dist/index.js
CHANGED
package/dist/render.d.ts
CHANGED
|
@@ -10,3 +10,5 @@ export interface RenderHTMLOptions {
|
|
|
10
10
|
_renderedPaths?: string[];
|
|
11
11
|
}
|
|
12
12
|
export declare function renderHTML(options: RenderHTMLOptions): string;
|
|
13
|
+
export declare function interpolate(template: string, data: Record<string, any>): string;
|
|
14
|
+
export declare function expandLoops(template: string, data: Record<string, any>): string;
|
package/dist/render.js
CHANGED
|
@@ -28,10 +28,21 @@ export function renderHTML(options) {
|
|
|
28
28
|
...templateData,
|
|
29
29
|
...data,
|
|
30
30
|
};
|
|
31
|
+
// Preliminary loop rendering
|
|
32
|
+
template = expandLoops(template, data);
|
|
31
33
|
// Inserting data, e.g. {{name}}
|
|
32
|
-
template = template
|
|
34
|
+
template = interpolate(template, data);
|
|
33
35
|
// Loading partials, e.g. <!-- include header -->
|
|
34
|
-
template = template.replace(/<!--\s*include\s
|
|
36
|
+
template = template.replace(/<!--\s*include\s+([^\s{]+)(?:\s+(\{[\s\S]*?\}))?\s*-->/g, (_, partialName, json) => {
|
|
37
|
+
let includeData = {};
|
|
38
|
+
if (json) {
|
|
39
|
+
try {
|
|
40
|
+
includeData = JSON.parse(json);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
return "<!-- Invalid include data -->";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
35
46
|
const partialPath = path.join(path.dirname(templatePath), partialName + ".html");
|
|
36
47
|
return renderHTML({
|
|
37
48
|
templatePath: partialPath,
|
|
@@ -40,7 +51,10 @@ export function renderHTML(options) {
|
|
|
40
51
|
realPath,
|
|
41
52
|
],
|
|
42
53
|
noLayout: true,
|
|
43
|
-
data
|
|
54
|
+
data: {
|
|
55
|
+
...data,
|
|
56
|
+
...includeData,
|
|
57
|
+
},
|
|
44
58
|
FF,
|
|
45
59
|
FFVar,
|
|
46
60
|
});
|
|
@@ -87,3 +101,36 @@ export function renderHTML(options) {
|
|
|
87
101
|
return `<-- Template ${options.templatePath} has errors -->`;
|
|
88
102
|
}
|
|
89
103
|
}
|
|
104
|
+
function resolvePath(data, key) {
|
|
105
|
+
let value = data;
|
|
106
|
+
for (const part of key.split(".")) {
|
|
107
|
+
if (value == null)
|
|
108
|
+
return undefined;
|
|
109
|
+
value = value[part];
|
|
110
|
+
}
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
export function interpolate(template, data) {
|
|
114
|
+
return template.replace(/{{(.*?)}}/g, (_, key) => resolvePath(data, key.trim()) ?? "");
|
|
115
|
+
}
|
|
116
|
+
export function expandLoops(template, data) {
|
|
117
|
+
return template
|
|
118
|
+
.replace(/<!--\s*each\s+(\w+)\s+as\s+(\w+)\s*-->([\s\S]*?)<!--\s*\/each\s*-->/g, (_, key, itemVar, body) => (resolvePath(data, key) ?? [])
|
|
119
|
+
.map((value, index) => interpolate(body, {
|
|
120
|
+
...data,
|
|
121
|
+
[itemVar]: value,
|
|
122
|
+
i: index,
|
|
123
|
+
}))
|
|
124
|
+
.join(""))
|
|
125
|
+
.replace(/<!--\s*range\s+(\w+)\s*-->([\s\S]*?)<!--\s*\/range\s*-->/g, (_, key, body) => {
|
|
126
|
+
const range = Array.from({
|
|
127
|
+
length: +key,
|
|
128
|
+
}, (_, i) => i);
|
|
129
|
+
return range
|
|
130
|
+
.map((i) => interpolate(body, {
|
|
131
|
+
...data,
|
|
132
|
+
i,
|
|
133
|
+
}))
|
|
134
|
+
.join("");
|
|
135
|
+
});
|
|
136
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -2,7 +2,8 @@ import { LoggerOptions } from "@wxn0brp/lucerna-log";
|
|
|
2
2
|
import FalconFrame from "./index.js";
|
|
3
3
|
import { FFResponse } from "./res.js";
|
|
4
4
|
import http from "http";
|
|
5
|
-
export type RouteHandler = (req: FFRequest, res: FFResponse, next
|
|
5
|
+
export type RouteHandler = (req: FFRequest, res: FFResponse, next: () => void) => void | any;
|
|
6
|
+
export type FinalHandler = (req: FFRequest, res: FFResponse) => void | any;
|
|
6
7
|
export type ErrorHandler = (err: Error, req: FFRequest, res: FFResponse) => void | any;
|
|
7
8
|
export type Method = "get" | "post" | "put" | "delete" | "patch" | "all";
|
|
8
9
|
export interface Params {
|