clear-router 2.5.13 → 2.5.14

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 (38) hide show
  1. package/README.md +2 -0
  2. package/dist/core/index.cjs +1 -1
  3. package/dist/core/index.d.cts +1 -1
  4. package/dist/core/index.d.mts +1 -1
  5. package/dist/core/index.mjs +1 -1
  6. package/dist/decorators/setup.cjs +1 -1
  7. package/dist/decorators/setup.d.mts +0 -1
  8. package/dist/decorators/setup.mjs +1 -1
  9. package/dist/express/index.cjs +14 -11
  10. package/dist/express/index.d.cts +11 -9
  11. package/dist/express/index.d.mts +11 -9
  12. package/dist/express/index.mjs +14 -11
  13. package/dist/fastify/index.cjs +16 -13
  14. package/dist/fastify/index.d.cts +11 -9
  15. package/dist/fastify/index.d.mts +11 -9
  16. package/dist/fastify/index.mjs +16 -13
  17. package/dist/h3/index.cjs +14 -11
  18. package/dist/h3/index.d.cts +11 -9
  19. package/dist/h3/index.d.mts +11 -9
  20. package/dist/h3/index.mjs +14 -11
  21. package/dist/hono/index.cjs +14 -11
  22. package/dist/hono/index.d.cts +11 -9
  23. package/dist/hono/index.d.mts +11 -9
  24. package/dist/hono/index.mjs +14 -11
  25. package/dist/index.cjs +101 -11
  26. package/dist/index.d.cts +37 -10
  27. package/dist/index.d.mts +37 -10
  28. package/dist/index.mjs +101 -11
  29. package/dist/koa/index.cjs +14 -11
  30. package/dist/koa/index.d.cts +11 -9
  31. package/dist/koa/index.d.mts +11 -9
  32. package/dist/koa/index.mjs +14 -11
  33. package/dist/{router-DiKqUyvk.d.mts → router-Bk8mXRu1.d.mts} +36 -9
  34. package/dist/{router-D9H9-T27.cjs → router-CAnd539U.cjs} +101 -11
  35. package/dist/{router-CPmZcbU0.mjs → router-DSq9dtuQ.mjs} +101 -11
  36. package/dist/{router-DTI0BowV.d.cts → router-jwZwD8ZT.d.cts} +36 -9
  37. package/dist/types/Route.d.mts +16 -1
  38. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import { C as ControllerAction, E as Response, S as ApiResourceMiddleware, T as HttpMethod, m as Route, p as Request, t as CoreRouter, w as ControllerHandler } from "../router-DTI0BowV.cjs";
1
+ import { C as ControllerAction, E as Response, S as ApiResourceMiddleware, T as HttpMethod, m as Route, p as Request, t as CoreRouter, w as ControllerHandler } from "../router-jwZwD8ZT.cjs";
2
2
  import { Context, HonoRequest, MiddlewareHandler } from "hono";
3
3
 
4
4
  //#region types/hono.d.ts
