Skip to content

Commit

Permalink
fix(common): Fix @useafter and @UseBefore decorators when it was used…
Browse files Browse the repository at this point in the history
… twice or more on the same class or method

BREAKING CHANGE:
If you use UseBefore or UseAfter as following:
```typescript
class MyController {
  @UseBefore(Middleware1)
  @UseBefore(Middleware2)
  test(){}
}
```
You have to inverse the middlewares:
```typescript
class MyController {
  @UseBefore(Middleware2)
  @UseBefore(Middleware1)
  test(){}
}
```

The calls order was wrong in v5. v6 fix this problem.
  • Loading branch information
Romakita committed Sep 19, 2020
1 parent c7dba7a commit 84c1e07
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"check-coverage": true,
"lines": 99.86,
"statements": 99.86,
"functions": 99.82,
"branches": 88.64
"functions": 99.76,
"branches": 88.52
}
10 changes: 8 additions & 2 deletions packages/common/src/mvc/decorators/method/useAfter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,38 @@ class CustomMiddleware {
use() {}
}

class CustomMiddleware2 {
use() {}
}

describe("UseAfter()", () => {
describe("when the decorator is use on a class", () => {
it("should add the middleware on the use stack", () => {
// WHEN
@UseAfter(CustomMiddleware)
@UseAfter(CustomMiddleware2)
class Test {
test() {}
}

// THEN
const store = Store.from(Test).get("middlewares");

expect(store).to.deep.eq({useAfter: [CustomMiddleware]});
expect(store).to.deep.eq({useAfter: [CustomMiddleware, CustomMiddleware2]});
});
});
describe("when the decorator is use on a method", () => {
it("should add the middleware on the use stack", () => {
// WHEN
class Test {
@UseAfter(CustomMiddleware)
@UseAfter(CustomMiddleware2)
test() {}
}

// THEN
const endpoint = EndpointMetadata.get(Test, "test");
expect(endpoint.afterMiddlewares).to.deep.equal([CustomMiddleware]);
expect(endpoint.afterMiddlewares).to.deep.equal([CustomMiddleware, CustomMiddleware2]);
});
});
describe("when the decorator is use in another way", () => {
Expand Down
11 changes: 9 additions & 2 deletions packages/common/src/mvc/decorators/method/useAfter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ export function UseAfter(...args: any[]): Function {
return JsonEntityFn((entity, parameters) => {
switch (entity.decoratorType) {
case DecoratorTypes.METHOD:
(entity as EndpointMetadata).after(args);
const endpoint = entity as EndpointMetadata;
endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);

break;

case DecoratorTypes.CLASS:
entity.store.merge("middlewares", {useAfter: args});
const middlewares = entity.store.get("middlewares") || {};

entity.store.set("middlewares", {
...middlewares,
useAfter: [...args, ...(middlewares.useAfter || [])]
});
break;

default:
Expand Down
10 changes: 8 additions & 2 deletions packages/common/src/mvc/decorators/method/useBefore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,38 @@ class CustomMiddleware {
use() {}
}

class CustomMiddleware2 {
use() {}
}

describe("UseBefore()", () => {
describe("when the decorator is use on a class", () => {
it("should add the middleware on the use stack", () => {
// WHEN
@UseBefore(CustomMiddleware)
@UseBefore(CustomMiddleware2)
class Test {
test() {}
}

// THEN
const result = Store.from(Test).get("middlewares");

expect(result).to.deep.eq({useBefore: [CustomMiddleware]});
expect(result).to.deep.eq({useBefore: [CustomMiddleware, CustomMiddleware2]});
});
});
describe("when the decorator is use on a method", () => {
it("should add the middleware on the use stack", () => {
// WHEN
class Test {
@UseBefore(CustomMiddleware)
@UseBefore(CustomMiddleware2)
test() {}
}

const endpoint = EndpointMetadata.get(Test, "test");
// THEN
expect(endpoint.beforeMiddlewares).to.deep.eq([CustomMiddleware]);
expect(endpoint.beforeMiddlewares).to.deep.eq([CustomMiddleware, CustomMiddleware2]);
});
});
describe("when the decorator is use in another way", () => {
Expand Down
13 changes: 10 additions & 3 deletions packages/common/src/mvc/decorators/method/useBefore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EndpointMetadata} from "../../models/EndpointMetadata";
import {DecoratorTypes, UnsupportedDecoratorType} from "@tsed/core";
import {JsonEntityFn} from "@tsed/schema";
import {EndpointMetadata} from "../../models/EndpointMetadata";

/**
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
Expand All @@ -26,11 +26,18 @@ export function UseBefore(...args: any[]): Function {
return JsonEntityFn((entity, parameters) => {
switch (entity.decoratorType) {
case DecoratorTypes.METHOD:
(entity as EndpointMetadata).before(args);
const endpoint = entity as EndpointMetadata;
endpoint.beforeMiddlewares = args.concat(endpoint.beforeMiddlewares);

break;

case DecoratorTypes.CLASS:
entity.store.merge("middlewares", {useBefore: args});
const middlewares = entity.store.get("middlewares") || {};

entity.store.set("middlewares", {
...middlewares,
useBefore: [...args, ...(middlewares.useBefore || [])]
});
break;

default:
Expand Down
12 changes: 6 additions & 6 deletions packages/platform-koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@
"ejs": "3.1.5"
},
"dependencies": {
"@koa/router": "9.4.0",
"koa-compose": "4.1.0",
"koa-static": "5.0.0",
"koa-mount": "4.0.0",
"multer": "1.4.2",
"encodeurl": "1.0.2"
"@koa/router": "^9.4.0",
"koa-compose": "^4.1.0",
"koa-static": "^5.0.0",
"koa-mount": "^4.0.0",
"multer": "^1.4.2",
"encodeurl": "^1.0.2"
},
"peerDependencies": {}
}
6 changes: 5 additions & 1 deletion packages/platform-koa/src/decorators/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {mapParamsOptions, UseParam} from "@tsed/common";
import {UseParam} from "@tsed/common";
import type {Context} from "koa";

/**
* Return the original Koa context.
* @decorator
* @operation
* @input
* @koa
*/
export function KoaCtx(): ParameterDecorator {
Expand All @@ -16,6 +18,8 @@ export function KoaCtx(): ParameterDecorator {
/**
* Return the original Koa context.
* @decorator
* @operation
* @input
* @koa
*/
export function Ctx(): ParameterDecorator {
Expand Down

0 comments on commit 84c1e07

Please sign in to comment.