clear-router 2.7.7 → 2.8.0

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.
Files changed (52) hide show
  1. package/dist/{Request-BhTJDR_5.d.mts → Request-Ci0UQ-Vl.d.mts} +27 -0
  2. package/dist/ResourceRouteSelection.cjs +26 -0
  3. package/dist/ResourceRouteSelection.d.cts +20 -0
  4. package/dist/ResourceRouteSelection.d.mts +20 -0
  5. package/dist/ResourceRouteSelection.mjs +25 -0
  6. package/dist/ResourceRoutes.cjs +60 -0
  7. package/dist/ResourceRoutes.d.cts +37 -0
  8. package/dist/ResourceRoutes.d.mts +37 -0
  9. package/dist/ResourceRoutes.mjs +60 -0
  10. package/dist/Route.cjs +32 -0
  11. package/dist/Route.d.cts +27 -0
  12. package/dist/Route.d.mts +27 -0
  13. package/dist/Route.mjs +32 -0
  14. package/dist/core/index.cjs +6 -0
  15. package/dist/core/router.cjs +57 -4
  16. package/dist/core/router.d.cts +13 -1
  17. package/dist/core/router.d.mts +13 -1
  18. package/dist/core/router.mjs +57 -4
  19. package/dist/decorators/setup.d.mts +0 -1
  20. package/dist/express/router.cjs +2 -2
  21. package/dist/express/router.d.cts +2 -1
  22. package/dist/express/router.d.mts +2 -1
  23. package/dist/express/router.mjs +2 -2
  24. package/dist/fastify/router.cjs +2 -2
  25. package/dist/fastify/router.d.cts +2 -1
  26. package/dist/fastify/router.d.mts +2 -1
  27. package/dist/fastify/router.mjs +2 -2
  28. package/dist/h3/router.cjs +2 -2
  29. package/dist/h3/router.d.cts +19 -18
  30. package/dist/h3/router.d.mts +19 -18
  31. package/dist/h3/router.mjs +2 -2
  32. package/dist/hono/router.cjs +2 -2
  33. package/dist/hono/router.d.cts +2 -1
  34. package/dist/hono/router.d.mts +2 -1
  35. package/dist/hono/router.mjs +2 -2
  36. package/dist/koa/router.cjs +2 -2
  37. package/dist/koa/router.d.cts +2 -1
  38. package/dist/koa/router.d.mts +2 -1
  39. package/dist/koa/router.mjs +2 -2
  40. package/dist/types/basic.d.cts +11 -1
  41. package/dist/types/basic.d.mts +11 -1
  42. package/dist/types/express.d.cts +6 -2
  43. package/dist/types/express.d.mts +6 -2
  44. package/dist/types/fastify.d.cts +3 -2
  45. package/dist/types/fastify.d.mts +3 -2
  46. package/dist/types/h3.d.cts +7 -2
  47. package/dist/types/h3.d.mts +7 -2
  48. package/dist/types/hono.d.cts +4 -3
  49. package/dist/types/hono.d.mts +4 -3
  50. package/dist/types/koa.d.cts +3 -2
  51. package/dist/types/koa.d.mts +3 -2
  52. package/package.json +1 -1
@@ -4,6 +4,7 @@ import { Route } from "../Route.mjs";
4
4
  import { Request } from "./Request.mjs";
5
5
  import { ClearRouterPluginArgumentsContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind } from "./plugins.mjs";
6
6
  import { Controller } from "../Controller.mjs";
7
+ import { ResourceRoutes } from "../ResourceRoutes.mjs";
7
8
  import { AsyncLocalStorage } from "node:async_hooks";
8
9
 
9
10
  //#region src/core/router.d.ts
