clear-router 2.5.12 → 2.5.14
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 +2 -0
- package/dist/{bindings-CDGLuAq4.cjs → bindings-CLsZjOEy.cjs} +8 -1
- package/dist/{bindings-B6x2HfzP.mjs → bindings-XLDXFpHZ.mjs} +8 -1
- package/dist/core/index.cjs +2 -2
- package/dist/core/index.d.cts +1 -1
- package/dist/core/index.d.mts +1 -1
- package/dist/core/index.mjs +2 -2
- package/dist/decorators/index.cjs +1 -1
- package/dist/decorators/index.mjs +1 -1
- package/dist/decorators/setup.cjs +2 -2
- package/dist/decorators/setup.d.mts +0 -1
- package/dist/decorators/setup.mjs +2 -2
- package/dist/express/index.cjs +16 -13
- package/dist/express/index.d.cts +11 -9
- package/dist/express/index.d.mts +11 -9
- package/dist/express/index.mjs +16 -13
- package/dist/fastify/index.cjs +18 -15
- package/dist/fastify/index.d.cts +11 -9
- package/dist/fastify/index.d.mts +11 -9
- package/dist/fastify/index.mjs +18 -15
- package/dist/h3/index.cjs +16 -13
- package/dist/h3/index.d.cts +11 -9
- package/dist/h3/index.d.mts +11 -9
- package/dist/h3/index.mjs +16 -13
- package/dist/hono/index.cjs +16 -13
- package/dist/hono/index.d.cts +11 -9
- package/dist/hono/index.d.mts +11 -9
- package/dist/hono/index.mjs +16 -13
- package/dist/index.cjs +114 -13
- package/dist/index.d.cts +54 -25
- package/dist/index.d.mts +54 -25
- package/dist/index.mjs +114 -13
- package/dist/koa/index.cjs +16 -13
- package/dist/koa/index.d.cts +11 -9
- package/dist/koa/index.d.mts +11 -9
- package/dist/koa/index.mjs +16 -13
- package/dist/{responses-DuZeRyGE.mjs → responses-BvETUeDL.mjs} +1 -1
- package/dist/{responses-CBP3RYjJ.cjs → responses-Bvnk0uvc.cjs} +1 -1
- package/dist/{router-91xVPlV0.d.mts → router-Bk8mXRu1.d.mts} +53 -24
- package/dist/{router-DPJfzvy5.cjs → router-CAnd539U.cjs} +107 -13
- package/dist/{router-BkJMl4xv.mjs → router-DSq9dtuQ.mjs} +107 -13
- package/dist/{router-35iBbCaF.d.cts → router-jwZwD8ZT.d.cts} +53 -24
- package/dist/types/Route.d.mts +16 -1
- package/dist/types/core/Response.d.mts +3 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as getStandardMetadata, c as Request, i as getDesignParamTypes, n as Container, o as isClass, r as getBindingMetadataFromTargets, s as Response } from "./bindings-
|
|
1
|
+
import { a as getStandardMetadata, c as Request, i as getDesignParamTypes, n as Container, o as isClass, r as getBindingMetadataFromTargets, s as Response } from "./bindings-XLDXFpHZ.mjs";
|
|
2
2
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
3
|
|
|
4
4
|
//#region src/Route.ts
|
|
@@ -10,21 +10,49 @@ var Route = class {
|
|
|
10
10
|
clearRequest;
|
|
11
11
|
methods;
|
|
12
12
|
path;
|
|
13
|
+
registrationPaths;
|
|
14
|
+
parameters;
|
|
15
|
+
routeName;
|
|
13
16
|
handler;
|
|
14
17
|
middlewares;
|
|
15
18
|
controllerName;
|
|
16
19
|
actionName;
|
|
17
20
|
handlerType;
|
|
18
21
|
middlewareCount;
|
|
19
|
-
constructor(methods, path, handler, middlewares = []) {
|
|
22
|
+
constructor(methods, path, handler, middlewares = [], options = {}) {
|
|
20
23
|
this.methods = methods;
|
|
21
24
|
this.path = path;
|
|
25
|
+
this.registrationPaths = options.registrationPaths || [path];
|
|
26
|
+
this.parameters = options.parameters || [];
|
|
22
27
|
this.handler = handler;
|
|
23
28
|
this.middlewares = middlewares;
|
|
24
29
|
this.handlerType = Array.isArray(handler) ? "controller" : "function";
|
|
25
30
|
this.middlewareCount = middlewares.length;
|
|
26
31
|
this.controllerName = Array.isArray(handler) ? handler[0]?.name : void 0;
|
|
27
32
|
this.actionName = Array.isArray(handler) ? handler[1] : typeof handler === "function" ? handler.constructor.name ?? handler.name : void 0;
|
|
33
|
+
this.onName = options.onName;
|
|
34
|
+
}
|
|
35
|
+
onName;
|
|
36
|
+
name(name) {
|
|
37
|
+
const previousName = this.routeName;
|
|
38
|
+
this.routeName = name;
|
|
39
|
+
this.onName?.(name, this, previousName);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
toPath(params = {}) {
|
|
43
|
+
return this.path.replace(/\/?\{([^{}]+)\}/g, (segment, raw) => {
|
|
44
|
+
const optional = raw.endsWith("?");
|
|
45
|
+
const [rawName, rawField] = (optional ? raw.slice(0, -1) : raw).split(":", 2);
|
|
46
|
+
const name = rawName.trim();
|
|
47
|
+
const field = rawField?.trim();
|
|
48
|
+
const value = params[name];
|
|
49
|
+
const resolved = field && value && typeof value === "object" ? value[field] : value;
|
|
50
|
+
if (typeof resolved === "undefined" || resolved === null || resolved === "") {
|
|
51
|
+
if (optional) return "";
|
|
52
|
+
throw new Error(`Missing required route parameter: ${name}`);
|
|
53
|
+
}
|
|
54
|
+
return `${segment.startsWith("/") ? "/" : ""}${encodeURIComponent(String(resolved))}`;
|
|
55
|
+
}) || "/";
|
|
28
56
|
}
|
|
29
57
|
};
|
|
30
58
|
|
|
@@ -107,6 +135,7 @@ var CoreRouter = class {
|
|
|
107
135
|
routes: [],
|
|
108
136
|
routesByPathMethod: {},
|
|
109
137
|
routesByMethod: {},
|
|
138
|
+
routesByName: {},
|
|
110
139
|
prefix: "",
|
|
111
140
|
groupMiddlewares: [],
|
|
112
141
|
globalMiddlewares: []
|
|
@@ -123,6 +152,7 @@ var CoreRouter = class {
|
|
|
123
152
|
"routes",
|
|
124
153
|
"routesByPathMethod",
|
|
125
154
|
"routesByMethod",
|
|
155
|
+
"routesByName",
|
|
126
156
|
"prefix",
|
|
127
157
|
"groupMiddlewares",
|
|
128
158
|
"globalMiddlewares"
|
|
@@ -255,6 +285,7 @@ var CoreRouter = class {
|
|
|
255
285
|
static routes = [];
|
|
256
286
|
static routesByPathMethod = {};
|
|
257
287
|
static routesByMethod = {};
|
|
288
|
+
static routesByName = {};
|
|
258
289
|
static prefix = "";
|
|
259
290
|
static groupMiddlewares = [];
|
|
260
291
|
static globalMiddlewares = [];
|
|
@@ -306,6 +337,7 @@ var CoreRouter = class {
|
|
|
306
337
|
if (!Array.isArray(this.routes)) this.routes = [];
|
|
307
338
|
if (!this.routesByPathMethod || typeof this.routesByPathMethod !== "object") this.routesByPathMethod = {};
|
|
308
339
|
if (!this.routesByMethod || typeof this.routesByMethod !== "object") this.routesByMethod = {};
|
|
340
|
+
if (!this.routesByName || typeof this.routesByName !== "object") this.routesByName = {};
|
|
309
341
|
if (typeof this.prefix !== "string") this.prefix = "";
|
|
310
342
|
if (!Array.isArray(this.groupMiddlewares)) this.groupMiddlewares = [];
|
|
311
343
|
if (!Array.isArray(this.globalMiddlewares)) this.globalMiddlewares = [];
|
|
@@ -320,6 +352,47 @@ var CoreRouter = class {
|
|
|
320
352
|
static normalizePath(path) {
|
|
321
353
|
return "/" + path.split("/").filter(Boolean).join("/");
|
|
322
354
|
}
|
|
355
|
+
static parseRouteParameters(path) {
|
|
356
|
+
const parameters = [];
|
|
357
|
+
const seen = /* @__PURE__ */ new Set();
|
|
358
|
+
const pattern = /\{([^{}]+)\}/g;
|
|
359
|
+
let match;
|
|
360
|
+
while ((match = pattern.exec(path)) !== null) {
|
|
361
|
+
const raw = match[1].trim();
|
|
362
|
+
const optional = raw.endsWith("?");
|
|
363
|
+
const [name, field] = (optional ? raw.slice(0, -1) : raw).split(":", 2).map((part) => part.trim());
|
|
364
|
+
if (!name || seen.has(name)) continue;
|
|
365
|
+
seen.add(name);
|
|
366
|
+
parameters.push({
|
|
367
|
+
name,
|
|
368
|
+
field: field || void 0,
|
|
369
|
+
optional
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
return parameters;
|
|
373
|
+
}
|
|
374
|
+
static expandRoutePath(path) {
|
|
375
|
+
let paths = [""];
|
|
376
|
+
const segments = this.normalizePath(path).split("/").filter(Boolean);
|
|
377
|
+
for (const segment of segments) {
|
|
378
|
+
const match = segment.match(/^\{([^{}]+)\}$/);
|
|
379
|
+
if (!match) {
|
|
380
|
+
paths = paths.map((current) => `${current}/${segment}`);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
const raw = match[1].trim();
|
|
384
|
+
const optional = raw.endsWith("?");
|
|
385
|
+
const [rawName] = (optional ? raw.slice(0, -1) : raw).split(":", 2);
|
|
386
|
+
const name = rawName.trim();
|
|
387
|
+
if (!name) continue;
|
|
388
|
+
const parameterSegment = `/:${name}`;
|
|
389
|
+
paths = optional ? paths.flatMap((current) => [current, `${current}${parameterSegment}`]) : paths.map((current) => `${current}${parameterSegment}`);
|
|
390
|
+
}
|
|
391
|
+
return paths.map((path) => path || "/");
|
|
392
|
+
}
|
|
393
|
+
static routeRegistrationPaths(path) {
|
|
394
|
+
return this.expandRoutePath(path);
|
|
395
|
+
}
|
|
323
396
|
/**
|
|
324
397
|
* Configures the router with the given options, such as method override settings.
|
|
325
398
|
*
|
|
@@ -393,11 +466,20 @@ var CoreRouter = class {
|
|
|
393
466
|
methods = Array.isArray(methods) ? methods : [methods];
|
|
394
467
|
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
395
468
|
const fullPath = this.normalizePath(`${activePrefix}/${path}`);
|
|
469
|
+
const registrationPaths = this.routeRegistrationPaths(fullPath);
|
|
470
|
+
const parameters = this.parseRouteParameters(fullPath);
|
|
396
471
|
const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
397
472
|
...this.globalMiddlewares,
|
|
398
473
|
...activeGroupMiddlewares,
|
|
399
474
|
...middlewares || []
|
|
400
|
-
]
|
|
475
|
+
], {
|
|
476
|
+
registrationPaths,
|
|
477
|
+
parameters,
|
|
478
|
+
onName: (name, route, previousName) => {
|
|
479
|
+
if (previousName && this.routesByName[previousName] === route) delete this.routesByName[previousName];
|
|
480
|
+
this.routesByName[name] = route;
|
|
481
|
+
}
|
|
482
|
+
});
|
|
401
483
|
if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, this.createDefaultOptionsHandler());
|
|
402
484
|
this.routes.push(route);
|
|
403
485
|
for (const method of methods.map((m) => m.toUpperCase())) {
|
|
@@ -405,6 +487,7 @@ var CoreRouter = class {
|
|
|
405
487
|
if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
|
|
406
488
|
this.routesByMethod[method].push(route);
|
|
407
489
|
}
|
|
490
|
+
return route;
|
|
408
491
|
}
|
|
409
492
|
/**
|
|
410
493
|
* Define a resourceful API controller with standard CRUD routes.
|
|
@@ -458,7 +541,7 @@ var CoreRouter = class {
|
|
|
458
541
|
* @param middlewares Optional middlewares to apply to the GET route.
|
|
459
542
|
*/
|
|
460
543
|
static get(path, handler, middlewares) {
|
|
461
|
-
this.add("get", path, handler, middlewares);
|
|
544
|
+
return this.add("get", path, handler, middlewares);
|
|
462
545
|
}
|
|
463
546
|
/**
|
|
464
547
|
* Adds a new POST route to the router.
|
|
@@ -469,7 +552,7 @@ var CoreRouter = class {
|
|
|
469
552
|
* @param middlewares
|
|
470
553
|
*/
|
|
471
554
|
static post(path, handler, middlewares) {
|
|
472
|
-
this.add("post", path, handler, middlewares);
|
|
555
|
+
return this.add("post", path, handler, middlewares);
|
|
473
556
|
}
|
|
474
557
|
/**
|
|
475
558
|
* Adds a new PUT route to the router.
|
|
@@ -480,7 +563,7 @@ var CoreRouter = class {
|
|
|
480
563
|
* @param middlewares
|
|
481
564
|
*/
|
|
482
565
|
static put(path, handler, middlewares) {
|
|
483
|
-
this.add("put", path, handler, middlewares);
|
|
566
|
+
return this.add("put", path, handler, middlewares);
|
|
484
567
|
}
|
|
485
568
|
/**
|
|
486
569
|
* Adds a new DELETE route to the router.
|
|
@@ -491,7 +574,7 @@ var CoreRouter = class {
|
|
|
491
574
|
* @param middlewares
|
|
492
575
|
*/
|
|
493
576
|
static delete(path, handler, middlewares) {
|
|
494
|
-
this.add("delete", path, handler, middlewares);
|
|
577
|
+
return this.add("delete", path, handler, middlewares);
|
|
495
578
|
}
|
|
496
579
|
/**
|
|
497
580
|
* Adds a new PATCH route to the router.
|
|
@@ -502,7 +585,7 @@ var CoreRouter = class {
|
|
|
502
585
|
* @param middlewares
|
|
503
586
|
*/
|
|
504
587
|
static patch(path, handler, middlewares) {
|
|
505
|
-
this.add("patch", path, handler, middlewares);
|
|
588
|
+
return this.add("patch", path, handler, middlewares);
|
|
506
589
|
}
|
|
507
590
|
/**
|
|
508
591
|
* Adds a new OPTIONS route to the router.
|
|
@@ -513,7 +596,7 @@ var CoreRouter = class {
|
|
|
513
596
|
* @param middlewares
|
|
514
597
|
*/
|
|
515
598
|
static options(path, handler, middlewares) {
|
|
516
|
-
this.add("options", path, handler, middlewares);
|
|
599
|
+
return this.add("options", path, handler, middlewares);
|
|
517
600
|
}
|
|
518
601
|
/**
|
|
519
602
|
* Adds a new HEAD route to the router.
|
|
@@ -524,7 +607,7 @@ var CoreRouter = class {
|
|
|
524
607
|
* @param middlewares
|
|
525
608
|
*/
|
|
526
609
|
static head(path, handler, middlewares) {
|
|
527
|
-
this.add("head", path, handler, middlewares);
|
|
610
|
+
return this.add("head", path, handler, middlewares);
|
|
528
611
|
}
|
|
529
612
|
/**
|
|
530
613
|
* Defines a group of routes with a common prefix.
|
|
@@ -566,8 +649,16 @@ var CoreRouter = class {
|
|
|
566
649
|
this.ensureState();
|
|
567
650
|
if (type === "method") return this.routesByMethod;
|
|
568
651
|
if (type === "path") return this.routesByPathMethod;
|
|
652
|
+
if (type === "name") return this.routesByName;
|
|
569
653
|
return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
|
|
570
654
|
}
|
|
655
|
+
static route(name) {
|
|
656
|
+
this.ensureState();
|
|
657
|
+
return this.routesByName[name];
|
|
658
|
+
}
|
|
659
|
+
static url(name, params) {
|
|
660
|
+
return this.route(name)?.toPath(params);
|
|
661
|
+
}
|
|
571
662
|
static resolveHandler(route) {
|
|
572
663
|
let handlerFunction;
|
|
573
664
|
let instance = null;
|
|
@@ -634,8 +725,7 @@ var CoreRouter = class {
|
|
|
634
725
|
designTokens
|
|
635
726
|
});
|
|
636
727
|
if (pluginArgs) return handlerFunction(...pluginArgs);
|
|
637
|
-
if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
|
|
638
|
-
if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
|
|
728
|
+
if (!metadata || !tokens.length) return handlerFunction(ctx, ctx.clearRequest);
|
|
639
729
|
const args = [];
|
|
640
730
|
for (const token of tokens) {
|
|
641
731
|
const resolved = await Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
|
|
@@ -664,7 +754,11 @@ var CoreRouter = class {
|
|
|
664
754
|
clearRequest.query = payload.query;
|
|
665
755
|
clearRequest.params = payload.params;
|
|
666
756
|
ctx.clearRequest = clearRequest;
|
|
667
|
-
|
|
757
|
+
Container.bind(Request, ctx.clearRequest);
|
|
758
|
+
if (!(ctx.clearResponse instanceof Response)) {
|
|
759
|
+
ctx.clearResponse = new Response(ctx.response ?? ctx.reply ?? ctx.res);
|
|
760
|
+
Container.bind(Response, ctx.clearResponse);
|
|
761
|
+
}
|
|
668
762
|
if (!instance) return;
|
|
669
763
|
instance.ctx = ctx;
|
|
670
764
|
instance.body = payload.body;
|
|
@@ -9,8 +9,10 @@ declare class Response$2 {
|
|
|
9
9
|
headers: Headers;
|
|
10
10
|
sent: boolean;
|
|
11
11
|
statusCode: number;
|
|
12
|
-
|
|
12
|
+
statusText: string;
|
|
13
|
+
constructor(init?: Partial<Response$2 | globalThis.Response>);
|
|
13
14
|
status(code: number): this;
|
|
15
|
+
setStatusText(text: string): this;
|
|
14
16
|
code(code: number): this;
|
|
15
17
|
setHeader(name: string, value: string): this;
|
|
16
18
|
header(name: string, value: string): this;
|
|
@@ -133,6 +135,11 @@ req: Request$1) => any | Promise<any>;
|
|
|
133
135
|
type Handler = RouteHandler | ControllerHandler;
|
|
134
136
|
//#endregion
|
|
135
137
|
//#region src/Route.d.ts
|
|
138
|
+
interface RouteParameter {
|
|
139
|
+
name: string;
|
|
140
|
+
field?: string;
|
|
141
|
+
optional: boolean;
|
|
142
|
+
}
|
|
136
143
|
declare class Route<X = any, M = Middleware | Middleware$1, H = any> {
|
|
137
144
|
ctx: X;
|
|
138
145
|
body: RequestData;
|
|
@@ -141,13 +148,23 @@ declare class Route<X = any, M = Middleware | Middleware$1, H = any> {
|
|
|
141
148
|
clearRequest: ClearRequest;
|
|
142
149
|
methods: HttpMethod[];
|
|
143
150
|
path: string;
|
|
151
|
+
registrationPaths: string[];
|
|
152
|
+
parameters: RouteParameter[];
|
|
153
|
+
routeName?: string;
|
|
144
154
|
handler: H;
|
|
145
155
|
middlewares: M[];
|
|
146
156
|
controllerName?: string;
|
|
147
157
|
actionName?: string;
|
|
148
158
|
handlerType: 'function' | 'controller';
|
|
149
159
|
middlewareCount: number;
|
|
150
|
-
constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[]
|
|
160
|
+
constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[], options?: {
|
|
161
|
+
registrationPaths?: string[];
|
|
162
|
+
parameters?: RouteParameter[];
|
|
163
|
+
onName?: (name: string, route: Route<X, M, H>, previousName?: string) => void;
|
|
164
|
+
});
|
|
165
|
+
private onName?;
|
|
166
|
+
name(name: string): this;
|
|
167
|
+
toPath(params?: RequestData): string;
|
|
151
168
|
}
|
|
152
169
|
//#endregion
|
|
153
170
|
//#region src/ClearRequest.d.ts
|
|
@@ -258,7 +275,7 @@ declare abstract class CoreRouter {
|
|
|
258
275
|
protected static createBaseConfig(): RouterConfig;
|
|
259
276
|
protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
|
|
260
277
|
protected static getDefaultConfig(): RouterConfig;
|
|
261
|
-
protected static resolveStateNamespace(
|
|
278
|
+
protected static resolveStateNamespace(): string;
|
|
262
279
|
protected static getStateStore(): Record<string, any>;
|
|
263
280
|
protected static getPluginStore(): Set<string>;
|
|
264
281
|
protected static getPluginPendingStore(): Set<Promise<void>>;
|
|
@@ -272,14 +289,15 @@ declare abstract class CoreRouter {
|
|
|
272
289
|
routes: Array<Route<any, any, any>>;
|
|
273
290
|
routesByPathMethod: Record<string, Route<any, any, any>>;
|
|
274
291
|
routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
|
|
292
|
+
routesByName: Record<string, Route<any, any, any>>;
|
|
275
293
|
prefix: string;
|
|
276
294
|
groupMiddlewares: any[];
|
|
277
295
|
globalMiddlewares: any[];
|
|
278
296
|
};
|
|
279
|
-
protected static bindStateAccessors(
|
|
297
|
+
protected static bindStateAccessors(): void;
|
|
280
298
|
protected static createDefaultOptionsHandler(): any;
|
|
281
299
|
static config: RouterConfig;
|
|
282
|
-
static configureDefaults(
|
|
300
|
+
static configureDefaults(options?: RouterConfig): void;
|
|
283
301
|
/**
|
|
284
302
|
* Use a registered plugin
|
|
285
303
|
*
|
|
@@ -288,8 +306,8 @@ declare abstract class CoreRouter {
|
|
|
288
306
|
* @param options
|
|
289
307
|
* @returns
|
|
290
308
|
*/
|
|
291
|
-
static use<Options = any>(
|
|
292
|
-
protected static pluginsReady(
|
|
309
|
+
static use<Options = any>(plugin: ClearRouterPluginInput<Options>, options?: Options): Promise<void>;
|
|
310
|
+
protected static pluginsReady(): Promise<void>;
|
|
293
311
|
protected static groupContext: AsyncLocalStorage<{
|
|
294
312
|
prefix: string;
|
|
295
313
|
groupMiddlewares: any[];
|
|
@@ -298,6 +316,7 @@ declare abstract class CoreRouter {
|
|
|
298
316
|
static routes: Array<Route<any, any, any>>;
|
|
299
317
|
static routesByPathMethod: Record<string, Route<any, any, any>>;
|
|
300
318
|
static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
|
|
319
|
+
static routesByName: Record<string, Route<any, any, any>>;
|
|
301
320
|
static prefix: string;
|
|
302
321
|
static groupMiddlewares: any[];
|
|
303
322
|
static globalMiddlewares: any[];
|
|
@@ -305,7 +324,7 @@ declare abstract class CoreRouter {
|
|
|
305
324
|
protected static createPluginRequestContext(ctx: any): ClearRouterPluginRequestContext;
|
|
306
325
|
protected static createPluginBind(): PluginBind;
|
|
307
326
|
protected static resolvePluginArguments(this: any, ctx: any, routeContext: Omit<ClearRouterPluginArgumentsContext, keyof ClearRouterPluginRequestContext>): Promise<any[] | undefined>;
|
|
308
|
-
protected static ensureState(
|
|
327
|
+
protected static ensureState(): void;
|
|
309
328
|
/**
|
|
310
329
|
* Normalizes a path by ensuring it starts with a single slash and does not have trailing
|
|
311
330
|
* slashes, while preserving dynamic segments and parameters.
|
|
@@ -314,6 +333,13 @@ declare abstract class CoreRouter {
|
|
|
314
333
|
* @returns The normalized path.
|
|
315
334
|
*/
|
|
316
335
|
static normalizePath(path: string): string;
|
|
336
|
+
protected static parseRouteParameters(path: string): Array<{
|
|
337
|
+
name: string;
|
|
338
|
+
field?: string;
|
|
339
|
+
optional: boolean;
|
|
340
|
+
}>;
|
|
341
|
+
protected static expandRoutePath(path: string): string[];
|
|
342
|
+
protected static routeRegistrationPaths(path: string): string[];
|
|
317
343
|
/**
|
|
318
344
|
* Configures the router with the given options, such as method override settings.
|
|
319
345
|
*
|
|
@@ -322,7 +348,7 @@ declare abstract class CoreRouter {
|
|
|
322
348
|
* @returns
|
|
323
349
|
*/
|
|
324
350
|
static configure(this: any, options?: RouterConfig): void;
|
|
325
|
-
protected static resolveMethodOverride(
|
|
351
|
+
protected static resolveMethodOverride(method: string, headers: Headers | Record<string, any>, body: unknown): HttpMethod | null;
|
|
326
352
|
/**
|
|
327
353
|
* Adds a new route to the router.
|
|
328
354
|
*
|
|
@@ -332,7 +358,7 @@ declare abstract class CoreRouter {
|
|
|
332
358
|
* @param handler
|
|
333
359
|
* @param middlewares
|
|
334
360
|
*/
|
|
335
|
-
static add(
|
|
361
|
+
static add(methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
336
362
|
/**
|
|
337
363
|
* Define a resourceful API controller with standard CRUD routes.
|
|
338
364
|
*
|
|
@@ -341,7 +367,7 @@ declare abstract class CoreRouter {
|
|
|
341
367
|
* @param controller
|
|
342
368
|
* @param options
|
|
343
369
|
*/
|
|
344
|
-
static apiResource(
|
|
370
|
+
static apiResource(basePath: string, controller: any, options?: {
|
|
345
371
|
only?: ControllerAction[];
|
|
346
372
|
except?: ControllerAction[];
|
|
347
373
|
middlewares?: ApiResourceMiddleware<any>;
|
|
@@ -354,7 +380,7 @@ declare abstract class CoreRouter {
|
|
|
354
380
|
* @param handler The handler function for the GET route.
|
|
355
381
|
* @param middlewares Optional middlewares to apply to the GET route.
|
|
356
382
|
*/
|
|
357
|
-
static get(
|
|
383
|
+
static get(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
358
384
|
/**
|
|
359
385
|
* Adds a new POST route to the router.
|
|
360
386
|
*
|
|
@@ -363,7 +389,7 @@ declare abstract class CoreRouter {
|
|
|
363
389
|
* @param handler
|
|
364
390
|
* @param middlewares
|
|
365
391
|
*/
|
|
366
|
-
static post(
|
|
392
|
+
static post(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
367
393
|
/**
|
|
368
394
|
* Adds a new PUT route to the router.
|
|
369
395
|
*
|
|
@@ -372,7 +398,7 @@ declare abstract class CoreRouter {
|
|
|
372
398
|
* @param handler
|
|
373
399
|
* @param middlewares
|
|
374
400
|
*/
|
|
375
|
-
static put(
|
|
401
|
+
static put(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
376
402
|
/**
|
|
377
403
|
* Adds a new DELETE route to the router.
|
|
378
404
|
*
|
|
@@ -381,7 +407,7 @@ declare abstract class CoreRouter {
|
|
|
381
407
|
* @param handler
|
|
382
408
|
* @param middlewares
|
|
383
409
|
*/
|
|
384
|
-
static delete(
|
|
410
|
+
static delete(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
385
411
|
/**
|
|
386
412
|
* Adds a new PATCH route to the router.
|
|
387
413
|
*
|
|
@@ -390,7 +416,7 @@ declare abstract class CoreRouter {
|
|
|
390
416
|
* @param handler
|
|
391
417
|
* @param middlewares
|
|
392
418
|
*/
|
|
393
|
-
static patch(
|
|
419
|
+
static patch(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
394
420
|
/**
|
|
395
421
|
* Adds a new OPTIONS route to the router.
|
|
396
422
|
*
|
|
@@ -399,7 +425,7 @@ declare abstract class CoreRouter {
|
|
|
399
425
|
* @param handler
|
|
400
426
|
* @param middlewares
|
|
401
427
|
*/
|
|
402
|
-
static options(
|
|
428
|
+
static options(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
403
429
|
/**
|
|
404
430
|
* Adds a new HEAD route to the router.
|
|
405
431
|
*
|
|
@@ -408,7 +434,7 @@ declare abstract class CoreRouter {
|
|
|
408
434
|
* @param handler
|
|
409
435
|
* @param middlewares
|
|
410
436
|
*/
|
|
411
|
-
static head(
|
|
437
|
+
static head(path: string, handler: any, middlewares?: any[] | any): Route<any, any, any>;
|
|
412
438
|
/**
|
|
413
439
|
* Defines a group of routes with a common prefix.
|
|
414
440
|
*
|
|
@@ -417,7 +443,7 @@ declare abstract class CoreRouter {
|
|
|
417
443
|
* @param callback
|
|
418
444
|
* @param middlewares
|
|
419
445
|
*/
|
|
420
|
-
static group(
|
|
446
|
+
static group(prefix: string, callback: () => void | Promise<void>, middlewares?: any[]): Promise<void>;
|
|
421
447
|
/**
|
|
422
448
|
* Adds global middlewares to the router, which will be applied to all routes.
|
|
423
449
|
*
|
|
@@ -425,24 +451,27 @@ declare abstract class CoreRouter {
|
|
|
425
451
|
* @param middlewares
|
|
426
452
|
* @param callback
|
|
427
453
|
*/
|
|
428
|
-
static middleware(
|
|
454
|
+
static middleware(middlewares: any[], callback: () => void): void;
|
|
429
455
|
/**
|
|
430
456
|
* Retrieves all registered routes in the router, optionally organized by path or method
|
|
431
457
|
* for easier access and management.
|
|
432
458
|
*
|
|
433
459
|
* @param this
|
|
434
460
|
*/
|
|
435
|
-
static allRoutes(
|
|
461
|
+
static allRoutes(): Array<Route<any, any, any>>;
|
|
436
462
|
/**
|
|
437
463
|
* @param this
|
|
438
464
|
* @param type - 'path' to get routes organized by path
|
|
439
465
|
*/
|
|
440
|
-
static allRoutes(
|
|
466
|
+
static allRoutes(type: 'path'): Record<string, Route<any, any, any>>;
|
|
441
467
|
/**
|
|
442
468
|
* @param this
|
|
443
469
|
* @param type - 'method' to get routes organized by method
|
|
444
470
|
*/
|
|
445
|
-
static allRoutes(
|
|
471
|
+
static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
|
|
472
|
+
static allRoutes(type: 'name'): Record<string, Route<any, any, any>>;
|
|
473
|
+
static route(name: string): Route<any, any, any> | undefined;
|
|
474
|
+
static url(name: string, params?: Record<string, any>): string | undefined;
|
|
446
475
|
protected static resolveHandler(route: Route<any, any, any>): {
|
|
447
476
|
handlerFunction: ((ctx: any, req: Request$1) => any | Promise<any>) | null;
|
|
448
477
|
instance: Controller<any> | null;
|
|
@@ -451,7 +480,7 @@ declare abstract class CoreRouter {
|
|
|
451
480
|
bindingHandler?: object;
|
|
452
481
|
bindingMetadata?: object;
|
|
453
482
|
};
|
|
454
|
-
protected static callHandler(
|
|
483
|
+
protected static callHandler(handlerFunction: (ctx: any, req: Request$1) => any | Promise<any>, ctx: any, bindingTarget?: object, bindingMethod?: PropertyKey, bindingHandler?: object, bindingMetadata?: object): Promise<any>;
|
|
455
484
|
protected static bindRequestToInstance(ctx: any, instance: Controller<any> | Route<any, any, any> | null, route: Route<any, any, any>, payload: {
|
|
456
485
|
body: Record<string, any>;
|
|
457
486
|
query: Record<string, any>;
|
package/dist/types/Route.d.mts
CHANGED
|
@@ -4,6 +4,11 @@ import { ClearRequest } from "./ClearRequest.mjs";
|
|
|
4
4
|
import { Middleware as Middleware$1 } from "../types/express.mjs";
|
|
5
5
|
|
|
6
6
|
//#region src/Route.d.ts
|
|
7
|
+
interface RouteParameter {
|
|
8
|
+
name: string;
|
|
9
|
+
field?: string;
|
|
10
|
+
optional: boolean;
|
|
11
|
+
}
|
|
7
12
|
declare class Route<X = any, M = Middleware | Middleware$1, H = any> {
|
|
8
13
|
ctx: X;
|
|
9
14
|
body: RequestData;
|
|
@@ -12,13 +17,23 @@ declare class Route<X = any, M = Middleware | Middleware$1, H = any> {
|
|
|
12
17
|
clearRequest: ClearRequest;
|
|
13
18
|
methods: HttpMethod[];
|
|
14
19
|
path: string;
|
|
20
|
+
registrationPaths: string[];
|
|
21
|
+
parameters: RouteParameter[];
|
|
22
|
+
routeName?: string;
|
|
15
23
|
handler: H;
|
|
16
24
|
middlewares: M[];
|
|
17
25
|
controllerName?: string;
|
|
18
26
|
actionName?: string;
|
|
19
27
|
handlerType: 'function' | 'controller';
|
|
20
28
|
middlewareCount: number;
|
|
21
|
-
constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[]
|
|
29
|
+
constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[], options?: {
|
|
30
|
+
registrationPaths?: string[];
|
|
31
|
+
parameters?: RouteParameter[];
|
|
32
|
+
onName?: (name: string, route: Route<X, M, H>, previousName?: string) => void;
|
|
33
|
+
});
|
|
34
|
+
private onName?;
|
|
35
|
+
name(name: string): this;
|
|
36
|
+
toPath(params?: RequestData): string;
|
|
22
37
|
}
|
|
23
38
|
//#endregion
|
|
24
39
|
export { Route };
|
|
@@ -4,8 +4,10 @@ declare class Response {
|
|
|
4
4
|
headers: Headers;
|
|
5
5
|
sent: boolean;
|
|
6
6
|
statusCode: number;
|
|
7
|
-
|
|
7
|
+
statusText: string;
|
|
8
|
+
constructor(init?: Partial<Response | globalThis.Response>);
|
|
8
9
|
status(code: number): this;
|
|
10
|
+
setStatusText(text: string): this;
|
|
9
11
|
code(code: number): this;
|
|
10
12
|
setHeader(name: string, value: string): this;
|
|
11
13
|
header(name: string, value: string): this;
|
package/package.json
CHANGED