clear-router 2.1.11 → 2.1.12

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.
@@ -5,10 +5,15 @@ import { H3, H3Event, Middleware as Middleware$1, TypedServerRequest } from "h3"
5
5
  type H3App = Omit<H3['fetch'], 'fetch'> & {
6
6
  fetch: (request: TypedServerRequest) => Promise<Response>;
7
7
  };
8
+ type HttpRequest = H3Event['req'] & {
9
+ getBody: () => Record<string, any>;
10
+ };
8
11
  /**
9
12
  * HTTP context passed to route handlers
10
13
  */
11
- type HttpContext$1 = H3Event & {};
14
+ type HttpContext$1 = Omit<H3Event, 'req'> & {
15
+ req: HttpRequest;
16
+ };
12
17
  /**
13
18
  * Route handler function type
14
19
  */
@@ -66,11 +71,14 @@ declare class ClearRequest<X = any, M = Middleware$1 | Middleware> {
66
71
  }
67
72
  //#endregion
68
73
  //#region types/express.d.ts
74
+ interface RequestWithGetBody extends Request {
75
+ getBody: () => Record<string, any>;
76
+ }
69
77
  /**
70
78
  * HTTP context passed to route handlers
71
79
  */
72
80
  interface HttpContext {
73
- req: Request;
81
+ req: RequestWithGetBody;
74
82
  res: Response$1;
75
83
  next: NextFunction;
76
84
  }
@@ -5,10 +5,15 @@ import { NextFunction, Request, Response as Response$1 } from "express";
5
5
  type H3App = Omit<H3['fetch'], 'fetch'> & {
6
6
  fetch: (request: TypedServerRequest) => Promise<Response>;
7
7
  };
8
+ type HttpRequest = H3Event['req'] & {
9
+ getBody: () => Record<string, any>;
10
+ };
8
11
  /**
9
12
  * HTTP context passed to route handlers
10
13
  */
11
- type HttpContext$1 = H3Event & {};
14
+ type HttpContext$1 = Omit<H3Event, 'req'> & {
15
+ req: HttpRequest;
16
+ };
12
17
  /**
13
18
  * Route handler function type
14
19
  */
@@ -66,11 +71,14 @@ declare class ClearRequest<X = any, M = Middleware$1 | Middleware> {
66
71
  }
67
72
  //#endregion
68
73
  //#region types/express.d.ts
74
+ interface RequestWithGetBody extends Request {
75
+ getBody: () => Record<string, any>;
76
+ }
69
77
  /**
70
78
  * HTTP context passed to route handlers
71
79
  */
72
80
  interface HttpContext {
73
- req: Request;
81
+ req: RequestWithGetBody;
74
82
  res: Response$1;
75
83
  next: NextFunction;
76
84
  }
@@ -69,6 +69,9 @@ var Router = class Router {
69
69
  const headerKeys = override.headerKeys;
70
70
  if (typeof headerKeys !== "undefined") this.config.methodOverride.headerKeys = (Array.isArray(headerKeys) ? headerKeys : [headerKeys]).map((e) => String(e).trim().toLowerCase()).filter(Boolean);
71
71
  }
