clear-router 2.1.5 → 2.1.7

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.
@@ -30,11 +30,19 @@ var Route = class {
30
30
  path;
31
31
  handler;
32
32
  middlewares;
33
+ controllerName;
34
+ actionName;
35
+ handlerType;
36
+ middlewareCount;
33
37
  constructor(methods, path, handler, middlewares = []) {
34
38
  this.methods = methods;
35
39
  this.path = path;
36
40
  this.handler = handler;
37
41
  this.middlewares = middlewares;
42
+ this.handlerType = Array.isArray(handler) ? "controller" : "function";
43
+ this.middlewareCount = middlewares.length;
44
+ this.controllerName = Array.isArray(handler) ? handler[0]?.name : void 0;
45
+ this.actionName = Array.isArray(handler) ? handler[1] : typeof handler === "function" ? handler.constructor.name ?? handler.name : void 0;
38
46
  }
39
47
  };
40
48
 
@@ -31,11 +31,19 @@ var Route = class {
31
31
  path;
32
32
  handler;
33
33
  middlewares;
34
+ controllerName;
35
+ actionName;
36
+ handlerType;
37
+ middlewareCount;
34
38
  constructor(methods, path, handler, middlewares = []) {
35
39
  this.methods = methods;
36
40
  this.path = path;
37
41
  this.handler = handler;
38
42
  this.middlewares = middlewares;
43
+ this.handlerType = Array.isArray(handler) ? "controller" : "function";
44
+ this.middlewareCount = middlewares.length;
45
+ this.controllerName = Array.isArray(handler) ? handler[0]?.name : void 0;
46
+ this.actionName = Array.isArray(handler) ? handler[1] : typeof handler === "function" ? handler.constructor.name ?? handler.name : void 0;
39
47
  }
40
48
  };
41
49
 
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_Route = require('../Route-97p-tEbS.cjs');
2
+ const require_Route = require('../Route-DhC4kNPX.cjs');
3
3
 
4
4
  //#region src/express/router.ts
5
5
  /**
@@ -15,6 +15,14 @@ var Router = class Router {
15
15
  */
16
16
  static routes = [];
17
17
  /**
18
+ * Mapping of routes by path and method for quick lookup.
19
+ */
20
+ static routesByPathMethod = {};
21
+ /**
22
+ * Mapping of routes by method for quick lookup.
23
+ */
24
+ static routesByMethod = {};
25
+ /**
18
26
  * Current route prefix
19
27
  */
20
28
  static prefix = "";
@@ -42,13 +50,23 @@ var Router = class Router {
42
50
  * @param middlewares - Array of middleware functions
43
51
  */
44
52
  static add(methods, path, handler, middlewares) {
45
- const methodArray = Array.isArray(methods) ? methods : [methods];
53
+ methods = Array.isArray(methods) ? methods : [methods];
46
54
  const fullPath = this.normalizePath(`${this.prefix}/${path}`);
47
- this.routes.push(new require_Route.Route(methodArray, fullPath, handler, [
55
+ const route = new require_Route.Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
48
56
  ...this.globalMiddlewares,
49
57
  ...this.groupMiddlewares,
50
58
  ...middlewares || []
51
- ]));
59
+ ]);
60
+ if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
61
+ res.set("Allow", "GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD");
62
+ res.sendStatus(204);
63
+ });
64
+ this.routes.push(route);
65
+ for (const method of methods.map((m) => m.toUpperCase())) {
66
+ this.routesByPathMethod[`${method.toUpperCase()} ${fullPath}`] = route;
67
+ if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
68
+ this.routesByMethod[method].push(route);
69
+ }
52
70
  }