@@ -37,6 +38,17 @@ declare abstract class CoreRouter {
37
38
  static prefix: string;
38
39
  static groupMiddlewares: any[];
39
40
  static globalMiddlewares: any[];
41
+ /**
42
+ * Resolve middlewares before assigning to adapter
43
+ *
44
+ * @param middleware
45
+ * @returns
46
+ */
47
+ protected static resolveMiddleware(middleware: any): any;
48
+ protected static resolveMiddlewares(middlewares?: any[]): any[];
49
+ protected static routeSpecificity(route: Route<any, any, any>): [number, number, number];
50
+ protected static orderedRoutes(): Array<Route<any, any, any>>;
51
+ protected static removeRouteMethod(route: Route<any, any, any>, method: HttpMethod, path: string): void;
40
52
  /**
41
53
  * Resets the router to it's default state
42
54
  */
@@ -134,7 +146,7 @@ declare abstract class CoreRouter {
134
146
  only?: ControllerAction[];
135
147
  except?: ControllerAction[];
136
148
  middlewares?: ApiResourceMiddleware<any>;
137
- }): void;
149
+ }): ResourceRoutes<any, any, any>;
138
150
  /**
139
151
  * Adds a new GET route to the router.
140
152
  *
@@ -1,6 +1,7 @@
1
1
  import { Request } from "./Request.mjs";
2
2
  import { Response } from "./Response.mjs";
3
3
  import { Container, getBindingMetadataFromTargets, getDesignParamTypes, getStandardMetadata, isClass } from "./bindings.mjs";
4
+ import { ResourceRoutes } from "../ResourceRoutes.mjs";
4
5
  import { Route } from "../Route.mjs";
5
6
  import { createRequire } from "node:module";
6
7
  import { AsyncLocalStorage } from "node:async_hooks";
@@ -45,6 +46,51 @@ var CoreRouter = class {
45
46
  static groupMiddlewares = [];
46
47
  static globalMiddlewares = [];
47
48
  /**
49
+ * Resolve middlewares before assigning to adapter
50
+ *
51
+ * @param middleware
52
+ * @returns
53
+ */
54
+ static resolveMiddleware(middleware) {
55
+ if (!middleware || typeof middleware === "function" && !isClass(middleware)) return middleware;
56
+ const instance = isClass(middleware) ? new middleware() : middleware;
57
+ if (instance && typeof instance.handle === "function") return instance.handle.bind(instance);
58
+ return middleware;
59
+ }
60
+ static resolveMiddlewares(middlewares = []) {
61
+ return middlewares.map((middleware) => this.resolveMiddleware(middleware));
62
+ }
63
+ static routeSpecificity(route) {
64
+ const path = route.registrationPaths.slice().sort((left, right) => right.length - left.length)[0] ?? route.path;
65
+ const segments = this.normalizePath(path).split("/").filter(Boolean);
66
+ return [
67
+ segments.filter((segment) => !segment.startsWith(":")).length,
68
+ segments.length,
69
+ path.length
70
+ ];
71
+ }
72
+ static orderedRoutes() {
73
+ return Array.from(this.routes).sort((left, right) => {
74
+ const leftScore = this.routeSpecificity(left);
75
+ const rightScore = this.routeSpecificity(right);
76
+ for (let index = 0; index < leftScore.length; index++) {
77
+ const difference = rightScore[index] - leftScore[index];
78
+ if (difference !== 0) return difference;
79
+ }
80
+ return 0;
81
+ });
82
+ }
83
+ static removeRouteMethod(route, method, path) {
84
+ route.methods = route.methods.filter((existingMethod) => existingMethod !== method);
85
+ this.routesByPathMethod.delete(`${method.toUpperCase()} ${path}`);
86
+ const methodKey = method.toUpperCase();
87
+ this.routesByMethod.set(methodKey, (this.routesByMethod.get(methodKey) ?? []).filter((existingRoute) => existingRoute !== route));
88
+ if (!route.methods.some((existingMethod) => existingMethod !== "options")) {
89
+ this.routes.delete(route);
90
+ if (route.routeName && this.routesByName.get(route.routeName) === route) this.routesByName.delete(route.routeName);
91
+ }
92
+ }
93
+ /**
48
94
  * Resets the router to it's default state
49
95
  */
