@tsed/platform-middlewares 8.11.0 → 8.11.2-rc.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.
- package/lib/esm/constants/constants.js +0 -1
- package/lib/esm/decorators/authOptions.js +0 -1
- package/lib/esm/decorators/middleware.js +0 -1
- package/lib/esm/decorators/use.js +0 -1
- package/lib/esm/decorators/useAfter.js +0 -1
- package/lib/esm/decorators/useAuth.js +0 -1
- package/lib/esm/decorators/useBefore.js +0 -1
- package/lib/esm/decorators/useBeforeEach.js +0 -1
- package/lib/esm/domain/MiddlewareMethods.js +0 -1
- package/lib/esm/domain/PlatformMiddlewareSettings.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/utils/getMiddlewaresForHook.js +0 -1
- package/package.json +10 -7
- package/lib/esm/constants/constants.js.map +0 -1
- package/lib/esm/decorators/authOptions.js.map +0 -1
- package/lib/esm/decorators/middleware.js.map +0 -1
- package/lib/esm/decorators/use.js.map +0 -1
- package/lib/esm/decorators/useAfter.js.map +0 -1
- package/lib/esm/decorators/useAuth.js.map +0 -1
- package/lib/esm/decorators/useBefore.js.map +0 -1
- package/lib/esm/decorators/useBeforeEach.js.map +0 -1
- package/lib/esm/domain/MiddlewareMethods.js.map +0 -1
- package/lib/esm/domain/PlatformMiddlewareSettings.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/utils/getMiddlewaresForHook.js.map +0 -1
- package/src/constants/constants.ts +0 -30
- package/src/decorators/authOptions.spec.ts +0 -122
- package/src/decorators/authOptions.ts +0 -57
- package/src/decorators/middleware.ts +0 -14
- package/src/decorators/use.spec.ts +0 -53
- package/src/decorators/use.ts +0 -41
- package/src/decorators/useAfter.spec.ts +0 -63
- package/src/decorators/useAfter.ts +0 -46
- package/src/decorators/useAuth.spec.ts +0 -135
- package/src/decorators/useAuth.ts +0 -66
- package/src/decorators/useBefore.spec.ts +0 -65
- package/src/decorators/useBefore.ts +0 -51
- package/src/decorators/useBeforeEach.spec.ts +0 -56
- package/src/decorators/useBeforeEach.ts +0 -39
- package/src/domain/MiddlewareMethods.ts +0 -3
- package/src/domain/PlatformMiddlewareSettings.ts +0 -15
- package/src/index.ts +0 -14
- package/src/utils/getMiddlewaresForHook.ts +0 -10
- package/src/utils/getMiddlewaresForHooks.spec.ts +0 -17
- package/vitest.config.mts +0 -21
package/lib/esm/index.js
CHANGED
|
@@ -3,4 +3,3 @@ export function getMiddlewaresForHook(hook, settings, defaultHook = "") {
|
|
|
3
3
|
const middlewares = settings.get("middlewares", []);
|
|
4
4
|
return middlewares.filter((options) => options.use && options.env === env && options.hook === hook);
|
|
5
5
|
}
|
|
6
|
-
//# sourceMappingURL=getMiddlewaresForHook.js.map
|
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.11.
|
|
5
|
+
"version": "8.11.2-rc.1",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
"tslib": "2.7.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@tsed/barrels": "8.11.
|
|
30
|
-
"@tsed/core": "8.11.
|
|
31
|
-
"@tsed/di": "8.11.
|
|
32
|
-
"@tsed/schema": "8.11.
|
|
33
|
-
"@tsed/typescript": "8.11.
|
|
29
|
+
"@tsed/barrels": "8.11.2-rc.1",
|
|
30
|
+
"@tsed/core": "8.11.2-rc.1",
|
|
31
|
+
"@tsed/di": "8.11.2-rc.1",
|
|
32
|
+
"@tsed/schema": "8.11.2-rc.1",
|
|
33
|
+
"@tsed/typescript": "8.11.2-rc.1",
|
|
34
34
|
"eslint": "9.12.0",
|
|
35
35
|
"typescript": "5.4.5",
|
|
36
36
|
"vitest": "3.1.4"
|
|
@@ -57,5 +57,8 @@
|
|
|
57
57
|
},
|
|
58
58
|
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/platform/platform-middlewares",
|
|
59
59
|
"author": "Romain Lenzotti",
|
|
60
|
-
"license": "MIT"
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"tag": "rc"
|
|
63
|
+
}
|
|
61
64
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/constants/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,KAAK;IACL,UAAU;IACV,SAAS;IACT,MAAM;IACN,QAAQ;IACR,KAAK;IACL,MAAM;IACN,MAAM;IACN,OAAO;IACP,YAAY;IACZ,OAAO;IACP,MAAM;IACN,UAAU;IACV,QAAQ;IACR,SAAS;IACT,OAAO;IACP,OAAO;IACP,MAAM;IACN,UAAU;IACV,UAAU;IACV,OAAO;IACP,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,WAAW;IACX,OAAO;IACP,QAAQ;IACR,YAAY;CACb,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"authOptions.js","sourceRoot":"","sources":["../../../src/decorators/authOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAGjB,eAAe,EACf,cAAc,EACd,KAAK,EAEL,wBAAwB,EACzB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAC,YAAY,EAAC,MAAM,cAAc,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,WAAW,CAAC,SAAoB,EAAE,UAAmC,EAAE;IACrF,OAAO,CAAI,GAAG,IAAyB,EAAqC,EAAE;QAC5E,QAAQ,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC;YAC9B,KAAK,cAAc,CAAC,MAAM;gBACxB,OAAO,YAAY,CAAC,CAAC,MAAM,EAAE,EAAE;oBAC7B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;oBAE3B,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;gBACxC,CAAC,CAAC,CAAC,GAAI,IAAkC,CAAC,CAAC;YAE7C,KAAK,cAAc,CAAC,KAAK;gBACvB,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAM,EAAE,QAAgB,EAAE,EAAE;oBACtD,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;gBACtE,CAAC,CAAC,CAAC;gBACH,kDAAkD;gBAClD,EAAE;gBACF,MAAM;gBACN,MAAM;YAER;gBACE,MAAM,IAAI,wBAAwB,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../../src/decorators/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAgB,YAAY,EAAC,MAAM,UAAU,CAAC;AAEhE;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,IAA0C;IACnE,OAAO,UAAU,CAAC;QAChB,GAAG,IAAI;QACP,IAAI,EAAE,YAAY,CAAC,UAAU;KAC9B,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use.js","sourceRoot":"","sources":["../../../src/decorators/use.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAE,wBAAwB,EAAC,MAAM,YAAY,CAAC;AACpE,OAAO,EAAC,YAAY,EAAE,SAAS,EAAC,MAAM,cAAc,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;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
|
1
|
-
{"version":3,"file":"MiddlewareMethods.js","sourceRoot":"","sources":["../../../src/domain/MiddlewareMethods.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformMiddlewareSettings.js","sourceRoot":"","sources":["../../../src/domain/PlatformMiddlewareSettings.ts"],"names":[],"mappings":""}
|
package/lib/esm/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wCAAwC,CAAC;AACvD,cAAc,kCAAkC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getMiddlewaresForHook.js","sourceRoot":"","sources":["../../../src/utils/getMiddlewaresForHook.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,qBAAqB,CAAC,IAAY,EAAE,QAAyB,EAAE,WAAW,GAAG,EAAE;IAC7F,MAAM,GAAG,GAAG,QAAQ,CAAC,GAAG,CAAC;IACzB,MAAM,WAAW,GAAG,QAAQ,CAAC,GAAG,CAAqC,aAAa,EAAE,EAAE,CAAC,CAAC;IAExF,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,KAAK,GAAG,IAAI,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;AACtG,CAAC"}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export const HTTP_METHODS = [
|
|
2
|
-
"all",
|
|
3
|
-
"checkout",
|
|
4
|
-
"connect",
|
|
5
|
-
"copy",
|
|
6
|
-
"delete",
|
|
7
|
-
"get",
|
|
8
|
-
"head",
|
|
9
|
-
"lock",
|
|
10
|
-
"merge",
|
|
11
|
-
"mkactivity",
|
|
12
|
-
"mkcol",
|
|
13
|
-
"move",
|
|
14
|
-
"m-search",
|
|
15
|
-
"notify",
|
|
16
|
-
"options",
|
|
17
|
-
"param",
|
|
18
|
-
"patch",
|
|
19
|
-
"post",
|
|
20
|
-
"propfind",
|
|
21
|
-
"propatch",
|
|
22
|
-
"purge",
|
|
23
|
-
"put",
|
|
24
|
-
"report",
|
|
25
|
-
"search",
|
|
26
|
-
"subscribe",
|
|
27
|
-
"trace",
|
|
28
|
-
"unlock",
|
|
29
|
-
"unsuscribe"
|
|
30
|
-
];
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import {decoratorArgs, prototypeOf, Store, UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
-
|
|
3
|
-
import {AuthOptions} from "./authOptions.js";
|
|
4
|
-
|
|
5
|
-
class Guard {
|
|
6
|
-
use() {}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
describe("AuthOptions()", () => {
|
|
10
|
-
describe("when the decorator is use on a method", () => {
|
|
11
|
-
class Test {
|
|
12
|
-
test() {}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
it("should add the middleware on the use stack", () => {
|
|
16
|
-
// WHEN
|
|
17
|
-
AuthOptions(Guard, {
|
|
18
|
-
security: [
|
|
19
|
-
{
|
|
20
|
-
auth: ["email"]
|
|
21
|
-
}
|
|
22
|
-
],
|
|
23
|
-
responses: {
|
|
24
|
-
"200": {
|
|
25
|
-
description: "Success"
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
})(...decoratorArgs(prototypeOf(Test), "test"));
|
|
29
|
-
|
|
30
|
-
// THEN
|
|
31
|
-
const store = Store.from(...decoratorArgs(prototypeOf(Test), "test"));
|
|
32
|
-
expect(store.get(Guard)).toEqual({
|
|
33
|
-
security: [
|
|
34
|
-
{
|
|
35
|
-
auth: ["email"]
|
|
36
|
-
}
|
|
37
|
-
],
|
|
38
|
-
responses: {
|
|
39
|
-
"200": {
|
|
40
|
-
description: "Success"
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
describe("when the decorator is use on a class", () => {
|
|
47
|
-
class Test {
|
|
48
|
-
test() {}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
it("should add the middleware on the use stack", () => {
|
|
52
|
-
// WHEN
|
|
53
|
-
AuthOptions(Guard, {
|
|
54
|
-
security: [
|
|
55
|
-
{
|
|
56
|
-
auth: ["email"]
|
|
57
|
-
}
|
|
58
|
-
],
|
|
59
|
-
responses: {
|
|
60
|
-
"200": {
|
|
61
|
-
description: "Success"
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
})(Test);
|
|
65
|
-
|
|
66
|
-
// THEN
|
|
67
|
-
const store = Store.from(...decoratorArgs(prototypeOf(Test), "test"));
|
|
68
|
-
expect(store.get(Guard)).toEqual({
|
|
69
|
-
security: [
|
|
70
|
-
{
|
|
71
|
-
auth: ["email"]
|
|
72
|
-
}
|
|
73
|
-
],
|
|
74
|
-
responses: {
|
|
75
|
-
"200": {
|
|
76
|
-
description: "Success"
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
store.set("responses", {});
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
describe("when the decorator is use on a class and method", () => {
|
|
85
|
-
it("should add the middleware on the use stack", () => {
|
|
86
|
-
// WHEN
|
|
87
|
-
|
|
88
|
-
@AuthOptions(Guard, {role: "test"})
|
|
89
|
-
class Test {
|
|
90
|
-
@AuthOptions(Guard, {role: "test2"})
|
|
91
|
-
test() {}
|
|
92
|
-
|
|
93
|
-
test2() {}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// THEN
|
|
97
|
-
const storeTest = Store.from(...decoratorArgs(prototypeOf(Test), "test"));
|
|
98
|
-
const storeTest2 = Store.from(...decoratorArgs(prototypeOf(Test), "test2"));
|
|
99
|
-
expect(storeTest.get(Guard)).toEqual({role: "test2"});
|
|
100
|
-
expect(storeTest2.get(Guard)).toEqual({role: "test"});
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
describe("when the decorator is use in another way", () => {
|
|
104
|
-
class Test {
|
|
105
|
-
test() {}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
it("should add the middleware on the use stack", () => {
|
|
109
|
-
// WHEN
|
|
110
|
-
let actualError;
|
|
111
|
-
try {
|
|
112
|
-
AuthOptions(Guard)(Test, "property");
|
|
113
|
-
} catch (er) {
|
|
114
|
-
actualError = er;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// THEN
|
|
118
|
-
expect(actualError).toBeInstanceOf(UnsupportedDecoratorType);
|
|
119
|
-
expect(actualError.message).toEqual("AuthOptions cannot be used as property.static decorator on Test.property");
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
});
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
decorateMethodsOf,
|
|
3
|
-
DecoratorMethodParameters,
|
|
4
|
-
DecoratorParameters,
|
|
5
|
-
decoratorTypeOf,
|
|
6
|
-
DecoratorTypes,
|
|
7
|
-
Store,
|
|
8
|
-
Type,
|
|
9
|
-
UnsupportedDecoratorType
|
|
10
|
-
} from "@tsed/core";
|
|
11
|
-
import {JsonEntityFn} from "@tsed/schema";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Change authentication options.
|
|
15
|
-
*
|
|
16
|
-
* ```typescript
|
|
17
|
-
* @Controller('/mypath')
|
|
18
|
-
* @UseAuth(MyAuthStrategy, {role: ''})
|
|
19
|
-
* class MyCtrl {
|
|
20
|
-
*
|
|
21
|
-
* @Get('/')
|
|
22
|
-
* @AuthOptions(MyAuthStrategy, {role: 'admin'})
|
|
23
|
-
* public getResource(){}
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @param guardAuth
|
|
28
|
-
* @param options {Object} Object passed to the customer auth strategy
|
|
29
|
-
* @returns {Function}
|
|
30
|
-
* @decorator
|
|
31
|
-
* @decorator
|
|
32
|
-
* @operation
|
|
33
|
-
*/
|
|
34
|
-
export function AuthOptions(guardAuth: Type<any>, options: Record<string, unknown> = {}): Function {
|
|
35
|
-
return <T>(...args: DecoratorParameters): TypedPropertyDescriptor<T> | void => {
|
|
36
|
-
switch (decoratorTypeOf(args)) {
|
|
37
|
-
case DecoratorTypes.METHOD:
|
|
38
|
-
return JsonEntityFn((entity) => {
|
|
39
|
-
const store = entity.store;
|
|
40
|
-
|
|
41
|
-
store.merge(guardAuth, options, true);
|
|
42
|
-
})(...(args as DecoratorMethodParameters));
|
|
43
|
-
|
|
44
|
-
case DecoratorTypes.CLASS:
|
|
45
|
-
decorateMethodsOf(args[0], (_: any, property: string) => {
|
|
46
|
-
Store.fromMethod(args[0], property).merge(guardAuth, options, true);
|
|
47
|
-
});
|
|
48
|
-
// methodsOf(args[0]).forEach(({propertyKey}) => {
|
|
49
|
-
//
|
|
50
|
-
// });
|
|
51
|
-
break;
|
|
52
|
-
|
|
53
|
-
default:
|
|
54
|
-
throw new UnsupportedDecoratorType(AuthOptions, args);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import {Injectable, ProviderOpts, ProviderType} from "@tsed/di";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Register a new Middleware class.
|
|
5
|
-
*
|
|
6
|
-
* @decorator
|
|
7
|
-
* @classDecorator
|
|
8
|
-
*/
|
|
9
|
-
export function Middleware(opts?: Partial<Omit<ProviderOpts, "type">>): ClassDecorator {
|
|
10
|
-
return Injectable({
|
|
11
|
-
...opts,
|
|
12
|
-
type: ProviderType.MIDDLEWARE
|
|
13
|
-
});
|
|
14
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import {Store} from "@tsed/core";
|
|
2
|
-
import {EndpointMetadata} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {Use} from "./use.js";
|
|
5
|
-
|
|
6
|
-
describe("Use()", () => {
|
|
7
|
-
describe("when the decorator is use on a method", () => {
|
|
8
|
-
it("should add the middleware on the use stack", () => {
|
|
9
|
-
const middleware = () => {};
|
|
10
|
-
|
|
11
|
-
class Test {
|
|
12
|
-
@Use(middleware)
|
|
13
|
-
test() {}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const endpoint = EndpointMetadata.get(Test, "test");
|
|
17
|
-
expect(endpoint.middlewares).toEqual([middleware]);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
describe("when the decorator is use on a class", () => {
|
|
22
|
-
it("should add the middleware on the use stack", () => {
|
|
23
|
-
const middleware = () => {};
|
|
24
|
-
|
|
25
|
-
@Use(middleware)
|
|
26
|
-
class Test {
|
|
27
|
-
test() {}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const store = Store.from(Test).get("middlewares");
|
|
31
|
-
expect(store.use[0]).toBeInstanceOf(Function);
|
|
32
|
-
expect(store.use[0]).toEqual(middleware);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe("when the decorator is use on a property", () => {
|
|
37
|
-
it("should throw error", () => {
|
|
38
|
-
const middleware = () => {};
|
|
39
|
-
|
|
40
|
-
let actualError: any;
|
|
41
|
-
try {
|
|
42
|
-
class Test {
|
|
43
|
-
@Use(middleware)
|
|
44
|
-
test: string;
|
|
45
|
-
}
|
|
46
|
-
} catch (er) {
|
|
47
|
-
actualError = er;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
expect(actualError.message).toEqual("Use cannot be used as property decorator on Test.test");
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
});
|
package/src/decorators/use.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import {DecoratorTypes, UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
-
import {JsonEntityFn, Operation} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
6
|
-
* the base of the requested path matches `path.
|
|
7
|
-
*
|
|
8
|
-
* ```typescript
|
|
9
|
-
* @Controller('/')
|
|
10
|
-
* @Use(Middleware1)
|
|
11
|
-
* export class Ctrl {
|
|
12
|
-
*
|
|
13
|
-
* @Get('/')
|
|
14
|
-
* @Use(Middleware2)
|
|
15
|
-
* get() { }
|
|
16
|
-
* }
|
|
17
|
-
*
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @returns {Function}
|
|
21
|
-
* @param args
|
|
22
|
-
* @decorator
|
|
23
|
-
* @operation
|
|
24
|
-
*/
|
|
25
|
-
export function Use(...args: any[]): Function {
|
|
26
|
-
return JsonEntityFn((entity, parameters) => {
|
|
27
|
-
switch (entity.decoratorType) {
|
|
28
|
-
case DecoratorTypes.METHOD:
|
|
29
|
-
return Operation(...args);
|
|
30
|
-
|
|
31
|
-
case DecoratorTypes.CLASS:
|
|
32
|
-
entity.store.merge("middlewares", {
|
|
33
|
-
use: args
|
|
34
|
-
});
|
|
35
|
-
break;
|
|
36
|
-
|
|
37
|
-
default:
|
|
38
|
-
throw new UnsupportedDecoratorType(Use, parameters);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import {Store, UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
-
import {EndpointMetadata} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {UseAfter} from "./useAfter.js";
|
|
5
|
-
|
|
6
|
-
class CustomMiddleware {
|
|
7
|
-
use() {}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
class CustomMiddleware2 {
|
|
11
|
-
use() {}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
describe("UseAfter()", () => {
|
|
15
|
-
describe("when the decorator is use on a class", () => {
|
|
16
|
-
it("should add the middleware on the use stack", () => {
|
|
17
|
-
// WHEN
|
|
18
|
-
@UseAfter(CustomMiddleware)
|
|
19
|
-
@UseAfter(CustomMiddleware2)
|
|
20
|
-
class Test {
|
|
21
|
-
test() {}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// THEN
|
|
25
|
-
const store = Store.from(Test).get("middlewares");
|
|
26
|
-
|
|
27
|
-
expect(store).toEqual({useAfter: [CustomMiddleware, CustomMiddleware2]});
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
describe("when the decorator is use on a method", () => {
|
|
31
|
-
it("should add the middleware on the use stack", () => {
|
|
32
|
-
// WHEN
|
|
33
|
-
class Test {
|
|
34
|
-
@UseAfter(CustomMiddleware)
|
|
35
|
-
@UseAfter(CustomMiddleware2)
|
|
36
|
-
test() {}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// THEN
|
|
40
|
-
const endpoint = EndpointMetadata.get(Test, "test");
|
|
41
|
-
expect(endpoint.afterMiddlewares).toEqual([CustomMiddleware, CustomMiddleware2]);
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
describe("when the decorator is use in another way", () => {
|
|
45
|
-
class Test {
|
|
46
|
-
test() {}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
it("should add the middleware on the use stack", () => {
|
|
50
|
-
// WHEN
|
|
51
|
-
let actualError;
|
|
52
|
-
try {
|
|
53
|
-
UseAfter(CustomMiddleware)(Test, "property");
|
|
54
|
-
} catch (er) {
|
|
55
|
-
actualError = er;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// THEN
|
|
59
|
-
expect(actualError).toBeInstanceOf(UnsupportedDecoratorType);
|
|
60
|
-
expect(actualError.message).toEqual("UseAfter cannot be used as property.static decorator on Test.property");
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
});
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import {DecoratorTypes, UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
-
import {JsonEntityFn} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
6
|
-
* the base of the requested path matches `path.
|
|
7
|
-
*
|
|
8
|
-
* ```typescript
|
|
9
|
-
* @Controller('/')
|
|
10
|
-
* @UseAfter(Middleware1)
|
|
11
|
-
* export class Ctrl {
|
|
12
|
-
*
|
|
13
|
-
* @Get('/')
|
|
14
|
-
* @UseAfter(Middleware2)
|
|
15
|
-
* get() { }
|
|
16
|
-
* }
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* @returns {function(any, any, any): *}
|
|
20
|
-
* @param args
|
|
21
|
-
* @decorator
|
|
22
|
-
* @operation
|
|
23
|
-
*/
|
|
24
|
-
export function UseAfter(...args: any[]): Function {
|
|
25
|
-
return JsonEntityFn((entity, parameters) => {
|
|
26
|
-
switch (entity.decoratorType) {
|
|
27
|
-
case DecoratorTypes.METHOD:
|
|
28
|
-
const endpoint = entity;
|
|
29
|
-
endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);
|
|
30
|
-
|
|
31
|
-
break;
|
|
32
|
-
|
|
33
|
-
case DecoratorTypes.CLASS:
|
|
34
|
-
const middlewares = entity.store.get("middlewares") || {};
|
|
35
|
-
|
|
36
|
-
entity.store.set("middlewares", {
|
|
37
|
-
...middlewares,
|
|
38
|
-
useAfter: [...args, ...(middlewares.useAfter || [])]
|
|
39
|
-
});
|
|
40
|
-
break;
|
|
41
|
-
|
|
42
|
-
default:
|
|
43
|
-
throw new UnsupportedDecoratorType(UseAfter, parameters);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import {decoratorArgs, prototypeOf, Store, UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
-
import {EndpointMetadata} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {UseAuth} from "./useAuth.js";
|
|
5
|
-
|
|
6
|
-
class Guard {
|
|
7
|
-
use() {}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
describe("UseAuth()", () => {
|
|
11
|
-
describe("when the decorator is use on a method", () => {
|
|
12
|
-
it("should add the middleware on the use stack", () => {
|
|
13
|
-
// WHEN
|
|
14
|
-
@UseAuth(Guard, {
|
|
15
|
-
security: [
|
|
16
|
-
{
|
|
17
|
-
auth: ["email"]
|
|
18
|
-
}
|
|
19
|
-
],
|
|
20
|
-
responses: {
|
|
21
|
-
"200": {
|
|
22
|
-
description: "Success"
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
})
|
|
26
|
-
class Test {
|
|
27
|
-
test() {}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// THEN
|
|
31
|
-
const args = decoratorArgs(prototypeOf(Test), "test");
|
|
32
|
-
const store = Store.from(...args);
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
const endpoint = EndpointMetadata.get(...args);
|
|
35
|
-
|
|
36
|
-
expect(endpoint.beforeMiddlewares).toEqual([Guard]);
|
|
37
|
-
expect(store.get(Guard)).toEqual({
|
|
38
|
-
responses: {
|
|
39
|
-
"200": {
|
|
40
|
-
description: "Success"
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
security: [
|
|
44
|
-
{
|
|
45
|
-
auth: ["email"]
|
|
46
|
-
}
|
|
47
|
-
]
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
describe("when the decorator is use on a class", () => {
|
|
52
|
-
it("should add the middleware on the use stack", () => {
|
|
53
|
-
// WHEN
|
|
54
|
-
@UseAuth(Guard, {
|
|
55
|
-
security: [
|
|
56
|
-
{
|
|
57
|
-
auth: ["email"]
|
|
58
|
-
}
|
|
59
|
-
],
|
|
60
|
-
responses: {
|
|
61
|
-
"200": {
|
|
62
|
-
description: "Success"
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
class Test {
|
|
67
|
-
test() {}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// THEN
|
|
71
|
-
const args = decoratorArgs(prototypeOf(Test), "test");
|
|
72
|
-
const store = Store.from(...args);
|
|
73
|
-
// @ts-ignore
|
|
74
|
-
const endpoint = EndpointMetadata.get(...args);
|
|
75
|
-
|
|
76
|
-
expect(endpoint.beforeMiddlewares).toEqual([Guard]);
|
|
77
|
-
|
|
78
|
-
expect(store.get(Guard)).toEqual({
|
|
79
|
-
responses: {
|
|
80
|
-
"200": {
|
|
81
|
-
description: "Success"
|
|
82
|
-
}
|
|
83
|
-
},
|
|
84
|
-
security: [
|
|
85
|
-
{
|
|
86
|
-
auth: ["email"]
|
|
87
|
-
}
|
|
88
|
-
]
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
describe("when the decorator is use on a class and method", () => {
|
|
93
|
-
it("should add the middleware on the use stack", () => {
|
|
94
|
-
// WHEN
|
|
95
|
-
|
|
96
|
-
@UseAuth(Guard, {defaultRole: "test"})
|
|
97
|
-
class Test {
|
|
98
|
-
@UseAuth(Guard, {role: "test2"})
|
|
99
|
-
test() {}
|
|
100
|
-
|
|
101
|
-
test2() {}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// THEN
|
|
105
|
-
const storeTest = Store.from(...decoratorArgs(prototypeOf(Test), "test"));
|
|
106
|
-
const storeTest2 = Store.from(...decoratorArgs(prototypeOf(Test), "test2"));
|
|
107
|
-
// @ts-ignore
|
|
108
|
-
const endpoint = EndpointMetadata.get(Test, "test");
|
|
109
|
-
expect(endpoint.beforeMiddlewares).toEqual([Guard]);
|
|
110
|
-
|
|
111
|
-
const endpoint2 = EndpointMetadata.get(Test, "test2");
|
|
112
|
-
expect(endpoint2.beforeMiddlewares).toEqual([Guard]);
|
|
113
|
-
|
|
114
|
-
expect(storeTest.get(Guard)).toEqual({role: "test2", defaultRole: "test"});
|
|
115
|
-
expect(storeTest2.get(Guard)).toEqual({defaultRole: "test"});
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
describe("when the decorator is use in another way", () => {
|
|
119
|
-
it("should add the middleware on the use stack", () => {
|
|
120
|
-
// WHEN
|
|
121
|
-
let actualError;
|
|
122
|
-
try {
|
|
123
|
-
class Test {}
|
|
124
|
-
|
|
125
|
-
UseAuth(Guard)(Test, "property");
|
|
126
|
-
} catch (er) {
|
|
127
|
-
actualError = er;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// THEN
|
|
131
|
-
expect(actualError).toBeInstanceOf(UnsupportedDecoratorType);
|
|
132
|
-
expect(actualError.message).toEqual("UseAuth cannot be used as property.static decorator on Test.property");
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
});
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
decorateMethodsOf,
|
|
3
|
-
DecoratorParameters,
|
|
4
|
-
decoratorTypeOf,
|
|
5
|
-
DecoratorTypes,
|
|
6
|
-
Store,
|
|
7
|
-
StoreFn,
|
|
8
|
-
Type,
|
|
9
|
-
UnsupportedDecoratorType,
|
|
10
|
-
useDecorators
|
|
11
|
-
} from "@tsed/core";
|
|
12
|
-
|
|
13
|
-
import {AuthOptions} from "./authOptions.js";
|
|
14
|
-
import {UseBefore} from "./useBefore.js";
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Use custom authentication strategy on your endpoint.
|
|
18
|
-
*
|
|
19
|
-
* ```typescript
|
|
20
|
-
* @Controller('/mypath')
|
|
21
|
-
* @UseAuth(MyAuthStrategy)
|
|
22
|
-
* class MyCtrl {
|
|
23
|
-
*
|
|
24
|
-
* @Get('/')
|
|
25
|
-
* @UseAuth(MyAuthStrategy, {role: 'admin'})
|
|
26
|
-
* public getResource(){}
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @param guardAuth {Type<any>} A middleware which implement a custom auth strategy
|
|
31
|
-
* @param options {Object} Object passed to the customer auth strategy
|
|
32
|
-
* @returns {Function}
|
|
33
|
-
* @decorator
|
|
34
|
-
* @operation
|
|
35
|
-
*/
|
|
36
|
-
export function UseAuth(guardAuth: Type<any>, options: Record<string, unknown> = {}): Function {
|
|
37
|
-
return <T>(...args: DecoratorParameters): TypedPropertyDescriptor<T> | void => {
|
|
38
|
-
switch (decoratorTypeOf(args)) {
|
|
39
|
-
case DecoratorTypes.METHOD:
|
|
40
|
-
return useDecorators(
|
|
41
|
-
StoreFn((store: Store) => {
|
|
42
|
-
if (!store.has(guardAuth)) {
|
|
43
|
-
return UseBefore(guardAuth);
|
|
44
|
-
}
|
|
45
|
-
}),
|
|
46
|
-
AuthOptions(guardAuth, options)
|
|
47
|
-
)(...args);
|
|
48
|
-
|
|
49
|
-
case DecoratorTypes.CLASS:
|
|
50
|
-
decorateMethodsOf(
|
|
51
|
-
args[0],
|
|
52
|
-
StoreFn((store: Store) => {
|
|
53
|
-
if (!store.has(guardAuth)) {
|
|
54
|
-
return UseBefore(guardAuth);
|
|
55
|
-
}
|
|
56
|
-
})
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
AuthOptions(guardAuth, options)(...args);
|
|
60
|
-
break;
|
|
61
|
-
|
|
62
|
-
default:
|
|
63
|
-
throw new UnsupportedDecoratorType(UseAuth, args);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import {Store, UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
-
import {EndpointMetadata} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {UseBefore} from "./useBefore.js";
|
|
5
|
-
|
|
6
|
-
class CustomMiddleware {
|
|
7
|
-
use() {}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
class CustomMiddleware2 {
|
|
11
|
-
use() {}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
describe("UseBefore()", () => {
|
|
15
|
-
describe("when the decorator is use on a class", () => {
|
|
16
|
-
it("should add the middleware on the use stack", () => {
|
|
17
|
-
// WHEN
|
|
18
|
-
@UseBefore(CustomMiddleware)
|
|
19
|
-
@UseBefore(CustomMiddleware) // should be ignored
|
|
20
|
-
@UseBefore(CustomMiddleware2)
|
|
21
|
-
class Test {
|
|
22
|
-
test() {}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// THEN
|
|
26
|
-
const result = Store.from(Test).get("middlewares");
|
|
27
|
-
|
|
28
|
-
expect(result).toEqual({useBefore: [CustomMiddleware, CustomMiddleware2]});
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
describe("when the decorator is use on a method", () => {
|
|
32
|
-
it("should add the middleware on the use stack", () => {
|
|
33
|
-
// WHEN
|
|
34
|
-
class Test {
|
|
35
|
-
@UseBefore(CustomMiddleware)
|
|
36
|
-
@UseBefore(CustomMiddleware)
|
|
37
|
-
@UseBefore(CustomMiddleware2)
|
|
38
|
-
test() {}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const endpoint = EndpointMetadata.get(Test, "test");
|
|
42
|
-
// THEN
|
|
43
|
-
expect(endpoint.beforeMiddlewares).toEqual([CustomMiddleware, CustomMiddleware2]);
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
describe("when the decorator is use in another way", () => {
|
|
47
|
-
class Test {
|
|
48
|
-
test() {}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
it("should add the middleware on the use stack", () => {
|
|
52
|
-
// WHEN
|
|
53
|
-
let actualError;
|
|
54
|
-
try {
|
|
55
|
-
UseBefore(CustomMiddleware)(Test, "property");
|
|
56
|
-
} catch (er) {
|
|
57
|
-
actualError = er;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// THEN
|
|
61
|
-
expect(actualError).toBeInstanceOf(UnsupportedDecoratorType);
|
|
62
|
-
expect(actualError.message).toEqual("UseBefore cannot be used as property.static decorator on Test.property");
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
});
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import {DecoratorTypes, isClass, UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
-
import {JsonEntityFn} from "@tsed/schema";
|
|
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
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
12
|
-
* the base of the requested path matches `path.
|
|
13
|
-
*
|
|
14
|
-
* ```typescript
|
|
15
|
-
* @Controller('/')
|
|
16
|
-
* @UseBefore(Middleware1) // called only one time before all endpoint
|
|
17
|
-
* export class Ctrl {
|
|
18
|
-
*
|
|
19
|
-
* @Get('/')
|
|
20
|
-
* @UseBefore(Middleware2)
|
|
21
|
-
* get() { }
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @returns {function(any, any, any): *}
|
|
26
|
-
* @param args
|
|
27
|
-
* @decorator
|
|
28
|
-
* @operation
|
|
29
|
-
*/
|
|
30
|
-
export function UseBefore(...args: any[]): Function {
|
|
31
|
-
return JsonEntityFn((entity, parameters) => {
|
|
32
|
-
switch (entity.decoratorType) {
|
|
33
|
-
case DecoratorTypes.METHOD:
|
|
34
|
-
entity.beforeMiddlewares = useOnce(entity.beforeMiddlewares, args);
|
|
35
|
-
|
|
36
|
-
break;
|
|
37
|
-
|
|
38
|
-
case DecoratorTypes.CLASS:
|
|
39
|
-
const middlewares = entity.store.get("middlewares") || {};
|
|
40
|
-
|
|
41
|
-
entity.store.set("middlewares", {
|
|
42
|
-
...middlewares,
|
|
43
|
-
useBefore: useOnce(middlewares.useBefore, args)
|
|
44
|
-
});
|
|
45
|
-
break;
|
|
46
|
-
|
|
47
|
-
default:
|
|
48
|
-
throw new UnsupportedDecoratorType(UseBefore, parameters);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import {UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
-
import {EndpointMetadata} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {UseBeforeEach} from "./useBeforeEach.js";
|
|
5
|
-
|
|
6
|
-
class CustomMiddleware {
|
|
7
|
-
use() {}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
describe("UseBeforeEach()", () => {
|
|
11
|
-
describe("when the decorator is use on a class", () => {
|
|
12
|
-
it("should add the middleware on the use stack", () => {
|
|
13
|
-
@UseBeforeEach(CustomMiddleware)
|
|
14
|
-
class Test {
|
|
15
|
-
test() {}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const endpoint = EndpointMetadata.get(Test, "test");
|
|
19
|
-
// THEN
|
|
20
|
-
expect(endpoint.beforeMiddlewares).toEqual([CustomMiddleware]);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
describe("when the decorator is use on a method", () => {
|
|
24
|
-
it("should add the middleware on the use stack", () => {
|
|
25
|
-
// WHEN
|
|
26
|
-
class Test {
|
|
27
|
-
@UseBeforeEach(CustomMiddleware)
|
|
28
|
-
test() {}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// THEN
|
|
32
|
-
const endpoint = EndpointMetadata.get(Test, "test");
|
|
33
|
-
expect(endpoint.beforeMiddlewares).toEqual([CustomMiddleware]);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
describe("when the decorator is use in another way", () => {
|
|
38
|
-
class Test {
|
|
39
|
-
test() {}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
it("should add the middleware on the use stack", () => {
|
|
43
|
-
// WHEN
|
|
44
|
-
let actualError;
|
|
45
|
-
try {
|
|
46
|
-
UseBeforeEach(CustomMiddleware)(Test, "property");
|
|
47
|
-
} catch (er) {
|
|
48
|
-
actualError = er;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// THEN
|
|
52
|
-
expect(actualError).toBeInstanceOf(UnsupportedDecoratorType);
|
|
53
|
-
expect(actualError.message).toEqual("UseBeforeEach cannot be used as property.static decorator on Test.property");
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
});
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import {decorateMethodsOf, DecoratorParameters, decoratorTypeOf, DecoratorTypes, UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
-
|
|
3
|
-
import {UseBefore} from "./useBefore.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
|
|
7
|
-
* the base of the requested path matches `path.
|
|
8
|
-
*
|
|
9
|
-
* ```typescript
|
|
10
|
-
* @Controller('/')
|
|
11
|
-
* @UseBeforeEach(Middleware1) // Called before each endpoint
|
|
12
|
-
* export class Ctrl {
|
|
13
|
-
*
|
|
14
|
-
* @Get('/')
|
|
15
|
-
* get() { }
|
|
16
|
-
* }
|
|
17
|
-
*
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @returns {Function}
|
|
21
|
-
* @param args
|
|
22
|
-
* @decorator
|
|
23
|
-
* @operation
|
|
24
|
-
*/
|
|
25
|
-
export function UseBeforeEach(...args: any[]): Function {
|
|
26
|
-
return <T>(...decoratorArgs: DecoratorParameters): TypedPropertyDescriptor<T> | void => {
|
|
27
|
-
switch (decoratorTypeOf(decoratorArgs)) {
|
|
28
|
-
case DecoratorTypes.METHOD:
|
|
29
|
-
return UseBefore(...args)(...decoratorArgs);
|
|
30
|
-
|
|
31
|
-
case DecoratorTypes.CLASS:
|
|
32
|
-
decorateMethodsOf(decoratorArgs[0], UseBefore(...args));
|
|
33
|
-
break;
|
|
34
|
-
|
|
35
|
-
default:
|
|
36
|
-
throw new UnsupportedDecoratorType(UseBeforeEach, decoratorArgs);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {Env, Type} from "@tsed/core";
|
|
2
|
-
|
|
3
|
-
export type PlatformMiddlewareLoadingOptions = {env?: Env; use: Function | Type<any> | string; hook?: string; options?: any};
|
|
4
|
-
export type PlatformMiddlewareSettings = Function | Type<any> | PlatformMiddlewareLoadingOptions | string | any;
|
|
5
|
-
|
|
6
|
-
declare global {
|
|
7
|
-
namespace TsED {
|
|
8
|
-
interface Configuration {
|
|
9
|
-
/**
|
|
10
|
-
* Load middlewares on the $beforeRoutesInit hook (or on the specified hook event name).
|
|
11
|
-
*/
|
|
12
|
-
middlewares: PlatformMiddlewareSettings[];
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Automatically generated by @tsed/barrels.
|
|
3
|
-
*/
|
|
4
|
-
export * from "./constants/constants.js";
|
|
5
|
-
export * from "./decorators/authOptions.js";
|
|
6
|
-
export * from "./decorators/middleware.js";
|
|
7
|
-
export * from "./decorators/use.js";
|
|
8
|
-
export * from "./decorators/useAfter.js";
|
|
9
|
-
export * from "./decorators/useAuth.js";
|
|
10
|
-
export * from "./decorators/useBefore.js";
|
|
11
|
-
export * from "./decorators/useBeforeEach.js";
|
|
12
|
-
export * from "./domain/MiddlewareMethods.js";
|
|
13
|
-
export * from "./domain/PlatformMiddlewareSettings.js";
|
|
14
|
-
export * from "./utils/getMiddlewaresForHook.js";
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import {DIConfiguration} from "@tsed/di";
|
|
2
|
-
|
|
3
|
-
import {PlatformMiddlewareLoadingOptions} from "../domain/PlatformMiddlewareSettings.js";
|
|
4
|
-
|
|
5
|
-
export function getMiddlewaresForHook(hook: string, settings: DIConfiguration, defaultHook = "") {
|
|
6
|
-
const env = settings.env;
|
|
7
|
-
const middlewares = settings.get<PlatformMiddlewareLoadingOptions[]>("middlewares", []);
|
|
8
|
-
|
|
9
|
-
return middlewares.filter((options) => options.use && options.env === env && options.hook === hook);
|
|
10
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import {DIConfiguration} from "@tsed/di";
|
|
2
|
-
|
|
3
|
-
import {getMiddlewaresForHook} from "./getMiddlewaresForHook.js";
|
|
4
|
-
|
|
5
|
-
describe("getMiddlewaresForHooks", () => {
|
|
6
|
-
it("should return the middlewares for given hook (without default hook)", () => {
|
|
7
|
-
const settings = new DIConfiguration();
|
|
8
|
-
settings.set("middlewares", [
|
|
9
|
-
{env: "production", use: vi.fn(), hook: "hook"},
|
|
10
|
-
{use: vi.fn(), hook: "hook"}
|
|
11
|
-
]);
|
|
12
|
-
|
|
13
|
-
const middlewares = getMiddlewaresForHook("hook", settings);
|
|
14
|
-
|
|
15
|
-
expect(middlewares).toHaveLength(1);
|
|
16
|
-
});
|
|
17
|
-
});
|
package/vitest.config.mts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
import {presets} from "@tsed/vitest/presets";
|
|
3
|
-
import {defineConfig} from "vitest/config";
|
|
4
|
-
|
|
5
|
-
export default defineConfig(
|
|
6
|
-
{
|
|
7
|
-
...presets,
|
|
8
|
-
test: {
|
|
9
|
-
...presets.test,
|
|
10
|
-
coverage: {
|
|
11
|
-
...presets.test.coverage,
|
|
12
|
-
thresholds: {
|
|
13
|
-
statements: 0,
|
|
14
|
-
branches: 0,
|
|
15
|
-
functions: 0,
|
|
16
|
-
lines: 0
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
);
|