@tsed/platform-middlewares 8.15.4 → 8.16.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ import { constant } from "@tsed/di";
2
+ import { $on } from "@tsed/hooks";
3
+ export function alterMiddlewaresForHook(middlewares, hook) {
4
+ const env = constant("env");
5
+ return middlewares.filter((options) => options.use && (!options.env || options.env === env) && options.hook === hook);
6
+ }
7
+ /**
8
+ * @deprecated will be remove in the next major version
9
+ */
10
+ export const getMiddlewaresForHook = alterMiddlewaresForHook;
11
+ $on("$alterMiddlewaresForHook", alterMiddlewaresForHook);
@@ -0,0 +1,43 @@
1
+ import { isClass, isFunction, isString, nameOf } from "@tsed/core";
2
+ import { configuration, constant, inject } from "@tsed/di";
3
+ import { $on } from "@tsed/hooks";
4
+ export async function beforeInit() {
5
+ const env = constant("env");
6
+ const defaultHook = "$beforeRoutesInit";
7
+ const middlewares = constant("middlewares", []);
8
+ // HACK to fix the circular dependency issue around platform-multer -> platform-http -> platform-middlewares
9
+ const platformAdapter = inject("PlatformAdapter");
10
+ const promises = middlewares.map(async (middleware) => {
11
+ if (isFunction(middleware)) {
12
+ return {
13
+ env,
14
+ hook: defaultHook,
15
+ use: middleware
16
+ };
17
+ }
18
+ if (isString(middleware)) {
19
+ middleware = { env, use: middleware, hook: defaultHook };
20
+ }
21
+ let { use, options } = middleware;
22
+ if (isString(use)) {
23
+ if (["text-parser", "raw-parser", "json-parser", "urlencoded-parser"].includes(use)) {
24
+ use = platformAdapter.bodyParser(use.replace("-parser", ""), options);
25
+ }
26
+ else {
27
+ const mod = await import(use);
28
+ use = (mod.default || mod)(options);
29
+ }
30
+ }
31
+ if (isClass(use) && ["$beforeInit", "$onInit", "$afterInit"].includes(middleware.hook)) {
32
+ 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`);
33
+ }
34
+ return {
35
+ env,
36
+ hook: defaultHook,
37
+ ...middleware,
38
+ use
39
+ };
40
+ });
41
+ configuration().set("middlewares", (await Promise.all(promises)).filter((middleware) => middleware.use));
42
+ }
43
+ $on("$beforeInit", beforeInit);
package/lib/esm/index.js CHANGED
@@ -11,4 +11,5 @@ export * from "./decorators/useBefore.js";
11
11
  export * from "./decorators/useBeforeEach.js";
12
12
  export * from "./domain/MiddlewareMethods.js";
13
13
  export * from "./domain/PlatformMiddlewareSettings.js";
14
- export * from "./utils/getMiddlewaresForHook.js";
14
+ export * from "./hooks/alterMiddlewaresForHook.js";
15
+ export * from "./hooks/beforeInit.js";
@@ -0,0 +1,6 @@
1
+ import { PlatformMiddlewareLoadingOptions } from "../domain/PlatformMiddlewareSettings.js";
2
+ export declare function alterMiddlewaresForHook(middlewares: PlatformMiddlewareLoadingOptions[], hook: string): PlatformMiddlewareLoadingOptions[];
3
+ /**
4
+ * @deprecated will be remove in the next major version
5
+ */
6
+ export declare const getMiddlewaresForHook: typeof alterMiddlewaresForHook;
@@ -0,0 +1 @@
1
+ export declare function beforeInit(): Promise<void>;
@@ -11,4 +11,5 @@ export * from "./decorators/useBefore.js";
11
11
  export * from "./decorators/useBeforeEach.js";
12
12
  export * from "./domain/MiddlewareMethods.js";
13
13
  export * from "./domain/PlatformMiddlewareSettings.js";
14
- export * from "./utils/getMiddlewaresForHook.js";
14
+ export * from "./hooks/alterMiddlewaresForHook.js";
15
+ export * from "./hooks/beforeInit.js";
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/platform-middlewares",
3
3
  "description": "Middlewares module for Ts.ED Framework",
4
4
  "type": "module",
5
- "version": "8.15.4",
5
+ "version": "8.16.0-rc.2",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -26,11 +26,12 @@
26
26
  "tslib": "2.7.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@tsed/barrels": "8.15.4",
30
- "@tsed/core": "8.15.4",
31
- "@tsed/di": "8.15.4",
32
- "@tsed/schema": "8.15.4",
33
- "@tsed/typescript": "8.15.4",
29
+ "@tsed/barrels": "8.16.0-rc.2",
30
+ "@tsed/core": "8.16.0-rc.2",
31
+ "@tsed/di": "8.16.0-rc.2",
32
+ "@tsed/platform-router": "8.16.0-rc.2",
33
+ "@tsed/schema": "8.16.0-rc.2",
34
+ "@tsed/typescript": "8.16.0-rc.2",
34
35
  "eslint": "9.12.0",
35
36
  "typescript": "5.4.5",
36
37
  "vitest": "3.2.4"
@@ -57,5 +58,8 @@
57
58
  },
58
59
  "homepage": "https://github.com/tsedio/tsed/tree/production/packages/platform/platform-middlewares",
59
60
  "author": "Romain Lenzotti",
60
- "license": "MIT"
61
+ "license": "MIT",
62
+ "publishConfig": {
63
+ "tag": "rc"
64
+ }
61
65
  }
@@ -1,5 +0,0 @@
1
- export function getMiddlewaresForHook(hook, settings, defaultHook = "") {
2
- const env = settings.env;
3
- const middlewares = settings.get("middlewares", []);
4
- return middlewares.filter((options) => options.use && options.env === env && options.hook === hook);
5
- }
@@ -1,3 +0,0 @@
1
- import { DIConfiguration } from "@tsed/di";
2
- import { PlatformMiddlewareLoadingOptions } from "../domain/PlatformMiddlewareSettings.js";
3
- export declare function getMiddlewaresForHook(hook: string, settings: DIConfiguration, defaultHook?: string): PlatformMiddlewareLoadingOptions[];