clear-router 2.1.6 → 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.
@@ -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,12 +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;
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");
193
209
  }
194
210
  static async apply(router) {
195
211
  for (const route of this.routes) {
@@ -218,7 +234,7 @@ var Router = class Router {
218
234
  }
219
235
  if (!handlerFunction) continue;
220
236
  for (const method of route.methods) {
221
- if (![
237
+ const allowedMethods = [
222
238
  "get",
223
239
  "post",
224
240
  "put",
@@ -226,7 +242,9 @@ var Router = class Router {
226
242
  "patch",
227
243
  "options",
228
244
  "head"
229
- ].includes(method)) {
245
+ ];
246
+ if (method === "options" && route.methods.length > 1) continue;
247
+ if (!allowedMethods.includes(method)) {
230
248
  const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
231
249
  console.error("[ROUTES]", error.message);
232
250
  throw error;
@@ -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(): Route<HttpContext, Middleware>[];
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
@@ -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(): Route<HttpContext, Middleware>[];
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
@@ -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,12 +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;
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");
192
208
  }
193
209
  static async apply(router) {
194
210
  for (const route of this.routes) {
@@ -217,7 +233,7 @@ var Router = class Router {
217
233
  }
218
234
  if (!handlerFunction) continue;
219
235
  for (const method of route.methods) {
220
- if (![
236
+ const allowedMethods = [
221
237
  "get",
222
238
  "post",
223
239
  "put",
@@ -225,7 +241,9 @@ var Router = class Router {
225
241
  "patch",
226
242
  "options",
227
243
  "head"
228
- ].includes(method)) {
244
+ ];
245
+ if (method === "options" && route.methods.length > 1) continue;
246
+ if (!allowedMethods.includes(method)) {
229
247
  const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
230
248
  console.error("[ROUTES]", error.message);
231
249
  throw error;
package/dist/h3/index.cjs CHANGED
@@ -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,12 +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;
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");
193
209
  }
194
210
  /**
195
211
  * Apply all registered routes to the provided H3 Router instance
@@ -225,7 +241,7 @@ var Router = class Router {
225
241
  }
226
242
  if (!handlerFunction) continue;
227
243
  for (const method of route.methods) {
228
- if (![
244
+ const allowedMethods = [
229
245
  "get",
230
246
  "post",
231
247
  "put",
@@ -233,7 +249,9 @@ var Router = class Router {
233
249
  "patch",
234
250
  "options",
235
251
  "head"
236
- ].includes(method)) {
252
+ ];
253
+ if (method === "options" && route.methods.length > 1) continue;
254
+ if (!allowedMethods.includes(method)) {
237
255
  const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
238
256
  console.error("[ROUTES]", error.message);
239
257
  throw error;
@@ -1,5 +1,4 @@
1
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
- import * as h3 from "h3";
3
2
  import { H3 } from "h3";
4
3
 
5
4
  //#region src/h3/router.d.ts
@@ -14,6 +13,14 @@ declare class Router {
14
13
  * All registered routes
15
14
  */
16
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>> };
17
24
  /**
18
25
  * Current route prefix
19
26
  */
@@ -117,7 +124,9 @@ declare class Router {
117
124
  * Get all registered routes with their information
118
125
  * @returns Array of route information objects
119
126
  */
120
- static allRoutes(): Route<h3.H3Event<h3.EventHandlerRequest>, Middleware>[];
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>>;
121
130
  /**
122
131
  * Apply all registered routes to the provided H3 Router instance
123
132
  * Handles controller-method binding and middleware application
@@ -1,5 +1,4 @@
1
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
- import * as h3 from "h3";
3
2
  import { H3 } from "h3";
4
3
 
5
4
  //#region src/h3/router.d.ts
@@ -14,6 +13,14 @@ declare class Router {
14
13
  * All registered routes
15
14
  */
16
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>> };
17
24
  /**
18
25
  * Current route prefix
19
26
  */
@@ -117,7 +124,9 @@ declare class Router {
117
124
  * Get all registered routes with their information
118
125
  * @returns Array of route information objects
119
126
  */
120
- static allRoutes(): Route<h3.H3Event<h3.EventHandlerRequest>, Middleware>[];
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>>;
121
130
  /**
122
131
  * Apply all registered routes to the provided H3 Router instance
123
132
  * Handles controller-method binding and middleware application
package/dist/h3/index.mjs CHANGED
@@ -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,12 +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;
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");
192
208
  }
193
209
  /**
194
210
  * Apply all registered routes to the provided H3 Router instance
@@ -224,7 +240,7 @@ var Router = class Router {
224
240
  }
225
241
  if (!handlerFunction) continue;
226
242
  for (const method of route.methods) {
227
- if (![
243
+ const allowedMethods = [
228
244
  "get",
229
245
  "post",
230
246
  "put",
@@ -232,7 +248,9 @@ var Router = class Router {
232
248
  "patch",
233
249
  "options",
234
250
  "head"
235
- ].includes(method)) {
251
+ ];
252
+ if (method === "options" && route.methods.length > 1) continue;
253
+ if (!allowedMethods.includes(method)) {
236
254
  const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
237
255
  console.error("[ROUTES]", error.message);
238
256
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-router",
3
- "version": "2.1.6",
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",