@@ -36,7 +36,7 @@ declare class Router extends CoreRouter {
36
36
  * @param handler
37
37
  * @param middlewares
38
38
  */
39
- static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
39
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
40
40
  /**
41
41
  * Define a resourceful API controller with standard CRUD routes
42
42
  *
@@ -56,7 +56,7 @@ declare class Router extends CoreRouter {
56
56
  * @param handler
57
57
  * @param middlewares
58
58
  */
59
- static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
59
+ static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
60
60
  /**
61
61
  * Define a POST route
62
62
  *
@@ -64,7 +64,7 @@ declare class Router extends CoreRouter {
64
64
  * @param handler
65
65
  * @param middlewares
66
66
  */
67
- static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
67
+ static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
68
68
  /**
69
69
  * Define a PUT route
70
70
  *
@@ -72,7 +72,7 @@ declare class Router extends CoreRouter {
72
72
  * @param handler
73
73
  * @param middlewares
74
74
  */
75
- static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
75
+ static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
76
76
  /**
77
77
  * Define a DELETE route
78
78
  *
@@ -80,7 +80,7 @@ declare class Router extends CoreRouter {
80
80
  * @param handler
81
81
  * @param middlewares
82
82
  */
83
- static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
83
+ static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
84
84
  /**
85
85
  * Define a PATCH route
86
86
  *
@@ -88,7 +88,7 @@ declare class Router extends CoreRouter {
88
88
  * @param handler
89
89
  * @param middlewares
90
90
  */
91
- static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
91
+ static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
92
92
  /**
93
93
  * Define an OPTIONS route
94
94
  *
@@ -96,7 +96,7 @@ declare class Router extends CoreRouter {
96
96
  * @param handler
97
97
  * @param middlewares
98
98
  */
99
- static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
99
+ static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
100
100
  /**
101
101
  * Define a HEAD route
102
102
  *
@@ -104,7 +104,7 @@ declare class Router extends CoreRouter {
104
104
  * @param handler
105
105
  * @param middlewares
106
106
  */
107
- static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
107
+ static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
108
108
  /**
109
109
  * Defines a group of routes with a common prefix.
110
110
  *
@@ -132,6 +132,8 @@ declare class Router extends CoreRouter {
132
132
  * @param type - 'method' to get routes organized by method
133
133
  */
134
134
  static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
135
+ static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
136
+ static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
135
137
  /**
136
138
  * Apply the defined routes to a Hono application instance
137
139
  *
@@ -1,4 +1,4 @@
1
- import { C as ControllerAction, E as Response, S as ApiResourceMiddleware, T as HttpMethod, m as Route, p as Request, t as CoreRouter, w as ControllerHandler } from "../router-DiKqUyvk.mjs";
1
+ import { C as ControllerAction, E as Response, S as ApiResourceMiddleware, T as HttpMethod, m as Route, p as Request, t as CoreRouter, w as ControllerHandler } from "../router-Bk8mXRu1.mjs";
2
2
  import { Context, HonoRequest, MiddlewareHandler } from "hono";
3
3
 
4
4
  //#region types/hono.d.ts
@@ -36,7 +36,7 @@ declare class Router extends CoreRouter {
36
36
  * @param handler
37
37
  * @param middlewares
38
38
  */
39
- static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
39
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
40
40
  /**
41
41
  * Define a resourceful API controller with standard CRUD routes
42
42
  *
@@ -56,7 +56,7 @@ declare class Router extends CoreRouter {
56
56
  * @param handler
57
57
  * @param middlewares
58
58
  */
59
- static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
59
+ static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
60
60
  /**
61
61
  * Define a POST route
62
62
  *
@@ -64,7 +64,7 @@ declare class Router extends CoreRouter {
64
64
  * @param handler
65
65
  * @param middlewares
66
66
  */
67
- static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
67
+ static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
68
68
  /**
69
69
  * Define a PUT route
70
70
  *
@@ -72,7 +72,7 @@ declare class Router extends CoreRouter {
72
72
  * @param handler
73
73
  * @param middlewares
74
74
  */
75
- static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
75
+ static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
76
76
  /**
77
77
  * Define a DELETE route
78
78
  *
@@ -80,7 +80,7 @@ declare class Router extends CoreRouter {
80
80
  * @param handler
81
81
  * @param middlewares
82
82
  */
83
- static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
83
+ static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
84
84
  /**
85
85
  * Define a PATCH route
86
86
  *
@@ -88,7 +88,7 @@ declare class Router extends CoreRouter {
88
88
  * @param handler
89
89
  * @param middlewares
90
90
  */
91
- static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
91
+ static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
92
92
  /**
93
93
  * Define an OPTIONS route
94
94
  *
@@ -96,7 +96,7 @@ declare class Router extends CoreRouter {
96
96
  * @param handler
97
97
  * @param middlewares
98
98
  */
99
- static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
99
+ static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
100
100
  /**
101
101
  * Define a HEAD route
102
102
  *
@@ -104,7 +104,7 @@ declare class Router extends CoreRouter {
104
104
  * @param handler
105
105
  * @param middlewares
106
106
  */
107
- static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
107
+ static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
108
108
  /**
109
109
  * Defines a group of routes with a common prefix.
110
110
  *
@@ -132,6 +132,8 @@ declare class Router extends CoreRouter {
132
132
  * @param type - 'method' to get routes organized by method
133
133
  */
134
134
  static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
135
+ static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
136
+ static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
135
137
  /**
136
138
  * Apply the defined routes to a Hono application instance
137
139
  *
@@ -1,5 +1,5 @@
1
1
  import "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-CPmZcbU0.mjs";
2
+ import { t as CoreRouter } from "../router-DSq9dtuQ.mjs";
3
3
  import { n as resolveResponseMeta } from "../responses-BvETUeDL.mjs";
4
4
 
5
5
  //#region src/hono/router.ts
@@ -59,7 +59,7 @@ var Router = class Router extends CoreRouter {
59
59
  * @param middlewares
60
60
  */
61
61
  static add(methods, path, handler, middlewares) {
62
- super.add(methods, path, handler, middlewares);
62
+ return super.add(methods, path, handler, middlewares);
63
63
  }
64
64
  /**
65
65
  * Define a resourceful API controller with standard CRUD routes
@@ -79,7 +79,7 @@ var Router = class Router extends CoreRouter {
79
79
  * @param middlewares
80
80
  */
81
81
  static get(path, handler, middlewares) {
82
- super.get(path, handler, middlewares);
82
+ return super.get(path, handler, middlewares);
83
83
  }
84
84
  /**
85
85
  * Define a POST route
@@ -89,7 +89,7 @@ var Router = class Router extends CoreRouter {
89
89
  * @param middlewares
90
90
  */
91
91
  static post(path, handler, middlewares) {
92
- super.post(path, handler, middlewares);
92
+ return super.post(path, handler, middlewares);
93
93
  }
94
94
  /**
95
95
  * Define a PUT route
@@ -99,7 +99,7 @@ var Router = class Router extends CoreRouter {
99
99
  * @param middlewares
100
100
  */
101
101
  static put(path, handler, middlewares) {
102
- super.put(path, handler, middlewares);
102
+ return super.put(path, handler, middlewares);
103
103
  }
104
104
  /**
105
105
  * Define a DELETE route
@@ -109,7 +109,7 @@ var Router = class Router extends CoreRouter {
109
109
  * @param middlewares
110
110
  */
111
111
  static delete(path, handler, middlewares) {
112
- super.delete(path, handler, middlewares);
112
+ return super.delete(path, handler, middlewares);
113
113
  }
114
114
  /**
115
115
  * Define a PATCH route
@@ -119,7 +119,7 @@ var Router = class Router extends CoreRouter {
119
119
  * @param middlewares
120
120
  */
121
121
  static patch(path, handler, middlewares) {
122
- super.patch(path, handler, middlewares);
122
+ return super.patch(path, handler, middlewares);
123
123
  }
124
124
  /**
125
125
  * Define an OPTIONS route
@@ -129,7 +129,7 @@ var Router = class Router extends CoreRouter {
129
129
  * @param middlewares
130
130
  */
131
131
  static options(path, handler, middlewares) {
132
- super.options(path, handler, middlewares);
132
+ return super.options(path, handler, middlewares);
133
133
  }
134
134
  /**
135
135
  * Define a HEAD route
@@ -139,7 +139,7 @@ var Router = class Router extends CoreRouter {
139
139
  * @param middlewares
140
140
  */
141
141
  static head(path, handler, middlewares) {
142
- super.head(path, handler, middlewares);
142
+ return super.head(path, handler, middlewares);
143
143
  }
144
144
  /**
145
145
  * Defines a group of routes with a common prefix.
@@ -163,6 +163,9 @@ var Router = class Router extends CoreRouter {
163
163
  static allRoutes(type) {
164
164
  return super.allRoutes(type);
165
165
  }
166
+ static route(name) {
167
+ return super.route(name);
168
+ }
166
169
  /**
167
170
  * Apply the defined routes to a Hono application instance
168
171
  *
@@ -202,7 +205,7 @@ var Router = class Router extends CoreRouter {
202
205
  ];
203
206
  if (method === "options" && route.methods.length > 1) continue;
204
207
  if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
205
- app[method](route.path, ...route.middlewares || [], async (context) => {
208
+ for (const registrationPath of route.registrationPaths) app[method](registrationPath, ...route.middlewares || [], async (context) => {
206
209
  const ctx = context;
207
210
  const reqBody = await Router.readBodyCached(ctx);
208
211
  const override = Router.resolveMethodOverride(ctx.req.method, ctx.req.header(), reqBody);
@@ -223,7 +226,7 @@ var Router = class Router extends CoreRouter {
223
226
  "put",
224
227
  "patch",
225
228
  "delete"
226
- ].includes(method)) app.post(route.path, ...route.middlewares || [], async (context) => {
229
+ ].includes(method)) for (const registrationPath of route.registrationPaths) app.post(registrationPath, ...route.middlewares || [], async (context) => {
227
230
  const ctx = context;
228
231
  const reqBody = await Router.readBodyCached(ctx);
229
232
  if (Router.resolveMethodOverride(ctx.req.method, ctx.req.header(), reqBody) !== method) return;
package/dist/index.cjs CHANGED
@@ -41,21 +41,49 @@ var Route = class {
41
41
  clearRequest;
42
42
  methods;
43
43
  path;
44
+ registrationPaths;
45
+ parameters;
46
+ routeName;
44
47
  handler;
45
48
  middlewares;
46
49
  controllerName;
47
50
  actionName;
48
51
  handlerType;
49
52
  middlewareCount;
50
- constructor(methods, path, handler, middlewares = []) {
53
+ constructor(methods, path, handler, middlewares = [], options = {}) {
51
54
  this.methods = methods;
52
55
  this.path = path;
56
+ this.registrationPaths = options.registrationPaths || [path];
57
+ this.parameters = options.parameters || [];
53
58
  this.handler = handler;
54
59
  this.middlewares = middlewares;
55
60
  this.handlerType = Array.isArray(handler) ? "controller" : "function";
56
61
  this.middlewareCount = middlewares.length;
57
62
  this.controllerName = Array.isArray(handler) ? handler[0]?.name : void 0;
58
63
  this.actionName = Array.isArray(handler) ? handler[1] : typeof handler === "function" ? handler.constructor.name ?? handler.name : void 0;
64
+ this.onName = options.onName;
65
+ }
66
+ onName;
67
+ name(name) {
68
+ const previousName = this.routeName;
69
+ this.routeName = name;
70
+ this.onName?.(name, this, previousName);
71
+ return this;
72
+ }
73
+ toPath(params = {}) {
74
+ return this.path.replace(/\/?\{([^{}]+)\}/g, (segment, raw) => {
75
+ const optional = raw.endsWith("?");
76
+ const [rawName, rawField] = (optional ? raw.slice(0, -1) : raw).split(":", 2);
77
+ const name = rawName.trim();
78
+ const field = rawField?.trim();
79
+ const value = params[name];
80
+ const resolved = field && value && typeof value === "object" ? value[field] : value;
81
+ if (typeof resolved === "undefined" || resolved === null || resolved === "") {
82
+ if (optional) return "";
83
+ throw new Error(`Missing required route parameter: ${name}`);
84
+ }
85
+ return `${segment.startsWith("/") ? "/" : ""}${encodeURIComponent(String(resolved))}`;
86
+ }) || "/";
59
87
  }
60
88
  };
61
89
 
@@ -334,6 +362,7 @@ var CoreRouter = class {
334
362
  routes: [],
335
363
  routesByPathMethod: {},
336
364
  routesByMethod: {},
365
+ routesByName: {},
337
366
  prefix: "",
338
367
  groupMiddlewares: [],
339
368
  globalMiddlewares: []
@@ -350,6 +379,7 @@ var CoreRouter = class {
350
379
  "routes",
351
380
  "routesByPathMethod",
352
381
  "routesByMethod",
382
+ "routesByName",
353
383
  "prefix",
354
384
  "groupMiddlewares",
355
385
  "globalMiddlewares"
@@ -482,6 +512,7 @@ var CoreRouter = class {
482
512
  static routes = [];
483
513
  static routesByPathMethod = {};
484
514
  static routesByMethod = {};
515
+ static routesByName = {};
485
516
  static prefix = "";
486
517
  static groupMiddlewares = [];
487
518
  static globalMiddlewares = [];
@@ -533,6 +564,7 @@ var CoreRouter = class {
533
564
  if (!Array.isArray(this.routes)) this.routes = [];
534
565
  if (!this.routesByPathMethod || typeof this.routesByPathMethod !== "object") this.routesByPathMethod = {};
535
566
  if (!this.routesByMethod || typeof this.routesByMethod !== "object") this.routesByMethod = {};
567
+ if (!this.routesByName || typeof this.routesByName !== "object") this.routesByName = {};
536
568
  if (typeof this.prefix !== "string") this.prefix = "";
537
569
  if (!Array.isArray(this.groupMiddlewares)) this.groupMiddlewares = [];
538
570
  if (!Array.isArray(this.globalMiddlewares)) this.globalMiddlewares = [];
@@ -547,6 +579,47 @@ var CoreRouter = class {
547
579
  static normalizePath(path) {
548
580
  return "/" + path.split("/").filter(Boolean).join("/");
549
581
  }
582
+ static parseRouteParameters(path) {
583
+ const parameters = [];
584
+ const seen = /* @__PURE__ */ new Set();
585
+ const pattern = /\{([^{}]+)\}/g;
586
+ let match;
587
+ while ((match = pattern.exec(path)) !== null) {
588
+ const raw = match[1].trim();
589
+ const optional = raw.endsWith("?");
590
+ const [name, field] = (optional ? raw.slice(0, -1) : raw).split(":", 2).map((part) => part.trim());
591
+ if (!name || seen.has(name)) continue;
592
+ seen.add(name);
593
+ parameters.push({
594
+ name,
595
+ field: field || void 0,
596
+ optional
597
+ });
598
+ }
599
+ return parameters;
600
+ }
601
+ static expandRoutePath(path) {
602
+ let paths = [""];
603
+ const segments = this.normalizePath(path).split("/").filter(Boolean);
604
+ for (const segment of segments) {
605
+ const match = segment.match(/^\{([^{}]+)\}$/);
606
+ if (!match) {
607
+ paths = paths.map((current) => `${current}/${segment}`);
608
+ continue;
609
+ }
610
+ const raw = match[1].trim();
611
+ const optional = raw.endsWith("?");
612
+ const [rawName] = (optional ? raw.slice(0, -1) : raw).split(":", 2);
613
+ const name = rawName.trim();
614
+ if (!name) continue;
615
+ const parameterSegment = `/:${name}`;
616
+ paths = optional ? paths.flatMap((current) => [current, `${current}${parameterSegment}`]) : paths.map((current) => `${current}${parameterSegment}`);
617
+ }
618
+ return paths.map((path) => path || "/");
619
+ }
620
+ static routeRegistrationPaths(path) {
621
+ return this.expandRoutePath(path);
622
+ }
550
623
  /**
551
624
  * Configures the router with the given options, such as method override settings.
552
625
  *
@@ -620,11 +693,20 @@ var CoreRouter = class {
620
693
  methods = Array.isArray(methods) ? methods : [methods];
621
694
  middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
622
695
  const fullPath = this.normalizePath(`${activePrefix}/${path}`);
696
+ const registrationPaths = this.routeRegistrationPaths(fullPath);
697
+ const parameters = this.parseRouteParameters(fullPath);
623
698
  const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
624
699
  ...this.globalMiddlewares,
625
700
  ...activeGroupMiddlewares,
626
701
  ...middlewares || []
627
- ]);
702
+ ], {
703
+ registrationPaths,
704
+ parameters,
705
+ onName: (name, route, previousName) => {
706
+ if (previousName && this.routesByName[previousName] === route) delete this.routesByName[previousName];
707
+ this.routesByName[name] = route;
708
+ }
709
+ });
628
710
  if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, this.createDefaultOptionsHandler());
629
711
  this.routes.push(route);
630
712
  for (const method of methods.map((m) => m.toUpperCase())) {
@@ -632,6 +714,7 @@ var CoreRouter = class {
632
714
  if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
633
715
  this.routesByMethod[method].push(route);
634
716
  }
717
+ return route;
635
718
  }
636
719
  /**
637
720
  * Define a resourceful API controller with standard CRUD routes.
@@ -685,7 +768,7 @@ var CoreRouter = class {
685
768
  * @param middlewares Optional middlewares to apply to the GET route.
686
769
  */
