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