Skip to content

Commit

Permalink
fix(common): throw exception when a Ts.ED middleware is registered on…
Browse files Browse the repository at this point in the history
… $beforeInit, $onInit and $afterInit hooks

Closes: #2510
  • Loading branch information
Romakita committed Nov 8, 2023
1 parent d664514 commit 48e3317
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 167 deletions.
6 changes: 4 additions & 2 deletions docs/docs/middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ Then add your middleware on the Server by using the right hook:

<<< @/docs/snippets/middlewares/global-middleware-configuration.ts

::: tip
Since v6.28.0, it's also possible to register middlewares from `middlewares` options on @@Configuration@@ decorator.
It's also possible to register middlewares from `middlewares` options on @@Configuration@@ decorator.
In addition, it's also possible to configure the environment for which the middleware should be loaded.

```typescript
Expand All @@ -62,6 +61,9 @@ export class Server {}

The middlewares added through `middlewares` options will always be registered after the middlewares registered through the hook methods!

::: warn
Only Express/Koa middlewares can be added on `$beforeInit`, `$onInit` and `$afterInit` hooks. At this step the PlatformContext is not available. Injectable Ts.ED middleware won't work as expected.
To add Ts.ED middleware, use the `$beforeRoutesInit` hook (it's the default hook value) or leave the `hook` property empty.
:::

## Endpoint middleware
Expand Down
62 changes: 61 additions & 1 deletion packages/platform/common/src/builder/PlatformBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Type} from "@tsed/core";
import {catchAsyncError, Type} from "@tsed/core";
import {Configuration, Controller, Injectable, InjectorService, Module} from "@tsed/di";
import {AfterInit} from "../interfaces/AfterInit";
import {AfterListen} from "../interfaces/AfterListen";
Expand Down Expand Up @@ -158,6 +158,66 @@ describe("PlatformBuilder", () => {
expect(platform.app.use).toHaveBeenCalledWith(middlewares[0].use);
expect(platform.app.use).toHaveBeenCalledWith(middlewares[2]);
});
it("should throw an error when a Ts.ED middleware is added on $afterInit hook", async () => {
const middlewares: any[] = [
{
hook: "$afterInit",
use: class TestMiddleware {}
}
];
// WHEN
const error = await catchAsyncError(() =>
PlatformCustom.bootstrap(ServerModule, {
httpPort: false,
httpsPort: false,
middlewares
})
);

expect(error?.message).toEqual(
'Ts.ED Middleware "TestMiddleware" middleware cannot be added on $afterInit hook. Use one of this hooks instead: $beforeRoutesInit, $onRoutesInit, $afterRoutesInit, $beforeListen, $afterListen, $onReady'
);
});
it("should throw an error when a Ts.ED middleware is added on $beforeInit hook", async () => {
const middlewares: any[] = [
{
hook: "$beforeInit",
use: class TestMiddleware {}
}
];
// WHEN
const error = await catchAsyncError(() =>
PlatformCustom.bootstrap(ServerModule, {
httpPort: false,
httpsPort: false,
middlewares
})
);

expect(error?.message).toEqual(
'Ts.ED Middleware "TestMiddleware" middleware cannot be added on $beforeInit hook. Use one of this hooks instead: $beforeRoutesInit, $onRoutesInit, $afterRoutesInit, $beforeListen, $afterListen, $onReady'
);
});
it("should throw an error when a Ts.ED middleware is added on $onInit hook", async () => {
const middlewares: any[] = [
{
hook: "$onInit",
use: class TestMiddleware {}
}
];
// WHEN
const error = await catchAsyncError(() =>
PlatformCustom.bootstrap(ServerModule, {
httpPort: false,
httpsPort: false,
middlewares
})
);

expect(error?.message).toEqual(
'Ts.ED Middleware "TestMiddleware" middleware cannot be added on $onInit hook. Use one of this hooks instead: $beforeRoutesInit, $onRoutesInit, $afterRoutesInit, $beforeListen, $afterListen, $onReady'
);
});
});
describe("static boostrap()", () => {
beforeAll(() => {
Expand Down
10 changes: 9 additions & 1 deletion packages/platform/common/src/builder/PlatformBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isFunction, isString, nameOf, Type} from "@tsed/core";
import {isClass, isFunction, isString, nameOf, Type} from "@tsed/core";
import {colors, InjectorService, ProviderOpts, setLoggerConfiguration, TokenProvider} from "@tsed/di";
import {getMiddlewaresForHook, PlatformMiddlewareLoadingOptions} from "@tsed/platform-middlewares";
import {PlatformLayer} from "@tsed/platform-router";
Expand Down Expand Up @@ -400,6 +400,14 @@ export class PlatformBuilder<App = TsED.Application> {
}
}

if (isClass(use) && ["$beforeInit", "$onInit", "$afterInit"].includes(middleware.hook!)) {
throw new Error(
`Ts.ED Middleware "${nameOf(use)}" middleware cannot be added on ${
middleware.hook
} hook. Use one of this hooks instead: $beforeRoutesInit, $onRoutesInit, $afterRoutesInit, $beforeListen, $afterListen, $onReady`
);
}

return {
env,
hook: defaultHook,
Expand Down
163 changes: 0 additions & 163 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5614,70 +5614,6 @@
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/common/-/common-7.43.0.tgz#30cf69ac12dd75c0dc303a45957ecdfa34f56a1c"
integrity sha512-EaKgXCVVhn+yh4lLwAGf4tX1To+YbkCS7kVnB4arblPknrNlKxUhq4h5SUejMY1OaNYvHJQ8KV+p+E6VJI+uFA==
dependencies:
"@tsed/core" "7.43.0"
"@tsed/di" "7.43.0"
"@tsed/exceptions" "7.43.0"
"@tsed/json-mapper" "7.43.0"
"@tsed/logger" ">=6.2.2"
"@tsed/logger-file" ">=6.2.2"
"@tsed/platform-exceptions" "7.43.0"
"@tsed/platform-log-middleware" "7.43.0"
"@tsed/platform-middlewares" "7.43.0"
"@tsed/platform-params" "7.43.0"
"@tsed/platform-response-filter" "7.43.0"
"@tsed/platform-router" "7.43.0"
"@tsed/platform-views" "7.43.0"
"@tsed/schema" "7.43.0"
"@types/json-schema" "7.0.11"
accepts "^1.3.8"
tslib "2.5.0"
uuid "8.3.2"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/core/-/core-7.43.0.tgz#a609579e476f56f486ef6bd3ef2bc50303ca14c2"
integrity sha512-RoZTKU1YZ9gI31NA7qZrbD7Ex78fKHPHIN8WygzfSC9ZgwKInjNtYxYW5T8MRGOOYRZEpj2R1yXZSYJCf25r5g==
dependencies:
reflect-metadata "^0.1.13"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/di/-/di-7.43.0.tgz#8333fb1292a5508a02103b821db356098303edab"
integrity sha512-xNpFSNFr/Vu7kd1KMLrLAg9X9MCoi/JGKSq6pZzeU0BKLQBB71Xbtvhu8VJqERNfELJJ1/W62Dq83DZ44jVEYQ==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/engines/-/engines-7.43.0.tgz#4c57ece5c8bbf4b71e73035fdb76f869d9b1e53e"
integrity sha512-/o9+uSvWEO9wFBUBMKkDRxSjEy6rgsl6Zv0nLHJhQuAgue/rY75zifOpFf+GghahNgXekNlU18zQBhyoKenx0w==
dependencies:
filedirname "^2.7.0"
fs-extra "11.1.1"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/exceptions/-/exceptions-7.43.0.tgz#e2af2b78d73d9bdefb6f74a862ed3c6888f3e942"
integrity sha512-VVWSWGj5nqrOQAHez395NA3XGb+4DSDEx4Gb31Fhh2gLYOonY+DU52P7je3bPgsoFwl60PRQOxNcLJ9ood2QWg==
dependencies:
change-case "4.1.2"
statuses ">=2.0.1"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/json-mapper/-/json-mapper-7.43.0.tgz#c194919268b70452c17c1259113bfefe00025e69"
integrity sha512-zeRS2q/el3iKiRk+zbhKTpa9tf905FzyVfucH89xvuq9XMIMafsnQa47zSQpInmiMwduxxwYeVAZqNmpRy/fZw==
dependencies:
tslib "2.5.0"

"@tsed/logger-file@>=6.2.2":
version "6.2.2"
resolved "https://registry.yarnpkg.com/@tsed/logger-file/-/logger-file-6.2.2.tgz#ffd1f9ab1a0e09160433676976c370a0800ba674"
Expand Down Expand Up @@ -5717,105 +5653,6 @@
normalize-path "3.0.0"
semver "7.3.7"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/normalize-path/-/normalize-path-7.43.0.tgz#682c153c9ee79558d56f47b69447cedad84dc032"
integrity sha512-iT9wndN321OsNTRLlXqjF8x0ys8YPb0YgOvpB+Pwsc9XzMyxENAl3SXvV3tGKaVkoE4Fwl39E1708MnsxQ9kFQ==
dependencies:
normalize-path "3.0.0"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/openspec/-/openspec-7.43.0.tgz#68a2afc22c104f388e6ba76af48f5c9ee837264d"
integrity sha512-rjISgz12GblD10FYUoWbXtUttuHxp/nPWsQDoPnZl9+tcTAqtjPcJZi5DgC5CwHus4PKXfCmWqFb63bRCOizug==

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-exceptions/-/platform-exceptions-7.43.0.tgz#98d342e38ee65767a93ec296cddbb4629e62cdca"
integrity sha512-298IAUQ3i0PhmpvXWeQCkBP6dnOdjZWU8+5kyR+icLOyxcFMgPUnYsOD4NqDI4v3932M9cxstI3BneGuThYnAg==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-express/-/platform-express-7.43.0.tgz#aa25e9a8f9e5b60f2c495bf853ada50ea16cd251"
integrity sha512-bumKpfNjpWzytiiBRdQzgt5Dufhk31NqG58AbXdARVvsJEQqgPB8noJwMN0XYH5m8tJ+RVGShNv4AVHzvogLJw==
dependencies:
express "^4.18.1"
multer "^1.4.5-lts.1"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-log-middleware/-/platform-log-middleware-7.43.0.tgz#a90d7679183d7d8206e30a98176616829ff78557"
integrity sha512-Q2OtbxryqgJ3YGxhN9iVNXilR5OnfvdAifBhE1JxWpe8yexTrm6heIzrVCu1JBA7f+40w+7We9R1UyrSNiaHKw==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-middlewares/-/platform-middlewares-7.43.0.tgz#1eef7b7da8bac9976b0a60aee4258469ee2efff6"
integrity sha512-4Kj1Iwxknu3VMYJdk9uOmDtrQQ7quMmhdFO6T2r/z2uOk89fjsUJHDAUmS8bHvcmrIodGrjfcPgYAqcy1vGcaQ==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-params/-/platform-params-7.43.0.tgz#8e6f208775f35effb38f7dd095ada26695fd75b6"
integrity sha512-MihhpipoR3TAyexCn+WvJbaJ8DHA2eso3K3FzOaU7XNuXmiTDn7ESkjG6IWyXXwGwdD7AHfgo6UL8pn8zp+ByQ==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-response-filter/-/platform-response-filter-7.43.0.tgz#c0f8856c47dfcf678ae698fef5a6e08c41ba04c1"
integrity sha512-zVISLDYgJICCsGH0W9m9Ix3itfxFGii4H6dOHFvmP+z0RYM0bHWNC9hfqwSZq59E3LMohA+qkt8JTKp/BJgDFw==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-router/-/platform-router-7.43.0.tgz#5f366c9aedfa1600451ff7ff4a7f5041b4dba39d"
integrity sha512-+TGN2L/TlN+G4SxfQONXuM4TFowtGim3wJA6O3SEAI85kH/oYVIaTuIv77ZZuIJivx/9wO1Z4IH/q3Tmfm1M9A==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-views/-/platform-views-7.43.0.tgz#ce931da6e1c61bb69ab36150dff5808092a3b563"
integrity sha512-jwwhEhPTEyZTfno4VTr9mGdp+EpYHV5bkm8A42dhbvdblSIiyLuVAInt8WleuX3kA9ttxX40ILFNq/Vcdg/4Dg==
dependencies:
"@tsed/engines" "7.43.0"
ejs "^3.1.5"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/schema/-/schema-7.43.0.tgz#8257cacf7e3191b0d14d7d2d6a9341fd9d313422"
integrity sha512-RXoswWQloJ/Qqz/5GrolzHzzmM4x/7f1WNtvPvlsurL+YLOm29Ym0ZB6BTeBidI4J2E71Bk1jU9McPR9SbPLmg==
dependencies:
"@tsed/openspec" "7.43.0"
change-case "^4.1.2"
fs-extra "^11.1.1"
json-schema "0.4.0"
picomatch "2.3.1"
statuses ">=2.0.1"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@tsed/swagger/-/swagger-7.43.0.tgz#e820be76eb0e00581afb98e8cc76c4d726fd7113"
integrity sha512-yD4xHWhlW0XzbyHJWokO50q4CvNl66IQMHHwqNMN93OXXB0EifLL8czLFr3e0y3wxwaoDGz3euKbkidiKSl/RA==
dependencies:
"@tsed/normalize-path" "7.43.0"
"@tsed/openspec" "7.43.0"
filedirname "^2.7.0"
fs-extra "11.1.1"
micromatch "4.0.5"
swagger-ui-dist "^4.5.2"
tslib "2.5.0"

"@tsed/[email protected]":
version "4.0.14"
resolved "https://registry.yarnpkg.com/@tsed/ts-doc/-/ts-doc-4.0.14.tgz#6f09a1b09962354c0a1e7847f475a3e35a191eed"
Expand Down

0 comments on commit 48e3317

Please sign in to comment.