687
770
  static get(path, handler, middlewares) {
688
- this.add("get", path, handler, middlewares);
771
+ return this.add("get", path, handler, middlewares);
689
772
  }
690
773
  /**
691
774
  * Adds a new POST route to the router.
@@ -696,7 +779,7 @@ var CoreRouter = class {
696
779
  * @param middlewares
697
780
  */
698
781
  static post(path, handler, middlewares) {
699
- this.add("post", path, handler, middlewares);
782
+ return this.add("post", path, handler, middlewares);
700
783
  }
701
784
  /**
702
785
  * Adds a new PUT route to the router.
@@ -707,7 +790,7 @@ var CoreRouter = class {
707
790
  * @param middlewares
708
791
  */
709
792
  static put(path, handler, middlewares) {
710
- this.add("put", path, handler, middlewares);
793
+ return this.add("put", path, handler, middlewares);
711
794
  }
712
795
  /**
713
796
  * Adds a new DELETE route to the router.
@@ -718,7 +801,7 @@ var CoreRouter = class {
718
801
  * @param middlewares
719
802
  */
720
803
  static delete(path, handler, middlewares) {
721
- this.add("delete", path, handler, middlewares);
804
+ return this.add("delete", path, handler, middlewares);
722
805
  }
723
806
  /**
724
807
  * Adds a new PATCH route to the router.
@@ -729,7 +812,7 @@ var CoreRouter = class {
729
812
  * @param middlewares
730
813
  */
