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