Skip to content

Commit 1540b2f

Browse files
committed
Add support for wildcard "all" routes
1 parent e7be722 commit 1540b2f

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/decorator/All.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {getMetadataArgsStorage} from "../index";
2+
3+
/**
4+
* Registers an action to be executed when a request comes on a given route.
5+
* Must be applied on a controller action.
6+
*/
7+
export function All(route?: RegExp): Function;
8+
9+
/**
10+
* Registers an action to be executed when a request comes on a given route.
11+
* Must be applied on a controller action.
12+
*/
13+
export function All(route?: string): Function;
14+
15+
/**
16+
* Registers an action to be executed when a request comes on a given route.
17+
* Must be applied on a controller action.
18+
*/
19+
export function All(route?: string|RegExp): Function {
20+
return function (object: Object, methodName: string) {
21+
getMetadataArgsStorage().actions.push({
22+
type: "all",
23+
target: object.constructor,
24+
method: methodName,
25+
route: route
26+
});
27+
};
28+
}

src/driver/express/ExpressDriver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class ExpressDriver extends BaseDriver {
165165
// This causes a double action execution on our side, which results in an unhandled rejection,
166166
// saying: "Can't set headers after they are sent".
167167
// The following line skips action processing when the request method does not match the action method.
168-
if (request.method.toLowerCase() !== actionMetadata.type)
168+
if (actionMetadata.type !== "all" && request.method.toLowerCase() !== actionMetadata.type)
169169
return next();
170170

171171
return executeCallback({request, response, next});
@@ -207,7 +207,7 @@ export class ExpressDriver extends BaseDriver {
207207

208208
case "session-param":
209209
return request.session[param.name];
210-
210+
211211
case "session":
212212
return request.session;
213213

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {importClassesFromDirectories} from "./util/importClassesFromDirectories"
1414

1515
export * from "./container";
1616

17+
export * from "./decorator/All";
1718
export * from "./decorator/Authorized";
1819
export * from "./decorator/Body";
1920
export * from "./decorator/BodyParam";

src/metadata/types/ActionType.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* Controller action type.
33
*/
4-
export type ActionType = "checkout"
4+
export type ActionType = "all"
5+
|"checkout"
56
|"connect"
67
|"copy"
78
|"delete"
@@ -26,4 +27,4 @@ export type ActionType = "checkout"
2627
|"subscribe"
2728
|"trace"
2829
|"unlock"
29-
|"unsubscribe";
30+
|"unsubscribe";

0 commit comments

Comments
 (0)