Skip to content

Commit

Permalink
fix(common): before middleware should execute before use middleware
Browse files Browse the repository at this point in the history
Closes: tsedio#1594
  • Loading branch information
Nathan authored and Romakita committed Oct 27, 2021
1 parent e3d6451 commit 2f5dc58
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ describe("PlatformControllerBuilder", () => {
"/",
Sinon.match.func,
PlatformAcceptMimesMiddleware,
provider.middlewares.use[0],
endpoint.beforeMiddlewares[0],
provider.middlewares.use[0],
endpoint.middlewares[0],
endpoint,
endpoint.afterMiddlewares[0],
Expand All @@ -139,8 +139,8 @@ describe("PlatformControllerBuilder", () => {
"/",
Sinon.match.func,
PlatformAcceptMimesMiddleware,
provider.middlewares.use[0],
endpoint.beforeMiddlewares[0],
provider.middlewares.use[0],
endpoint.middlewares[0],
endpoint,
endpoint.afterMiddlewares[0],
Expand All @@ -163,8 +163,8 @@ describe("PlatformControllerBuilder", () => {
expect(router.use.getCall(1)).to.have.been.calledWithExactly(
Sinon.match.func,
PlatformAcceptMimesMiddleware,
provider.middlewares.use[0],
endpoint.beforeMiddlewares[0],
provider.middlewares.use[0],
endpoint.middlewares[0],
endpoint,
endpoint.afterMiddlewares[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export class PlatformControllerBuilder {
.concat(useCtxHandler(bindEndpointMiddleware(endpoint)))
.concat(PlatformAcceptMimesMiddleware)
.concat(hasFiles && PlatformMulterMiddleware)
.concat(use) // Controller use-middlewares
.concat(beforeMiddlewares) // Endpoint before-middlewares
.concat(use) // Controller use-middlewares
// .concat(endpoint.cache && PlatformCacheMiddleware)
.concat(mldwrs) // Endpoint middlewares
.concat(endpoint) // Endpoint metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class AfterCustomMiddleware {
}
}

@Middleware()
class UseCustomMiddleware {
use(@Context() ctx: Context) {
ctx.get("stacks").push("Use - Ctrl");
}
}

@Middleware()
class AfterEndpointCustomMiddleware {
use(@Context() ctx: Context) {
Expand All @@ -49,7 +56,7 @@ class UseEndpointMiddleware {
@Controller("/middlewares")
@UseBefore(BeforeCustomMiddleware)
@UseAfter(AfterCustomMiddleware)
@Use(AfterCustomMiddleware)
@Use(UseCustomMiddleware)
class TestMiddlewaresCtrl {
@Get("/scenario-1")
@UseBefore(BeforeEndpointCustomMiddleware)
Expand Down Expand Up @@ -85,8 +92,8 @@ export function testMiddlewares(options: PlatformTestOptions) {
expect(response.body).to.deep.equal({
stacks: [
"UseBefore - Ctrl",
"UseAfter - Ctrl",
"UseBefore - endpoint",
"Use - Ctrl",
"Use - endpoint",
"endpoint",
"UseAfter - endpoint",
Expand Down

0 comments on commit 2f5dc58

Please sign in to comment.