clear-router 2.8.9 → 2.9.1
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 +15 -1
- package/dist/Route.cjs +200 -12
- package/dist/Route.d.cts +125 -1
- package/dist/Route.d.mts +125 -1
- package/dist/Route.mjs +199 -12
- package/dist/RouteGroup.cjs +1 -0
- package/dist/RouteGroup.mjs +1 -0
- package/dist/RouteRegistrar.cjs +68 -0
- package/dist/RouteRegistrar.d.cts +62 -0
- package/dist/RouteRegistrar.d.mts +62 -0
- package/dist/RouteRegistrar.mjs +67 -0
- package/dist/core/CoreRouter.cjs +309 -0
- package/dist/core/CoreRouter.d.cts +168 -0
- package/dist/core/CoreRouter.d.mts +168 -0
- package/dist/core/CoreRouter.mjs +309 -0
- package/dist/core/index.cjs +3 -0
- package/dist/core/index.d.cts +2 -1
- package/dist/core/index.d.mts +2 -1
- package/dist/core/index.mjs +2 -1
- package/dist/decorators/index.cjs +4 -1
- package/dist/decorators/index.d.cts +2 -1
- package/dist/decorators/index.d.mts +2 -1
- package/dist/decorators/index.mjs +2 -1
- package/dist/decorators/middleware.cjs +100 -0
- package/dist/decorators/middleware.d.cts +51 -0
- package/dist/decorators/middleware.d.mts +51 -0
- package/dist/decorators/middleware.mjs +98 -0
- package/dist/decorators/setup.cjs +4 -1
- package/dist/decorators/setup.d.cts +2 -1
- package/dist/decorators/setup.d.mts +2 -2
- package/dist/decorators/setup.mjs +2 -1
- package/dist/express/router.cjs +13 -4
- package/dist/express/router.d.cts +1 -0
- package/dist/express/router.d.mts +1 -0
- package/dist/express/router.mjs +13 -4
- package/dist/fastify/router.cjs +13 -4
- package/dist/fastify/router.d.cts +1 -0
- package/dist/fastify/router.d.mts +1 -0
- package/dist/fastify/router.mjs +13 -4
- package/dist/h3/router.cjs +13 -4
- package/dist/h3/router.d.cts +1 -0
- package/dist/h3/router.d.mts +1 -0
- package/dist/h3/router.mjs +13 -4
- package/dist/hono/router.cjs +11 -4
- package/dist/hono/router.d.cts +1 -0
- package/dist/hono/router.d.mts +1 -0
- package/dist/hono/router.mjs +11 -4
- package/dist/index.cjs +6 -0
- package/dist/index.d.cts +4 -2
- package/dist/index.d.mts +4 -3
- package/dist/index.mjs +4 -2
- package/dist/koa/router.cjs +13 -4
- package/dist/koa/router.d.cts +1 -0
- package/dist/koa/router.d.mts +1 -0
- package/dist/koa/router.mjs +13 -4
- package/dist/types/basic.d.cts +2 -0
- package/dist/types/basic.d.mts +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/decorators/middleware.ts
|
|
3
|
+
const CLASS_KEY = "__class__";
|
|
4
|
+
const metadataKey = Symbol.for("clear-router:middleware-metadata");
|
|
5
|
+
const store = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
/**
|
|
7
|
+
* Attach one or more middleware to a controller class or a controller method.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* @Middleware([auth])
|
|
12
|
+
* class AccountController {}
|
|
13
|
+
*
|
|
14
|
+
* class LoginController {
|
|
15
|
+
* @Middleware(GuestMiddleware)
|
|
16
|
+
* create () {}
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Accepts the middleware variadically or as arrays, and every supported
|
|
21
|
+
* middleware shape: callbacks, middleware classes, or instances exposing a
|
|
22
|
+
* `handle` method.
|
|
23
|
+
*
|
|
24
|
+
* @param middlewares
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
function middleware(...middlewares) {
|
|
28
|
+
const flattened = middlewares.flat();
|
|
29
|
+
return ((target, propertyKeyOrContext) => {
|
|
30
|
+
if (isStandardClassContext(propertyKeyOrContext)) {
|
|
31
|
+
setClassMiddlewareMetadata(target, flattened);
|
|
32
|
+
setStandardMetadata(propertyKeyOrContext.metadata, CLASS_KEY, flattened);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (isStandardMethodContext(propertyKeyOrContext)) {
|
|
36
|
+
setStandardMetadata(propertyKeyOrContext.metadata, propertyKeyOrContext.name, flattened);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (typeof propertyKeyOrContext === "undefined") {
|
|
40
|
+
setClassMiddlewareMetadata(target, flattened);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
setMiddlewareMetadata(target, propertyKeyOrContext, flattened);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Collect the decorator-declared middleware for a controller route handler,
|
|
48
|
+
* ordered class-level first then method-level. Returns an empty array for
|
|
49
|
+
* non-controller (callback) handlers or handlers without any decorators.
|
|
50
|
+
*
|
|
51
|
+
* @param handler
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
54
|
+
function getControllerMiddlewares(handler) {
|
|
55
|
+
if (!Array.isArray(handler) || handler.length !== 2) return [];
|
|
56
|
+
const [controller, method] = handler;
|
|
57
|
+
if (!controller) return [];
|
|
58
|
+
const constructor = typeof controller === "function" ? controller : controller.constructor;
|
|
59
|
+
const prototype = typeof controller === "function" ? controller.prototype : Object.getPrototypeOf(controller);
|
|
60
|
+
const standardMetadata = constructor?.[Symbol.metadata];
|
|
61
|
+
const classMiddlewares = getMiddlewareMetadata(constructor, CLASS_KEY) ?? getMiddlewareMetadata(prototype, CLASS_KEY) ?? getStandardMetadata(standardMetadata, CLASS_KEY) ?? [];
|
|
62
|
+
const methodMiddlewares = getMiddlewareMetadata(prototype, method) ?? getMiddlewareMetadata(constructor, method) ?? getStandardMetadata(standardMetadata, method) ?? [];
|
|
63
|
+
return [...classMiddlewares, ...methodMiddlewares];
|
|
64
|
+
}
|
|
65
|
+
function setClassMiddlewareMetadata(target, middlewares) {
|
|
66
|
+
setMiddlewareMetadata(target, CLASS_KEY, middlewares);
|
|
67
|
+
const prototype = target.prototype;
|
|
68
|
+
if (prototype) setMiddlewareMetadata(prototype, CLASS_KEY, middlewares);
|
|
69
|
+
}
|
|
70
|
+
function setMiddlewareMetadata(target, propertyKey, middlewares) {
|
|
71
|
+
const map = store.get(target) ?? /* @__PURE__ */ new Map();
|
|
72
|
+
const existing = map.get(propertyKey);
|
|
73
|
+
map.set(propertyKey, existing ? [...existing, ...middlewares] : middlewares);
|
|
74
|
+
store.set(target, map);
|
|
75
|
+
}
|
|
76
|
+
function getMiddlewareMetadata(target, propertyKey) {
|
|
77
|
+
if (!target) return void 0;
|
|
78
|
+
return store.get(target)?.get(propertyKey);
|
|
79
|
+
}
|
|
80
|
+
function setStandardMetadata(metadata, propertyKey, middlewares) {
|
|
81
|
+
if (!metadata) return;
|
|
82
|
+
const record = metadata;
|
|
83
|
+
record[metadataKey] = record[metadataKey] ?? {};
|
|
84
|
+
const existing = record[metadataKey][propertyKey];
|
|
85
|
+
record[metadataKey][propertyKey] = existing ? [...existing, ...middlewares] : middlewares;
|
|
86
|
+
}
|
|
87
|
+
function getStandardMetadata(metadata, propertyKey) {
|
|
88
|
+
const record = metadata && metadata[metadataKey];
|
|
89
|
+
return record ? record[propertyKey] : void 0;
|
|
90
|
+
}
|
|
91
|
+
function isStandardMethodContext(value) {
|
|
92
|
+
return Boolean(value && typeof value === "object" && value.kind === "method" && typeof value.name !== "undefined");
|
|
93
|
+
}
|
|
94
|
+
function isStandardClassContext(value) {
|
|
95
|
+
return Boolean(value && typeof value === "object" && value.kind === "class" && typeof value.name !== "undefined");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
exports.getControllerMiddlewares = getControllerMiddlewares;
|
|
100
|
+
exports.middleware = middleware;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ClassMiddleware, MiddlewareHandle } from "../types/basic.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/decorators/middleware.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Any value accepted as a middleware by the router: a plain callback, a class
|
|
6
|
+
* exposing a `handle` method, or an already-instantiated object exposing one.
|
|
7
|
+
*/
|
|
8
|
+
type MiddlewareInput = MiddlewareHandle | ClassMiddleware;
|
|
9
|
+
/**
|
|
10
|
+
* Decorator produced by {@link Middleware}. Works both as a class decorator (to
|
|
11
|
+
* apply the middleware to every action of a controller) and as a method
|
|
12
|
+
* decorator (to apply it to a single action). Supports the legacy
|
|
13
|
+
* (`experimentalDecorators`) and TC39 standard decorator signatures.
|
|
14
|
+
*/
|
|
15
|
+
type MiddlewareDecorator = MethodDecorator & ClassDecorator & {
|
|
16
|
+
<This, Value extends (this: This, ...args: any[]) => any>(value: Value, context: ClassMethodDecoratorContext<This, Value>): void | Value;
|
|
17
|
+
<Value extends abstract new (...args: any[]) => any>(value: Value, context: ClassDecoratorContext<Value>): void | Value;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Attach one or more middleware to a controller class or a controller method.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* @Middleware([auth])
|
|
25
|
+
* class AccountController {}
|
|
26
|
+
*
|
|
27
|
+
* class LoginController {
|
|
28
|
+
* @Middleware(GuestMiddleware)
|
|
29
|
+
* create () {}
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Accepts the middleware variadically or as arrays, and every supported
|
|
34
|
+
* middleware shape: callbacks, middleware classes, or instances exposing a
|
|
35
|
+
* `handle` method.
|
|
36
|
+
*
|
|
37
|
+
* @param middlewares
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
declare function middleware(...middlewares: Array<MiddlewareInput | MiddlewareInput[]>): MiddlewareDecorator;
|
|
41
|
+
/**
|
|
42
|
+
* Collect the decorator-declared middleware for a controller route handler,
|
|
43
|
+
* ordered class-level first then method-level. Returns an empty array for
|
|
44
|
+
* non-controller (callback) handlers or handlers without any decorators.
|
|
45
|
+
*
|
|
46
|
+
* @param handler
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
declare function getControllerMiddlewares(handler: any): MiddlewareInput[];
|
|
50
|
+
//#endregion
|
|
51
|
+
export { MiddlewareDecorator, MiddlewareInput, getControllerMiddlewares, middleware };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ClassMiddleware, MiddlewareHandle } from "../types/basic.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/decorators/middleware.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Any value accepted as a middleware by the router: a plain callback, a class
|
|
6
|
+
* exposing a `handle` method, or an already-instantiated object exposing one.
|
|
7
|
+
*/
|
|
8
|
+
type MiddlewareInput = MiddlewareHandle | ClassMiddleware;
|
|
9
|
+
/**
|
|
10
|
+
* Decorator produced by {@link Middleware}. Works both as a class decorator (to
|
|
11
|
+
* apply the middleware to every action of a controller) and as a method
|
|
12
|
+
* decorator (to apply it to a single action). Supports the legacy
|
|
13
|
+
* (`experimentalDecorators`) and TC39 standard decorator signatures.
|
|
14
|
+
*/
|
|
15
|
+
type MiddlewareDecorator = MethodDecorator & ClassDecorator & {
|
|
16
|
+
<This, Value extends (this: This, ...args: any[]) => any>(value: Value, context: ClassMethodDecoratorContext<This, Value>): void | Value;
|
|
17
|
+
<Value extends abstract new (...args: any[]) => any>(value: Value, context: ClassDecoratorContext<Value>): void | Value;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Attach one or more middleware to a controller class or a controller method.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* @Middleware([auth])
|
|
25
|
+
* class AccountController {}
|
|
26
|
+
*
|
|
27
|
+
* class LoginController {
|
|
28
|
+
* @Middleware(GuestMiddleware)
|
|
29
|
+
* create () {}
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Accepts the middleware variadically or as arrays, and every supported
|
|
34
|
+
* middleware shape: callbacks, middleware classes, or instances exposing a
|
|
35
|
+
* `handle` method.
|
|
36
|
+
*
|
|
37
|
+
* @param middlewares
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
declare function middleware(...middlewares: Array<MiddlewareInput | MiddlewareInput[]>): MiddlewareDecorator;
|
|
41
|
+
/**
|
|
42
|
+
* Collect the decorator-declared middleware for a controller route handler,
|
|
43
|
+
* ordered class-level first then method-level. Returns an empty array for
|
|
44
|
+
* non-controller (callback) handlers or handlers without any decorators.
|
|
45
|
+
*
|
|
46
|
+
* @param handler
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
declare function getControllerMiddlewares(handler: any): MiddlewareInput[];
|
|
50
|
+
//#endregion
|
|
51
|
+
export { MiddlewareDecorator, MiddlewareInput, getControllerMiddlewares, middleware };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
//#region src/decorators/middleware.ts
|
|
2
|
+
const CLASS_KEY = "__class__";
|
|
3
|
+
const metadataKey = Symbol.for("clear-router:middleware-metadata");
|
|
4
|
+
const store = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
/**
|
|
6
|
+
* Attach one or more middleware to a controller class or a controller method.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* @Middleware([auth])
|
|
11
|
+
* class AccountController {}
|
|
12
|
+
*
|
|
13
|
+
* class LoginController {
|
|
14
|
+
* @Middleware(GuestMiddleware)
|
|
15
|
+
* create () {}
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Accepts the middleware variadically or as arrays, and every supported
|
|
20
|
+
* middleware shape: callbacks, middleware classes, or instances exposing a
|
|
21
|
+
* `handle` method.
|
|
22
|
+
*
|
|
23
|
+
* @param middlewares
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
function middleware(...middlewares) {
|
|
27
|
+
const flattened = middlewares.flat();
|
|
28
|
+
return ((target, propertyKeyOrContext) => {
|
|
29
|
+
if (isStandardClassContext(propertyKeyOrContext)) {
|
|
30
|
+
setClassMiddlewareMetadata(target, flattened);
|
|
31
|
+
setStandardMetadata(propertyKeyOrContext.metadata, CLASS_KEY, flattened);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (isStandardMethodContext(propertyKeyOrContext)) {
|
|
35
|
+
setStandardMetadata(propertyKeyOrContext.metadata, propertyKeyOrContext.name, flattened);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (typeof propertyKeyOrContext === "undefined") {
|
|
39
|
+
setClassMiddlewareMetadata(target, flattened);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
setMiddlewareMetadata(target, propertyKeyOrContext, flattened);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Collect the decorator-declared middleware for a controller route handler,
|
|
47
|
+
* ordered class-level first then method-level. Returns an empty array for
|
|
48
|
+
* non-controller (callback) handlers or handlers without any decorators.
|
|
49
|
+
*
|
|
50
|
+
* @param handler
|
|
51
|
+
* @returns
|
|
52
|
+
*/
|
|
53
|
+
function getControllerMiddlewares(handler) {
|
|
54
|
+
if (!Array.isArray(handler) || handler.length !== 2) return [];
|
|
55
|
+
const [controller, method] = handler;
|
|
56
|
+
if (!controller) return [];
|
|
57
|
+
const constructor = typeof controller === "function" ? controller : controller.constructor;
|
|
58
|
+
const prototype = typeof controller === "function" ? controller.prototype : Object.getPrototypeOf(controller);
|
|
59
|
+
const standardMetadata = constructor?.[Symbol.metadata];
|
|
60
|
+
const classMiddlewares = getMiddlewareMetadata(constructor, CLASS_KEY) ?? getMiddlewareMetadata(prototype, CLASS_KEY) ?? getStandardMetadata(standardMetadata, CLASS_KEY) ?? [];
|
|
61
|
+
const methodMiddlewares = getMiddlewareMetadata(prototype, method) ?? getMiddlewareMetadata(constructor, method) ?? getStandardMetadata(standardMetadata, method) ?? [];
|
|
62
|
+
return [...classMiddlewares, ...methodMiddlewares];
|
|
63
|
+
}
|
|
64
|
+
function setClassMiddlewareMetadata(target, middlewares) {
|
|
65
|
+
setMiddlewareMetadata(target, CLASS_KEY, middlewares);
|
|
66
|
+
const prototype = target.prototype;
|
|
67
|
+
if (prototype) setMiddlewareMetadata(prototype, CLASS_KEY, middlewares);
|
|
68
|
+
}
|
|
69
|
+
function setMiddlewareMetadata(target, propertyKey, middlewares) {
|
|
70
|
+
const map = store.get(target) ?? /* @__PURE__ */ new Map();
|
|
71
|
+
const existing = map.get(propertyKey);
|
|
72
|
+
map.set(propertyKey, existing ? [...existing, ...middlewares] : middlewares);
|
|
73
|
+
store.set(target, map);
|
|
74
|
+
}
|
|
75
|
+
function getMiddlewareMetadata(target, propertyKey) {
|
|
76
|
+
if (!target) return void 0;
|
|
77
|
+
return store.get(target)?.get(propertyKey);
|
|
78
|
+
}
|
|
79
|
+
function setStandardMetadata(metadata, propertyKey, middlewares) {
|
|
80
|
+
if (!metadata) return;
|
|
81
|
+
const record = metadata;
|
|
82
|
+
record[metadataKey] = record[metadataKey] ?? {};
|
|
83
|
+
const existing = record[metadataKey][propertyKey];
|
|
84
|
+
record[metadataKey][propertyKey] = existing ? [...existing, ...middlewares] : middlewares;
|
|
85
|
+
}
|
|
86
|
+
function getStandardMetadata(metadata, propertyKey) {
|
|
87
|
+
const record = metadata && metadata[metadataKey];
|
|
88
|
+
return record ? record[propertyKey] : void 0;
|
|
89
|
+
}
|
|
90
|
+
function isStandardMethodContext(value) {
|
|
91
|
+
return Boolean(value && typeof value === "object" && value.kind === "method" && typeof value.name !== "undefined");
|
|
92
|
+
}
|
|
93
|
+
function isStandardClassContext(value) {
|
|
94
|
+
return Boolean(value && typeof value === "object" && value.kind === "class" && typeof value.name !== "undefined");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
//#endregion
|
|
98
|
+
export { getControllerMiddlewares, middleware };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_middleware = require('./middleware.cjs');
|
|
2
3
|
const require_bindings = require('../core/bindings.cjs');
|
|
3
4
|
const require_CoreRouter = require('../core/CoreRouter.cjs');
|
|
4
5
|
require('./index.cjs');
|
|
@@ -12,4 +13,6 @@ require_CoreRouter.CoreRouter.configureDefaults({ container: {
|
|
|
12
13
|
|
|
13
14
|
//#endregion
|
|
14
15
|
exports.Bind = require_bindings.Bind;
|
|
15
|
-
exports.Container = require_bindings.Container;
|
|
16
|
+
exports.Container = require_bindings.Container;
|
|
17
|
+
exports.getControllerMiddlewares = require_middleware.getControllerMiddlewares;
|
|
18
|
+
exports.middleware = require_middleware.middleware;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container } from "../core/bindings.cjs";
|
|
2
|
-
|
|
2
|
+
import { MiddlewareDecorator, MiddlewareInput, getControllerMiddlewares, middleware } from "./middleware.cjs";
|
|
3
|
+
export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container, MiddlewareDecorator, MiddlewareInput, getControllerMiddlewares, middleware };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container } from "../core/bindings.mjs";
|
|
2
|
-
import "
|
|
3
|
-
export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
|
|
2
|
+
import { MiddlewareDecorator, MiddlewareInput, getControllerMiddlewares, middleware } from "./middleware.mjs";
|
|
3
|
+
export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container, MiddlewareDecorator, MiddlewareInput, getControllerMiddlewares, middleware };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getControllerMiddlewares, middleware } from "./middleware.mjs";
|
|
1
2
|
import { Bind, Container } from "../core/bindings.mjs";
|
|
2
3
|
import { CoreRouter } from "../core/CoreRouter.mjs";
|
|
3
4
|
import "./index.mjs";
|
|
@@ -10,4 +11,4 @@ CoreRouter.configureDefaults({ container: {
|
|
|
10
11
|
} });
|
|
11
12
|
|
|
12
13
|
//#endregion
|
|
13
|
-
export { Bind, Container };
|
|
14
|
+
export { Bind, Container, getControllerMiddlewares, middleware };
|
package/dist/express/router.cjs
CHANGED
|
@@ -11,6 +11,9 @@ const require_responses = require('../core/responses.cjs');
|
|
|
11
11
|
*/
|
|
12
12
|
var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
13
13
|
static routerStateNamespace = "clear-router:express";
|
|
14
|
+
static formatWildcardParam(name) {
|
|
15
|
+
return `*${name}`;
|
|
16
|
+
}
|
|
14
17
|
static ensureRequestBodyAccessor(req) {
|
|
15
18
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
16
19
|
}
|
|
@@ -196,7 +199,7 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
196
199
|
console.error("[ROUTES]", error.message);
|
|
197
200
|
throw error;
|
|
198
201
|
}
|
|
199
|
-
for (const registrationPath of route
|
|
202
|
+
for (const registrationPath of this.resolveRegistrationPaths(route)) router[method](registrationPath, (req, _res, next) => {
|
|
200
203
|
Router.ensureRequestBodyAccessor(req);
|
|
201
204
|
const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
|
|
202
205
|
if (method === "post" && override && override !== "post") return next("route");
|
|
@@ -210,10 +213,13 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
210
213
|
next
|
|
211
214
|
};
|
|
212
215
|
const inst = instance ?? route;
|
|
216
|
+
const params = Router.matchRoute(route, ctx, ctx.req.params);
|
|
217
|
+
if (params === false) return next("route");
|
|
218
|
+
Object.assign(ctx.req.params, params);
|
|
213
219
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
214
220
|
body: ctx.req.getBody(),
|
|
215
221
|
query: ctx.req.query,
|
|
216
|
-
params
|
|
222
|
+
params,
|
|
217
223
|
method
|
|
218
224
|
});
|
|
219
225
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
@@ -228,7 +234,7 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
228
234
|
"put",
|
|
229
235
|
"patch",
|
|
230
236
|
"delete"
|
|
231
|
-
].includes(method)) for (const registrationPath of route
|
|
237
|
+
].includes(method)) for (const registrationPath of this.resolveRegistrationPaths(route)) router.post(registrationPath, (req, _res, next) => {
|
|
232
238
|
Router.ensureRequestBodyAccessor(req);
|
|
233
239
|
if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return next("route");
|
|
234
240
|
req.method = method.toUpperCase();
|
|
@@ -242,10 +248,13 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
242
248
|
next
|
|
243
249
|
};
|
|
244
250
|
const inst = instance ?? route;
|
|
251
|
+
const params = Router.matchRoute(route, ctx, ctx.req.params);
|
|
252
|
+
if (params === false) return next("route");
|
|
253
|
+
Object.assign(ctx.req.params, params);
|
|
245
254
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
246
255
|
body: ctx.req.getBody(),
|
|
247
256
|
query: ctx.req.query,
|
|
248
|
-
params
|
|
257
|
+
params,
|
|
249
258
|
method
|
|
250
259
|
});
|
|
251
260
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
@@ -16,6 +16,7 @@ import { Router } from "express";
|
|
|
16
16
|
*/
|
|
17
17
|
declare class Router$1 extends CoreRouter {
|
|
18
18
|
protected static routerStateNamespace: string;
|
|
19
|
+
protected static formatWildcardParam(name: string): string;
|
|
19
20
|
private static ensureRequestBodyAccessor;
|
|
20
21
|
private static sendReturnValue;
|
|
21
22
|
/**
|
|
@@ -16,6 +16,7 @@ import { Router } from "express";
|
|
|
16
16
|
*/
|
|
17
17
|
declare class Router$1 extends CoreRouter {
|
|
18
18
|
protected static routerStateNamespace: string;
|
|
19
|
+
protected static formatWildcardParam(name: string): string;
|
|
19
20
|
private static ensureRequestBodyAccessor;
|
|
20
21
|
private static sendReturnValue;
|
|
21
22
|
/**
|
package/dist/express/router.mjs
CHANGED
|
@@ -11,6 +11,9 @@ import { isFetchResponse, resolveResponseMeta, responseWasSent } from "../core/r
|
|
|
11
11
|
*/
|
|
12
12
|
var Router = class Router extends CoreRouter {
|
|
13
13
|
static routerStateNamespace = "clear-router:express";
|
|
14
|
+
static formatWildcardParam(name) {
|
|
15
|
+
return `*${name}`;
|
|
16
|
+
}
|
|
14
17
|
static ensureRequestBodyAccessor(req) {
|
|
15
18
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
16
19
|
}
|
|
@@ -196,7 +199,7 @@ var Router = class Router extends CoreRouter {
|
|
|
196
199
|
console.error("[ROUTES]", error.message);
|
|
197
200
|
throw error;
|
|
198
201
|
}
|
|
199
|
-
for (const registrationPath of route
|
|
202
|
+
for (const registrationPath of this.resolveRegistrationPaths(route)) router[method](registrationPath, (req, _res, next) => {
|
|
200
203
|
Router.ensureRequestBodyAccessor(req);
|
|
201
204
|
const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
|
|
202
205
|
if (method === "post" && override && override !== "post") return next("route");
|
|
@@ -210,10 +213,13 @@ var Router = class Router extends CoreRouter {
|
|
|
210
213
|
next
|
|
211
214
|
};
|
|
212
215
|
const inst = instance ?? route;
|
|
216
|
+
const params = Router.matchRoute(route, ctx, ctx.req.params);
|
|
217
|
+
if (params === false) return next("route");
|
|
218
|
+
Object.assign(ctx.req.params, params);
|
|
213
219
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
214
220
|
body: ctx.req.getBody(),
|
|
215
221
|
query: ctx.req.query,
|
|
216
|
-
params
|
|
222
|
+
params,
|
|
217
223
|
method
|
|
218
224
|
});
|
|
219
225
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
@@ -228,7 +234,7 @@ var Router = class Router extends CoreRouter {
|
|
|
228
234
|
"put",
|
|
229
235
|
"patch",
|
|
230
236
|
"delete"
|
|
231
|
-
].includes(method)) for (const registrationPath of route
|
|
237
|
+
].includes(method)) for (const registrationPath of this.resolveRegistrationPaths(route)) router.post(registrationPath, (req, _res, next) => {
|
|
232
238
|
Router.ensureRequestBodyAccessor(req);
|
|
233
239
|
if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return next("route");
|
|
234
240
|
req.method = method.toUpperCase();
|
|
@@ -242,10 +248,13 @@ var Router = class Router extends CoreRouter {
|
|
|
242
248
|
next
|
|
243
249
|
};
|
|
244
250
|
const inst = instance ?? route;
|
|
251
|
+
const params = Router.matchRoute(route, ctx, ctx.req.params);
|
|
252
|
+
if (params === false) return next("route");
|
|
253
|
+
Object.assign(ctx.req.params, params);
|
|
245
254
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
246
255
|
body: ctx.req.getBody(),
|
|
247
256
|
query: ctx.req.query,
|
|
248
|
-
params
|
|
257
|
+
params,
|
|
249
258
|
method
|
|
250
259
|
});
|
|
251
260
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
package/dist/fastify/router.cjs
CHANGED
|
@@ -10,6 +10,9 @@ const require_responses = require('../core/responses.cjs');
|
|
|
10
10
|
*/
|
|
11
11
|
var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
12
12
|
static routerStateNamespace = "clear-router:fastify";
|
|
13
|
+
static formatWildcardParam() {
|
|
14
|
+
return "*";
|
|
15
|
+
}
|
|
13
16
|
static ensureRequestBodyAccessor(req) {
|
|
14
17
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
15
18
|
}
|
|
@@ -191,7 +194,7 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
191
194
|
];
|
|
192
195
|
if (method === "options" && route.methods.length > 1) continue;
|
|
193
196
|
if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
194
|
-
for (const registrationPath of route
|
|
197
|
+
for (const registrationPath of this.resolveRegistrationPaths(route)) app.route({
|
|
195
198
|
method: method.toUpperCase(),
|
|
196
199
|
url: registrationPath,
|
|
197
200
|
preHandler: route.middlewares,
|
|
@@ -204,10 +207,13 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
204
207
|
reply
|
|
205
208
|
};
|
|
206
209
|
const inst = instance ?? route;
|
|
210
|
+
const params = Router.matchRoute(route, ctx, ctx.req.params ?? {});
|
|
211
|
+
if (params === false) return reply.code(404).send();
|
|
212
|
+
Object.assign(ctx.req.params ??= {}, params);
|
|
207
213
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
208
214
|
body: ctx.req.getBody(),
|
|
209
215
|
query: ctx.req.query ?? {},
|
|
210
|
-
params
|
|
216
|
+
params,
|
|
211
217
|
method
|
|
212
218
|
});
|
|
213
219
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
@@ -220,7 +226,7 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
220
226
|
"put",
|
|
221
227
|
"patch",
|
|
222
228
|
"delete"
|
|
223
|
-
].includes(method)) for (const registrationPath of route
|
|
229
|
+
].includes(method)) for (const registrationPath of this.resolveRegistrationPaths(route)) app.route({
|
|
224
230
|
method: "POST",
|
|
225
231
|
url: registrationPath,
|
|
226
232
|
preHandler: route.middlewares,
|
|
@@ -232,10 +238,13 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
232
238
|
reply
|
|
233
239
|
};
|
|
234
240
|
const inst = instance ?? route;
|
|
241
|
+
const params = Router.matchRoute(route, ctx, ctx.req.params ?? {});
|
|
242
|
+
if (params === false) return reply.code(404).send();
|
|
243
|
+
Object.assign(ctx.req.params ??= {}, params);
|
|
235
244
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
236
245
|
body: ctx.req.getBody(),
|
|
237
246
|
query: ctx.req.query ?? {},
|
|
238
|
-
params
|
|
247
|
+
params,
|
|
239
248
|
method
|
|
240
249
|
});
|
|
241
250
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
@@ -14,6 +14,7 @@ import { CoreRouter } from "../core/CoreRouter.cjs";
|
|
|
14
14
|
*/
|
|
15
15
|
declare class Router extends CoreRouter {
|
|
16
16
|
protected static routerStateNamespace: string;
|
|
17
|
+
protected static formatWildcardParam(): string;
|
|
17
18
|
private static ensureRequestBodyAccessor;
|
|
18
19
|
private static sendReturnValue;
|
|
19
20
|
/**
|
|
@@ -14,6 +14,7 @@ import { CoreRouter } from "../core/CoreRouter.mjs";
|
|
|
14
14
|
*/
|
|
15
15
|
declare class Router extends CoreRouter {
|
|
16
16
|
protected static routerStateNamespace: string;
|
|
17
|
+
protected static formatWildcardParam(): string;
|
|
17
18
|
private static ensureRequestBodyAccessor;
|
|
18
19
|
private static sendReturnValue;
|
|
19
20
|
/**
|
package/dist/fastify/router.mjs
CHANGED
|
@@ -10,6 +10,9 @@ import { isFetchResponse, resolveResponseMeta, responseWasSent } from "../core/r
|
|
|
10
10
|
*/
|
|
11
11
|
var Router = class Router extends CoreRouter {
|
|
12
12
|
static routerStateNamespace = "clear-router:fastify";
|
|
13
|
+
static formatWildcardParam() {
|
|
14
|
+
return "*";
|
|
15
|
+
}
|
|
13
16
|
static ensureRequestBodyAccessor(req) {
|
|
14
17
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
15
18
|
}
|
|
@@ -191,7 +194,7 @@ var Router = class Router extends CoreRouter {
|
|
|
191
194
|
];
|
|
192
195
|
if (method === "options" && route.methods.length > 1) continue;
|
|
193
196
|
if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
194
|
-
for (const registrationPath of route
|
|
197
|
+
for (const registrationPath of this.resolveRegistrationPaths(route)) app.route({
|
|
195
198
|
method: method.toUpperCase(),
|
|
196
199
|
url: registrationPath,
|
|
197
200
|
preHandler: route.middlewares,
|
|
@@ -204,10 +207,13 @@ var Router = class Router extends CoreRouter {
|
|
|
204
207
|
reply
|
|
205
208
|
};
|
|
206
209
|
const inst = instance ?? route;
|
|
210
|
+
const params = Router.matchRoute(route, ctx, ctx.req.params ?? {});
|
|
211
|
+
if (params === false) return reply.code(404).send();
|
|
212
|
+
Object.assign(ctx.req.params ??= {}, params);
|
|
207
213
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
208
214
|
body: ctx.req.getBody(),
|
|
209
215
|
query: ctx.req.query ?? {},
|
|
210
|
-
params
|
|
216
|
+
params,
|
|
211
217
|
method
|
|
212
218
|
});
|
|
213
219
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
@@ -220,7 +226,7 @@ var Router = class Router extends CoreRouter {
|
|
|
220
226
|
"put",
|
|
221
227
|
"patch",
|
|
222
228
|
"delete"
|
|
223
|
-
].includes(method)) for (const registrationPath of route
|
|
229
|
+
].includes(method)) for (const registrationPath of this.resolveRegistrationPaths(route)) app.route({
|
|
224
230
|
method: "POST",
|
|
225
231
|
url: registrationPath,
|
|
226
232
|
preHandler: route.middlewares,
|
|
@@ -232,10 +238,13 @@ var Router = class Router extends CoreRouter {
|
|
|
232
238
|
reply
|
|
233
239
|
};
|
|
234
240
|
const inst = instance ?? route;
|
|
241
|
+
const params = Router.matchRoute(route, ctx, ctx.req.params ?? {});
|
|
242
|
+
if (params === false) return reply.code(404).send();
|
|
243
|
+
Object.assign(ctx.req.params ??= {}, params);
|
|
235
244
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
236
245
|
body: ctx.req.getBody(),
|
|
237
246
|
query: ctx.req.query ?? {},
|
|
238
|
-
params
|
|
247
|
+
params,
|
|
239
248
|
method
|
|
240
249
|
});
|
|
241
250
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
package/dist/h3/router.cjs
CHANGED
|
@@ -11,6 +11,9 @@ let h3 = require("h3");
|
|
|
11
11
|
*/
|
|
12
12
|
var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
13
13
|
static routerStateNamespace = "clear-router:h3";
|
|
14
|
+
static formatWildcardParam(name) {
|
|
15
|
+
return `**:${name}`;
|
|
16
|
+
}
|
|
14
17
|
static bodyCache = /* @__PURE__ */ new WeakMap();
|
|
15
18
|
static toResponse(ctx, value, method, path) {
|
|
16
19
|
const meta = require_responses.resolveResponseMeta(value, {
|
|
@@ -205,17 +208,20 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
205
208
|
console.error("[ROUTES]", error.message);
|
|
206
209
|
throw error;
|
|
207
210
|
}
|
|
208
|
-
for (const registrationPath of route
|
|
211
|
+
for (const registrationPath of this.resolveRegistrationPaths(route)) app[method](registrationPath, async (event) => {
|
|
209
212
|
try {
|
|
210
213
|
const ctx = event;
|
|
211
214
|
const reqBody = await Router.readBodyCached(ctx);
|
|
212
215
|
const override = Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody);
|
|
213
216
|
if (method === "post" && override && override !== "post") return;
|
|
214
217
|
const inst = instance ?? route;
|
|
218
|
+
const params = Router.matchRoute(route, ctx, (0, h3.getRouterParams)(ctx, { decode: true }));
|
|
219
|
+
if (params === false) return Symbol.for("h3.notFound");
|
|
220
|
+
ctx.context = Object.assign(ctx.context ?? {}, { params });
|
|
215
221
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
216
222
|
body: reqBody,
|
|
217
223
|
query: (0, h3.getQuery)(ctx),
|
|
218
|
-
params
|
|
224
|
+
params,
|
|
219
225
|
method
|
|
220
226
|
});
|
|
221
227
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
@@ -230,16 +236,19 @@ var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
|
230
236
|
"put",
|
|
231
237
|
"patch",
|
|
232
238
|
"delete"
|
|
233
|
-
].includes(method)) for (const registrationPath of route
|
|
239
|
+
].includes(method)) for (const registrationPath of this.resolveRegistrationPaths(route)) app.post(registrationPath, async (event) => {
|
|
234
240
|
try {
|
|
235
241
|
const ctx = event;
|
|
236
242
|
const reqBody = await Router.readBodyCached(ctx);
|
|
237
243
|
if (Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody) !== method) return Symbol.for("h3.notFound");
|
|
238
244
|
const inst = instance ?? route;
|
|
245
|
+
const params = Router.matchRoute(route, ctx, (0, h3.getRouterParams)(ctx, { decode: true }));
|
|
246
|
+
if (params === false) return Symbol.for("h3.notFound");
|
|
247
|
+
ctx.context = Object.assign(ctx.context ?? {}, { params });
|
|
239
248
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
240
249
|
body: reqBody,
|
|
241
250
|
query: (0, h3.getQuery)(ctx),
|
|
242
|
-
params
|
|
251
|
+
params,
|
|
243
252
|
method
|
|
244
253
|
});
|
|
245
254
|
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|