@wxn0brp/falcon-frame 0.6.1 → 0.6.3

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
@@ -4,7 +4,7 @@ import { renderHTML } from "./render.js";
4
4
  import { FFResponse } from "./res.js";
5
5
  import { Router } from "./router.js";
6
6
  import type { BeforeHandleRequest, CombinedVars, EngineCallback, ErrorHandler, FFOpts, FFRequest, RouteHandler, ValidationErrorFormatter } from "./types.js";
7
- export declare class FalconFrame<Vars extends Record<string, any> = Record<string, any>> extends Router {
7
+ export declare class FalconFrame<Vars extends Record<string, any> = {}> extends Router {
8
8
  logger: Logger;
9
9
  bodyParsers: RouteHandler[];
10
10
  vars: CombinedVars<Vars>;
@@ -19,9 +19,9 @@ export declare class FalconFrame<Vars extends Record<string, any> = Record<strin
19
19
  listen(port: number | string, callback?: (() => void) | boolean, beforeHandleRequest?: BeforeHandleRequest): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
20
20
  getApp(beforeHandleRequest?: BeforeHandleRequest): (req: any, res: any) => Promise<void>;
21
21
  engine(ext: string, callback: EngineCallback): this;
22
- setVar(key: keyof CombinedVars<Vars>, value: typeof this.vars[keyof CombinedVars<Vars>]): void;
23
- set(key: keyof CombinedVars<Vars>, value: typeof this.vars[keyof CombinedVars<Vars>]): void;
24
- getVar(key: keyof CombinedVars<Vars>): typeof this.vars[keyof CombinedVars<Vars>];
22
+ setVar<K extends keyof CombinedVars<Vars>>(key: K, value: CombinedVars<Vars>[K]): this;
23
+ set<K extends keyof CombinedVars<Vars>>(key: K, value: CombinedVars<Vars>[K]): this;
24
+ getVar<K extends keyof CombinedVars<Vars>>(key: K): CombinedVars<Vars>[K];
25
25
  /**
26
26
  * Sets the allowed origins for CORS.
27
27
  * This method is a shortcut that simplifies CORS configuration
@@ -44,6 +44,6 @@ export declare class FalconFrame<Vars extends Record<string, any> = Record<strin
44
44
  }
45
45
  export default FalconFrame;
46
46
  export * as Helpers from "./helpers.js";
47
- export { FFOpts as Opts } from "./types.js";
47
+ export type { FFOpts as Opts } from "./types.js";
48
48
  export { validateBody } from "./valid.js";
49
49
  export { FFRequest, FFResponse, renderHTML, RouteHandler, Router };
package/dist/index.js CHANGED
@@ -102,15 +102,13 @@ export class FalconFrame extends Router {
102
102
  return this;
103
103
  }
104
104
  setVar(key, value) {
105
- // @ts-ignore
106
105
  this.vars[key] = value;
106
+ return this;
107
107
  }
108
108
  set(key, value) {
109
- // @ts-ignore
110
- this.vars[key] = value;
109
+ return this.setVar(key, value);
111
110
  }
112
111
  getVar(key) {
113
- // @ts-ignore
114
112
  return this.vars[key];
115
113
  }
116
114
  /**
package/dist/render.d.ts CHANGED
@@ -1,2 +1,5 @@
1
1
  import FalconFrame from "./index.js";
2
- export declare function renderHTML(templatePath: string, data?: Record<string, any>, renderedPaths?: string[], FF?: FalconFrame): string;
2
+ export interface RenderOptions {
3
+ noLayout?: boolean;
4
+ }
5
+ export declare function renderHTML(templatePath: string, data?: Record<string, any>, renderedPaths?: string[], FF?: FalconFrame, opts?: RenderOptions): string;
package/dist/render.js CHANGED
@@ -1,27 +1,35 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- export function renderHTML(templatePath, data = {}, renderedPaths = [], FF) {
3
+ export function renderHTML(templatePath, data = {}, renderedPaths = [], FF, opts = {}) {
4
4
  try {
5
5
  const realPath = path.resolve(templatePath);
6
6
  if (renderedPaths.includes(realPath))
7
7
  return `<!-- Circular dependency detected: tried to render ${templatePath} again -->`;
8
- const FFData = FF && FF.getVar("render data");
9
- if (FFData) {
10
- data = {
11
- ...FFData,
12
- ...data,
13
- };
14
- }
15
8
  let template = fs.readFileSync(templatePath, "utf8");
9
+ // Loading internal data, e.g. <!-- data { "title": "My title" } -->
10
+ const templateDataMatch = template.match(/<!--\s*data\s*(\{.*?\})\s*-->/s);
11
+ let templateData = {};
12
+ if (templateDataMatch) {
13
+ try {
14
+ templateData = JSON.parse(templateDataMatch[1]);
15
+ template = template.replace(templateDataMatch[0], "");
16
+ }
17
+ catch (err) {
18
+ template = template.replace(templateDataMatch[0], "<!-- Invalid template data -->");
19
+ }
20
+ }
21
+ const FFData = FF && FF.getVar("render data");
22
+ data = {
23
+ ...(FFData || {}),
24
+ ...templateData,
25
+ ...data,
26
+ };
16
27
  // Inserting data, e.g. {{name}}
17
28
  template = template.replace(/{{(.*?)}}/g, (_, key) => data[key.trim()] || "");
18
29
  // Loading partials, e.g. <!-- include header -->
19
30
  template = template.replace(/<!--\s*include\s*(.*?)\s*-->/g, (_, partialName) => {
20
31
  const partialPath = path.join(path.dirname(templatePath), partialName + ".html");
21
- return renderHTML(partialPath, data, [
22
- ...renderedPaths,
23
- realPath,
24
- ], FF);
32
+ return renderHTML(partialPath, data, [...renderedPaths, realPath], FF, { noLayout: true });
25
33
  });
26
34
  // Loading files, e.g. /* include style.css */
27
35
  template = template.replace(/\/\*\s*include\s*(.*?)\s*\*\//g, (_, fileName) => {
@@ -35,15 +43,16 @@ export function renderHTML(templatePath, data = {}, renderedPaths = [], FF) {
35
43
  });
36
44
  // Layout
37
45
  const FFLayout = FF && FF.getVar("layout");
38
- if (FFLayout) {
46
+ if (!opts.noLayout && FFLayout) {
39
47
  const hasHtmlStructure = /<\s*html|<\s*body/i.test(template);
40
48
  const forceLayout = /<!--\s*force-layout\s*-->/.test(template);
41
49
  const forceNoLayout = /<!--\s*force-no-layout\s*-->/.test(template);
50
+ template = template.replace(/<!--\s*layout\s*-->|<!--\s*force-layout\s*-->|<!--\s*force-no-layout\s*-->/g, "");
42
51
  if (hasHtmlStructure && !forceLayout)
43
52
  return template;
44
53
  if (!hasHtmlStructure && forceNoLayout)
45
54
  return template;
46
- return renderHTML(FFLayout, { ...data, body: template }, [...renderedPaths, realPath], FF);
55
+ return renderHTML(FFLayout, { ...data, body: template }, [...renderedPaths, realPath], FF, { noLayout: true });
47
56
  }
48
57
  return template;
49
58
  }
package/package.json CHANGED
@@ -1,16 +1,28 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",
7
7
  "license": "MIT",
8
8
  "type": "module",
9
+ "description": "Lightweight modular TypeScript web framework",
9
10
  "homepage": "https://github.com/wxn0brP/FalconFrame",
10
11
  "repository": {
11
12
  "type": "git",
12
13
  "url": "https://github.com/wxn0brP/FalconFrame.git"
13
14
  },
15
+ "keywords": [
16
+ "http",
17
+ "web",
18
+ "framework",
19
+ "typescript",
20
+ "modular",
21
+ "lightweight",
22
+ "middleware",
23
+ "middleware-first",
24
+ "middleware-chain"
25
+ ],
14
26
  "devDependencies": {
15
27
  "@types/node": "*",
16
28
  "tsc-alias": "*",