731
814
  static patch(path, handler, middlewares) {
732
- this.add("patch", path, handler, middlewares);
815
+ return this.add("patch", path, handler, middlewares);
733
816
  }
734
817
  /**
735
818
  * Adds a new OPTIONS route to the router.
@@ -740,7 +823,7 @@ var CoreRouter = class {
740
823
  * @param middlewares
741
824
  */
742
825
  static options(path, handler, middlewares) {
743
- this.add("options", path, handler, middlewares);
826
+ return this.add("options", path, handler, middlewares);
744
827
  }
745
828
  /**
746
829
  * Adds a new HEAD route to the router.
@@ -751,7 +834,7 @@ var CoreRouter = class {
751
834
  * @param middlewares
752
835
  */
753
836
  static head(path, handler, middlewares) {
754
- this.add("head", path, handler, middlewares);
837
+ return this.add("head", path, handler, middlewares);
755
838
  }
756
839
  /**
757
840
  * Defines a group of routes with a common prefix.
@@ -793,8 +876,16 @@ var CoreRouter = class {
793
876
  this.ensureState();
794
877
  if (type === "method") return this.routesByMethod;
795
878
  if (type === "path") return this.routesByPathMethod;
879
+ if (type === "name") return this.routesByName;
796
880
  return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
797
881
  }
882
+ static route(name) {
883
+ this.ensureState();
884
+ return this.routesByName[name];
885
+ }
886
+ static url(name, params) {
887
+ return this.route(name)?.toPath(params);
888
+ }
798
889
  static resolveHandler(route) {
799
890
  let handlerFunction;
800
891
  let instance = null;
@@ -861,8 +952,7 @@ var CoreRouter = class {
861
952
  designTokens
862
953
  });
863
954
  if (pluginArgs) return handlerFunction(...pluginArgs);
864
- if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
865
- if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
955
+ if (!metadata || !tokens.length) return handlerFunction(ctx, ctx.clearRequest);
866
956
  const args = [];
867
957
  for (const token of tokens) {
868
958
  const resolved = await Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
package/dist/index.d.cts CHANGED
@@ -58,6 +58,11 @@ declare class Response$1 {
58
58
  }
59
59
  //#endregion
60
60
  //#region src/Route.d.ts
61
+ interface RouteParameter {
62
+ name: string;
63
+ field?: string;
64
+ optional: boolean;
65
+ }
61
66
  declare class Route<X = any, M = Middleware$1 | Middleware, H = any> {
62
67
  ctx: X;
63
68
  body: RequestData;
@@ -66,13 +71,23 @@ declare class Route<X = any, M = Middleware$1 | Middleware, H = any> {
66
71
  clearRequest: ClearRequest;
67
72
  methods: HttpMethod[];
68
73
  path: string;
74
+ registrationPaths: string[];
75
+ parameters: RouteParameter[];
76
+ routeName?: string;
69
77
  handler: H;
70
78
  middlewares: M[];
71
79
  controllerName?: string;
72
80
  actionName?: string;
73
81
  handlerType: 'function' | 'controller';
74
82
  middlewareCount: number;
75
- constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[]);
83
+ constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[], options?: {
84
+ registrationPaths?: string[];
85
+ parameters?: RouteParameter[];
86
+ onName?: (name: string, route: Route<X, M, H>, previousName?: string) => void;
87
+ });
88
+ private onName?;
89
+ name(name: string): this;
90
+ toPath(params?: RequestData): string;
76
91
  }
77
92
  //#endregion
78
93
  //#region src/core/Request.d.ts
@@ -222,6 +237,7 @@ declare abstract class CoreRouter {
222
237
  routes: Array<Route<any, any, any>>;
223
238
  routesByPathMethod: Record<string, Route<any, any, any>>;
224
239
  routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
240
+ routesByName: Record<string, Route<any, any, any>>;
225
241
  prefix: string;
226
242
  groupMiddlewares: any[];
227
243
  globalMiddlewares: any[];
@@ -248,6 +264,7 @@ declare abstract class CoreRouter {
248
264
  static routes: Array<Route<any, any, any>>;
249
265
  static routesByPathMethod: Record<string, Route<any, any, any>>;
250
266
  static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
267
+ static routesByName: Record<string, Route<any, any, any>>;
251
268
  static prefix: string;
252
269
  static groupMiddlewares: any[];
253
270
  static globalMiddlewares: any[];
@@ -264,6 +281,13 @@ declare abstract class CoreRouter {
264
281
  * @returns The normalized path.
265
282
  */
