clear-router 2.1.6 → 2.1.8
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.
- package/dist/{express-D9GR9yTH.d.mts → basic-Chn8OGPD.d.cts} +21 -20
- package/dist/{express-JHxK-EqQ.d.cts → basic-DJmmZq1h.d.mts} +21 -20
- package/dist/express/index.cjs +32 -12
- package/dist/express/index.d.cts +21 -10
- package/dist/express/index.d.mts +21 -10
- package/dist/express/index.mjs +32 -12
- package/dist/h3/index.cjs +32 -12
- package/dist/h3/index.d.cts +21 -11
- package/dist/h3/index.d.mts +21 -11
- package/dist/h3/index.mjs +32 -12
- package/dist/index.d.cts +23 -25
- package/dist/index.d.mts +23 -25
- package/dist/types/ClearRequest.d.mts +1 -1
- package/dist/types/Route.d.mts +1 -1
- package/dist/types/basic.d.mts +5 -1
- package/dist/types/express.d.mts +1 -1
- package/dist/types/h3.d.mts +1 -1
- package/package.json +2 -1
|
@@ -1,24 +1,6 @@
|
|
|
1
|
-
import { H3, H3Event, Middleware as Middleware$1, TypedServerRequest } from "h3";
|
|
2
1
|
import { NextFunction, Request, Response as Response$1 } from "express";
|
|
2
|
+
import { H3, H3Event, Middleware as Middleware$1, TypedServerRequest } from "h3";
|
|
3
3
|
|
|
4
|
-
//#region types/basic.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Controller method reference
|
|
7
|
-
*/
|
|
8
|
-
type ControllerHandler = [any, string];
|
|
9
|
-
/**
|
|
10
|
-
* HTTP methods supported by the router
|
|
11
|
-
*/
|
|
12
|
-
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
13
|
-
/**
|
|
14
|
-
* Common controller action names
|
|
15
|
-
*/
|
|
16
|
-
type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
|
|
17
|
-
/**
|
|
18
|
-
* Generic Object type for request data
|
|
19
|
-
*/
|
|
20
|
-
type RequestData = Record<string, any>;
|
|
21
|
-
//#endregion
|
|
22
4
|
//#region types/h3.d.ts
|
|
23
5
|
type H3App = Omit<H3['fetch'], 'fetch'> & {
|
|
24
6
|
fetch: (request: TypedServerRequest) => Promise<Response>;
|
|
@@ -115,4 +97,23 @@ type Handler = RouteHandler | ControllerHandler;
|
|
|
115
97
|
*/
|
|
116
98
|
type Middleware = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
|
|
117
99
|
//#endregion
|
|
118
|
-
|
|
100
|
+
//#region types/basic.d.ts
|
|
101
|
+
/**
|
|
102
|
+
* Controller method reference
|
|
103
|
+
*/
|
|
104
|
+
type ControllerHandler = [any, string];
|
|
105
|
+
/**
|
|
106
|
+
* HTTP methods supported by the router
|
|
107
|
+
*/
|
|
108
|
+
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
109
|
+
/**
|
|
110
|
+
* Common controller action names
|
|
111
|
+
*/
|
|
112
|
+
type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
|
|
113
|
+
/**
|
|
114
|
+
* Generic Object type for request data
|
|
115
|
+
*/
|
|
116
|
+
type RequestData = Record<string, any>;
|
|
117
|
+
type ApiResourceMiddleware<M extends Middleware | Middleware$1> = M | M[] | { [K in ControllerAction]?: M | M[] };
|
|
118
|
+
//#endregion
|
|
119
|
+
export { HttpContext as a, H3App as c, Middleware$1 as d, Handler as i, Handler$1 as l, ControllerAction as n, Middleware as o, HttpMethod as r, Route as s, ApiResourceMiddleware as t, HttpContext$1 as u };
|
|
@@ -1,24 +1,6 @@
|
|
|
1
|
-
import { NextFunction, Request, Response as Response$1 } from "express";
|
|
2
1
|
import { H3, H3Event, Middleware as Middleware$1, TypedServerRequest } from "h3";
|
|
2
|
+
import { NextFunction, Request, Response as Response$1 } from "express";
|
|
3
3
|
|
|
4
|
-
//#region types/basic.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Controller method reference
|
|
7
|
-
*/
|
|
8
|
-
type ControllerHandler = [any, string];
|
|
9
|
-
/**
|
|
10
|
-
* HTTP methods supported by the router
|
|
11
|
-
*/
|
|
12
|
-
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
13
|
-
/**
|
|
14
|
-
* Common controller action names
|
|
15
|
-
*/
|
|
16
|
-
type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
|
|
17
|
-
/**
|
|
18
|
-
* Generic Object type for request data
|
|
19
|
-
*/
|
|
20
|
-
type RequestData = Record<string, any>;
|
|
21
|
-
//#endregion
|
|
22
4
|
//#region types/h3.d.ts
|
|
23
5
|
type H3App = Omit<H3['fetch'], 'fetch'> & {
|
|
24
6
|
fetch: (request: TypedServerRequest) => Promise<Response>;
|
|
@@ -115,4 +97,23 @@ type Handler = RouteHandler | ControllerHandler;
|
|
|
115
97
|
*/
|
|
116
98
|
type Middleware = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
|
|
117
99
|
//#endregion
|
|
118
|
-
|
|
100
|
+
//#region types/basic.d.ts
|
|
101
|
+
/**
|
|
102
|
+
* Controller method reference
|
|
103
|
+
*/
|
|
104
|
+
type ControllerHandler = [any, string];
|
|
105
|
+
/**
|
|
106
|
+
* HTTP methods supported by the router
|
|
107
|
+
*/
|
|
108
|
+
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
109
|
+
/**
|
|
110
|
+
* Common controller action names
|
|
111
|
+
*/
|
|
112
|
+
type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
|
|
113
|
+
/**
|
|
114
|
+
* Generic Object type for request data
|
|
115
|
+
*/
|
|
116
|
+
type RequestData = Record<string, any>;
|
|
117
|
+
type ApiResourceMiddleware<M extends Middleware | Middleware$1> = M | M[] | { [K in ControllerAction]?: M | M[] };
|
|
118
|
+
//#endregion
|
|
119
|
+
export { HttpContext as a, H3App as c, Middleware$1 as d, Handler as i, Handler$1 as l, ControllerAction as n, Middleware as o, HttpMethod as r, Route as s, ApiResourceMiddleware as t, HttpContext$1 as u };
|
package/dist/express/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,24 @@ var Router = class Router {
|
|
|
42
50
|
* @param middlewares - Array of middleware functions
|
|
43
51
|
*/
|
|
44
52
|
static add(methods, path, handler, middlewares) {
|
|
45
|
-
|
|
53
|
+
methods = Array.isArray(methods) ? methods : [methods];
|
|
54
|
+
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
46
55
|
const fullPath = this.normalizePath(`${this.prefix}/${path}`);
|
|
47
|
-
|
|
56
|
+
const route = new require_Route.Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
48
57
|
...this.globalMiddlewares,
|
|
49
58
|
...this.groupMiddlewares,
|
|
50
59
|
...middlewares || []
|
|
51
|
-
])
|
|
60
|
+
]);
|
|
61
|
+
if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
|
|
62
|
+
res.set("Allow", "GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD");
|
|
63
|
+
res.sendStatus(204);
|
|
64
|
+
});
|
|
65
|
+
this.routes.push(route);
|
|
66
|
+
for (const method of methods.map((m) => m.toUpperCase())) {
|
|
67
|
+
this.routesByPathMethod[`${method.toUpperCase()} ${fullPath}`] = route;
|
|
68
|
+
if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
|
|
69
|
+
this.routesByMethod[method].push(route);
|
|
70
|
+
}
|
|
52
71
|
}
|
|
53
72
|
/**
|
|
54
73
|
* Register RESTful API resource routes for a controller with optional action filtering
|
|
@@ -87,7 +106,8 @@ var Router = class Router {
|
|
|
87
106
|
if (except.includes(action)) continue;
|
|
88
107
|
if (typeof preController[action] === "function") {
|
|
89
108
|
const { method, path } = actions[action];
|
|
90
|
-
|
|
109
|
+
const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
|
|
110
|
+
this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0);
|
|
91
111
|
}
|
|
92
112
|
}
|
|
93
113
|
}
|
|
@@ -184,12 +204,10 @@ var Router = class Router {
|
|
|
184
204
|
callback();
|
|
185
205
|
this.globalMiddlewares = prevMiddlewares;
|
|
186
206
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
static allRoutes() {
|
|
192
|
-
return this.routes;
|
|
207
|
+
static allRoutes(type) {
|
|
208
|
+
if (type === "method") return this.routesByMethod;
|
|
209
|
+
if (type === "path") return this.routesByPathMethod;
|
|
210
|
+
return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
|
|
193
211
|
}
|
|
194
212
|
static async apply(router) {
|
|
195
213
|
for (const route of this.routes) {
|
|
@@ -218,7 +236,7 @@ var Router = class Router {
|
|
|
218
236
|
}
|
|
219
237
|
if (!handlerFunction) continue;
|
|
220
238
|
for (const method of route.methods) {
|
|
221
|
-
|
|
239
|
+
const allowedMethods = [
|
|
222
240
|
"get",
|
|
223
241
|
"post",
|
|
224
242
|
"put",
|
|
@@ -226,7 +244,9 @@ var Router = class Router {
|
|
|
226
244
|
"patch",
|
|
227
245
|
"options",
|
|
228
246
|
"head"
|
|
229
|
-
]
|
|
247
|
+
];
|
|
248
|
+
if (method === "options" && route.methods.length > 1) continue;
|
|
249
|
+
if (!allowedMethods.includes(method)) {
|
|
230
250
|
const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
231
251
|
console.error("[ROUTES]", error.message);
|
|
232
252
|
throw error;
|
package/dist/express/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as
|
|
1
|
+
import { a as HttpContext, i as Handler, n as ControllerAction, o as Middleware, r as HttpMethod, s as Route, t as ApiResourceMiddleware } from "../basic-Chn8OGPD.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
|
*/
|
|
@@ -39,7 +47,7 @@ declare class Router {
|
|
|
39
47
|
* @param handler - Route handler function or controller reference
|
|
40
48
|
* @param middlewares - Array of middleware functions
|
|
41
49
|
*/
|
|
42
|
-
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
50
|
+
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
43
51
|
/**
|
|
44
52
|
* Register RESTful API resource routes for a controller with optional action filtering
|
|
45
53
|
*
|
|
@@ -50,6 +58,7 @@ declare class Router {
|
|
|
50
58
|
static apiResource(basePath: string, controller: any, options?: {
|
|
51
59
|
only?: ControllerAction[];
|
|
52
60
|
except?: ControllerAction[];
|
|
61
|
+
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
53
62
|
}): void;
|
|
54
63
|
/**
|
|
55
64
|
* Register a GET route
|
|
@@ -57,49 +66,49 @@ declare class Router {
|
|
|
57
66
|
* @param handler - Route handler
|
|
58
67
|
* @param middlewares - Middleware functions
|
|
59
68
|
*/
|
|
60
|
-
static get(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
69
|
+
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
61
70
|
/**
|
|
62
71
|
* Register a POST route
|
|
63
72
|
* @param path - Route path
|
|
64
73
|
* @param handler - Route handler
|
|
65
74
|
* @param middlewares - Middleware functions
|
|
66
75
|
*/
|
|
67
|
-
static post(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
76
|
+
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
68
77
|
/**
|
|
69
78
|
* Register a PUT route
|
|
70
79
|
* @param path - Route path
|
|
71
80
|
* @param handler - Route handler
|
|
72
81
|
* @param middlewares - Middleware functions
|
|
73
82
|
*/
|
|
74
|
-
static put(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
83
|
+
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
75
84
|
/**
|
|
76
85
|
* Register a DELETE route
|
|
77
86
|
* @param path - Route path
|
|
78
87
|
* @param handler - Route handler
|
|
79
88
|
* @param middlewares - Middleware functions
|
|
80
89
|
*/
|
|
81
|
-
static delete(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
90
|
+
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
82
91
|
/**
|
|
83
92
|
* Register a PATCH route
|
|
84
93
|
* @param path - Route path
|
|
85
94
|
* @param handler - Route handler
|
|
86
95
|
* @param middlewares - Middleware functions
|
|
87
96
|
*/
|
|
88
|
-
static patch(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
97
|
+
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
89
98
|
/**
|
|
90
99
|
* Register an OPTIONS route
|
|
91
100
|
* @param path - Route path
|
|
92
101
|
* @param handler - Route handler
|
|
93
102
|
* @param middlewares - Middleware functions
|
|
94
103
|
*/
|
|
95
|
-
static options(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
104
|
+
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
96
105
|
/**
|
|
97
106
|
* Register a HEAD route
|
|
98
107
|
* @param path - Route path
|
|
99
108
|
* @param handler - Route handler
|
|
100
109
|
* @param middlewares - Middleware functions
|
|
101
110
|
*/
|
|
102
|
-
static head(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
111
|
+
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
103
112
|
/**
|
|
104
113
|
* Group routes with a common prefix and middlewares
|
|
105
114
|
* @param prefix - URL prefix for grouped routes
|
|
@@ -117,7 +126,9 @@ declare class Router {
|
|
|
117
126
|
* Get all registered routes with their information
|
|
118
127
|
* @returns Array of route information objects
|
|
119
128
|
*/
|
|
120
|
-
static allRoutes(): Route<HttpContext, Middleware
|
|
129
|
+
static allRoutes(type?: 'path'): Record<string, Route<HttpContext, Middleware>>;
|
|
130
|
+
static allRoutes(type?: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
|
|
131
|
+
static allRoutes(type?: 'method'): Array<Route<HttpContext, Middleware>>;
|
|
121
132
|
/**
|
|
122
133
|
* Apply all registered routes to the provided Express Router instance
|
|
123
134
|
* Handles controller-method binding and middleware application
|
package/dist/express/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as
|
|
1
|
+
import { a as HttpContext, i as Handler, n as ControllerAction, o as Middleware, r as HttpMethod, s as Route, t as ApiResourceMiddleware } from "../basic-DJmmZq1h.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
|
*/
|
|
@@ -39,7 +47,7 @@ declare class Router {
|
|
|
39
47
|
* @param handler - Route handler function or controller reference
|
|
40
48
|
* @param middlewares - Array of middleware functions
|
|
41
49
|
*/
|
|
42
|
-
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
50
|
+
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
43
51
|
/**
|
|
44
52
|
* Register RESTful API resource routes for a controller with optional action filtering
|
|
45
53
|
*
|
|
@@ -50,6 +58,7 @@ declare class Router {
|
|
|
50
58
|
static apiResource(basePath: string, controller: any, options?: {
|
|
51
59
|
only?: ControllerAction[];
|
|
52
60
|
except?: ControllerAction[];
|
|
61
|
+
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
53
62
|
}): void;
|
|
54
63
|
/**
|
|
55
64
|
* Register a GET route
|
|
@@ -57,49 +66,49 @@ declare class Router {
|
|
|
57
66
|
* @param handler - Route handler
|
|
58
67
|
* @param middlewares - Middleware functions
|
|
59
68
|
*/
|
|
60
|
-
static get(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
69
|
+
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
61
70
|
/**
|
|
62
71
|
* Register a POST route
|
|
63
72
|
* @param path - Route path
|
|
64
73
|
* @param handler - Route handler
|
|
65
74
|
* @param middlewares - Middleware functions
|
|
66
75
|
*/
|
|
67
|
-
static post(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
76
|
+
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
68
77
|
/**
|
|
69
78
|
* Register a PUT route
|
|
70
79
|
* @param path - Route path
|
|
71
80
|
* @param handler - Route handler
|
|
72
81
|
* @param middlewares - Middleware functions
|
|
73
82
|
*/
|
|
74
|
-
static put(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
83
|
+
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
75
84
|
/**
|
|
76
85
|
* Register a DELETE route
|
|
77
86
|
* @param path - Route path
|
|
78
87
|
* @param handler - Route handler
|
|
79
88
|
* @param middlewares - Middleware functions
|
|
80
89
|
*/
|
|
81
|
-
static delete(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
90
|
+
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
82
91
|
/**
|
|
83
92
|
* Register a PATCH route
|
|
84
93
|
* @param path - Route path
|
|
85
94
|
* @param handler - Route handler
|
|
86
95
|
* @param middlewares - Middleware functions
|
|
87
96
|
*/
|
|
88
|
-
static patch(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
97
|
+
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
89
98
|
/**
|
|
90
99
|
* Register an OPTIONS route
|
|
91
100
|
* @param path - Route path
|
|
92
101
|
* @param handler - Route handler
|
|
93
102
|
* @param middlewares - Middleware functions
|
|
94
103
|
*/
|
|
95
|
-
static options(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
104
|
+
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
96
105
|
/**
|
|
97
106
|
* Register a HEAD route
|
|
98
107
|
* @param path - Route path
|
|
99
108
|
* @param handler - Route handler
|
|
100
109
|
* @param middlewares - Middleware functions
|
|
101
110
|
*/
|
|
102
|
-
static head(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
111
|
+
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
103
112
|
/**
|
|
104
113
|
* Group routes with a common prefix and middlewares
|
|
105
114
|
* @param prefix - URL prefix for grouped routes
|
|
@@ -117,7 +126,9 @@ declare class Router {
|
|
|
117
126
|
* Get all registered routes with their information
|
|
118
127
|
* @returns Array of route information objects
|
|
119
128
|
*/
|
|
120
|
-
static allRoutes(): Route<HttpContext, Middleware
|
|
129
|
+
static allRoutes(type?: 'path'): Record<string, Route<HttpContext, Middleware>>;
|
|
130
|
+
static allRoutes(type?: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
|
|
131
|
+
static allRoutes(type?: 'method'): Array<Route<HttpContext, Middleware>>;
|
|
121
132
|
/**
|
|
122
133
|
* Apply all registered routes to the provided Express Router instance
|
|
123
134
|
* Handles controller-method binding and middleware application
|
package/dist/express/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,24 @@ var Router = class Router {
|
|
|
41
49
|
* @param middlewares - Array of middleware functions
|
|
42
50
|
*/
|
|
43
51
|
static add(methods, path, handler, middlewares) {
|
|
44
|
-
|
|
52
|
+
methods = Array.isArray(methods) ? methods : [methods];
|
|
53
|
+
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
45
54
|
const fullPath = this.normalizePath(`${this.prefix}/${path}`);
|
|
46
|
-
|
|
55
|
+
const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
47
56
|
...this.globalMiddlewares,
|
|
48
57
|
...this.groupMiddlewares,
|
|
49
58
|
...middlewares || []
|
|
50
|
-
])
|
|
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
|
+
}
|
|
51
70
|
}
|
|
52
71
|
/**
|
|
53
72
|
* Register RESTful API resource routes for a controller with optional action filtering
|
|
@@ -86,7 +105,8 @@ var Router = class Router {
|
|
|
86
105
|
if (except.includes(action)) continue;
|
|
87
106
|
if (typeof preController[action] === "function") {
|
|
88
107
|
const { method, path } = actions[action];
|
|
89
|
-
|
|
108
|
+
const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
|
|
109
|
+
this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0);
|
|
90
110
|
}
|
|
91
111
|
}
|
|
92
112
|
}
|
|
@@ -183,12 +203,10 @@ var Router = class Router {
|
|
|
183
203
|
callback();
|
|
184
204
|
this.globalMiddlewares = prevMiddlewares;
|
|
185
205
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
static allRoutes() {
|
|
191
|
-
return this.routes;
|
|
206
|
+
static allRoutes(type) {
|
|
207
|
+
if (type === "method") return this.routesByMethod;
|
|
208
|
+
if (type === "path") return this.routesByPathMethod;
|
|
209
|
+
return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
|
|
192
210
|
}
|
|
193
211
|
static async apply(router) {
|
|
194
212
|
for (const route of this.routes) {
|
|
@@ -217,7 +235,7 @@ var Router = class Router {
|
|
|
217
235
|
}
|
|
218
236
|
if (!handlerFunction) continue;
|
|
219
237
|
for (const method of route.methods) {
|
|
220
|
-
|
|
238
|
+
const allowedMethods = [
|
|
221
239
|
"get",
|
|
222
240
|
"post",
|
|
223
241
|
"put",
|
|
@@ -225,7 +243,9 @@ var Router = class Router {
|
|
|
225
243
|
"patch",
|
|
226
244
|
"options",
|
|
227
245
|
"head"
|
|
228
|
-
]
|
|
246
|
+
];
|
|
247
|
+
if (method === "options" && route.methods.length > 1) continue;
|
|
248
|
+
if (!allowedMethods.includes(method)) {
|
|
229
249
|
const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
230
250
|
console.error("[ROUTES]", error.message);
|
|
231
251
|
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,24 @@ var Router = class Router {
|
|
|
42
50
|
* @param middlewares - Array of middleware functions
|
|
43
51
|
*/
|
|
44
52
|
static add(methods, path, handler, middlewares) {
|
|
45
|
-
|
|
53
|
+
methods = Array.isArray(methods) ? methods : [methods];
|
|
54
|
+
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
46
55
|
const fullPath = this.normalizePath(`${this.prefix}/${path}`);
|
|
47
|
-
|
|
56
|
+
const route = new require_Route.Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
48
57
|
...this.globalMiddlewares,
|
|
49
58
|
...this.groupMiddlewares,
|
|
50
59
|
...middlewares || []
|
|
51
|
-
])
|
|
60
|
+
]);
|
|
61
|
+
if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
|
|
62
|
+
res.headers.set("Allow", "GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD");
|
|
63
|
+
res.status = 204;
|
|
64
|
+
});
|
|
65
|
+
this.routes.push(route);
|
|
66
|
+
for (const method of methods.map((m) => m.toUpperCase())) {
|
|
67
|
+
this.routesByPathMethod[`${method} ${fullPath}`] = route;
|
|
68
|
+
if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
|
|
69
|
+
this.routesByMethod[method].push(route);
|
|
70
|
+
}
|
|
52
71
|
}
|
|
53
72
|
/**
|
|
54
73
|
* Register RESTful API resource routes for a controller with optional action filtering
|
|
@@ -87,7 +106,8 @@ var Router = class Router {
|
|
|
87
106
|
if (except.includes(action)) continue;
|
|
88
107
|
if (typeof preController[action] === "function") {
|
|
89
108
|
const { method, path } = actions[action];
|
|
90
|
-
|
|
109
|
+
const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
|
|
110
|
+
this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0);
|
|
91
111
|
}
|
|
92
112
|
}
|
|
93
113
|
}
|
|
@@ -184,12 +204,10 @@ var Router = class Router {
|
|
|
184
204
|
callback();
|
|
185
205
|
this.globalMiddlewares = prevMiddlewares;
|
|
186
206
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
static allRoutes() {
|
|
192
|
-
return this.routes;
|
|
207
|
+
static allRoutes(type) {
|
|
208
|
+
if (type === "method") return this.routesByMethod;
|
|
209
|
+
if (type === "path") return this.routesByPathMethod;
|
|
210
|
+
return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
|
|
193
211
|
}
|
|
194
212
|
/**
|
|
195
213
|
* Apply all registered routes to the provided H3 Router instance
|
|
@@ -225,7 +243,7 @@ var Router = class Router {
|
|
|
225
243
|
}
|
|
226
244
|
if (!handlerFunction) continue;
|
|
227
245
|
for (const method of route.methods) {
|
|
228
|
-
|
|
246
|
+
const allowedMethods = [
|
|
229
247
|
"get",
|
|
230
248
|
"post",
|
|
231
249
|
"put",
|
|
@@ -233,7 +251,9 @@ var Router = class Router {
|
|
|
233
251
|
"patch",
|
|
234
252
|
"options",
|
|
235
253
|
"head"
|
|
236
|
-
]
|
|
254
|
+
];
|
|
255
|
+
if (method === "options" && route.methods.length > 1) continue;
|
|
256
|
+
if (!allowedMethods.includes(method)) {
|
|
237
257
|
const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
238
258
|
console.error("[ROUTES]", error.message);
|
|
239
259
|
throw error;
|
package/dist/h3/index.d.cts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import * as h3 from "h3";
|
|
1
|
+
import { c as H3App, d as Middleware, l as Handler, n as ControllerAction, r as HttpMethod, s as Route, t as ApiResourceMiddleware, u as HttpContext } from "../basic-Chn8OGPD.cjs";
|
|
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
|
*/
|
|
@@ -39,7 +46,7 @@ declare class Router {
|
|
|
39
46
|
* @param handler - Route handler function or controller reference
|
|
40
47
|
* @param middlewares - Array of middleware functions
|
|
41
48
|
*/
|
|
42
|
-
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
49
|
+
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
43
50
|
/**
|
|
44
51
|
* Register RESTful API resource routes for a controller with optional action filtering
|
|
45
52
|
*
|
|
@@ -50,6 +57,7 @@ declare class Router {
|
|
|
50
57
|
static apiResource(basePath: string, controller: any, options?: {
|
|
51
58
|
only?: ControllerAction[];
|
|
52
59
|
except?: ControllerAction[];
|
|
60
|
+
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
53
61
|
}): void;
|
|
54
62
|
/**
|
|
55
63
|
* Register a GET route
|
|
@@ -57,49 +65,49 @@ declare class Router {
|
|
|
57
65
|
* @param handler - Route handler
|
|
58
66
|
* @param middlewares - Middleware functions
|
|
59
67
|
*/
|
|
60
|
-
static get(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
68
|
+
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
61
69
|
/**
|
|
62
70
|
* Register a POST route
|
|
63
71
|
* @param path - Route path
|
|
64
72
|
* @param handler - Route handler
|
|
65
73
|
* @param middlewares - Middleware functions
|
|
66
74
|
*/
|
|
67
|
-
static post(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
75
|
+
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
68
76
|
/**
|
|
69
77
|
* Register a PUT route
|
|
70
78
|
* @param path - Route path
|
|
71
79
|
* @param handler - Route handler
|
|
72
80
|
* @param middlewares - Middleware functions
|
|
73
81
|
*/
|
|
74
|
-
static put(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
82
|
+
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
75
83
|
/**
|
|
76
84
|
* Register a DELETE route
|
|
77
85
|
* @param path - Route path
|
|
78
86
|
* @param handler - Route handler
|
|
79
87
|
* @param middlewares - Middleware functions
|
|
80
88
|
*/
|
|
81
|
-
static delete(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
89
|
+
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
82
90
|
/**
|
|
83
91
|
* Register a PATCH route
|
|
84
92
|
* @param path - Route path
|
|
85
93
|
* @param handler - Route handler
|
|
86
94
|
* @param middlewares - Middleware functions
|
|
87
95
|
*/
|
|
88
|
-
static patch(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
96
|
+
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
89
97
|
/**
|
|
90
98
|
* Register an OPTIONS route
|
|
91
99
|
* @param path - Route path
|
|
92
100
|
* @param handler - Route handler
|
|
93
101
|
* @param middlewares - Middleware functions
|
|
94
102
|
*/
|
|
95
|
-
static options(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
103
|
+
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
96
104
|
/**
|
|
97
105
|
* Register a HEAD route
|
|
98
106
|
* @param path - Route path
|
|
99
107
|
* @param handler - Route handler
|
|
100
108
|
* @param middlewares - Middleware functions
|
|
101
109
|
*/
|
|
102
|
-
static head(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
110
|
+
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
103
111
|
/**
|
|
104
112
|
* Group routes with a common prefix and middlewares
|
|
105
113
|
* @param prefix - URL prefix for grouped routes
|
|
@@ -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<
|
|
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 H3 Router instance
|
|
123
133
|
* Handles controller-method binding and middleware application
|
package/dist/h3/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import * as h3 from "h3";
|
|
1
|
+
import { c as H3App, d as Middleware, l as Handler, n as ControllerAction, r as HttpMethod, s as Route, t as ApiResourceMiddleware, u as HttpContext } from "../basic-DJmmZq1h.mjs";
|
|
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
|
*/
|
|
@@ -39,7 +46,7 @@ declare class Router {
|
|
|
39
46
|
* @param handler - Route handler function or controller reference
|
|
40
47
|
* @param middlewares - Array of middleware functions
|
|
41
48
|
*/
|
|
42
|
-
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
49
|
+
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
43
50
|
/**
|
|
44
51
|
* Register RESTful API resource routes for a controller with optional action filtering
|
|
45
52
|
*
|
|
@@ -50,6 +57,7 @@ declare class Router {
|
|
|
50
57
|
static apiResource(basePath: string, controller: any, options?: {
|
|
51
58
|
only?: ControllerAction[];
|
|
52
59
|
except?: ControllerAction[];
|
|
60
|
+
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
53
61
|
}): void;
|
|
54
62
|
/**
|
|
55
63
|
* Register a GET route
|
|
@@ -57,49 +65,49 @@ declare class Router {
|
|
|
57
65
|
* @param handler - Route handler
|
|
58
66
|
* @param middlewares - Middleware functions
|
|
59
67
|
*/
|
|
60
|
-
static get(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
68
|
+
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
61
69
|
/**
|
|
62
70
|
* Register a POST route
|
|
63
71
|
* @param path - Route path
|
|
64
72
|
* @param handler - Route handler
|
|
65
73
|
* @param middlewares - Middleware functions
|
|
66
74
|
*/
|
|
67
|
-
static post(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
75
|
+
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
68
76
|
/**
|
|
69
77
|
* Register a PUT route
|
|
70
78
|
* @param path - Route path
|
|
71
79
|
* @param handler - Route handler
|
|
72
80
|
* @param middlewares - Middleware functions
|
|
73
81
|
*/
|
|
74
|
-
static put(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
82
|
+
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
75
83
|
/**
|
|
76
84
|
* Register a DELETE route
|
|
77
85
|
* @param path - Route path
|
|
78
86
|
* @param handler - Route handler
|
|
79
87
|
* @param middlewares - Middleware functions
|
|
80
88
|
*/
|
|
81
|
-
static delete(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
89
|
+
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
82
90
|
/**
|
|
83
91
|
* Register a PATCH route
|
|
84
92
|
* @param path - Route path
|
|
85
93
|
* @param handler - Route handler
|
|
86
94
|
* @param middlewares - Middleware functions
|
|
87
95
|
*/
|
|
88
|
-
static patch(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
96
|
+
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
89
97
|
/**
|
|
90
98
|
* Register an OPTIONS route
|
|
91
99
|
* @param path - Route path
|
|
92
100
|
* @param handler - Route handler
|
|
93
101
|
* @param middlewares - Middleware functions
|
|
94
102
|
*/
|
|
95
|
-
static options(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
103
|
+
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
96
104
|
/**
|
|
97
105
|
* Register a HEAD route
|
|
98
106
|
* @param path - Route path
|
|
99
107
|
* @param handler - Route handler
|
|
100
108
|
* @param middlewares - Middleware functions
|
|
101
109
|
*/
|
|
102
|
-
static head(path: string, handler: Handler, middlewares?: Middleware[]): void;
|
|
110
|
+
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
103
111
|
/**
|
|
104
112
|
* Group routes with a common prefix and middlewares
|
|
105
113
|
* @param prefix - URL prefix for grouped routes
|
|
@@ -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<
|
|
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 H3 Router instance
|
|
123
133
|
* 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,24 @@ var Router = class Router {
|
|
|
41
49
|
* @param middlewares - Array of middleware functions
|
|
42
50
|
*/
|
|
43
51
|
static add(methods, path, handler, middlewares) {
|
|
44
|
-
|
|
52
|
+
methods = Array.isArray(methods) ? methods : [methods];
|
|
53
|
+
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
45
54
|
const fullPath = this.normalizePath(`${this.prefix}/${path}`);
|
|
46
|
-
|
|
55
|
+
const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
47
56
|
...this.globalMiddlewares,
|
|
48
57
|
...this.groupMiddlewares,
|
|
49
58
|
...middlewares || []
|
|
50
|
-
])
|
|
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
|
+
}
|
|
51
70
|
}
|
|
52
71
|
/**
|
|
53
72
|
* Register RESTful API resource routes for a controller with optional action filtering
|
|
@@ -86,7 +105,8 @@ var Router = class Router {
|
|
|
86
105
|
if (except.includes(action)) continue;
|
|
87
106
|
if (typeof preController[action] === "function") {
|
|
88
107
|
const { method, path } = actions[action];
|
|
89
|
-
|
|
108
|
+
const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
|
|
109
|
+
this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0);
|
|
90
110
|
}
|
|
91
111
|
}
|
|
92
112
|
}
|
|
@@ -183,12 +203,10 @@ var Router = class Router {
|
|
|
183
203
|
callback();
|
|
184
204
|
this.globalMiddlewares = prevMiddlewares;
|
|
185
205
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
static allRoutes() {
|
|
191
|
-
return this.routes;
|
|
206
|
+
static allRoutes(type) {
|
|
207
|
+
if (type === "method") return this.routesByMethod;
|
|
208
|
+
if (type === "path") return this.routesByPathMethod;
|
|
209
|
+
return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
|
|
192
210
|
}
|
|
193
211
|
/**
|
|
194
212
|
* Apply all registered routes to the provided H3 Router instance
|
|
@@ -224,7 +242,7 @@ var Router = class Router {
|
|
|
224
242
|
}
|
|
225
243
|
if (!handlerFunction) continue;
|
|
226
244
|
for (const method of route.methods) {
|
|
227
|
-
|
|
245
|
+
const allowedMethods = [
|
|
228
246
|
"get",
|
|
229
247
|
"post",
|
|
230
248
|
"put",
|
|
@@ -232,7 +250,9 @@ var Router = class Router {
|
|
|
232
250
|
"patch",
|
|
233
251
|
"options",
|
|
234
252
|
"head"
|
|
235
|
-
]
|
|
253
|
+
];
|
|
254
|
+
if (method === "options" && route.methods.length > 1) continue;
|
|
255
|
+
if (!allowedMethods.includes(method)) {
|
|
236
256
|
const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
237
257
|
console.error("[ROUTES]", error.message);
|
|
238
258
|
throw error;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,26 +1,6 @@
|
|
|
1
1
|
import { NextFunction, Request, Response as Response$1 } from "express";
|
|
2
|
-
import { H3Event, Middleware } from "h3";
|
|
2
|
+
import { H3Event, Middleware as Middleware$1 } from "h3";
|
|
3
3
|
|
|
4
|
-
//#region types/basic.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Controller method reference
|
|
7
|
-
*/
|
|
8
|
-
type ControllerHandler = [any, string];
|
|
9
|
-
/**
|
|
10
|
-
* HTTP methods supported by the router
|
|
11
|
-
*/
|
|
12
|
-
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
13
|
-
/**
|
|
14
|
-
* Generic Object type for request data
|
|
15
|
-
*/
|
|
16
|
-
type RequestData = Record<string, any>;
|
|
17
|
-
//#endregion
|
|
18
|
-
//#region types/express.d.ts
|
|
19
|
-
/**
|
|
20
|
-
* Middleware function type
|
|
21
|
-
*/
|
|
22
|
-
type Middleware$1 = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
|
|
23
|
-
//#endregion
|
|
24
4
|
//#region types/h3.d.ts
|
|
25
5
|
/**
|
|
26
6
|
* HTTP context passed to route handlers
|
|
@@ -33,20 +13,38 @@ type RouteHandler = (
|
|
|
33
13
|
/**
|
|
34
14
|
* H3 event context
|
|
35
15
|
*/
|
|
36
|
-
|
|
37
16
|
ctx: HttpContext,
|
|
38
17
|
/**
|
|
39
18
|
* ClearRequest instance
|
|
40
19
|
*/
|
|
41
|
-
|
|
42
20
|
req: ClearRequest) => any | Promise<any>;
|
|
43
21
|
/**
|
|
44
22
|
* Handler can be either a function or controller reference
|
|
45
23
|
*/
|
|
46
24
|
type Handler = RouteHandler | ControllerHandler;
|
|
47
25
|
//#endregion
|
|
26
|
+
//#region types/basic.d.ts
|
|
27
|
+
/**
|
|
28
|
+
* Controller method reference
|
|
29
|
+
*/
|
|
30
|
+
type ControllerHandler = [any, string];
|
|
31
|
+
/**
|
|
32
|
+
* HTTP methods supported by the router
|
|
33
|
+
*/
|
|
34
|
+
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
35
|
+
/**
|
|
36
|
+
* Generic Object type for request data
|
|
37
|
+
*/
|
|
38
|
+
type RequestData = Record<string, any>;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region types/express.d.ts
|
|
41
|
+
/**
|
|
42
|
+
* Middleware function type
|
|
43
|
+
*/
|
|
44
|
+
type Middleware = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
|
|
45
|
+
//#endregion
|
|
48
46
|
//#region src/Route.d.ts
|
|
49
|
-
declare class Route<X = any, M = Middleware | Middleware
|
|
47
|
+
declare class Route<X = any, M = Middleware$1 | Middleware> {
|
|
50
48
|
ctx: X;
|
|
51
49
|
body: RequestData;
|
|
52
50
|
query: RequestData;
|
|
@@ -64,7 +62,7 @@ declare class Route<X = any, M = Middleware | Middleware$1> {
|
|
|
64
62
|
}
|
|
65
63
|
//#endregion
|
|
66
64
|
//#region src/ClearRequest.d.ts
|
|
67
|
-
declare class ClearRequest<X = any, M = Middleware | Middleware
|
|
65
|
+
declare class ClearRequest<X = any, M = Middleware$1 | Middleware> {
|
|
68
66
|
[key: string]: any;
|
|
69
67
|
/**
|
|
70
68
|
* @param body - Parsed request body
|
package/dist/index.d.mts
CHANGED
|
@@ -1,26 +1,6 @@
|
|
|
1
1
|
import { NextFunction, Request, Response as Response$1 } from "express";
|
|
2
|
-
import { H3Event, Middleware } from "h3";
|
|
2
|
+
import { H3Event, Middleware as Middleware$1 } from "h3";
|
|
3
3
|
|
|
4
|
-
//#region types/basic.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Controller method reference
|
|
7
|
-
*/
|
|
8
|
-
type ControllerHandler = [any, string];
|
|
9
|
-
/**
|
|
10
|
-
* HTTP methods supported by the router
|
|
11
|
-
*/
|
|
12
|
-
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
13
|
-
/**
|
|
14
|
-
* Generic Object type for request data
|
|
15
|
-
*/
|
|
16
|
-
type RequestData = Record<string, any>;
|
|
17
|
-
//#endregion
|
|
18
|
-
//#region types/express.d.ts
|
|
19
|
-
/**
|
|
20
|
-
* Middleware function type
|
|
21
|
-
*/
|
|
22
|
-
type Middleware$1 = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
|
|
23
|
-
//#endregion
|
|
24
4
|
//#region types/h3.d.ts
|
|
25
5
|
/**
|
|
26
6
|
* HTTP context passed to route handlers
|
|
@@ -33,20 +13,38 @@ type RouteHandler = (
|
|
|
33
13
|
/**
|
|
34
14
|
* H3 event context
|
|
35
15
|
*/
|
|
36
|
-
|
|
37
16
|
ctx: HttpContext,
|
|
38
17
|
/**
|
|
39
18
|
* ClearRequest instance
|
|
40
19
|
*/
|
|
41
|
-
|
|
42
20
|
req: ClearRequest) => any | Promise<any>;
|
|
43
21
|
/**
|
|
44
22
|
* Handler can be either a function or controller reference
|
|
45
23
|
*/
|
|
46
24
|
type Handler = RouteHandler | ControllerHandler;
|
|
47
25
|
//#endregion
|
|
26
|
+
//#region types/basic.d.ts
|
|
27
|
+
/**
|
|
28
|
+
* Controller method reference
|
|
29
|
+
*/
|
|
30
|
+
type ControllerHandler = [any, string];
|
|
31
|
+
/**
|
|
32
|
+
* HTTP methods supported by the router
|
|
33
|
+
*/
|
|
34
|
+
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
35
|
+
/**
|
|
36
|
+
* Generic Object type for request data
|
|
37
|
+
*/
|
|
38
|
+
type RequestData = Record<string, any>;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region types/express.d.ts
|
|
41
|
+
/**
|
|
42
|
+
* Middleware function type
|
|
43
|
+
*/
|
|
44
|
+
type Middleware = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
|
|
45
|
+
//#endregion
|
|
48
46
|
//#region src/Route.d.ts
|
|
49
|
-
declare class Route<X = any, M = Middleware | Middleware
|
|
47
|
+
declare class Route<X = any, M = Middleware$1 | Middleware> {
|
|
50
48
|
ctx: X;
|
|
51
49
|
body: RequestData;
|
|
52
50
|
query: RequestData;
|
|
@@ -64,7 +62,7 @@ declare class Route<X = any, M = Middleware | Middleware$1> {
|
|
|
64
62
|
}
|
|
65
63
|
//#endregion
|
|
66
64
|
//#region src/ClearRequest.d.ts
|
|
67
|
-
declare class ClearRequest<X = any, M = Middleware | Middleware
|
|
65
|
+
declare class ClearRequest<X = any, M = Middleware$1 | Middleware> {
|
|
68
66
|
[key: string]: any;
|
|
69
67
|
/**
|
|
70
68
|
* @param body - Parsed request body
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { RequestData } from "../types/basic.mjs";
|
|
2
1
|
import { Middleware } from "../types/h3.mjs";
|
|
3
2
|
import { Route } from "./Route.mjs";
|
|
4
3
|
import { Middleware as Middleware$1 } from "../types/express.mjs";
|
|
4
|
+
import { RequestData } from "../types/basic.mjs";
|
|
5
5
|
|
|
6
6
|
//#region src/ClearRequest.d.ts
|
|
7
7
|
declare class ClearRequest<X = any, M = Middleware | Middleware$1> {
|
package/dist/types/Route.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { HttpMethod, RequestData } from "../types/basic.mjs";
|
|
2
1
|
import { Handler, Middleware } from "../types/h3.mjs";
|
|
3
2
|
import { ClearRequest } from "./ClearRequest.mjs";
|
|
4
3
|
import { Middleware as Middleware$1 } from "../types/express.mjs";
|
|
4
|
+
import { HttpMethod, RequestData } from "../types/basic.mjs";
|
|
5
5
|
|
|
6
6
|
//#region src/Route.d.ts
|
|
7
7
|
declare class Route<X = any, M = Middleware | Middleware$1> {
|
package/dist/types/basic.d.mts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { Middleware } from "./h3.mjs";
|
|
2
|
+
import { Middleware as Middleware$1 } from "./express.mjs";
|
|
3
|
+
|
|
1
4
|
//#region types/basic.d.ts
|
|
2
5
|
/**
|
|
3
6
|
* Controller method reference
|
|
@@ -15,5 +18,6 @@ type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
|
|
|
15
18
|
* Generic Object type for request data
|
|
16
19
|
*/
|
|
17
20
|
type RequestData = Record<string, any>;
|
|
21
|
+
type ApiResourceMiddleware<M extends Middleware$1 | Middleware> = M | M[] | { [K in ControllerAction]?: M | M[] };
|
|
18
22
|
//#endregion
|
|
19
|
-
export { ControllerAction, ControllerHandler, HttpMethod, RequestData };
|
|
23
|
+
export { ApiResourceMiddleware, ControllerAction, ControllerHandler, HttpMethod, RequestData };
|
package/dist/types/express.d.mts
CHANGED
package/dist/types/h3.d.mts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clear-router",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.8",
|
|
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",
|