Skip to content

Commit

Permalink
fix: update middle scope works
Browse files Browse the repository at this point in the history
  • Loading branch information
lengfangbing committed Sep 24, 2020
1 parent b03d145 commit f7c21b7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion decorator/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const consumeRoutes: ClassDecorator = (target: Function) => {
const router = getRouterInitial();
const path = target.prototype.decorator_prefix_min || '';
getRoutes().forEach(val => {
router[val.method](path + val.path, val.handler, val.middleware.concat(getMiddlewares()));
router[val.method](path + val.path, val.handler, getMiddlewares().concat(val.middleware));
});
clearMiddlewares();
clearRoutes();
Expand Down
4 changes: 2 additions & 2 deletions decorator/decorator.type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Middleware} from "./middleware.ts";
import {DecorationApplication} from "./application.ts";
import {Router} from "./router.ts";
import {ListenOptions, ReqMethod, MethodFuncArgument} from "../model.ts";
import {ListenOptions, ReqMethod, MiddlewareFunc, MethodFuncArgument} from "../model.ts";

export declare type Entity = {
app: DecorationApplication | null,
Expand All @@ -11,7 +11,7 @@ export declare type Entity = {
routes: Array<{
path: string;
method: ReqMethod,
middleware: Function[],
middleware: MiddlewareFunc[],
handler: Function,
}>;
middlewares: MethodFuncArgument;
Expand Down
5 changes: 4 additions & 1 deletion examples/decorator/demo2/routes/route1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class Route1 {
await next();
console.log('route1 middle end');
}
@Get('/test')
@Get('/test', [async (req: Req, res: Res, next: Function) => {
console.log('route1 middle for /test');
await next();
}])
test(req: Req, res: Res) {
res.body = {
url: 'route1 test'
Expand Down
5 changes: 4 additions & 1 deletion examples/decorator/demo2/routes/route2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class Route2 {
await next();
console.log('route2 middle end');
}
@Get('/test')
@Get('/test', [async (req: Req, res: Res, next: Function) => {
console.log('route2 middle for /test');
await next();
}])
test(req: Req, res: Res) {
res.body = {
prefix: '/route2'
Expand Down

0 comments on commit f7c21b7

Please sign in to comment.