53
71
  /**
54
72
  * Register RESTful API resource routes for a controller with optional action filtering
@@ -184,17 +202,10 @@ var Router = class Router {
184
202
  callback();
185
203
  this.globalMiddlewares = prevMiddlewares;
186
204
  }
187
- /**
188
- * Get all registered routes with their information
189
- * @returns Array of route information objects
190
- */
191
- static allRoutes() {
192
- return this.routes.map((route) => ({
193
- methods: route.methods,
194
- path: route.path,
195
- middlewareCount: route.middlewares.length,
196
- handlerType: typeof route.handler === "function" ? "function" : "controller"
197
- }));
205
+ static allRoutes(type) {
206
+ if (type === "method") return this.routesByMethod;
207
+ if (type === "path") return this.routesByPathMethod;
208
+ return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
198
209
  }
199
210
  static async apply(router) {
200
211
  for (const route of this.routes) {
@@ -223,7 +234,7 @@ var Router = class Router {
223
234
  }
224
235
  if (!handlerFunction) continue;
225
236
  for (const method of route.methods) {
226
- if (![
237
+ const allowedMethods = [
227
238
  "get",
228
239
  "post",
229
240
  "put",
@@ -231,7 +242,9 @@ var Router = class Router {
231
242
  "patch",
232
243
  "options",
233
244
  "head"
234
- ].includes(method)) {
245
+ ];
246
+ if (method === "options" && route.methods.length > 1) continue;
247
+ if (!allowedMethods.includes(method)) {
235
248
  const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
236
249
  console.error("[ROUTES]", error.message);
237
250
  throw error;
@@ -1,4 +1,4 @@
1
- import { d as RouteInfo, i as Route, l as ControllerAction, n as HttpContext, r as Middleware, t as Handler, u as HttpMethod } from "../express-sjYra4oE.cjs";
1
+ import { i as Route, l as ControllerAction, n as HttpContext, r as Middleware, t as Handler, u as HttpMethod } from "../express-JHxK-EqQ.cjs";
2
2
  import { Router as Router$1 } from "express";
3
3
 
4
4
  //#region src/express/router.d.ts
@@ -14,6 +14,14 @@ declare class Router {
14
14
  * All registered routes
15
15
  */
16
16
  static routes: Array<Route<HttpContext, Middleware>>;
17
+ /**
18
+ * Mapping of routes by path and method for quick lookup.
19
+ */
20
+ static routesByPathMethod: Record<string, Route<HttpContext, Middleware>>;
21
+ /**
22
+ * Mapping of routes by method for quick lookup.
23
+ */
24
+ static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
17
25
  /**
18
26
  * Current route prefix
19
27
  */
@@ -117,7 +125,9 @@ declare class Router {
117
125
  * Get all registered routes with their information
118
126
  * @returns Array of route information objects
119
127
  */
120
- static allRoutes(): RouteInfo[];
128
+ static allRoutes(type?: 'path'): Record<string, Route<HttpContext, Middleware>>;
129
+ static allRoutes(type?: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
130
+ static allRoutes(type?: 'method'): Array<Route<HttpContext, Middleware>>;
121
131
  /**
122
132
  * Apply all registered routes to the provided Express Router instance
123
133
  * Handles controller-method binding and middleware application
@@ -1,4 +1,4 @@
1
- import { d as RouteInfo, i as Route, l as ControllerAction, n as HttpContext, r as Middleware, t as Handler, u as HttpMethod } from "../express-WiUTZlHA.mjs";
1
+ import { i as Route, l as ControllerAction, n as HttpContext, r as Middleware, t as Handler, u as HttpMethod } from "../express-D9GR9yTH.mjs";
2
2
  import { Router as Router$1 } from "express";
3
3
 
4
4
  //#region src/express/router.d.ts
@@ -14,6 +14,14 @@ declare class Router {
14
14
  * All registered routes
15
15
  */
16
16
  static routes: Array<Route<HttpContext, Middleware>>;
17
+ /**
18
+ * Mapping of routes by path and method for quick lookup.
19
+ */
20
+ static routesByPathMethod: Record<string, Route<HttpContext, Middleware>>;
21
+ /**
22
+ * Mapping of routes by method for quick lookup.
23
+ */
24
+ static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
17
25
  /**
18
26
  * Current route prefix
19
27
  */
@@ -117,7 +125,9 @@ declare class Router {
117
125
  * Get all registered routes with their information
118
126
  * @returns Array of route information objects
119
127
  */
120
- static allRoutes(): RouteInfo[];
128
+ static allRoutes(type?: 'path'): Record<string, Route<HttpContext, Middleware>>;
129
+ static allRoutes(type?: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
130
+ static allRoutes(type?: 'method'): Array<Route<HttpContext, Middleware>>;
121
131
  /**
122
132
  * Apply all registered routes to the provided Express Router instance
123
133
  * Handles controller-method binding and middleware application
@@ -1,4 +1,4 @@
1
- import { n as ClearRequest, t as Route } from "../Route-BtmoiYq5.mjs";
1
+ import { n as ClearRequest, t as Route } from "../Route-BbPXcDGX.mjs";
2
2
 
3
3
  //#region src/express/router.ts
4
4
  /**
@@ -14,6 +14,14 @@ var Router = class Router {
14
14
  */
15
15
  static routes = [];
16
16
  /**
17
+ * Mapping of routes by path and method for quick lookup.
18
+ */
19
+ static routesByPathMethod = {};
20
+ /**
21
+ * Mapping of routes by method for quick lookup.
22
+ */
23
+ static routesByMethod = {};
24
+ /**
17
25
  * Current route prefix
18
26
  */
19
27
  static prefix = "";
@@ -41,13 +49,23 @@ var Router = class Router {
41
49
  * @param middlewares - Array of middleware functions
42
50
  */
43
51
  static add(methods, path, handler, middlewares) {
44
- const methodArray = Array.isArray(methods) ? methods : [methods];
52
+ methods = Array.isArray(methods) ? methods : [methods];
45
53
  const fullPath = this.normalizePath(`${this.prefix}/${path}`);
46
- this.routes.push(new Route(methodArray, fullPath, handler, [
54
+ const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
47
55
  ...this.globalMiddlewares,
48
56
  ...this.groupMiddlewares,
49
57
  ...middlewares || []
50
- ]));
58
+ ]);
59
+ if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
60
+ res.set("Allow", "GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD");
61
+ res.sendStatus(204);
62
+ });
63
+ this.routes.push(route);
64
+ for (const method of methods.map((m) => m.toUpperCase())) {
65
+ this.routesByPathMethod[`${method.toUpperCase()} ${fullPath}`] = route;
66
+ if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
67
+ this.routesByMethod[method].push(route);
68
+ }
51
69
  }
52
70
  /**
53
71
  * Register RESTful API resource routes for a controller with optional action filtering
@@ -183,17 +201,10 @@ var Router = class Router {
183
201
  callback();
184
202
  this.globalMiddlewares = prevMiddlewares;
185
203
  }
186
- /**
187
- * Get all registered routes with their information
188
- * @returns Array of route information objects
189
- */
190
- static allRoutes() {
191
- return this.routes.map((route) => ({
192
- methods: route.methods,
193
- path: route.path,
194
- middlewareCount: route.middlewares.length,
195
- handlerType: typeof route.handler === "function" ? "function" : "controller"
196
- }));
204
+ static allRoutes(type) {
205
+ if (type === "method") return this.routesByMethod;
206
+ if (type === "path") return this.routesByPathMethod;
207
+ return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
197
208
  }
198
209
  static async apply(router) {
199
210
  for (const route of this.routes) {
@@ -222,7 +233,7 @@ var Router = class Router {
222
233
  }
223
234
  if (!handlerFunction) continue;
224
235
  for (const method of route.methods) {
225
- if (![
236
+ const allowedMethods = [
226
237
  "get",
227
238
  "post",
228
239
  "put",
@@ -230,7 +241,9 @@ var Router = class Router {
230
241
  "patch",
231
242
  "options",
232
243
  "head"
233
- ].includes(method)) {
244
+ ];
245
+ if (method === "options" && route.methods.length > 1) continue;
246
+ if (!allowedMethods.includes(method)) {
234
247
  const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
235
248
  console.error("[ROUTES]", error.message);
236
249
  throw error;
@@ -10,15 +10,6 @@ type ControllerHandler = [any, string];
10
10
  * HTTP methods supported by the router
11
11
  */
12
12
  type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
13
- /**
14
- * Route information object
15
- */
16
- interface RouteInfo {
17
- methods: HttpMethod[];
18
- path: string;
19
- middlewareCount: number;
20
- handlerType: 'function' | 'controller';
21
- }
22
13
  /**
23
14
  * Common controller action names
24
15
  */
@@ -66,6 +57,10 @@ declare class Route<X = any, M = Middleware$1 | Middleware> {
66
57
  path: string;
67
58
  handler: Handler$1;
68
59
  middlewares: M[];
60
+ controllerName?: string;
61
+ actionName?: string;
62
+ handlerType: 'function' | 'controller';
63
+ middlewareCount: number;
69
64
  constructor(methods: HttpMethod[], path: string, handler: Handler$1, middlewares?: M[]);
70
65
  }
71
66
  //#endregion
@@ -120,4 +115,4 @@ type Handler = RouteHandler | ControllerHandler;
120
115
  */
121
116
  type Middleware = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
122
117
  //#endregion
123
- export { H3App as a, Middleware$1 as c, RouteInfo as d, Route as i, ControllerAction as l, HttpContext as n, Handler$1 as o, Middleware as r, HttpContext$1 as s, Handler as t, HttpMethod as u };
118
+ export { H3App as a, Middleware$1 as c, Route as i, ControllerAction as l, HttpContext as n, Handler$1 as o, Middleware as r, HttpContext$1 as s, Handler as t, HttpMethod as u };
@@ -10,15 +10,6 @@ type ControllerHandler = [any, string];
10
10
  * HTTP methods supported by the router
11
11
  */
12
12
  type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
13
- /**
14
- * Route information object
15
- */
16
- interface RouteInfo {
17
- methods: HttpMethod[];
18
- path: string;
19
- middlewareCount: number;
20
- handlerType: 'function' | 'controller';
21
- }
22
13
  /**
23
14
  * Common controller action names
24
15
  */
@@ -66,6 +57,10 @@ declare class Route<X = any, M = Middleware$1 | Middleware> {
66
57
  path: string;
67
58
  handler: Handler$1;
68
59
  middlewares: M[];
60
+ controllerName?: string;
61
+ actionName?: string;
62
+ handlerType: 'function' | 'controller';
63
+ middlewareCount: number;
69
64
  constructor(methods: HttpMethod[], path: string, handler: Handler$1, middlewares?: M[]);
70
65
  }
71
66
  //#endregion
@@ -120,4 +115,4 @@ type Handler = RouteHandler | ControllerHandler;
120
115
  */
121
116
  type Middleware = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
122
117
  //#endregion
123
- export { H3App as a, Middleware$1 as c, RouteInfo as d, Route as i, ControllerAction as l, HttpContext as n, Handler$1 as o, Middleware as r, HttpContext$1 as s, Handler as t, HttpMethod as u };
118
+ export { H3App as a, Middleware$1 as c, Route as i, ControllerAction as l, HttpContext as n, Handler$1 as o, Middleware as r, HttpContext$1 as s, Handler as t, HttpMethod as u };
package/dist/h3/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_Route = require('../Route-97p-tEbS.cjs');
2
+ const require_Route = require('../Route-DhC4kNPX.cjs');
3
3
  let h3 = require("h3");
4
4
 
5
5
  //#region src/h3/router.ts
@@ -15,6 +15,14 @@ var Router = class Router {
15
15
  */
16
16
  static routes = [];
17
17
  /**
18
+ * Mapping of routes by path and method for quick lookup.
19
+ */
20
+ static routesByPathMethod = {};
21
+ /**
22
+ * Mapping of routes by method for quick lookup.
23
+ */
24
+ static routesByMethod = {};
25
+ /**
18
26
  * Current route prefix
19
27
  */
20
28
  static prefix = "";
@@ -42,13 +50,23 @@ var Router = class Router {
42
50
  * @param middlewares - Array of middleware functions
43
51
  */
44
52
  static add(methods, path, handler, middlewares) {
45
- const methodArray = Array.isArray(methods) ? methods : [methods];
53
+ methods = Array.isArray(methods) ? methods : [methods];
46
54
  const fullPath = this.normalizePath(`${this.prefix}/${path}`);
47
- this.routes.push(new require_Route.Route(methodArray, fullPath, handler, [
55
+ const route = new require_Route.Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
48
56
  ...this.globalMiddlewares,
49
57
  ...this.groupMiddlewares,
50
58
  ...middlewares || []
51
- ]));
59
+ ]);
60
+ if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
61
+ res.headers.set("Allow", "GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD");
62
+ res.status = 204;
63
+ });
64
+ this.routes.push(route);
65
+ for (const method of methods.map((m) => m.toUpperCase())) {
66
+ this.routesByPathMethod[`${method} ${fullPath}`] = route;
67
+ if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
68
+ this.routesByMethod[method].push(route);
69
+ }
52
70
  }
53
71
  /**
54
72
  * Register RESTful API resource routes for a controller with optional action filtering
@@ -184,17 +202,10 @@ var Router = class Router {
184
202
  callback();
185
203
  this.globalMiddlewares = prevMiddlewares;
186
204
  }
187
- /**
188
- * Get all registered routes with their information
189
- * @returns Array of route information objects
190
- */
191
- static allRoutes() {
192
- return this.routes.map((route) => ({
193
- methods: route.methods,
194
- path: route.path,
195
- middlewareCount: route.middlewares.length,
196
- handlerType: typeof route.handler === "function" ? "function" : "controller"
197
- }));
205
+ static allRoutes(type) {
206
+ if (type === "method") return this.routesByMethod;
207
+ if (type === "path") return this.routesByPathMethod;
208
+ return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
198
209
  }
199
210
  /**
200
211
  * Apply all registered routes to the provided H3 Router instance
@@ -230,7 +241,7 @@ var Router = class Router {
230
241
  }
231
242
  if (!handlerFunction) continue;
232
243
  for (const method of route.methods) {
233
- if (![
244
+ const allowedMethods = [
234
245
  "get",
235
246
  "post",
236
247
  "put",
@@ -238,7 +249,9 @@ var Router = class Router {
238
249
  "patch",
239
250
  "options",
240
251
  "head"
241
- ].includes(method)) {
252
+ ];
253
+ if (method === "options" && route.methods.length > 1) continue;
254
+ if (!allowedMethods.includes(method)) {
242
255
  const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
243
256
  console.error("[ROUTES]", error.message);
244
257
  throw error;
@@ -1,4 +1,4 @@
1
- import { a as H3App, c as Middleware, d as RouteInfo, i as Route, l as ControllerAction, o as Handler, s as HttpContext, u as HttpMethod } from "../express-sjYra4oE.cjs";
1
+ import { a as H3App, c as Middleware, i as Route, l as ControllerAction, o as Handler, s as HttpContext, u as HttpMethod } from "../express-JHxK-EqQ.cjs";
2
2
  import { H3 } from "h3";
3
3
 
4
4
  //#region src/h3/router.d.ts
@@ -13,6 +13,14 @@ declare class Router {
13
13
  * All registered routes
14
14
  */
15
15
  static routes: Array<Route<HttpContext, Middleware>>;
16
+ /**
17
+ * Mapping of routes by path and method for quick lookup.
18
+ */
19
+ static routesByPathMethod: Record<string, Route<HttpContext, Middleware>>;
20
+ /**
21
+ * Mapping of routes by method for quick lookup.
22
+ */
23
+ static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
16
24
  /**
17
25
  * Current route prefix
18
26
  */
@@ -116,7 +124,9 @@ declare class Router {
116
124
  * Get all registered routes with their information
117
125
  * @returns Array of route information objects
118
126
  */
119
- static allRoutes(): RouteInfo[];
127
+ static allRoutes(type?: 'path'): Record<string, Route<HttpContext, Middleware>>;
128
+ static allRoutes(type?: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
129
+ static allRoutes(type?: 'method'): Array<Route<HttpContext, Middleware>>;
120
130
  /**
121
131
  * Apply all registered routes to the provided H3 Router instance
122
132
  * Handles controller-method binding and middleware application
@@ -1,4 +1,4 @@
1
- import { a as H3App, c as Middleware, d as RouteInfo, i as Route, l as ControllerAction, o as Handler, s as HttpContext, u as HttpMethod } from "../express-WiUTZlHA.mjs";
1
+ import { a as H3App, c as Middleware, i as Route, l as ControllerAction, o as Handler, s as HttpContext, u as HttpMethod } from "../express-D9GR9yTH.mjs";
2
2
  import { H3 } from "h3";
3
3
 
4
4
  //#region src/h3/router.d.ts
@@ -13,6 +13,14 @@ declare class Router {
13
13
  * All registered routes
14
14
  */
15
15
  static routes: Array<Route<HttpContext, Middleware>>;
16
+ /**
17
+ * Mapping of routes by path and method for quick lookup.
18
+ */
19
+ static routesByPathMethod: Record<string, Route<HttpContext, Middleware>>;
20
+ /**
21
+ * Mapping of routes by method for quick lookup.
22
+ */
23
+ static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
16
24
  /**
17
25
  * Current route prefix
18
26
  */
@@ -116,7 +124,9 @@ declare class Router {
116
124
  * Get all registered routes with their information
117
125
  * @returns Array of route information objects
118
126
  */
119
- static allRoutes(): RouteInfo[];
127
+ static allRoutes(type?: 'path'): Record<string, Route<HttpContext, Middleware>>;
128
+ static allRoutes(type?: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
129
+ static allRoutes(type?: 'method'): Array<Route<HttpContext, Middleware>>;
120
130
  /**
121
131
  * Apply all registered routes to the provided H3 Router instance
122
132
  * Handles controller-method binding and middleware application
package/dist/h3/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as ClearRequest, t as Route } from "../Route-BtmoiYq5.mjs";
1
+ import { n as ClearRequest, t as Route } from "../Route-BbPXcDGX.mjs";
2
2
  import { getQuery, getRouterParams, readBody } from "h3";
3
3
 
4
4
  //#region src/h3/router.ts
@@ -14,6 +14,14 @@ var Router = class Router {
14
14
  */
15
15
  static routes = [];
16
16
  /**
17
+ * Mapping of routes by path and method for quick lookup.
18
+ */
19
+ static routesByPathMethod = {};
20
+ /**
21
+ * Mapping of routes by method for quick lookup.
22
+ */
23
+ static routesByMethod = {};
24
+ /**
17
25
  * Current route prefix
18
26
  */
19
27
  static prefix = "";
@@ -41,13 +49,23 @@ var Router = class Router {
41
49
  * @param middlewares - Array of middleware functions
42
50
  */
43
51
  static add(methods, path, handler, middlewares) {
44
- const methodArray = Array.isArray(methods) ? methods : [methods];
52
+ methods = Array.isArray(methods) ? methods : [methods];
45
53
  const fullPath = this.normalizePath(`${this.prefix}/${path}`);
46
- this.routes.push(new Route(methodArray, fullPath, handler, [
54
+ const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
47
55
  ...this.globalMiddlewares,
48
56
  ...this.groupMiddlewares,
49
57
  ...middlewares || []
50
- ]));
58
+ ]);
59
+ if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
60
+ res.headers.set("Allow", "GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD");
61
+ res.status = 204;
62
+ });
63
+ this.routes.push(route);
64
+ for (const method of methods.map((m) => m.toUpperCase())) {
65
+ this.routesByPathMethod[`${method} ${fullPath}`] = route;
66
+ if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
67
+ this.routesByMethod[method].push(route);
68
+ }
51
69
  }
52
70
  /**
53
71
  * Register RESTful API resource routes for a controller with optional action filtering
@@ -183,17 +201,10 @@ var Router = class Router {
183
201
  callback();
184
202
  this.globalMiddlewares = prevMiddlewares;
185
203
  }
186
- /**
187
- * Get all registered routes with their information
188
- * @returns Array of route information objects
189
- */
190
- static allRoutes() {
191
- return this.routes.map((route) => ({
192
- methods: route.methods,
193
- path: route.path,
194
- middlewareCount: route.middlewares.length,
195
- handlerType: typeof route.handler === "function" ? "function" : "controller"
196
- }));
204
+ static allRoutes(type) {
205
+ if (type === "method") return this.routesByMethod;
206
+ if (type === "path") return this.routesByPathMethod;
207
+ return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
197
208
  }
198
209
  /**
199
210
  * Apply all registered routes to the provided H3 Router instance
@@ -229,7 +240,7 @@ var Router = class Router {
229
240
  }
230
241
  if (!handlerFunction) continue;
231
242
  for (const method of route.methods) {
232
- if (![
243
+ const allowedMethods = [
233
244
  "get",
234
245
  "post",
235
246
  "put",
@@ -237,7 +248,9 @@ var Router = class Router {
237
248
  "patch",
238
249
  "options",
239
250
  "head"
240
- ].includes(method)) {
251
+ ];
252
+ if (method === "options" && route.methods.length > 1) continue;
253
+ if (!allowedMethods.includes(method)) {
241
254
  const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
242
255
  console.error("[ROUTES]", error.message);
243
256
  throw error;
package/dist/index.cjs CHANGED
@@ -42,11 +42,19 @@ var Route = class {
42
42
  path;
43
43
  handler;
44
44
  middlewares;
45
+ controllerName;
46
+ actionName;
47
+ handlerType;
48
+ middlewareCount;
45
49
  constructor(methods, path, handler, middlewares = []) {
46
50
  this.methods = methods;
47
51
  this.path = path;
48
52
  this.handler = handler;
49
53
  this.middlewares = middlewares;
54
+ this.handlerType = Array.isArray(handler) ? "controller" : "function";
55
+ this.middlewareCount = middlewares.length;
56
+ this.controllerName = Array.isArray(handler) ? handler[0]?.name : void 0;
57
+ this.actionName = Array.isArray(handler) ? handler[1] : typeof handler === "function" ? handler.constructor.name ?? handler.name : void 0;
50
58
  }
51
59
  };
52
60
 
package/dist/index.d.cts CHANGED
@@ -56,6 +56,10 @@ declare class Route<X = any, M = Middleware | Middleware$1> {
56
56
  path: string;
57
57
  handler: Handler;
58
58
  middlewares: M[];
59
+ controllerName?: string;
60
+ actionName?: string;
61
+ handlerType: 'function' | 'controller';
62
+ middlewareCount: number;
59
63
  constructor(methods: HttpMethod[], path: string, handler: Handler, middlewares?: M[]);
60
64
  }
61
65
  //#endregion
package/dist/index.d.mts CHANGED
@@ -56,6 +56,10 @@ declare class Route<X = any, M = Middleware | Middleware$1> {
56
56
  path: string;
57
57
  handler: Handler;
58
58
  middlewares: M[];
59
+ controllerName?: string;
60
+ actionName?: string;
61
+ handlerType: 'function' | 'controller';
62
+ middlewareCount: number;
59
63
  constructor(methods: HttpMethod[], path: string, handler: Handler, middlewares?: M[]);
60
64
  }
61
65
  //#endregion
package/dist/index.mjs CHANGED
@@ -40,11 +40,19 @@ var Route = class {
40
40
  path;
41
41
  handler;
42
42
  middlewares;
43
+ controllerName;
44
+ actionName;
45
+ handlerType;
46
+ middlewareCount;
43
47
  constructor(methods, path, handler, middlewares = []) {
44
48
  this.methods = methods;
45
49
  this.path = path;
46
50
  this.handler = handler;
47
51
  this.middlewares = middlewares;
52
+ this.handlerType = Array.isArray(handler) ? "controller" : "function";
53
+ this.middlewareCount = middlewares.length;
54
+ this.controllerName = Array.isArray(handler) ? handler[0]?.name : void 0;
55
+ this.actionName = Array.isArray(handler) ? handler[1] : typeof handler === "function" ? handler.constructor.name ?? handler.name : void 0;
48
56
  }
49
57
  };
50
58
 
@@ -14,6 +14,10 @@ declare class Route<X = any, M = Middleware | Middleware$1> {
14
14
  path: string;
15
15
  handler: Handler;
16
16
  middlewares: M[];
17
+ controllerName?: string;
18
+ actionName?: string;
19
+ handlerType: 'function' | 'controller';
20
+ middlewareCount: number;
17
21
  constructor(methods: HttpMethod[], path: string, handler: Handler, middlewares?: M[]);
18
22
  }
19
23
  //#endregion
@@ -7,15 +7,6 @@ type ControllerHandler = [any, string];
7
7
  * HTTP methods supported by the router
8
8
  */
9
9
  type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
10
- /**
11
- * Route information object
12
- */
13
- interface RouteInfo {
14
- methods: HttpMethod[];
15
- path: string;
16
- middlewareCount: number;
17
- handlerType: 'function' | 'controller';
18
- }
19
10
  /**
20
11
  * Common controller action names
21
12
  */
@@ -25,4 +16,4 @@ type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
25
16
  */
26
17
  type RequestData = Record<string, any>;
27
18
  //#endregion
28
- export { ControllerAction, ControllerHandler, HttpMethod, RequestData, RouteInfo };
19
+ export { ControllerAction, ControllerHandler, HttpMethod, RequestData };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-router",
3
- "version": "2.1.5",
3
+ "version": "2.1.7",
4
4
  "description": "Laravel-style routing system for Express.js and H3, with CommonJS, ESM, and TypeScript support",
5
5
  "keywords": [
6
6
  "h3",
@@ -72,6 +72,7 @@
72
72
  "@types/express": "^4.17.21",
73
73
  "@types/node": "^20.10.6",
74
74
  "@types/supertest": "^6.0.3",
75
+ "@vitest/coverage-v8": "4.0.18",
75
76
  "eslint": "^10.0.2",
76
77
  "supertest": "^7.1.1",
77
78
  "tsdown": "^0.20.3",