50
96
  static reset() {
@@ -456,17 +502,22 @@ var CoreRouter = class {
456
502
  const fullPath = this.normalizePath(`${activePrefix}/${path}`);
457
503
  const registrationPaths = this.routeRegistrationPaths(fullPath);
458
504
  const parameters = this.parseRouteParameters(fullPath);
459
- const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
505
+ for (const method of methods) {
506
+ const existing = this.routesByPathMethod.get(`${method.toUpperCase()} ${fullPath}`);
507
+ if (existing) this.removeRouteMethod(existing, method, fullPath);
508
+ }
509
+ const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, this.resolveMiddlewares([
460
510
  ...this.globalMiddlewares,
461
511
  ...activeGroupMiddlewares,
462
512
  ...middlewares || []
463
- ], {
513
+ ]), {
464
514
  registrationPaths,
465
515
  parameters,
466
516
  onName: (name, route, previousName) => {
467
517
  if (previousName && this.routesByName.get(previousName) === route) this.routesByName.delete(previousName);
468
518
  this.routesByName.set(name, route);
469
- }
519
+ },
520
+ normalizeMiddleware: (middleware) => this.resolveMiddleware(middleware)
470
521
  });
471
522
  if (!methods.includes("options") && !this.routesByPathMethod.get(`OPTIONS ${fullPath}`)) this.options(path, this.createDefaultOptionsHandler());
472
523
  this.routes.add(route);
@@ -486,6 +537,7 @@ var CoreRouter = class {
486
537
  * @param options
487
538
  */
488
539
  static apiResource(basePath, controller, options) {
540
+ const resourceRoutes = {};
489
541
  let paramName = "id";
490
542
  if (!!this.config.inferParamName && this.hasPackageInstalled("@h3ravel/support")) {
491
543
  const { str } = createRequire(import.meta.url)("@h3ravel/support");
@@ -522,9 +574,10 @@ var CoreRouter = class {
522
574
  const { method, path } = actions[action];
523
575
  const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
524
576
  const name = `${basePath}${path}`.replace(/\/:[^/]+|\/\{[^}]+\}/g, "").replace(/\{(\w+):[^}]+\}/g, "$1").replace(/\/|:|[{}]/g, ".").replace(/\.{2,}/g, ".").replace(/^\.|\.$/g, "");
525
- this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0).name(name + "." + action.toLowerCase());
577
+ resourceRoutes[action] = this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0).name(name + "." + action.toLowerCase());
526
578
  }
527
579
  }
580
+ return new ResourceRoutes(resourceRoutes);
528
581
  }
529
582
  /**
530
583
  * Adds a new GET route to the router.
@@ -1,3 +1,2 @@
1
1
  import { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container } from "../core/bindings.mjs";
2
- import "reflect-metadata";
3
2
  export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
@@ -62,7 +62,7 @@ var Router = class Router extends require_router.CoreRouter {
62
62
  * @param options
63
63
  */
64
64
  static apiResource(basePath, controller, options) {
65
- super.apiResource(basePath, controller, options);
65
+ return super.apiResource(basePath, controller, options);
66
66
  }
