@wxn0brp/falcon-frame 0.6.0 → 0.6.2

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
@@ -1,33 +1,27 @@
1
- import { Logger, LoggerOptions } from "@wxn0brp/lucerna-log";
1
+ import { Logger } from "@wxn0brp/lucerna-log";
2
2
  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, EngineCallback, ErrorHandler, FFRequest, RouteHandler, ValidationErrorFormatter } from "./types.js";
7
- export interface Opts {
8
- loggerOpts?: LoggerOptions;
9
- bodyLimit?: string;
10
- disableJsonParser?: boolean;
11
- disableUrlencodedParser?: boolean;
12
- }
13
- export declare class FalconFrame<Vars extends Record<string, any> = any> extends Router {
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 {
14
8
  logger: Logger;
15
9
  bodyParsers: RouteHandler[];
16
- vars: Vars;
17
- opts: Opts;
10
+ vars: CombinedVars<Vars>;
11
+ opts: FFOpts;
18
12
  engines: Record<string, EngineCallback>;
19
13
  _400_formatter: ValidationErrorFormatter;
20
14
  _404: RouteHandler;
21
15
  _413: RouteHandler;
22
16
  _500: ErrorHandler;
23
- constructor(opts?: Partial<Opts>);
17
+ constructor(opts?: Partial<FFOpts>);
24
18
  addBodyParser(parser: RouteHandler): this;
25
19
  listen(port: number | string, callback?: (() => void) | boolean, beforeHandleRequest?: BeforeHandleRequest): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
26
20
  getApp(beforeHandleRequest?: BeforeHandleRequest): (req: any, res: any) => Promise<void>;
27
21
  engine(ext: string, callback: EngineCallback): this;
28
- setVar(key: keyof Vars, value: typeof this.vars[keyof Vars]): void;
29
- set(key: keyof Vars, value: typeof this.vars[keyof Vars]): void;
30
- getVar(key: string): typeof this.vars[keyof Vars];
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>];
31
25
  /**
32
26
  * Sets the allowed origins for CORS.
33
27
  * This method is a shortcut that simplifies CORS configuration
@@ -50,5 +44,6 @@ export declare class FalconFrame<Vars extends Record<string, any> = any> extends
50
44
  }
51
45
  export default FalconFrame;
52
46
  export * as Helpers from "./helpers.js";
47
+ export type { FFOpts as Opts } from "./types.js";
53
48
  export { validateBody } from "./valid.js";
54
49
  export { FFRequest, FFResponse, renderHTML, RouteHandler, Router };
package/dist/index.js CHANGED
@@ -92,7 +92,7 @@ export class FalconFrame extends Router {
92
92
  if (result || res._ended)
93
93
  return;
94
94
  }
95
- await handleRequest(req, res, this);
95
+ handleRequest(req, res, this);
96
96
  };
97
97
  }
98
98
  engine(ext, callback) {
package/dist/render.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import FalconFrame from "./index.js";
2
- interface RenderData {
3
- [key: string]: string;
2
+ export interface RenderOptions {
3
+ noLayout?: boolean;
4
4
  }
5
- export declare function renderHTML(templatePath: string, data: RenderData, renderedPaths?: string[], FF?: FalconFrame): string;
6
- export {};
5
+ export declare function renderHTML(templatePath: string, data?: Record<string, any>, renderedPaths?: string[], FF?: FalconFrame, opts?: RenderOptions): string;
package/dist/render.js CHANGED
@@ -1,26 +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
- if (FF && FF.vars["render data"]) {
9
- data = {
10
- ...FF.vars["render data"],
11
- ...data,
12
- };
13
- }
14
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
+ };
15
27
  // Inserting data, e.g. {{name}}
16
28
  template = template.replace(/{{(.*?)}}/g, (_, key) => data[key.trim()] || "");
17
29
  // Loading partials, e.g. <!-- include header -->
18
30
  template = template.replace(/<!--\s*include\s*(.*?)\s*-->/g, (_, partialName) => {
19
31
  const partialPath = path.join(path.dirname(templatePath), partialName + ".html");
20
- return renderHTML(partialPath, data, [
21
- ...renderedPaths,
22
- realPath,
23
- ], FF);
32
+ return renderHTML(partialPath, data, [...renderedPaths, realPath], FF, { noLayout: true });
24
33
  });
25
34
  // Loading files, e.g. /* include style.css */
26
35
  template = template.replace(/\/\*\s*include\s*(.*?)\s*\*\//g, (_, fileName) => {
@@ -33,15 +42,17 @@ export function renderHTML(templatePath, data, renderedPaths = [], FF) {
33
42
  }
34
43
  });
35
44
  // Layout
36
- if (FF && FF.vars["layout"]) {
45
+ const FFLayout = FF && FF.getVar("layout");
46
+ if (!opts.noLayout && FFLayout) {
37
47
  const hasHtmlStructure = /<\s*html|<\s*body/i.test(template);
38
48
  const forceLayout = /<!--\s*force-layout\s*-->/.test(template);
39
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, "");
40
51
  if (hasHtmlStructure && !forceLayout)
41
52
  return template;
42
53
  if (!hasHtmlStructure && forceNoLayout)
43
54
  return template;
44
- return renderHTML(FF.vars["layout"], { ...data, body: template }, [...renderedPaths, realPath], FF);
55
+ return renderHTML(FFLayout, { ...data, body: template }, [...renderedPaths, realPath], FF, { noLayout: true });
45
56
  }
46
57
  return template;
47
58
  }
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { LoggerOptions } from "@wxn0brp/lucerna-log";
1
2
  import FalconFrame from "./index.js";
2
3
  import { FFResponse } from "./res.js";
3
4
  import http from "http";
@@ -73,3 +74,16 @@ export interface RenderOptions {
73
74
  notAppendExt?: boolean;
74
75
  notShareFF?: boolean;
75
76
  }
77
+ export interface FFOpts {
78
+ loggerOpts?: LoggerOptions;
79
+ bodyLimit?: string;
80
+ disableJsonParser?: boolean;
81
+ disableUrlencodedParser?: boolean;
82
+ }
83
+ export type FFVars = {
84
+ "render data": Record<string, any>;
85
+ "view engine": string;
86
+ "views": string;
87
+ "layout": string;
88
+ };
89
+ export type CombinedVars<ExtraVars> = ExtraVars & FFVars;
package/package.json CHANGED
@@ -1,16 +1,28 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
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": "*",