72
+ static ensureRequestBodyAccessor(req) {
73
+ if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
74
+ }
72
75
  static resolveMethodOverride(method, headers, body) {
73
76
  if (!this.config.methodOverride?.enabled || method.toLowerCase() !== "post") return null;
74
77
  let override;
@@ -309,11 +312,13 @@ var Router = class Router {
309
312
  throw error;
310
313
  }
311
314
  router[method](route.path, (req, res, next) => {
315
+ Router.ensureRequestBodyAccessor(req);
312
316
  const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
313
317
  if (method === "post" && override && override !== "post") return next("route");
314
318
  return next();
315
319
  }, ...route.middlewares || [], async (req, res, next) => {
316
320
  try {
321
+ Router.ensureRequestBodyAccessor(req);
317
322
  const ctx = {
318
323
  req,
319
324
  res,
@@ -332,11 +337,13 @@ var Router = class Router {
332
337
  "patch",
333
338
  "delete"
334
339
  ].includes(method)) router.post(route.path, (req, res, next) => {
340
+ Router.ensureRequestBodyAccessor(req);
335
341
  if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return next("route");
336
342
  req.method = method.toUpperCase();
337
343
  return next();
338
344
  }, ...route.middlewares || [], async (req, res, next) => {
339
345
  try {
346
+ Router.ensureRequestBodyAccessor(req);
340
347
  const ctx = {
341
348
  req,
342
349
  res,
@@ -355,8 +362,9 @@ var Router = class Router {
355
362
  }
356
363
  static async bindRequestToInstance(ctx, instance, route) {
357
364
  if (!instance) return;
365
+ Router.ensureRequestBodyAccessor(ctx.req);
358
366
  instance.ctx = ctx;
359
- instance.body = ctx.req.body;
367
+ instance.body = ctx.req.getBody();
360
368
  instance.query = ctx.req.query;
361
369
  instance.params = ctx.req.params;
362
370
  instance.clearRequest = new require_Route.ClearRequest({
@@ -1,4 +1,4 @@
1
- import { a as Handler, c as Route, i as RouterConfig, n as ControllerAction, o as HttpContext, r as HttpMethod, s as Middleware, t as ApiResourceMiddleware } from "../basic-DXbqD6cP.cjs";
1
+ import { a as Handler, c as Route, i as RouterConfig, n as ControllerAction, o as HttpContext, r as HttpMethod, s as Middleware, t as ApiResourceMiddleware } from "../basic-C_1O6RVq.cjs";
2
2
  import { Router as Router$1 } from "express";
3
3
 
4
4
  //#region src/express/router.d.ts
@@ -49,6 +49,7 @@ declare class Router {
49
49
  * @returns
50
50
  */
51
51
  static configure(options?: RouterConfig): void;
52
+ private static ensureRequestBodyAccessor;
52
53
  private static resolveMethodOverride;
53
54
  /**
54
55
  * Add a route with specified HTTP methods, path, handler, and middlewares
@@ -1,4 +1,4 @@
1
- import { a as Handler, c as Route, i as RouterConfig, n as ControllerAction, o as HttpContext, r as HttpMethod, s as Middleware, t as ApiResourceMiddleware } from "../basic-vvrFwa_Y.mjs";
1
+ import { a as Handler, c as Route, i as RouterConfig, n as ControllerAction, o as HttpContext, r as HttpMethod, s as Middleware, t as ApiResourceMiddleware } from "../basic-cLeny2Zk.mjs";
2
2
  import { Router as Router$1 } from "express";
3
3
 
4
4
  //#region src/express/router.d.ts
@@ -49,6 +49,7 @@ declare class Router {
49
49
  * @returns
50
50
  */
51
51
  static configure(options?: RouterConfig): void;
52
+ private static ensureRequestBodyAccessor;
52
53
  private static resolveMethodOverride;
53
54
  /**
54
55
  * Add a route with specified HTTP methods, path, handler, and middlewares
@@ -68,6 +68,9 @@ var Router = class Router {
68
68
  const headerKeys = override.headerKeys;
69
69
  if (typeof headerKeys !== "undefined") this.config.methodOverride.headerKeys = (Array.isArray(headerKeys) ? headerKeys : [headerKeys]).map((e) => String(e).trim().toLowerCase()).filter(Boolean);
70
70
  }
71
+ static ensureRequestBodyAccessor(req) {
72
+ if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
73
+ }
71
74
  static resolveMethodOverride(method, headers, body) {
72
75
  if (!this.config.methodOverride?.enabled || method.toLowerCase() !== "post") return null;
73
76
  let override;
@@ -308,11 +311,13 @@ var Router = class Router {
308
311
  throw error;
309
312
  }
310
313
  router[method](route.path, (req, res, next) => {
314
+ Router.ensureRequestBodyAccessor(req);
311
315
  const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
312
316
  if (method === "post" && override && override !== "post") return next("route");
313
317
  return next();
314
318
  }, ...route.middlewares || [], async (req, res, next) => {
315
319
  try {
320
+ Router.ensureRequestBodyAccessor(req);
316
321
  const ctx = {
317
322
  req,
318
323
  res,
@@ -331,11 +336,13 @@ var Router = class Router {
331
336
  "patch",
332
337
  "delete"
333
338
  ].includes(method)) router.post(route.path, (req, res, next) => {
339
+ Router.ensureRequestBodyAccessor(req);
334
340
  if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return next("route");
335
341
  req.method = method.toUpperCase();
336
342
  return next();
337
343
  }, ...route.middlewares || [], async (req, res, next) => {
338
344
  try {
345
+ Router.ensureRequestBodyAccessor(req);
339
346
  const ctx = {
340
347
  req,
341
348
  res,
@@ -354,8 +361,9 @@ var Router = class Router {
354
361
  }
355
362
  static async bindRequestToInstance(ctx, instance, route) {
356
363
  if (!instance) return;
364
+ Router.ensureRequestBodyAccessor(ctx.req);
357
365
  instance.ctx = ctx;
358
- instance.body = ctx.req.body;
366
+ instance.body = ctx.req.getBody();
359
367
  instance.query = ctx.req.query;
360
368
  instance.params = ctx.req.params;
361
369
  instance.clearRequest = new ClearRequest({
package/dist/h3/index.cjs CHANGED
@@ -65,8 +65,17 @@ var Router = class Router {
65
65
  if (typeof headerKeys !== "undefined") this.config.methodOverride.headerKeys = (Array.isArray(headerKeys) ? headerKeys : [headerKeys]).map((e) => String(e).trim().toLowerCase()).filter(Boolean);
66
66
  }
67
67
  static async readBodyCached(ctx) {
68
- if (this.bodyCache.has(ctx)) return this.bodyCache.get(ctx);
69
- const body = await (0, h3.readBody)(ctx) ?? {};
68
+ if (this.bodyCache.has(ctx)) {
69
+ const cached = this.bodyCache.get(ctx);
70
+ ctx.req.getBody = () => cached;
71
+ return cached;
72
+ }
73
+ let body = {};
74
+ if (ctx.req.headers.get("content-type")?.includes("multipart/form-data")) (await ctx.req.formData()).forEach((value, key) => {
75
+ body[key] = value;
76
+ });
77
+ else body = await (0, h3.readBody)(ctx) ?? {};
78
+ ctx.req.getBody = () => body;
70
79
  this.bodyCache.set(ctx, body);
71
80
  return body;
72
81
  }
@@ -1,4 +1,4 @@
1
- import { c as Route, d as HttpContext, f as Middleware, i as RouterConfig, l as H3App, n as ControllerAction, r as HttpMethod, t as ApiResourceMiddleware, u as Handler } from "../basic-DXbqD6cP.cjs";
1
+ import { c as Route, d as HttpContext, f as Middleware, i as RouterConfig, l as H3App, n as ControllerAction, r as HttpMethod, t as ApiResourceMiddleware, u as Handler } from "../basic-C_1O6RVq.cjs";
2
2
  import { H3 } from "h3";
3
3
 
4
4
  //#region src/h3/router.d.ts
@@ -1,4 +1,4 @@
1
- import { c as Route, d as HttpContext, f as Middleware, i as RouterConfig, l as H3App, n as ControllerAction, r as HttpMethod, t as ApiResourceMiddleware, u as Handler } from "../basic-vvrFwa_Y.mjs";
1
+ import { c as Route, d as HttpContext, f as Middleware, i as RouterConfig, l as H3App, n as ControllerAction, r as HttpMethod, t as ApiResourceMiddleware, u as Handler } from "../basic-cLeny2Zk.mjs";
2
2
  import { H3 } from "h3";
3
3
 
4
4
  //#region src/h3/router.d.ts
package/dist/h3/index.mjs CHANGED
@@ -64,8 +64,17 @@ var Router = class Router {
64
64
  if (typeof headerKeys !== "undefined") this.config.methodOverride.headerKeys = (Array.isArray(headerKeys) ? headerKeys : [headerKeys]).map((e) => String(e).trim().toLowerCase()).filter(Boolean);
65
65
  }
66
66
  static async readBodyCached(ctx) {
67
- if (this.bodyCache.has(ctx)) return this.bodyCache.get(ctx);
68
- const body = await readBody(ctx) ?? {};
67
+ if (this.bodyCache.has(ctx)) {
68
+ const cached = this.bodyCache.get(ctx);
69
+ ctx.req.getBody = () => cached;
70
+ return cached;
71
+ }
72
+ let body = {};
73
+ if (ctx.req.headers.get("content-type")?.includes("multipart/form-data")) (await ctx.req.formData()).forEach((value, key) => {
74
+ body[key] = value;
75
+ });
76
+ else body = await readBody(ctx) ?? {};
77
+ ctx.req.getBody = () => body;
69
78
  this.bodyCache.set(ctx, body);
70
79
  return body;
71
80
  }
package/dist/index.d.cts CHANGED
@@ -2,10 +2,15 @@ import { NextFunction, Request, Response as Response$1 } from "express";
2
2
  import { H3Event, Middleware as Middleware$1 } from "h3";
3
3
 
4
4
  //#region types/h3.d.ts
5
+ type HttpRequest = H3Event['req'] & {
6
+ getBody: () => Record<string, any>;
7
+ };
5
8
  /**
6
9
  * HTTP context passed to route handlers
7
10
  */
8
- type HttpContext = H3Event & {};
11
+ type HttpContext = Omit<H3Event, 'req'> & {
12
+ req: HttpRequest;
13
+ };
9
14
  /**
10
15
  * Route handler function type
11
16
  */
@@ -13,10 +18,12 @@ type RouteHandler = (
13
18
  /**
14
19
  * H3 event context
15
20
  */
21
+
16
22
  ctx: HttpContext,
17
23
  /**
18
24
  * ClearRequest instance
19
25
  */
26
+
20
27
  req: ClearRequest) => any | Promise<any>;
21
28
  /**
22
29
  * Handler can be either a function or controller reference
package/dist/index.d.mts CHANGED
@@ -2,10 +2,15 @@ import { NextFunction, Request, Response as Response$1 } from "express";
2
2
  import { H3Event, Middleware as Middleware$1 } from "h3";
3
3
 
4
4
  //#region types/h3.d.ts
5
+ type HttpRequest = H3Event['req'] & {
6
+ getBody: () => Record<string, any>;
7
+ };
5
8
  /**
6
9
  * HTTP context passed to route handlers
7
10
  */
8
- type HttpContext = H3Event & {};
11
+ type HttpContext = Omit<H3Event, 'req'> & {
12
+ req: HttpRequest;
13
+ };
9
14
  /**
10
15
  * Route handler function type
11
16
  */
@@ -13,10 +18,12 @@ type RouteHandler = (
13
18
  /**
14
19
  * H3 event context
15
20
  */
21
+
16
22
  ctx: HttpContext,
17
23
  /**
18
24
  * ClearRequest instance
19
25
  */
26
+
20
27
  req: ClearRequest) => any | Promise<any>;
21
28
  /**
22
29
  * Handler can be either a function or controller reference
@@ -3,11 +3,14 @@ import { ControllerHandler } from "./basic.mjs";
3
3
  import { NextFunction, Request, Response } from "express";
4
4
 
5
5
  //#region types/express.d.ts
6
+ interface RequestWithGetBody extends Request {
7
+ getBody: () => Record<string, any>;
8
+ }
6
9
  /**
7
10
  * HTTP context passed to route handlers
8
11
  */
9
12
  interface HttpContext {
10
- req: Request;
13
+ req: RequestWithGetBody;
11
14
  res: Response;
12
15
  next: NextFunction;
13
16
  }
@@ -34,4 +37,4 @@ type Handler = RouteHandler | ControllerHandler;
34
37
  */
35
38
  type Middleware = (req: Request, res: Response, next: NextFunction) => any | Promise<any>;
36
39
  //#endregion
37
- export { Handler, HttpContext, Middleware, RouteHandler };
40
+ export { Handler, HttpContext, Middleware, RequestWithGetBody, RouteHandler };
@@ -7,10 +7,15 @@ type H3App = Omit<H3['fetch'], 'fetch'> & {
7
7
  fetch: (request: TypedServerRequest) => Promise<Response>;
8
8
  };
9
9
  type MaybePromise<T = unknown> = T | Promise<T>;
10
+ type HttpRequest = H3Event['req'] & {
11
+ getBody: () => Record<string, any>;
12
+ };
10
13
  /**
11
14
  * HTTP context passed to route handlers
12
15
  */
13
- type HttpContext = H3Event & {};
16
+ type HttpContext = Omit<H3Event, 'req'> & {
17
+ req: HttpRequest;
18
+ };
14
19
  /**
15
20
  * Route handler function type
16
21
  */
@@ -31,4 +36,4 @@ req: ClearRequest) => any | Promise<any>;
31
36
  type Handler = RouteHandler | ControllerHandler;
32
37
  type NextFunction = () => MaybePromise<unknown | undefined>;
33
38
  //#endregion
34
- export { H3App, Handler, HttpContext, MaybePromise, type Middleware, NextFunction, RouteHandler };
39
+ export { H3App, Handler, HttpContext, HttpRequest, MaybePromise, type Middleware, NextFunction, RouteHandler };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-router",
3
- "version": "2.1.11",
3
+ "version": "2.1.12",
4
4
  "description": "Laravel-style routing system for Express.js and H3, with CommonJS, ESM, and TypeScript support",
5
5
  "keywords": [
6
6
  "h3",
@@ -80,6 +80,7 @@
80
80
  "typescript": "^5.3.3",
81
81
  "typescript-eslint": "^8.56.1",
82
82
  "vite-tsconfig-paths": "^6.1.1",
83
+ "vitepress": "^1.6.4",
83
84
  "vitest": "^4.0.18"
84
85
  },
85
86
  "engines": {
@@ -88,6 +89,9 @@
88
89
  "scripts": {
89
90
  "test": "vitest",
90
91
  "lint": "eslint",
92
+ "docs:dev": "vitepress dev docs",
93
+ "docs:build": "vitepress build docs",
94
+ "docs:preview": "vitepress preview docs",
91
95
  "test:esm": "vitest tests/esm.test.ts",
92
96
  "test:ts": "vitest tests/typescript.test.ts",
93
97
  "test:coverage": "vitest run --coverage",