67
67
  /**
68
68
  * Adds a new GET route to the router.
@@ -160,7 +160,7 @@ var Router = class Router extends require_router.CoreRouter {
160
160
  return super.route(name);
161
161
  }
162
162
  static async apply(router) {
163
- for (const route of Array.from(this.routes)) {
163
+ for (const route of this.orderedRoutes()) {
164
164
  let handlerFunction = null;
165
165
  let instance = null;
166
166
  let bindingTarget;
@@ -1,6 +1,7 @@
1
1
  import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.cjs";
2
2
  import { Handler, HttpContext, Middleware } from "../types/express.cjs";
3
3
  import { Route } from "../Route.cjs";
4
+ import { ResourceRoutes } from "../ResourceRoutes.cjs";
4
5
  import { CoreRouter } from "../core/router.cjs";
5
6
  import { Router } from "express";
6
7
 
@@ -36,7 +37,7 @@ declare class Router$1 extends CoreRouter {
36
37
  only?: ControllerAction[];
37
38
  except?: ControllerAction[];
38
39
  middlewares?: ApiResourceMiddleware<Middleware>;
39
- }): void;
40
+ }): ResourceRoutes<HttpContext, Middleware, Handler>;
40
41
  /**
41
42
  * Adds a new GET route to the router.
42
43
  *
@@ -1,6 +1,7 @@
1
1
  import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.mjs";
2
2
  import { Handler, HttpContext, Middleware } from "../types/express.mjs";
3
3
  import { Route } from "../Route.mjs";
4
+ import { ResourceRoutes } from "../ResourceRoutes.mjs";
4
5
  import { CoreRouter } from "../core/router.mjs";
5
6
  import { Router } from "express";
6
7
 
@@ -36,7 +37,7 @@ declare class Router$1 extends CoreRouter {
36
37
  only?: ControllerAction[];
37
38
  except?: ControllerAction[];
38
39
  middlewares?: ApiResourceMiddleware<Middleware>;
39
- }): void;
40
+ }): ResourceRoutes<HttpContext, Middleware, Handler>;
40
41
  /**
41
42
  * Adds a new GET route to the router.
42
43
  *
@@ -62,7 +62,7 @@ var Router = class Router extends CoreRouter {
62
62
  * @param options
63
63
  */
64
64
  static apiResource(basePath, controller, options) {
65
- super.apiResource(basePath, controller, options);
65
+ return super.apiResource(basePath, controller, options);
66
66
  }
67
67
  /**
68
68
  * Adds a new GET route to the router.
@@ -160,7 +160,7 @@ var Router = class Router extends CoreRouter {
160
160
  return super.route(name);
161
161
  }
162
162
  static async apply(router) {
163
- for (const route of Array.from(this.routes)) {
163
+ for (const route of this.orderedRoutes()) {
164
164
  let handlerFunction = null;
165
165
  let instance = null;
166
166
  let bindingTarget;
@@ -55,7 +55,7 @@ var Router = class Router extends require_router.CoreRouter {
55
55
  * @param options Optional configuration for the resource
56
56
  */
57
57
  static apiResource(basePath, controller, options) {
58
- super.apiResource(basePath, controller, options);
58
+ return super.apiResource(basePath, controller, options);
59
59
  }
60
60
  /**
61
61
  * Define a GET route
@@ -159,7 +159,7 @@ var Router = class Router extends require_router.CoreRouter {
159
159
  * @returns The Fastify application instance with the applied routes
160
160
  */
161
161
  static apply(app) {
162
- for (const route of Array.from(this.routes)) {
162
+ for (const route of this.orderedRoutes()) {
163
163
  let handlerFunction = null;
164
164
  let instance = null;
165
165
  let bindingTarget;
@@ -1,6 +1,7 @@
1
1
  import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.cjs";
2
2
  import { FastifyApp, Handler, HttpContext, Middleware } from "../types/fastify.cjs";
3
3
  import { Route } from "../Route.cjs";
4
+ import { ResourceRoutes } from "../ResourceRoutes.cjs";
4
5
  import { CoreRouter } from "../core/router.cjs";
5
6
 
6
7
  //#region src/fastify/router.d.ts
@@ -34,7 +35,7 @@ declare class Router extends CoreRouter {
34
35
  only?: ControllerAction[];
35
36
  except?: ControllerAction[];
36
37
  middlewares?: ApiResourceMiddleware<Middleware>;
37
- }): void;
38
+ }): ResourceRoutes<HttpContext, Middleware, Handler>;
38
39
  /**
39
40
  * Define a GET route
40
41
  *
@@ -1,6 +1,7 @@
1
1
  import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.mjs";
2
2
  import { FastifyApp, Handler, HttpContext, Middleware } from "../types/fastify.mjs";
3
3
  import { Route } from "../Route.mjs";
4
+ import { ResourceRoutes } from "../ResourceRoutes.mjs";
4
5
  import { CoreRouter } from "../core/router.mjs";
5
6
 
6
7
  //#region src/fastify/router.d.ts
@@ -34,7 +35,7 @@ declare class Router extends CoreRouter {
34
35
  only?: ControllerAction[];
35
36
  except?: ControllerAction[];
36
37
  middlewares?: ApiResourceMiddleware<Middleware>;
37
- }): void;
38
+ }): ResourceRoutes<HttpContext, Middleware, Handler>;
38
39
  /**
39
40
  * Define a GET route
40
41
  *
@@ -55,7 +55,7 @@ var Router = class Router extends CoreRouter {
55
55
  * @param options Optional configuration for the resource
56
56
  */
