clear-router 2.1.12 → 2.2.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.
- package/README.md +4 -4
- package/dist/core/index.cjs +4 -0
- package/dist/core/index.d.cts +2 -0
- package/dist/core/index.d.mts +2 -0
- package/dist/core/index.mjs +3 -0
- package/dist/express/index.cjs +86 -247
- package/dist/express/index.d.cts +68 -97
- package/dist/express/index.d.mts +68 -97
- package/dist/express/index.mjs +86 -247
- package/dist/h3/index.cjs +88 -242
- package/dist/h3/index.d.cts +72 -93
- package/dist/h3/index.d.mts +72 -93
- package/dist/h3/index.mjs +88 -242
- package/dist/index.cjs +342 -0
- package/dist/index.d.cts +184 -38
- package/dist/index.d.mts +184 -38
- package/dist/index.mjs +343 -1
- package/dist/router-BNVIrTi3.cjs +397 -0
- package/dist/router-BiCuy5TZ.mjs +392 -0
- package/dist/router-C1jVRytA.d.mts +311 -0
- package/dist/router-CZIh1ZPJ.d.cts +311 -0
- package/dist/types/Route.d.mts +4 -4
- package/package.json +6 -2
- package/dist/Route-BbPXcDGX.mjs +0 -50
- package/dist/Route-DhC4kNPX.cjs +0 -62
- package/dist/basic-C_1O6RVq.d.cts +0 -138
- package/dist/basic-cLeny2Zk.d.mts +0 -138
package/dist/h3/index.d.mts
CHANGED
|
@@ -1,64 +1,32 @@
|
|
|
1
|
-
import { c as Route, d as HttpContext, f as Middleware, i as
|
|
1
|
+
import { c as Route, d as HttpContext, f as Middleware, i as HttpMethod, l as H3App, n as ApiResourceMiddleware, r as ControllerAction, t as CoreRouter, u as Handler } from "../router-C1jVRytA.mjs";
|
|
2
2
|
import { H3 } from "h3";
|
|
3
3
|
|
|
4
4
|
//#region src/h3/router.d.ts
|
|
5
5
|
/**
|
|
6
|
-
* @class clear-router
|
|
6
|
+
* @class clear-router H3 Router
|
|
7
7
|
* @description Laravel-style routing system for Express.js and H3 with support for CommonJS, ESM, and TypeScript
|
|
8
8
|
* @author 3m1n3nc3
|
|
9
9
|
* @repository https://github.com/toneflix/clear-router
|
|
10
10
|
*/
|
|
11
|
-
declare class Router {
|
|
12
|
-
static config: RouterConfig;
|
|
11
|
+
declare class Router extends CoreRouter {
|
|
13
12
|
private static readonly bodyCache;
|
|
14
|
-
private static readonly groupContext;
|
|
15
|
-
/**
|
|
16
|
-
* All registered routes
|
|
17
|
-
*/
|
|
18
|
-
static routes: Array<Route<HttpContext, Middleware>>;
|
|
19
|
-
/**
|
|
20
|
-
* Mapping of routes by path and method for quick lookup.
|
|
21
|
-
*/
|
|
22
|
-
static routesByPathMethod: Record<string, Route<HttpContext, Middleware>>;
|
|
23
|
-
/**
|
|
24
|
-
* Mapping of routes by method for quick lookup.
|
|
25
|
-
*/
|
|
26
|
-
static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware>> };
|
|
27
|
-
/**
|
|
28
|
-
* Current route prefix
|
|
29
|
-
*/
|
|
30
|
-
static prefix: string;
|
|
31
|
-
/**
|
|
32
|
-
* Group-level middlewares
|
|
33
|
-
*/
|
|
34
|
-
static groupMiddlewares: Middleware[];
|
|
35
|
-
/**
|
|
36
|
-
* Global-level middlewares
|
|
37
|
-
*/
|
|
38
|
-
static globalMiddlewares: Middleware[];
|
|
39
|
-
/**
|
|
40
|
-
* Normalize path by removing duplicate slashes and ensuring leading slash
|
|
41
|
-
* @param path - Path to normalize
|
|
42
|
-
* @returns Normalized path
|
|
43
|
-
*/
|
|
44
|
-
static normalizePath(path: string): string;
|
|
45
|
-
static configure(options?: RouterConfig): void;
|
|
46
13
|
private static readBodyCached;
|
|
47
|
-
private static resolveMethodOverride;
|
|
48
14
|
/**
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
15
|
+
* Adds a new route to the router with the specified HTTP methods, path, handler, and optional middlewares.
|
|
16
|
+
*
|
|
17
|
+
* @param methods
|
|
18
|
+
* @param path
|
|
19
|
+
* @param handler
|
|
20
|
+
* @param middlewares
|
|
21
|
+
*/
|
|
55
22
|
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
56
23
|
/**
|
|
57
|
-
*
|
|
24
|
+
* Adds a new API resource route to the router for a given base path and controller, with options
|
|
25
|
+
* to specify included/excluded actions and middlewares.
|
|
58
26
|
*
|
|
59
|
-
* @param basePath
|
|
60
|
-
* @param controller
|
|
61
|
-
* @param options
|
|
27
|
+
* @param basePath
|
|
28
|
+
* @param controller
|
|
29
|
+
* @param options
|
|
62
30
|
*/
|
|
63
31
|
static apiResource(basePath: string, controller: any, options?: {
|
|
64
32
|
only?: ControllerAction[];
|
|
@@ -66,83 +34,94 @@ declare class Router {
|
|
|
66
34
|
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
67
35
|
}): void;
|
|
68
36
|
/**
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* @param
|
|
72
|
-
* @param
|
|
37
|
+
* Adds a new GET route to the router with the specified path, handler, and optional middlewares.
|
|
38
|
+
*
|
|
39
|
+
* @param path
|
|
40
|
+
* @param handler
|
|
41
|
+
* @param middlewares
|
|
73
42
|
*/
|
|
74
43
|
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
75
44
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* @param
|
|
79
|
-
* @param
|
|
45
|
+
* Adds a new POST route to the router with the specified path, handler, and optional middlewares.
|
|
46
|
+
*
|
|
47
|
+
* @param path
|
|
48
|
+
* @param handler
|
|
49
|
+
* @param middlewares
|
|
80
50
|
*/
|
|
81
51
|
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
82
52
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* @param
|
|
86
|
-
* @param
|
|
53
|
+
* Adds a new PUT route to the router with the specified path, handler, and optional middlewares.
|
|
54
|
+
*
|
|
55
|
+
* @param path
|
|
56
|
+
* @param handler
|
|
57
|
+
* @param middlewares
|
|
87
58
|
*/
|
|
88
59
|
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
89
60
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* @param
|
|
93
|
-
* @param
|
|
61
|
+
* Adds a new DELETE route to the router with the specified path, handler, and optional middlewares.
|
|
62
|
+
*
|
|
63
|
+
* @param path
|
|
64
|
+
* @param handler
|
|
65
|
+
* @param middlewares
|
|
94
66
|
*/
|
|
95
67
|
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
96
68
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* @param
|
|
100
|
-
* @param
|
|
69
|
+
* Adds a new PATCH route to the router with the specified path, handler, and optional middlewares.
|
|
70
|
+
*
|
|
71
|
+
* @param path
|
|
72
|
+
* @param handler
|
|
73
|
+
* @param middlewares
|
|
101
74
|
*/
|
|
102
75
|
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
103
76
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* @param
|
|
107
|
-
* @param
|
|
77
|
+
* Adds a new OPTIONS route to the router with the specified path, handler, and optional middlewares.
|
|
78
|
+
*
|
|
79
|
+
* @param path
|
|
80
|
+
* @param handler
|
|
81
|
+
* @param middlewares
|
|
108
82
|
*/
|
|
109
83
|
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
110
84
|
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* @param
|
|
114
|
-
* @param
|
|
85
|
+
* Adds a new HEAD route to the router with the specified path, handler, and optional middlewares.
|
|
86
|
+
*
|
|
87
|
+
* @param path
|
|
88
|
+
* @param handler
|
|
89
|
+
* @param middlewares
|
|
115
90
|
*/
|
|
116
91
|
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
|
|
117
92
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* @param
|
|
93
|
+
* Defines a group of routes with a common prefix and optional middlewares, allowing for better
|
|
94
|
+
* organization and reuse of route configurations.
|
|
95
|
+
*
|
|
96
|
+
* @param prefix
|
|
97
|
+
* @param callback
|
|
98
|
+
* @param middlewares
|
|
122
99
|
*/
|
|
123
100
|
static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
|
|
124
101
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* @param
|
|
102
|
+
* Adds global middlewares to the router, which will be applied to all routes.
|
|
103
|
+
*
|
|
104
|
+
* @param middlewares
|
|
105
|
+
* @param callback
|
|
128
106
|
*/
|
|
129
107
|
static middleware(middlewares: Middleware[], callback: () => void): void;
|
|
130
108
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
static allRoutes(
|
|
109
|
+
* Retrieves all registered routes in the router, optionally organized by path or method
|
|
110
|
+
* for easier access and management.
|
|
111
|
+
*
|
|
112
|
+
* @param type
|
|
113
|
+
*/
|
|
114
|
+
static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
|
|
115
|
+
static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
116
|
+
static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
|
|
137
117
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* All errors are thrown to H3 error handling middleware
|
|
118
|
+
* Applies the registered routes to the given H3 application instance, setting up the
|
|
119
|
+
* necessary handlers and middlewares for each route.
|
|
141
120
|
*
|
|
142
|
-
* @param app
|
|
121
|
+
* @param app
|
|
122
|
+
* @returns
|
|
143
123
|
*/
|
|
144
124
|
static apply(app: H3): H3App;
|
|
145
|
-
private static bindRequestToInstance;
|
|
146
125
|
}
|
|
147
126
|
//#endregion
|
|
148
127
|
export { Router };
|
package/dist/h3/index.mjs
CHANGED
|
@@ -1,68 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
1
|
+
import { t as CoreRouter } from "../router-BiCuy5TZ.mjs";
|
|
3
2
|
import { getQuery, getRouterParams, readBody } from "h3";
|
|
4
3
|
|
|
5
4
|
//#region src/h3/router.ts
|
|
6
5
|
/**
|
|
7
|
-
* @class clear-router
|
|
6
|
+
* @class clear-router H3 Router
|
|
8
7
|
* @description Laravel-style routing system for Express.js and H3 with support for CommonJS, ESM, and TypeScript
|
|
9
8
|
* @author 3m1n3nc3
|
|
10
9
|
* @repository https://github.com/toneflix/clear-router
|
|
11
10
|
*/
|
|
12
|
-
var Router = class Router {
|
|
13
|
-
static config = { methodOverride: {
|
|
14
|
-
enabled: true,
|
|
15
|
-
bodyKeys: ["_method"],
|
|
16
|
-
headerKeys: ["x-http-method"]
|
|
17
|
-
} };
|
|
11
|
+
var Router = class Router extends CoreRouter {
|
|
18
12
|
static bodyCache = /* @__PURE__ */ new WeakMap();
|
|
19
|
-
static groupContext = new AsyncLocalStorage();
|
|
20
|
-
/**
|
|
21
|
-
* All registered routes
|
|
22
|
-
*/
|
|
23
|
-
static routes = [];
|
|
24
|
-
/**
|
|
25
|
-
* Mapping of routes by path and method for quick lookup.
|
|
26
|
-
*/
|
|
27
|
-
static routesByPathMethod = {};
|
|
28
|
-
/**
|
|
29
|
-
* Mapping of routes by method for quick lookup.
|
|
30
|
-
*/
|
|
31
|
-
static routesByMethod = {};
|
|
32
|
-
/**
|
|
33
|
-
* Current route prefix
|
|
34
|
-
*/
|
|
35
|
-
static prefix = "";
|
|
36
|
-
/**
|
|
37
|
-
* Group-level middlewares
|
|
38
|
-
*/
|
|
39
|
-
static groupMiddlewares = [];
|
|
40
|
-
/**
|
|
41
|
-
* Global-level middlewares
|
|
42
|
-
*/
|
|
43
|
-
static globalMiddlewares = [];
|
|
44
|
-
/**
|
|
45
|
-
* Normalize path by removing duplicate slashes and ensuring leading slash
|
|
46
|
-
* @param path - Path to normalize
|
|
47
|
-
* @returns Normalized path
|
|
48
|
-
*/
|
|
49
|
-
static normalizePath(path) {
|
|
50
|
-
return "/" + path.split("/").filter(Boolean).join("/");
|
|
51
|
-
}
|
|
52
|
-
static configure(options) {
|
|
53
|
-
if (!this.config.methodOverride) this.config.methodOverride = {
|
|
54
|
-
enabled: true,
|
|
55
|
-
bodyKeys: ["_method"],
|
|
56
|
-
headerKeys: ["x-http-method"]
|
|
57
|
-
};
|
|
58
|
-
const override = options?.methodOverride;
|
|
59
|
-
if (!override) return;
|
|
60
|
-
if (typeof override.enabled === "boolean") this.config.methodOverride.enabled = override.enabled;
|
|
61
|
-
const bodyKeys = override.bodyKeys;
|
|
62
|
-
if (typeof bodyKeys !== "undefined") this.config.methodOverride.bodyKeys = (Array.isArray(bodyKeys) ? bodyKeys : [bodyKeys]).map((e) => String(e).trim()).filter(Boolean);
|
|
63
|
-
const headerKeys = override.headerKeys;
|
|
64
|
-
if (typeof headerKeys !== "undefined") this.config.methodOverride.headerKeys = (Array.isArray(headerKeys) ? headerKeys : [headerKeys]).map((e) => String(e).trim().toLowerCase()).filter(Boolean);
|
|
65
|
-
}
|
|
66
13
|
static async readBodyCached(ctx) {
|
|
67
14
|
if (this.bodyCache.has(ctx)) {
|
|
68
15
|
const cached = this.bodyCache.get(ctx);
|
|
@@ -78,231 +25,136 @@ var Router = class Router {
|
|
|
78
25
|
this.bodyCache.set(ctx, body);
|
|
79
26
|
return body;
|
|
80
27
|
}
|
|
81
|
-
static resolveMethodOverride(method, headers, body) {
|
|
82
|
-
if (!this.config.methodOverride?.enabled || method.toLowerCase() !== "post") return null;
|
|
83
|
-
let override;
|
|
84
|
-
for (const key of this.config.methodOverride?.headerKeys || []) {
|
|
85
|
-
const value = headers.get(key);
|
|
86
|
-
if (value) {
|
|
87
|
-
override = value;
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
if (!override && body && typeof body === "object") for (const key of this.config.methodOverride?.bodyKeys || []) {
|
|
92
|
-
const value = body[key];
|
|
93
|
-
if (typeof value !== "undefined" && value !== null && value !== "") {
|
|
94
|
-
override = value;
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
const normalized = String(override || "").trim().toLowerCase();
|
|
99
|
-
if (!normalized) return null;
|
|
100
|
-
if ([
|
|
101
|
-
"put",
|
|
102
|
-
"patch",
|
|
103
|
-
"delete",
|
|
104
|
-
"post"
|
|
105
|
-
].includes(normalized)) return normalized;
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
28
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* @param
|
|
112
|
-
* @param
|
|
113
|
-
* @param
|
|
29
|
+
* Adds a new route to the router with the specified HTTP methods, path, handler, and optional middlewares.
|
|
30
|
+
*
|
|
31
|
+
* @param methods
|
|
32
|
+
* @param path
|
|
33
|
+
* @param handler
|
|
34
|
+
* @param middlewares
|
|
114
35
|
*/
|
|
115
36
|
static add(methods, path, handler, middlewares) {
|
|
116
|
-
|
|
117
|
-
const activePrefix = context?.prefix ?? this.prefix;
|
|
118
|
-
const activeGroupMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
119
|
-
methods = Array.isArray(methods) ? methods : [methods];
|
|
120
|
-
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
121
|
-
const fullPath = this.normalizePath(`${activePrefix}/${path}`);
|
|
122
|
-
const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
123
|
-
...this.globalMiddlewares,
|
|
124
|
-
...activeGroupMiddlewares,
|
|
125
|
-
...middlewares || []
|
|
126
|
-
]);
|
|
127
|
-
if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
|
|
128
|
-
res.headers.set("Allow", "GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD");
|
|
129
|
-
res.status = 204;
|
|
130
|
-
});
|
|
131
|
-
this.routes.push(route);
|
|
132
|
-
for (const method of methods.map((m) => m.toUpperCase())) {
|
|
133
|
-
this.routesByPathMethod[`${method} ${fullPath}`] = route;
|
|
134
|
-
if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
|
|
135
|
-
this.routesByMethod[method].push(route);
|
|
136
|
-
}
|
|
37
|
+
super.add(methods, path, handler, middlewares);
|
|
137
38
|
}
|
|
138
39
|
/**
|
|
139
|
-
*
|
|
40
|
+
* Adds a new API resource route to the router for a given base path and controller, with options
|
|
41
|
+
* to specify included/excluded actions and middlewares.
|
|
140
42
|
*
|
|
141
|
-
* @param basePath
|
|
142
|
-
* @param controller
|
|
143
|
-
* @param options
|
|
43
|
+
* @param basePath
|
|
44
|
+
* @param controller
|
|
45
|
+
* @param options
|
|
144
46
|
*/
|
|
145
47
|
static apiResource(basePath, controller, options) {
|
|
146
|
-
|
|
147
|
-
index: {
|
|
148
|
-
method: "get",
|
|
149
|
-
path: "/"
|
|
150
|
-
},
|
|
151
|
-
show: {
|
|
152
|
-
method: "get",
|
|
153
|
-
path: "/:id"
|
|
154
|
-
},
|
|
155
|
-
create: {
|
|
156
|
-
method: "post",
|
|
157
|
-
path: "/"
|
|
158
|
-
},
|
|
159
|
-
update: {
|
|
160
|
-
method: "put",
|
|
161
|
-
path: "/:id"
|
|
162
|
-
},
|
|
163
|
-
destroy: {
|
|
164
|
-
method: "delete",
|
|
165
|
-
path: "/:id"
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
const only = options?.only || Object.keys(actions);
|
|
169
|
-
const except = options?.except || [];
|
|
170
|
-
const preController = typeof controller === "function" ? new controller() : controller;
|
|
171
|
-
for (const action of only) {
|
|
172
|
-
if (except.includes(action)) continue;
|
|
173
|
-
if (typeof preController[action] === "function") {
|
|
174
|
-
const { method, path } = actions[action];
|
|
175
|
-
const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
|
|
176
|
-
this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
48
|
+
super.apiResource(basePath, controller, options);
|
|
179
49
|
}
|
|
180
50
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
* @param
|
|
184
|
-
* @param
|
|
51
|
+
* Adds a new GET route to the router with the specified path, handler, and optional middlewares.
|
|
52
|
+
*
|
|
53
|
+
* @param path
|
|
54
|
+
* @param handler
|
|
55
|
+
* @param middlewares
|
|
185
56
|
*/
|
|
186
57
|
static get(path, handler, middlewares) {
|
|
187
|
-
|
|
58
|
+
super.get(path, handler, middlewares);
|
|
188
59
|
}
|
|
189
60
|
/**
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
* @param
|
|
193
|
-
* @param
|
|
61
|
+
* Adds a new POST route to the router with the specified path, handler, and optional middlewares.
|
|
62
|
+
*
|
|
63
|
+
* @param path
|
|
64
|
+
* @param handler
|
|
65
|
+
* @param middlewares
|
|
194
66
|
*/
|
|
195
67
|
static post(path, handler, middlewares) {
|
|
196
|
-
|
|
68
|
+
super.post(path, handler, middlewares);
|
|
197
69
|
}
|
|
198
70
|
/**
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
* @param
|
|
202
|
-
* @param
|
|
71
|
+
* Adds a new PUT route to the router with the specified path, handler, and optional middlewares.
|
|
72
|
+
*
|
|
73
|
+
* @param path
|
|
74
|
+
* @param handler
|
|
75
|
+
* @param middlewares
|
|
203
76
|
*/
|
|
204
77
|
static put(path, handler, middlewares) {
|
|
205
|
-
|
|
78
|
+
super.put(path, handler, middlewares);
|
|
206
79
|
}
|
|
207
80
|
/**
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
* @param
|
|
211
|
-
* @param
|
|
81
|
+
* Adds a new DELETE route to the router with the specified path, handler, and optional middlewares.
|
|
82
|
+
*
|
|
83
|
+
* @param path
|
|
84
|
+
* @param handler
|
|
85
|
+
* @param middlewares
|
|
212
86
|
*/
|
|
213
87
|
static delete(path, handler, middlewares) {
|
|
214
|
-
|
|
88
|
+
super.delete(path, handler, middlewares);
|
|
215
89
|
}
|
|
216
90
|
/**
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
* @param
|
|
220
|
-
* @param
|
|
91
|
+
* Adds a new PATCH route to the router with the specified path, handler, and optional middlewares.
|
|
92
|
+
*
|
|
93
|
+
* @param path
|
|
94
|
+
* @param handler
|
|
95
|
+
* @param middlewares
|
|
221
96
|
*/
|
|
222
97
|
static patch(path, handler, middlewares) {
|
|
223
|
-
|
|
98
|
+
super.patch(path, handler, middlewares);
|
|
224
99
|
}
|
|
225
100
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
* @param
|
|
229
|
-
* @param
|
|
101
|
+
* Adds a new OPTIONS route to the router with the specified path, handler, and optional middlewares.
|
|
102
|
+
*
|
|
103
|
+
* @param path
|
|
104
|
+
* @param handler
|
|
105
|
+
* @param middlewares
|
|
230
106
|
*/
|
|
231
107
|
static options(path, handler, middlewares) {
|
|
232
|
-
|
|
108
|
+
super.options(path, handler, middlewares);
|
|
233
109
|
}
|
|
234
110
|
/**
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
* @param
|
|
238
|
-
* @param
|
|
111
|
+
* Adds a new HEAD route to the router with the specified path, handler, and optional middlewares.
|
|
112
|
+
*
|
|
113
|
+
* @param path
|
|
114
|
+
* @param handler
|
|
115
|
+
* @param middlewares
|
|
239
116
|
*/
|
|
240
117
|
static head(path, handler, middlewares) {
|
|
241
|
-
|
|
118
|
+
super.head(path, handler, middlewares);
|
|
242
119
|
}
|
|
243
120
|
/**
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
* @param
|
|
121
|
+
* Defines a group of routes with a common prefix and optional middlewares, allowing for better
|
|
122
|
+
* organization and reuse of route configurations.
|
|
123
|
+
*
|
|
124
|
+
* @param prefix
|
|
125
|
+
* @param callback
|
|
126
|
+
* @param middlewares
|
|
248
127
|
*/
|
|
249
128
|
static async group(prefix, callback, middlewares) {
|
|
250
|
-
|
|
251
|
-
const previousPrefix = context?.prefix ?? this.prefix;
|
|
252
|
-
const previousMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
253
|
-
const fullPrefix = [previousPrefix, prefix].filter(Boolean).join("/");
|
|
254
|
-
const nextContext = {
|
|
255
|
-
prefix: this.normalizePath(fullPrefix),
|
|
256
|
-
groupMiddlewares: [...previousMiddlewares, ...middlewares || []]
|
|
257
|
-
};
|
|
258
|
-
await this.groupContext.run(nextContext, async () => {
|
|
259
|
-
await Promise.resolve(callback());
|
|
260
|
-
});
|
|
129
|
+
await super.group(prefix, callback, middlewares);
|
|
261
130
|
}
|
|
262
131
|
/**
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
* @param
|
|
132
|
+
* Adds global middlewares to the router, which will be applied to all routes.
|
|
133
|
+
*
|
|
134
|
+
* @param middlewares
|
|
135
|
+
* @param callback
|
|
266
136
|
*/
|
|
267
137
|
static middleware(middlewares, callback) {
|
|
268
|
-
|
|
269
|
-
this.globalMiddlewares = [...prevMiddlewares, ...middlewares || []];
|
|
270
|
-
callback();
|
|
271
|
-
this.globalMiddlewares = prevMiddlewares;
|
|
138
|
+
super.middleware(middlewares, callback);
|
|
272
139
|
}
|
|
273
140
|
static allRoutes(type) {
|
|
274
|
-
|
|
275
|
-
if (type === "path") return this.routesByPathMethod;
|
|
276
|
-
return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
|
|
141
|
+
return super.allRoutes(type);
|
|
277
142
|
}
|
|
278
143
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* All errors are thrown to H3 error handling middleware
|
|
144
|
+
* Applies the registered routes to the given H3 application instance, setting up the
|
|
145
|
+
* necessary handlers and middlewares for each route.
|
|
282
146
|
*
|
|
283
|
-
* @param app
|
|
147
|
+
* @param app
|
|
148
|
+
* @returns
|
|
284
149
|
*/
|
|
285
150
|
static apply(app) {
|
|
286
151
|
for (const route of this.routes) {
|
|
287
152
|
let handlerFunction = null;
|
|
288
153
|
let instance = null;
|
|
289
154
|
try {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
*/
|
|
294
|
-
handlerFunction = route.handler.bind(route);
|
|
295
|
-
else if (Array.isArray(route.handler) && route.handler.length === 2) {
|
|
296
|
-
const [Controller, method] = route.handler;
|
|
297
|
-
if (["function", "object"].includes(typeof Controller) && typeof Controller[method] === "function") {
|
|
298
|
-
instance = Controller;
|
|
299
|
-
handlerFunction = Controller[method].bind(Controller);
|
|
300
|
-
} else if (typeof Controller === "function") {
|
|
301
|
-
instance = new Controller();
|
|
302
|
-
if (typeof instance[method] === "function") handlerFunction = instance[method].bind(instance);
|
|
303
|
-
else throw new Error(`Method "${method}" not found in controller instance "${Controller.name}"`);
|
|
304
|
-
} else throw new Error(`Invalid controller type for route: ${route.path}`);
|
|
305
|
-
} else throw new Error(`Invalid handler format for route: ${route.path}`);
|
|
155
|
+
const resolved = this.resolveHandler(route);
|
|
156
|
+
handlerFunction = resolved.handlerFunction;
|
|
157
|
+
instance = resolved.instance;
|
|
306
158
|
} catch (error) {
|
|
307
159
|
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
308
160
|
throw error;
|
|
@@ -331,7 +183,11 @@ var Router = class Router {
|
|
|
331
183
|
const override = Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody);
|
|
332
184
|
if (method === "post" && override && override !== "post") return;
|
|
333
185
|
const inst = instance ?? route;
|
|
334
|
-
|
|
186
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
187
|
+
body: reqBody,
|
|
188
|
+
query: getQuery(ctx),
|
|
189
|
+
params: getRouterParams(ctx, { decode: true })
|
|
190
|
+
});
|
|
335
191
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
336
192
|
return await Promise.resolve(result);
|
|
337
193
|
} catch (error) {
|
|
@@ -348,7 +204,11 @@ var Router = class Router {
|
|
|
348
204
|
const reqBody = await Router.readBodyCached(ctx);
|
|
349
205
|
if (Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody) !== method) return;
|
|
350
206
|
const inst = instance ?? route;
|
|
351
|
-
|
|
207
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
208
|
+
body: reqBody,
|
|
209
|
+
query: getQuery(ctx),
|
|
210
|
+
params: getRouterParams(ctx, { decode: true })
|
|
211
|
+
});
|
|
352
212
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
353
213
|
return await Promise.resolve(result);
|
|
354
214
|
} catch (error) {
|
|
@@ -359,20 +219,6 @@ var Router = class Router {
|
|
|
359
219
|
}
|
|
360
220
|
return app;
|
|
361
221
|
}
|
|
362
|
-
static async bindRequestToInstance(ctx, instance, route, body) {
|
|
363
|
-
if (!instance) return;
|
|
364
|
-
instance.ctx = ctx;
|
|
365
|
-
instance.body = body ?? await Router.readBodyCached(ctx);
|
|
366
|
-
instance.query = getQuery(ctx);
|
|
367
|
-
instance.params = getRouterParams(ctx, { decode: true });
|
|
368
|
-
instance.clearRequest = new ClearRequest({
|
|
369
|
-
ctx,
|
|
370
|
-
route,
|
|
371
|
-
body: instance.body,
|
|
372
|
-
query: instance.query,
|
|
373
|
-
params: instance.params
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
222
|
};
|
|
377
223
|
|
|
378
224
|
//#endregion
|