elegance-js 2.1.4 → 2.1.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/global.d.ts CHANGED
@@ -37,6 +37,8 @@ declare global {
37
37
  type Endpoint = (req: IncomingMessage, res: ServerResponse) => Promise<void>;
38
38
  /** The type for middleware functions in middleware.ts files. */
39
39
  type Middleware = (req: IncomingMessage, res: ServerResponse, next: () => void) => Promise<void>;
40
+ /** On dynamic pages, the requestHook, if present, shall be called by the server, before serving the page. */
41
+ type RequestHook = (req: IncomingMessage, res: ServerResponse) => Promise<boolean>;
40
42
  /** The type for const layout in layout.ts files. */
41
43
  type Layout = ((child: Child) => (AnyBuiltElement | Promise<AnyBuiltElement>));
42
44
  /** The type for const metadata in layout.ts files. */
@@ -1,6 +1,7 @@
1
+ import type { IncomingMessage, ServerResponse } from "http";
1
2
  export declare const PAGE_MAP: Map<string, PageInformation>;
2
3
  export declare const LAYOUT_MAP: Map<string, LayoutInformation>;
3
4
  export declare const processPageElements: (element: Child, objectAttributes: Array<any>, recursionLevel: number, stack?: any[]) => Child;
4
- export declare const buildDynamicPage: (DIST_DIR: string, directory: string, pageInfo: PageInformation) => Promise<{
5
+ export declare const buildDynamicPage: (DIST_DIR: string, directory: string, pageInfo: PageInformation, req: IncomingMessage, res: ServerResponse) => Promise<false | {
5
6
  resultHTML: any;
6
7
  }>;
@@ -436,11 +436,17 @@ async function handlePageRequest(root, pathname, req, res, DIST_DIR2, pageInfo)
436
436
  }
437
437
  if (isDynamic) {
438
438
  try {
439
- const { resultHTML } = await buildDynamicPage(
439
+ const result = await buildDynamicPage(
440
440
  DIST_DIR2,
441
441
  pathname,
442
- pageInfo
442
+ pageInfo,
443
+ req2,
444
+ res2
443
445
  );
446
+ if (result === false) {
447
+ return;
448
+ }
449
+ const { resultHTML } = result;
444
450
  if (resultHTML === false) {
445
451
  return;
446
452
  }
@@ -1358,7 +1364,7 @@ var buildPage = async (DIST_DIR2, directory, filePath, name) => {
1358
1364
  );
1359
1365
  return sendHardReloadInstruction === true;
1360
1366
  };
1361
- var buildDynamicPage = async (DIST_DIR2, directory, pageInfo) => {
1367
+ var buildDynamicPage = async (DIST_DIR2, directory, pageInfo, req, res) => {
1362
1368
  directory = directory === "/" ? "" : directory;
1363
1369
  const filePath = pageInfo.filePath;
1364
1370
  initializeState();
@@ -1376,8 +1382,16 @@ var buildDynamicPage = async (DIST_DIR2, directory, pageInfo) => {
1376
1382
  metadata: pageMetadata,
1377
1383
  isDynamic,
1378
1384
  shippedModules: shippedModules2,
1379
- ignoreLayout
1385
+ ignoreLayout,
1386
+ requestHook
1380
1387
  } = await import("file://" + filePath);
1388
+ if (requestHook) {
1389
+ const hook = requestHook;
1390
+ const doContinue = await hook(req, res);
1391
+ if (!doContinue) {
1392
+ return false;
1393
+ }
1394
+ }
1381
1395
  if (shippedModules2 !== void 0) {
1382
1396
  modules = shippedModules2;
1383
1397
  }
@@ -997,7 +997,7 @@ var buildPage = async (DIST_DIR2, directory, filePath, name) => {
997
997
  );
998
998
  return sendHardReloadInstruction === true;
999
999
  };
1000
- var buildDynamicPage = async (DIST_DIR2, directory, pageInfo) => {
1000
+ var buildDynamicPage = async (DIST_DIR2, directory, pageInfo, req, res) => {
1001
1001
  directory = directory === "/" ? "" : directory;
1002
1002
  const filePath = pageInfo.filePath;
1003
1003
  initializeState();
@@ -1015,8 +1015,16 @@ var buildDynamicPage = async (DIST_DIR2, directory, pageInfo) => {
1015
1015
  metadata: pageMetadata,
1016
1016
  isDynamic,
1017
1017
  shippedModules: shippedModules2,
1018
- ignoreLayout
1018
+ ignoreLayout,
1019
+ requestHook
1019
1020
  } = await import("file://" + filePath);
1021
+ if (requestHook) {
1022
+ const hook = requestHook;
1023
+ const doContinue = await hook(req, res);
1024
+ if (!doContinue) {
1025
+ return false;
1026
+ }
1027
+ }
1020
1028
  if (shippedModules2 !== void 0) {
1021
1029
  modules = shippedModules2;
1022
1030
  }
@@ -1313,11 +1321,17 @@ async function handlePageRequest(root, pathname, req, res, DIST_DIR2, pageInfo)
1313
1321
  }
1314
1322
  if (isDynamic) {
1315
1323
  try {
1316
- const { resultHTML } = await buildDynamicPage(
1324
+ const result = await buildDynamicPage(
1317
1325
  DIST_DIR2,
1318
1326
  pathname,
1319
- pageInfo
1327
+ pageInfo,
1328
+ req2,
1329
+ res2
1320
1330
  );
1331
+ if (result === false) {
1332
+ return;
1333
+ }
1334
+ const { resultHTML } = result;
1321
1335
  if (resultHTML === false) {
1322
1336
  return;
1323
1337
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elegance-js",
3
- "version": "2.1.4",
3
+ "version": "2.1.5",
4
4
  "description": "Web-Framework",
5
5
  "type": "module",
6
6
  "bin": {