57
57
  static apiResource(basePath, controller, options) {
58
- super.apiResource(basePath, controller, options);
58
+ return super.apiResource(basePath, controller, options);
59
59
  }
60
60
  /**
61
61
  * Define a GET route
@@ -159,7 +159,7 @@ var Router = class Router extends CoreRouter {
159
159
  * @returns The Fastify application instance with the applied routes
160
160
  */
161
161
  static apply(app) {
162
- for (const route of Array.from(this.routes)) {
162
+ for (const route of this.orderedRoutes()) {
163
163
  let handlerFunction = null;
164
164
  let instance = null;
165
165
  let bindingTarget;
@@ -64,7 +64,7 @@ var Router = class Router extends require_router.CoreRouter {
64
64
  * @param options
65
65
  */
66
66
  static apiResource(basePath, controller, options) {
67
- super.apiResource(basePath, controller, options);
67
+ return super.apiResource(basePath, controller, options);
68
68
  }
69
69
  /**
70
70
  * Adds a new GET route to the router with the specified path, handler, and optional middlewares.
@@ -169,7 +169,7 @@ var Router = class Router extends require_router.CoreRouter {
169
169
  * @returns
170
170
  */
171
171
  static apply(app) {
172
- for (const route of Array.from(this.routes)) {
172
+ for (const route of this.orderedRoutes()) {
173
173
  let handlerFunction = null;
174
174
  let instance = null;
175
175
  let bindingTarget;
@@ -1,6 +1,7 @@
1
1
  import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.cjs";
2
- import { H3App, Handler, HttpContext, Middleware } from "../types/h3.cjs";
2
+ import { H3App, Handler, HttpContext, Middleware as Middleware$1 } from "../types/h3.cjs";
3
3
  import { Route } from "../Route.cjs";
4
+ import { ResourceRoutes } from "../ResourceRoutes.cjs";
4
5
  import { CoreRouter } from "../core/router.cjs";
5
6
  import { H3 } from "h3";
6
7
 
@@ -24,7 +25,7 @@ declare class Router extends CoreRouter {
24
25
  * @param handler
25
26
  * @param middlewares
26
27
  */
27
- static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
28
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
28
29
  /**
29
30
  * Define a resourceful API controller with standard CRUD routes
30
31
  *
@@ -35,8 +36,8 @@ declare class Router extends CoreRouter {
35
36
  static apiResource(basePath: string, controller: any, options?: {
36
37
  only?: ControllerAction[];
37
38
  except?: ControllerAction[];
38
- middlewares?: ApiResourceMiddleware<Middleware>;
39
- }): void;
39
+ middlewares?: ApiResourceMiddleware<Middleware$1>;
40
+ }): ResourceRoutes<HttpContext, Middleware$1, Handler>;
40
41
  /**
41
42
  * Adds a new GET route to the router with the specified path, handler, and optional middlewares.
42
43
  *
@@ -44,7 +45,7 @@ declare class Router extends CoreRouter {
44
45
  * @param handler
45
46
  * @param middlewares
46
47
  */
47
- static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
48
+ static get(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
48
49
  /**
49
50
  * Adds a new POST route to the router with the specified path, handler, and optional middlewares.
50
51
  *
@@ -52,7 +53,7 @@ declare class Router extends CoreRouter {
52
53
  * @param handler
53
54
  * @param middlewares
54
55
  */
55
- static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
56
+ static post(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
56
57
  /**
57
58
  * Adds a new PUT route to the router with the specified path, handler, and optional middlewares.
58
59
  *
@@ -60,7 +61,7 @@ declare class Router extends CoreRouter {
60
61
  * @param handler
61
62
  * @param middlewares
62
63
  */
63
- static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
64
+ static put(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
64
65
  /**
65
66
  * Adds a new DELETE route to the router with the specified path, handler, and optional middlewares.
66
67
  *
@@ -68,7 +69,7 @@ declare class Router extends CoreRouter {
68
69
  * @param handler
69
70
  * @param middlewares
70
71
  */
71
- static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
72
+ static delete(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
72
73
  /**
73
74
  * Adds a new PATCH route to the router with the specified path, handler, and optional middlewares.
74
75
  *
@@ -76,7 +77,7 @@ declare class Router extends CoreRouter {
76
77
  * @param handler
77
78
  * @param middlewares
78
79
  */
79
- static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
80
+ static patch(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
80
81
  /**
81
82
  * Adds a new OPTIONS route to the router with the specified path, handler, and optional middlewares.
82
83
  *
@@ -84,7 +85,7 @@ declare class Router extends CoreRouter {
84
85
  * @param handler
85
86
  * @param middlewares
86
87
  */
87
- static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
88
+ static options(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
88
89
  /**
89
90
  * Adds a new HEAD route to the router.
90
91
  *
@@ -92,7 +93,7 @@ declare class Router extends CoreRouter {
92
93
  * @param handler
93
94
  * @param middlewares
94
95
  */
95
- static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
96
+ static head(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
96
97
  /**
97
98
  * Defines a group of routes with a common prefix.
98
99
  *
@@ -100,25 +101,25 @@ declare class Router extends CoreRouter {
100
101
  * @param callback
101
102
  * @param middlewares
102
103
  */
103
- static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
104
+ static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware$1[]): Promise<void>;
104
105
  /**
105
106
  * Adds global middlewares to the router, which will be applied to all routes.
106
107
  *
107
108
  * @param middlewares
108
109
  * @param callback
109
110
  */
110
- static middleware(middlewares: Middleware[], callback: () => void): void;
111
+ static middleware(middlewares: Middleware$1[], callback: () => void): void;
111
112
  /**
112
113
  * Retrieves all registered routes in the router, optionally organized by path or method
113
114
  * for easier access and management.
114
115
  *
115
116
  * @param type
116
117
  */
117
- static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
118
- static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
119
- static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
120
- static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
121
- static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
118
+ static allRoutes(): Array<Route<HttpContext, Middleware$1, Handler>>;
119
+ static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware$1, Handler>>;
120
+ static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware$1, Handler>> };
121
+ static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware$1, Handler>>;
122
+ static route(name: string): Route<HttpContext, Middleware$1, Handler> | undefined;
122
123
  /**
123
124
  * Applies the registered routes to the given H3 application instance, setting up the
124
125
  * necessary handlers and middlewares for each route.
@@ -1,6 +1,7 @@
1
1
  import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.mjs";
2
- import { H3App, Handler, HttpContext, Middleware } from "../types/h3.mjs";
2
+ import { H3App, Handler, HttpContext, Middleware as Middleware$1 } from "../types/h3.mjs";
3
3
  import { Route } from "../Route.mjs";
4
+ import { ResourceRoutes } from "../ResourceRoutes.mjs";
4
5
  import { CoreRouter } from "../core/router.mjs";
5
6
  import { H3 } from "h3";
6
7
 
@@ -24,7 +25,7 @@ declare class Router extends CoreRouter {
24
25
  * @param handler
25
26
  * @param middlewares
26
27
  */
27
- static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
28
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
28
29
  /**
29
30
  * Define a resourceful API controller with standard CRUD routes
30
31
  *
@@ -35,8 +36,8 @@ declare class Router extends CoreRouter {
35
36
  static apiResource(basePath: string, controller: any, options?: {
36
37
  only?: ControllerAction[];
37
38
  except?: ControllerAction[];
38
- middlewares?: ApiResourceMiddleware<Middleware>;
39
- }): void;
39
+ middlewares?: ApiResourceMiddleware<Middleware$1>;
40
+ }): ResourceRoutes<HttpContext, Middleware$1, Handler>;
40
41
  /**
41
42
  * Adds a new GET route to the router with the specified path, handler, and optional middlewares.
42
43
  *
@@ -44,7 +45,7 @@ declare class Router extends CoreRouter {
44
45
  * @param handler
45
46
  * @param middlewares
46
47
  */
47
- static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
48
+ static get(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
48
49
  /**
49
50
  * Adds a new POST route to the router with the specified path, handler, and optional middlewares.
50
51
  *
@@ -52,7 +53,7 @@ declare class Router extends CoreRouter {
52
53
  * @param handler
53
54
  * @param middlewares
54
55
  */
55
- static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
56
+ static post(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
56
57
  /**
57
58
  * Adds a new PUT route to the router with the specified path, handler, and optional middlewares.
58
59
  *
@@ -60,7 +61,7 @@ declare class Router extends CoreRouter {
60
61
  * @param handler
61
62
  * @param middlewares
62
63
  */
63
- static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
64
+ static put(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
64
65
  /**
65
66
  * Adds a new DELETE route to the router with the specified path, handler, and optional middlewares.
66
67
  *
@@ -68,7 +69,7 @@ declare class Router extends CoreRouter {
68
69
  * @param handler
69
70
  * @param middlewares
70
71
  */
71
- static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
72
+ static delete(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
72
73
  /**
73
74
  * Adds a new PATCH route to the router with the specified path, handler, and optional middlewares.
74
75
  *
@@ -76,7 +77,7 @@ declare class Router extends CoreRouter {
76
77
  * @param handler
77
78
  * @param middlewares
78
79
  */
79
- static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
80
+ static patch(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
80
81
  /**
81
82
  * Adds a new OPTIONS route to the router with the specified path, handler, and optional middlewares.
82
83
  *
@@ -84,7 +85,7 @@ declare class Router extends CoreRouter {
84
85
  * @param handler
85
86
  * @param middlewares
86
87
  */
87
- static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
88
+ static options(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
88
89
  /**
89
90
  * Adds a new HEAD route to the router.
90
91
  *
@@ -92,7 +93,7 @@ declare class Router extends CoreRouter {
92
93
  * @param handler
93
94
  * @param middlewares
94
95
  */
95
- static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
96
+ static head(path: string, handler: Handler, middlewares?: Middleware$1[] | Middleware$1): Route<HttpContext, Middleware$1, Handler>;
96
97
  /**
97
98
  * Defines a group of routes with a common prefix.
98
99
  *
@@ -100,25 +101,25 @@ declare class Router extends CoreRouter {
100
101
  * @param callback
101
102
  * @param middlewares
102
103
  */
103
- static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
104
+ static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware$1[]): Promise<void>;
104
105
  /**
105
106
  * Adds global middlewares to the router, which will be applied to all routes.
106
107
  *
107
108
  * @param middlewares
108
109
  * @param callback
109
110
  */
110
- static middleware(middlewares: Middleware[], callback: () => void): void;
111
+ static middleware(middlewares: Middleware$1[], callback: () => void): void;
111
112
  /**
112
113
  * Retrieves all registered routes in the router, optionally organized by path or method
113
114
  * for easier access and management.
114
115
  *
115
116
  * @param type
116
117
  */
117
- static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
118
- static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
119
- static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
120
- static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
121
- static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
118
+ static allRoutes(): Array<Route<HttpContext, Middleware$1, Handler>>;
119
+ static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware$1, Handler>>;
120
+ static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware$1, Handler>> };
121
+ static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware$1, Handler>>;
122
+ static route(name: string): Route<HttpContext, Middleware$1, Handler> | undefined;
122
123
  /**
123
124
  * Applies the registered routes to the given H3 application instance, setting up the
124
125
  * necessary handlers and middlewares for each route.
@@ -64,7 +64,7 @@ var Router = class Router extends CoreRouter {
64
64
  * @param options
65
65
  */
66
66
  static apiResource(basePath, controller, options) {
67
- super.apiResource(basePath, controller, options);
67
+ return super.apiResource(basePath, controller, options);
68
68
  }
69
69
  /**
70
70
  * Adds a new GET route to the router with the specified path, handler, and optional middlewares.
@@ -169,7 +169,7 @@ var Router = class Router extends CoreRouter {
169
169
  * @returns
170
170
  */
171
171
  static apply(app) {
172
- for (const route of Array.from(this.routes)) {
172
+ for (const route of this.orderedRoutes()) {
173
173
  let handlerFunction = null;
174
174
  let instance = null;
175
175
  let bindingTarget;
@@ -68,7 +68,7 @@ var Router = class Router extends require_router.CoreRouter {
68
68
  * @param options
69
69
  */
70
70
  static apiResource(basePath, controller, options) {
71
- super.apiResource(basePath, controller, options);
71
+ return super.apiResource(basePath, controller, options);
72
72
  }
73
73
  /**
74
74
  * Define a GET route
@@ -172,7 +172,7 @@ var Router = class Router extends require_router.CoreRouter {
172
172
  * @returns The Hono application instance with the applied routes
173
173
  */
174
174
  static apply(app) {
175
- for (const route of Array.from(this.routes)) {
175
+ for (const route of this.orderedRoutes()) {
176
176
  let handlerFunction = null;
177
177
  let instance = null;
178
178
  let bindingTarget;
@@ -1,6 +1,7 @@
1
1
  import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.cjs";
2
2
  import { Handler, HonoApp, HttpContext, Middleware } from "../types/hono.cjs";
3
3
  import { Route } from "../Route.cjs";
4
+ import { ResourceRoutes } from "../ResourceRoutes.cjs";
4
5
  import { CoreRouter } from "../core/router.cjs";
5
6
 
6
7
  //#region src/hono/router.d.ts
@@ -36,7 +37,7 @@ declare class Router extends CoreRouter {
36
37
  only?: ControllerAction[];
37
38
  except?: ControllerAction[];
38
39
  middlewares?: ApiResourceMiddleware<Middleware>;
39
- }): void;
40
+ }): ResourceRoutes<HttpContext, Middleware, Handler>;
40
41
  /**
41
42
  * Define a GET route
42
43
  *
@@ -1,6 +1,7 @@
1
1
  import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.mjs";
2
2
  import { Handler, HonoApp, HttpContext, Middleware } from "../types/hono.mjs";
3
3
  import { Route } from "../Route.mjs";
4
+ import { ResourceRoutes } from "../ResourceRoutes.mjs";
4
5
  import { CoreRouter } from "../core/router.mjs";
5
6
 
6
7
  //#region src/hono/router.d.ts
@@ -36,7 +37,7 @@ declare class Router extends CoreRouter {
36
37
  only?: ControllerAction[];
37
38
  except?: ControllerAction[];
38
39
  middlewares?: ApiResourceMiddleware<Middleware>;
39
- }): void;
40
+ }): ResourceRoutes<HttpContext, Middleware, Handler>;
40
41
  /**
41
42
  * Define a GET route
42
43
  *
@@ -68,7 +68,7 @@ var Router = class Router extends CoreRouter {
68
68
  * @param options
69
69
  */
70
70
  static apiResource(basePath, controller, options) {
71
- super.apiResource(basePath, controller, options);
71
+ return super.apiResource(basePath, controller, options);
72
72
  }
73
73
  /**
74
74
  * Define a GET route
@@ -172,7 +172,7 @@ var Router = class Router extends CoreRouter {
172
172
  * @returns The Hono application instance with the applied routes
173
173
  */
174
174
  static apply(app) {
175
- for (const route of Array.from(this.routes)) {
175
+ for (const route of this.orderedRoutes()) {
176
176
  let handlerFunction = null;
177
177
  let instance = null;
178
178
  let bindingTarget;