@wxn0brp/falcon-frame 0.0.4 → 0.0.5

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 CHANGED
@@ -2,6 +2,7 @@ import { Logger, LoggerOptions } from "@wxn0brp/lucerna-log";
2
2
  import http from "http";
3
3
  import { FFResponse } from "./res.js";
4
4
  import { FFRequest, Method, Middleware, RouteHandler } from "./types.js";
5
+ import { renderHTML } from "./render.js";
5
6
  export declare class FalconFrame {
6
7
  middlewares: Middleware[];
7
8
  logger: Logger;
@@ -18,4 +19,4 @@ export declare class FalconFrame {
18
19
  getApp(): (req: any, res: any) => void;
19
20
  }
20
21
  export default FalconFrame;
21
- export { FFResponse, FFRequest, RouteHandler, };
22
+ export { FFResponse, FFRequest, RouteHandler, renderHTML, };
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { handleStaticFiles } from "./helpers.js";
4
4
  import { handleRequest } from "./req.js";
5
5
  import { FFResponse } from "./res.js";
6
6
  import { FFRequest } from "./types.js";
7
+ import { renderHTML } from "./render.js";
7
8
  export class FalconFrame {
8
9
  middlewares = [];
9
10
  logger;
@@ -66,4 +67,4 @@ export class FalconFrame {
66
67
  }
67
68
  }
68
69
  export default FalconFrame;
69
- export { FFResponse, FFRequest, };
70
+ export { FFResponse, FFRequest, renderHTML, };
@@ -0,0 +1,5 @@
1
+ interface RenderData {
2
+ [key: string]: string;
3
+ }
4
+ export declare function renderHTML(templatePath: string, data: RenderData): string;
5
+ export {};
package/dist/render.js ADDED
@@ -0,0 +1,16 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ export function renderHTML(templatePath, data) {
4
+ let template = fs.readFileSync(templatePath, "utf8");
5
+ // Inserting data, e.g. {{name}}
6
+ template = template.replace(/{{(.*?)}}/g, (_, key) => data[key.trim()] || "");
7
+ // Loading partials, e.g. <!-- include header -->
8
+ template = template.replace(/<!--\s*include\s*(.*?)\s*-->/g, (_, partialName) => {
9
+ const partialPath = path.join(path.dirname(templatePath), partialName + ".html");
10
+ if (fs.existsSync(partialPath))
11
+ return renderHTML(partialPath, data);
12
+ else
13
+ return `<!-- Partial "${partialName}" (${partialPath.replace(process.cwd() + "/", "")}) not found -->`;
14
+ });
15
+ return template;
16
+ }
package/dist/res.d.ts CHANGED
@@ -7,4 +7,5 @@ export declare class FFResponse extends http.ServerResponse {
7
7
  status(code: number): this;
8
8
  redirect(url: string): this;
9
9
  sendFile(filePath: string): this;
10
+ render(templatePath: string, data: any): this;
10
11
  }
package/dist/res.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import http from "http";
2
2
  import { getContentType } from "./helpers.js";
3
3
  import { createReadStream } from "fs";
4
+ import { renderHTML } from "./render.js";
4
5
  export class FFResponse extends http.ServerResponse {
5
6
  _ended = false;
6
7
  json(data) {
@@ -36,4 +37,9 @@ export class FFResponse extends http.ServerResponse {
36
37
  createReadStream(filePath).pipe(this);
37
38
  return this;
38
39
  }
40
+ render(templatePath, data) {
41
+ this.setHeader("Content-Type", "text/html");
42
+ this.end(renderHTML(templatePath, data));
43
+ return this;
44
+ }
39
45
  }
package/dist/types.d.ts CHANGED
@@ -9,7 +9,7 @@ export interface Cookies {
9
9
  [key: string]: string;
10
10
  }
11
11
  export interface Query {
12
- [key: string]: string | string[];
12
+ [key: string]: string;
13
13
  }
14
14
  export interface Body {
15
15
  [key: string]: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",