clear-router 2.1.11 → 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 +91 -244
- package/dist/express/index.d.cts +68 -96
- package/dist/express/index.d.mts +68 -96
- package/dist/express/index.mjs +91 -244
- package/dist/h3/index.cjs +99 -244
- package/dist/h3/index.d.cts +72 -93
- package/dist/h3/index.d.mts +72 -93
- package/dist/h3/index.mjs +99 -244
- package/dist/index.cjs +342 -0
- package/dist/index.d.cts +184 -31
- package/dist/index.d.mts +184 -31
- 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/dist/types/express.d.mts +5 -2
- package/dist/types/h3.d.mts +7 -2
- package/package.json +10 -2
- package/dist/Route-BbPXcDGX.mjs +0 -50
- package/dist/Route-DhC4kNPX.cjs +0 -62
- package/dist/basic-DXbqD6cP.d.cts +0 -130
- package/dist/basic-vvrFwa_Y.d.mts +0 -130
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,299 +1,160 @@
|
|
|
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
|
-
if (this.bodyCache.has(ctx))
|
|
68
|
-
|
|
14
|
+
if (this.bodyCache.has(ctx)) {
|
|
15
|
+
const cached = this.bodyCache.get(ctx);
|
|
16
|
+
ctx.req.getBody = () => cached;
|
|
17
|
+
return cached;
|
|
18
|
+
}
|
|
19
|
+
let body = {};
|
|
20
|
+
if (ctx.req.headers.get("content-type")?.includes("multipart/form-data")) (await ctx.req.formData()).forEach((value, key) => {
|
|
21
|
+
body[key] = value;
|
|
22
|
+
});
|
|
23
|
+
else body = await readBody(ctx) ?? {};
|
|
24
|
+
ctx.req.getBody = () => body;
|
|
69
25
|
this.bodyCache.set(ctx, body);
|
|
70
26
|
return body;
|
|
71
27
|
}
|
|
72
|
-
static resolveMethodOverride(method, headers, body) {
|
|
73
|
-
if (!this.config.methodOverride?.enabled || method.toLowerCase() !== "post") return null;
|
|
74
|
-
let override;
|
|
75
|
-
for (const key of this.config.methodOverride?.headerKeys || []) {
|
|
76
|
-
const value = headers.get(key);
|
|
77
|
-
if (value) {
|
|
78
|
-
override = value;
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (!override && body && typeof body === "object") for (const key of this.config.methodOverride?.bodyKeys || []) {
|
|
83
|
-
const value = body[key];
|
|
84
|
-
if (typeof value !== "undefined" && value !== null && value !== "") {
|
|
85
|
-
override = value;
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
const normalized = String(override || "").trim().toLowerCase();
|
|
90
|
-
if (!normalized) return null;
|
|
91
|
-
if ([
|
|
92
|
-
"put",
|
|
93
|
-
"patch",
|
|
94
|
-
"delete",
|
|
95
|
-
"post"
|
|
96
|
-
].includes(normalized)) return normalized;
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
28
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @param
|
|
103
|
-
* @param
|
|
104
|
-
* @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
|
|
105
35
|
*/
|
|
106
36
|
static add(methods, path, handler, middlewares) {
|
|
107
|
-
|
|
108
|
-
const activePrefix = context?.prefix ?? this.prefix;
|
|
109
|
-
const activeGroupMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
110
|
-
methods = Array.isArray(methods) ? methods : [methods];
|
|
111
|
-
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
112
|
-
const fullPath = this.normalizePath(`${activePrefix}/${path}`);
|
|
113
|
-
const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
114
|
-
...this.globalMiddlewares,
|
|
115
|
-
...activeGroupMiddlewares,
|
|
116
|
-
...middlewares || []
|
|
117
|
-
]);
|
|
118
|
-
if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
|
|
119
|
-
res.headers.set("Allow", "GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD");
|
|
120
|
-
res.status = 204;
|
|
121
|
-
});
|
|
122
|
-
this.routes.push(route);
|
|
123
|
-
for (const method of methods.map((m) => m.toUpperCase())) {
|
|
124
|
-
this.routesByPathMethod[`${method} ${fullPath}`] = route;
|
|
125
|
-
if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
|
|
126
|
-
this.routesByMethod[method].push(route);
|
|
127
|
-
}
|
|
37
|
+
super.add(methods, path, handler, middlewares);
|
|
128
38
|
}
|
|
129
39
|
/**
|
|
130
|
-
*
|
|
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.
|
|
131
42
|
*
|
|
132
|
-
* @param basePath
|
|
133
|
-
* @param controller
|
|
134
|
-
* @param options
|
|
43
|
+
* @param basePath
|
|
44
|
+
* @param controller
|
|
45
|
+
* @param options
|
|
135
46
|
*/
|
|
136
47
|
static apiResource(basePath, controller, options) {
|
|
137
|
-
|
|
138
|
-
index: {
|
|
139
|
-
method: "get",
|
|
140
|
-
path: "/"
|
|
141
|
-
},
|
|
142
|
-
show: {
|
|
143
|
-
method: "get",
|
|
144
|
-
path: "/:id"
|
|
145
|
-
},
|
|
146
|
-
create: {
|
|
147
|
-
method: "post",
|
|
148
|
-
path: "/"
|
|
149
|
-
},
|
|
150
|
-
update: {
|
|
151
|
-
method: "put",
|
|
152
|
-
path: "/:id"
|
|
153
|
-
},
|
|
154
|
-
destroy: {
|
|
155
|
-
method: "delete",
|
|
156
|
-
path: "/:id"
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
const only = options?.only || Object.keys(actions);
|
|
160
|
-
const except = options?.except || [];
|
|
161
|
-
const preController = typeof controller === "function" ? new controller() : controller;
|
|
162
|
-
for (const action of only) {
|
|
163
|
-
if (except.includes(action)) continue;
|
|
164
|
-
if (typeof preController[action] === "function") {
|
|
165
|
-
const { method, path } = actions[action];
|
|
166
|
-
const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
|
|
167
|
-
this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
48
|
+
super.apiResource(basePath, controller, options);
|
|
170
49
|
}
|
|
171
50
|
/**
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* @param
|
|
175
|
-
* @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
|
|
176
56
|
*/
|
|
177
57
|
static get(path, handler, middlewares) {
|
|
178
|
-
|
|
58
|
+
super.get(path, handler, middlewares);
|
|
179
59
|
}
|
|
180
60
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
* @param
|
|
184
|
-
* @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
|
|
185
66
|
*/
|
|
186
67
|
static post(path, handler, middlewares) {
|
|
187
|
-
|
|
68
|
+
super.post(path, handler, middlewares);
|
|
188
69
|
}
|
|
189
70
|
/**
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
* @param
|
|
193
|
-
* @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
|
|
194
76
|
*/
|
|
195
77
|
static put(path, handler, middlewares) {
|
|
196
|
-
|
|
78
|
+
super.put(path, handler, middlewares);
|
|
197
79
|
}
|
|
198
80
|
/**
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
* @param
|
|
202
|
-
* @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
|
|
203
86
|
*/
|
|
204
87
|
static delete(path, handler, middlewares) {
|
|
205
|
-
|
|
88
|
+
super.delete(path, handler, middlewares);
|
|
206
89
|
}
|
|
207
90
|
/**
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
* @param
|
|
211
|
-
* @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
|
|
212
96
|
*/
|
|
213
97
|
static patch(path, handler, middlewares) {
|
|
214
|
-
|
|
98
|
+
super.patch(path, handler, middlewares);
|
|
215
99
|
}
|
|
216
100
|
/**
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
* @param
|
|
220
|
-
* @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
|
|
221
106
|
*/
|
|
222
107
|
static options(path, handler, middlewares) {
|
|
223
|
-
|
|
108
|
+
super.options(path, handler, middlewares);
|
|
224
109
|
}
|
|
225
110
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
* @param
|
|
229
|
-
* @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
|
|
230
116
|
*/
|
|
231
117
|
static head(path, handler, middlewares) {
|
|
232
|
-
|
|
118
|
+
super.head(path, handler, middlewares);
|
|
233
119
|
}
|
|
234
120
|
/**
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
* @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
|
|
239
127
|
*/
|
|
240
128
|
static async group(prefix, callback, middlewares) {
|
|
241
|
-
|
|
242
|
-
const previousPrefix = context?.prefix ?? this.prefix;
|
|
243
|
-
const previousMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
244
|
-
const fullPrefix = [previousPrefix, prefix].filter(Boolean).join("/");
|
|
245
|
-
const nextContext = {
|
|
246
|
-
prefix: this.normalizePath(fullPrefix),
|
|
247
|
-
groupMiddlewares: [...previousMiddlewares, ...middlewares || []]
|
|
248
|
-
};
|
|
249
|
-
await this.groupContext.run(nextContext, async () => {
|
|
250
|
-
await Promise.resolve(callback());
|
|
251
|
-
});
|
|
129
|
+
await super.group(prefix, callback, middlewares);
|
|
252
130
|
}
|
|
253
131
|
/**
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
* @param
|
|
132
|
+
* Adds global middlewares to the router, which will be applied to all routes.
|
|
133
|
+
*
|
|
134
|
+
* @param middlewares
|
|
135
|
+
* @param callback
|
|
257
136
|
*/
|
|
258
137
|
static middleware(middlewares, callback) {
|
|
259
|
-
|
|
260
|
-
this.globalMiddlewares = [...prevMiddlewares, ...middlewares || []];
|
|
261
|
-
callback();
|
|
262
|
-
this.globalMiddlewares = prevMiddlewares;
|
|
138
|
+
super.middleware(middlewares, callback);
|
|
263
139
|
}
|
|
264
140
|
static allRoutes(type) {
|
|
265
|
-
|
|
266
|
-
if (type === "path") return this.routesByPathMethod;
|
|
267
|
-
return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
|
|
141
|
+
return super.allRoutes(type);
|
|
268
142
|
}
|
|
269
143
|
/**
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
* 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.
|
|
273
146
|
*
|
|
274
|
-
* @param app
|
|
147
|
+
* @param app
|
|
148
|
+
* @returns
|
|
275
149
|
*/
|
|
276
150
|
static apply(app) {
|
|
277
151
|
for (const route of this.routes) {
|
|
278
152
|
let handlerFunction = null;
|
|
279
153
|
let instance = null;
|
|
280
154
|
try {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
*/
|
|
285
|
-
handlerFunction = route.handler.bind(route);
|
|
286
|
-
else if (Array.isArray(route.handler) && route.handler.length === 2) {
|
|
287
|
-
const [Controller, method] = route.handler;
|
|
288
|
-
if (["function", "object"].includes(typeof Controller) && typeof Controller[method] === "function") {
|
|
289
|
-
instance = Controller;
|
|
290
|
-
handlerFunction = Controller[method].bind(Controller);
|
|
291
|
-
} else if (typeof Controller === "function") {
|
|
292
|
-
instance = new Controller();
|
|
293
|
-
if (typeof instance[method] === "function") handlerFunction = instance[method].bind(instance);
|
|
294
|
-
else throw new Error(`Method "${method}" not found in controller instance "${Controller.name}"`);
|
|
295
|
-
} else throw new Error(`Invalid controller type for route: ${route.path}`);
|
|
296
|
-
} 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;
|
|
297
158
|
} catch (error) {
|
|
298
159
|
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
299
160
|
throw error;
|
|
@@ -322,7 +183,11 @@ var Router = class Router {
|
|
|
322
183
|
const override = Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody);
|
|
323
184
|
if (method === "post" && override && override !== "post") return;
|
|
324
185
|
const inst = instance ?? route;
|
|
325
|
-
|
|
186
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
187
|
+
body: reqBody,
|
|
188
|
+
query: getQuery(ctx),
|
|
189
|
+
params: getRouterParams(ctx, { decode: true })
|
|
190
|
+
});
|
|
326
191
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
327
192
|
return await Promise.resolve(result);
|
|
328
193
|
} catch (error) {
|
|
@@ -339,7 +204,11 @@ var Router = class Router {
|
|
|
339
204
|
const reqBody = await Router.readBodyCached(ctx);
|
|
340
205
|
if (Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody) !== method) return;
|
|
341
206
|
const inst = instance ?? route;
|
|
342
|
-
|
|
207
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
208
|
+
body: reqBody,
|
|
209
|
+
query: getQuery(ctx),
|
|
210
|
+
params: getRouterParams(ctx, { decode: true })
|
|
211
|
+
});
|
|
343
212
|
const result = handlerFunction(ctx, inst.clearRequest);
|
|
344
213
|
return await Promise.resolve(result);
|
|
345
214
|
} catch (error) {
|
|
@@ -350,20 +219,6 @@ var Router = class Router {
|
|
|
350
219
|
}
|
|
351
220
|
return app;
|
|
352
221
|
}
|
|
353
|
-
static async bindRequestToInstance(ctx, instance, route, body) {
|
|
354
|
-
if (!instance) return;
|
|
355
|
-
instance.ctx = ctx;
|
|
356
|
-
instance.body = body ?? await Router.readBodyCached(ctx);
|
|
357
|
-
instance.query = getQuery(ctx);
|
|
358
|
-
instance.params = getRouterParams(ctx, { decode: true });
|
|
359
|
-
instance.clearRequest = new ClearRequest({
|
|
360
|
-
ctx,
|
|
361
|
-
route,
|
|
362
|
-
body: instance.body,
|
|
363
|
-
query: instance.query,
|
|
364
|
-
params: instance.params
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
222
|
};
|
|
368
223
|
|
|
369
224
|
//#endregion
|