Skip to content

Commit

Permalink
feat: middleware decorator allow multi middleware function (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
412799755 authored Jul 28, 2022
1 parent 5da127f commit a4b55f7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
10 changes: 7 additions & 3 deletions core/controller-decorator/src/decorator/Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import assert from 'assert';
import ControllerInfoUtil from '../util/ControllerInfoUtil';
import MethodInfoUtil from '../util/MethodInfoUtil';

export function Middleware(middleware: MiddlewareFunc) {
export function Middleware(...middlewares: MiddlewareFunc []) {
function classMiddleware(constructor: EggProtoImplClass) {
ControllerInfoUtil.addControllerMiddleware(middleware, constructor);
middlewares.forEach(mid => {
ControllerInfoUtil.addControllerMiddleware(mid, constructor);
});
}

function methodMiddleware(target: any, propertyKey: PropertyKey) {
assert(typeof propertyKey === 'string',
`[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
const controllerClazz = target.constructor as EggProtoImplClass;
const methodName = propertyKey as string;
MethodInfoUtil.addMethodMiddleware(middleware, controllerClazz, methodName);
middlewares.forEach(mid => {
MethodInfoUtil.addMethodMiddleware(mid, controllerClazz, methodName);
});
}

return function(target: any, propertyKey?: PropertyKey) {
Expand Down
8 changes: 7 additions & 1 deletion core/controller-decorator/test/Middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import { MiddlewareController } from './fixtures/MiddlewareController';
import { MiddlewareController, MiddlewaresController } from './fixtures/MiddlewareController';
import ControllerInfoUtil from '../src/util/ControllerInfoUtil';
import MethodInfoUtil from '../src/util/MethodInfoUtil';

Expand All @@ -10,4 +10,10 @@ describe('test/Middleware.test.ts', () => {
assert(controllerMws.length === 1);
assert(methodMws.length === 2);
});
it('Middleware with muti params should work', () => {
const controllerMws = ControllerInfoUtil.getControllerMiddlewares(MiddlewaresController);
const methodMws = MethodInfoUtil.getMethodMiddlewares(MiddlewaresController, 'hello');
assert(controllerMws.length === 1);
assert(methodMws.length === 2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ export class MiddlewareController {
return;
}
}

@Middleware(middleware1)
export class MiddlewaresController {

@Middleware(middleware2, middleware3)
async hello(): Promise<void> {
return;
}
}
11 changes: 8 additions & 3 deletions plugin/controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ exports.teggController = {
## Usage

### Middleware
Middleware 支持多个入参,依次传入要生效的中间件
中间件注解,可以添加在类/方法上。添加在类上时,对类上所有方法生效,添加在方法上时,只对当前方法生效。

```ts
Expand All @@ -53,13 +54,17 @@ export default async function globalLog(ctx: Context, next: any) {
return next();
}

export default async function globalLog2(ctx: Context, next: any) {
ctx.logger.info('have a request2');
return next();
}

// app/controller/FooController.ts
import { Middleware } from '@eggjs/tegg';

@Middleware(globalLog)
@Middleware(globalLog,globalLog2)
export class FooController {

@Middleware(methodCount)

async hello() {
}
}
Expand Down

0 comments on commit a4b55f7

Please sign in to comment.