266
283
  static normalizePath(path: string): string;
284
+ protected static parseRouteParameters(path: string): Array<{
285
+ name: string;
286
+ field?: string;
287
+ optional: boolean;
288
+ }>;
289
+ protected static expandRoutePath(path: string): string[];
290
+ protected static routeRegistrationPaths(path: string): string[];
267
291
  /**
268
292
  * Configures the router with the given options, such as method override settings.
269
293
  *
@@ -282,7 +306,7 @@ declare abstract class CoreRouter {
282
306
  * @param handler
283
307
  * @param middlewares
284
308
  */
285
- static add(methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): void;
309
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
286
310
  /**
287
311
  * Define a resourceful API controller with standard CRUD routes.
288
312
  *
@@ -304,7 +328,7 @@ declare abstract class CoreRouter {
304
328
  * @param handler The handler function for the GET route.
305
329
  * @param middlewares Optional middlewares to apply to the GET route.
306
330
  */
307
- static get(path: string, handler: any, middlewares?: any[] | any): void;
331
+ static get(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
308
332
  /**
309
333
  * Adds a new POST route to the router.
310
334
  *
@@ -313,7 +337,7 @@ declare abstract class CoreRouter {
313
337
  * @param handler
314
338
  * @param middlewares
315
339
  */
316
- static post(path: string, handler: any, middlewares?: any[] | any): void;
340
+ static post(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
317
341
  /**
318
342
  * Adds a new PUT route to the router.
319
343
  *
@@ -322,7 +346,7 @@ declare abstract class CoreRouter {
322
346
  * @param handler
323
347
  * @param middlewares
324
348
  */
325
- static put(path: string, handler: any, middlewares?: any[] | any): void;
349
+ static put(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
326
350
  /**
327
351
  * Adds a new DELETE route to the router.
328
352
  *
@@ -331,7 +355,7 @@ declare abstract class CoreRouter {
331
355
  * @param handler
332
356
  * @param middlewares
333
357
  */
334
- static delete(path: string, handler: any, middlewares?: any[] | any): void;
358
+ static delete(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
335
359
  /**
336
360
  * Adds a new PATCH route to the router.
337
361
  *
@@ -340,7 +364,7 @@ declare abstract class CoreRouter {
340
364
  * @param handler
341
365
  * @param middlewares
342
366
  */
343
- static patch(path: string, handler: any, middlewares?: any[] | any): void;
367
+ static patch(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
344
368
  /**
345
369
  * Adds a new OPTIONS route to the router.
346
370
  *
@@ -349,7 +373,7 @@ declare abstract class CoreRouter {
349
373
  * @param handler
350
374
  * @param middlewares
351
375
  */
352
- static options(path: string, handler: any, middlewares?: any[] | any): void;
376
+ static options(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
353
377
  /**
354
378
  * Adds a new HEAD route to the router.
355
379
  *
@@ -358,7 +382,7 @@ declare abstract class CoreRouter {
358
382
  * @param handler
359
383
  * @param middlewares
360
384
  */
361
- static head(path: string, handler: any, middlewares?: any[] | any): void;
385
+ static head(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
362
386
  /**
363
387
  * Defines a group of routes with a common prefix.
364
388
  *
@@ -393,6 +417,9 @@ declare abstract class CoreRouter {
393
417
  * @param type - 'method' to get routes organized by method
394
418
  */
395
419
  static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
420
+ static allRoutes(type: 'name'): Record<string, Route<any, any, any>>;
421
+ static route(name: string): Route<any, any, any> | undefined;
422
+ static url(name: string, params?: Record<string, any>): string | undefined;
396
423
  protected static resolveHandler(route: Route<any, any, any>): {
397
424
  handlerFunction: ((ctx: any, req: Request) => any | Promise<any>) | null;
398
425
  instance: Controller<any> | null;
@@ -410,4 +437,4 @@ declare abstract class CoreRouter {
410
437
  }): void;
411
438
  }
412
439
  //#endregion
413
- export { ClearRequest, ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, Controller, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response$1 as Response, Route, definePlugin };
440
+ export { ClearRequest, ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, Controller, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response$1 as Response, Route, RouteParameter, definePlugin };