@tsed/platform-middlewares 8.7.0 → 8.7.1

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.
@@ -1,5 +1,10 @@
1
- import { DecoratorTypes, UnsupportedDecoratorType } from "@tsed/core";
1
+ import { DecoratorTypes, isClass, UnsupportedDecoratorType } from "@tsed/core";
2
2
  import { JsonEntityFn } from "@tsed/schema";
3
+ const useOnce = (list = [], middlewares = []) => {
4
+ return middlewares
5
+ .filter((middleware) => (isClass(middleware) ? !list.find((current) => current === middleware) : middleware))
6
+ .concat(list);
7
+ };
3
8
  /**
4
9
  * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
5
10
  * the base of the requested path matches `path.
@@ -24,13 +29,13 @@ export function UseBefore(...args) {
24
29
  return JsonEntityFn((entity, parameters) => {
25
30
  switch (entity.decoratorType) {
26
31
  case DecoratorTypes.METHOD:
27
- entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);
32
+ entity.beforeMiddlewares = useOnce(entity.beforeMiddlewares, args);
28
33
  break;
29
34
  case DecoratorTypes.CLASS:
30
35
  const middlewares = entity.store.get("middlewares") || {};
31
36
  entity.store.set("middlewares", {
32
37
  ...middlewares,
33
- useBefore: [...args, ...(middlewares.useBefore || [])]
38
+ useBefore: useOnce(middlewares.useBefore, args)
34
39
  });
35
40
  break;
36
41
  default:
@@ -1 +1 @@
1
- {"version":3,"file":"useBefore.js","sourceRoot":"","sources":["../../../src/decorators/useBefore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAE,wBAAwB,EAAC,MAAM,YAAY,CAAC;AACpE,OAAO,EAAC,YAAY,EAAC,MAAM,cAAc,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,SAAS,CAAC,GAAG,IAAW;IACtC,OAAO,YAAY,CAAC,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE;QACzC,QAAQ,MAAM,CAAC,aAAa,EAAE,CA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YAAY,CAAC,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE;QACzC,QAAQ,MAAM,CAAC,aAAa,EAAE,CAAC;YAC7B,KAAK,cAAc,CAAC,MAAM;gBACxB,MAAM,CAAC,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,iBAAiB,EAAE,IAAI,CAAC,CAAC;gBAEnE,MAAM;YAER,KAAK,cAAc,CAAC,KAAK;gBACvB,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;gBAE1D,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,EAAE;oBAC9B,GAAG,WAAW;oBACd,SAAS,EAAE,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,IAAI,CAAC;iBAChD,CAAC,CAAC;gBACH,MAAM;YAER;gBACE,MAAM,IAAI,wBAAwB,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;QAC9D,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
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.7.0",
5
+ "version": "8.7.1",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -26,19 +26,19 @@
26
26
  "tslib": "2.7.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@tsed/barrels": "8.7.0",
30
- "@tsed/core": "8.7.0",
31
- "@tsed/di": "8.7.0",
32
- "@tsed/schema": "8.7.0",
33
- "@tsed/typescript": "8.7.0",
29
+ "@tsed/barrels": "8.7.1",
30
+ "@tsed/core": "8.7.1",
31
+ "@tsed/di": "8.7.1",
32
+ "@tsed/schema": "8.7.1",
33
+ "@tsed/typescript": "8.7.1",
34
34
  "eslint": "9.12.0",
35
35
  "typescript": "5.4.5",
36
36
  "vitest": "3.0.9"
37
37
  },
38
38
  "peerDependencies": {
39
- "@tsed/core": ">=8.7.0",
40
- "@tsed/di": ">=8.7.0",
41
- "@tsed/schema": ">=8.7.0"
39
+ "@tsed/core": ">=8.7.1",
40
+ "@tsed/di": ">=8.7.1",
41
+ "@tsed/schema": ">=8.7.1"
42
42
  },
43
43
  "peerDependenciesMeta": {
44
44
  "@tsed/core": {
@@ -16,6 +16,7 @@ describe("UseBefore()", () => {
16
16
  it("should add the middleware on the use stack", () => {
17
17
  // WHEN
18
18
  @UseBefore(CustomMiddleware)
19
+ @UseBefore(CustomMiddleware) // should be ignored
19
20
  @UseBefore(CustomMiddleware2)
20
21
  class Test {
21
22
  test() {}
@@ -31,6 +32,7 @@ describe("UseBefore()", () => {
31
32
  it("should add the middleware on the use stack", () => {
32
33
  // WHEN
33
34
  class Test {
35
+ @UseBefore(CustomMiddleware)
34
36
  @UseBefore(CustomMiddleware)
35
37
  @UseBefore(CustomMiddleware2)
36
38
  test() {}
@@ -1,6 +1,12 @@
1
- import {DecoratorTypes, UnsupportedDecoratorType} from "@tsed/core";
1
+ import {DecoratorTypes, isClass, UnsupportedDecoratorType} from "@tsed/core";
2
2
  import {JsonEntityFn} from "@tsed/schema";
3
3
 
4
+ const useOnce = (list: any[] = [], middlewares: any[] = []) => {
5
+ return middlewares
6
+ .filter((middleware) => (isClass(middleware) ? !list.find((current: any) => current === middleware) : middleware))
7
+ .concat(list);
8
+ };
9
+
4
10
  /**
5
11
  * Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
6
12
  * the base of the requested path matches `path.
@@ -25,7 +31,7 @@ export function UseBefore(...args: any[]): Function {
25
31
  return JsonEntityFn((entity, parameters) => {
26
32
  switch (entity.decoratorType) {
27
33
  case DecoratorTypes.METHOD:
28
- entity.beforeMiddlewares = args.concat(entity.beforeMiddlewares);
34
+ entity.beforeMiddlewares = useOnce(entity.beforeMiddlewares, args);
29
35
 
30
36
  break;
31
37
 
@@ -34,7 +40,7 @@ export function UseBefore(...args: any[]): Function {
34
40
 
35
41
  entity.store.set("middlewares", {
36
42
  ...middlewares,
37
- useBefore: [...args, ...(middlewares.useBefore || [])]
43
+ useBefore: useOnce(middlewares.useBefore, args)
38
44
  });
39
45
  break;
40
46