@e22m4u/ts-rest-router 0.1.1 → 0.2.0
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/README.md +118 -118
- package/dist/cjs/index.cjs +346 -360
- package/dist/esm/controller-registry.js +28 -28
- package/dist/esm/debuggable-service.d.ts +10 -1
- package/dist/esm/debuggable-service.js +14 -3
- package/dist/esm/decorators/after-action/after-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{after/after-decorator.js → after-action/after-action-decorator.js} +7 -7
- package/dist/esm/decorators/{before/before-decorator.spec.js → after-action/after-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{after/after-metadata.d.ts → after-action/after-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/after-action/after-action-metadata.js +5 -0
- package/dist/esm/decorators/{after/after-reflector.d.ts → after-action/after-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{before/before-reflector.js → after-action/after-action-reflector.js} +6 -6
- package/dist/esm/decorators/{before/before-reflector.spec.js → after-action/after-action-reflector.spec.js} +23 -23
- package/dist/esm/decorators/after-action/index.d.ts +3 -0
- package/dist/esm/decorators/after-action/index.js +3 -0
- package/dist/esm/decorators/before-action/before-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{before/before-decorator.js → before-action/before-action-decorator.js} +7 -7
- package/dist/esm/decorators/{after/after-decorator.spec.js → before-action/before-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{before/before-metadata.d.ts → before-action/before-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/before-action/before-action-metadata.js +5 -0
- package/dist/esm/decorators/{before/before-reflector.d.ts → before-action/before-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{after/after-reflector.js → before-action/before-action-reflector.js} +6 -6
- package/dist/esm/decorators/{after/after-reflector.spec.js → before-action/before-action-reflector.spec.js} +23 -23
- package/dist/esm/decorators/before-action/index.d.ts +3 -0
- package/dist/esm/decorators/before-action/index.js +3 -0
- package/dist/esm/decorators/index.d.ts +4 -4
- package/dist/esm/decorators/index.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +6 -0
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.js +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +44 -32
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +1 -1
- package/dist/esm/decorators/rest-action/index.d.ts +3 -0
- package/dist/esm/decorators/rest-action/index.js +3 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.d.ts +57 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.js +52 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.spec.js +401 -0
- package/dist/esm/decorators/{action/action-metadata.d.ts → rest-action/rest-action-metadata.d.ts} +6 -6
- package/dist/esm/decorators/rest-action/rest-action-metadata.js +5 -0
- package/dist/esm/decorators/rest-action/rest-action-reflector.d.ts +22 -0
- package/dist/esm/decorators/{action/action-reflector.js → rest-action/rest-action-reflector.js} +6 -6
- package/dist/esm/decorators/{action/action-reflector.spec.js → rest-action/rest-action-reflector.spec.js} +13 -13
- package/dist/esm/decorators/rest-controller/index.d.ts +3 -0
- package/dist/esm/decorators/rest-controller/index.js +3 -0
- package/dist/esm/decorators/rest-controller/rest-controller-decorator.d.ts +14 -0
- package/dist/esm/decorators/{controller/controller-decorator.js → rest-controller/rest-controller-decorator.js} +5 -5
- package/dist/esm/decorators/{controller/controller-decorator.spec.js → rest-controller/rest-controller-decorator.spec.js} +15 -15
- package/dist/esm/decorators/{controller/controller-metadata.d.ts → rest-controller/rest-controller-metadata.d.ts} +5 -5
- package/dist/esm/decorators/rest-controller/rest-controller-metadata.js +5 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.d.ts +20 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.js +24 -0
- package/dist/esm/decorators/{controller/controller-reflector.spec.js → rest-controller/rest-controller-reflector.spec.js} +12 -12
- package/dist/esm/errors/not-a-controller-error.js +1 -1
- package/dist/esm/types.d.ts +0 -10
- package/dist/esm/utils/index.d.ts +0 -1
- package/dist/esm/utils/index.js +0 -1
- package/package.json +19 -19
- package/src/controller-registry.spec.ts +122 -122
- package/src/controller-registry.ts +33 -35
- package/src/debuggable-service.ts +17 -4
- package/src/decorators/{after/after-decorator.spec.ts → after-action/after-action-decorator.spec.ts} +19 -19
- package/src/decorators/{before/before-decorator.ts → after-action/after-action-decorator.ts} +9 -9
- package/src/decorators/{after/after-metadata.ts → after-action/after-action-metadata.ts} +5 -5
- package/src/decorators/{before/before-reflector.spec.ts → after-action/after-action-reflector.spec.ts} +33 -23
- package/src/decorators/{before/before-reflector.ts → after-action/after-action-reflector.ts} +13 -9
- package/src/decorators/after-action/index.ts +3 -0
- package/src/decorators/{before/before-decorator.spec.ts → before-action/before-action-decorator.spec.ts} +19 -19
- package/src/decorators/{after/after-decorator.ts → before-action/before-action-decorator.ts} +9 -9
- package/src/decorators/before-action/before-action-metadata.ts +17 -0
- package/src/decorators/{after/after-reflector.spec.ts → before-action/before-action-reflector.spec.ts} +40 -23
- package/src/decorators/{after/after-reflector.ts → before-action/before-action-reflector.ts} +18 -9
- package/src/decorators/before-action/index.ts +3 -0
- package/src/decorators/index.ts +4 -4
- package/src/decorators/request-context/request-context-decorator.spec.ts +7 -0
- package/src/decorators/request-context/request-context-decorator.ts +4 -4
- package/src/decorators/request-data/request-data-decorator.spec.ts +45 -32
- package/src/decorators/request-data/request-data-decorator.ts +10 -10
- package/src/decorators/request-data/request-data-metadata.ts +1 -1
- package/src/decorators/rest-action/index.ts +3 -0
- package/src/decorators/rest-action/rest-action-decorator.spec.ts +325 -0
- package/src/decorators/rest-action/rest-action-decorator.ts +166 -0
- package/src/decorators/{action/action-metadata.ts → rest-action/rest-action-metadata.ts} +7 -7
- package/src/decorators/{action/action-reflector.spec.ts → rest-action/rest-action-reflector.spec.ts} +13 -13
- package/src/decorators/rest-action/rest-action-reflector.ts +41 -0
- package/src/decorators/rest-controller/index.ts +3 -0
- package/src/decorators/{controller/controller-decorator.spec.ts → rest-controller/rest-controller-decorator.spec.ts} +16 -16
- package/src/decorators/{controller/controller-decorator.ts → rest-controller/rest-controller-decorator.ts} +14 -10
- package/src/decorators/{controller/controller-metadata.ts → rest-controller/rest-controller-metadata.ts} +6 -7
- package/src/decorators/{controller/controller-reflector.spec.ts → rest-controller/rest-controller-reflector.spec.ts} +21 -12
- package/src/decorators/rest-controller/rest-controller-reflector.ts +32 -0
- package/src/errors/not-a-controller-error.ts +4 -1
- package/src/types.ts +0 -10
- package/src/utils/index.ts +0 -1
- package/README-ru.md +0 -268
- package/dist/esm/decorators/action/action-decorator.d.ts +0 -52
- package/dist/esm/decorators/action/action-decorator.js +0 -62
- package/dist/esm/decorators/action/action-decorator.spec.js +0 -59
- package/dist/esm/decorators/action/action-metadata.js +0 -5
- package/dist/esm/decorators/action/action-reflector.d.ts +0 -22
- package/dist/esm/decorators/action/index.d.ts +0 -3
- package/dist/esm/decorators/action/index.js +0 -3
- package/dist/esm/decorators/after/after-decorator.d.ts +0 -9
- package/dist/esm/decorators/after/after-metadata.js +0 -5
- package/dist/esm/decorators/after/index.d.ts +0 -3
- package/dist/esm/decorators/after/index.js +0 -3
- package/dist/esm/decorators/before/before-decorator.d.ts +0 -9
- package/dist/esm/decorators/before/before-metadata.js +0 -5
- package/dist/esm/decorators/before/index.d.ts +0 -3
- package/dist/esm/decorators/before/index.js +0 -3
- package/dist/esm/decorators/controller/controller-decorator.d.ts +0 -14
- package/dist/esm/decorators/controller/controller-metadata.js +0 -5
- package/dist/esm/decorators/controller/controller-reflector.d.ts +0 -20
- package/dist/esm/decorators/controller/controller-reflector.js +0 -24
- package/dist/esm/decorators/controller/index.d.ts +0 -3
- package/dist/esm/decorators/controller/index.js +0 -3
- package/dist/esm/utils/create-debugger.d.ts +0 -44
- package/dist/esm/utils/create-debugger.js +0 -81
- package/dist/esm/utils/create-debugger.spec.d.ts +0 -1
- package/dist/esm/utils/create-debugger.spec.js +0 -8
- package/src/decorators/action/action-decorator.spec.ts +0 -42
- package/src/decorators/action/action-decorator.ts +0 -98
- package/src/decorators/action/action-reflector.ts +0 -38
- package/src/decorators/action/index.ts +0 -3
- package/src/decorators/after/index.ts +0 -3
- package/src/decorators/before/before-metadata.ts +0 -17
- package/src/decorators/before/index.ts +0 -3
- package/src/decorators/controller/controller-reflector.ts +0 -28
- package/src/decorators/controller/index.ts +0 -3
- package/src/utils/create-debugger.spec.ts +0 -9
- package/src/utils/create-debugger.ts +0 -98
- /package/dist/esm/decorators/{action/action-decorator.spec.d.ts → after-action/after-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{action/action-reflector.spec.d.ts → after-action/after-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-decorator.spec.d.ts → before-action/before-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-reflector.spec.d.ts → before-action/before-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-decorator.spec.d.ts → rest-action/rest-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-reflector.spec.d.ts → rest-action/rest-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-decorator.spec.d.ts → rest-controller/rest-controller-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-reflector.spec.d.ts → rest-controller/rest-controller-reflector.spec.d.ts} +0 -0
@@ -1,39 +1,39 @@
|
|
1
1
|
/* eslint mocha/no-sibling-hooks: 0 */
|
2
2
|
import {expect} from 'chai';
|
3
|
-
import {
|
4
|
-
import {
|
3
|
+
import {beforeAction} from './before-action-decorator.js';
|
4
|
+
import {BeforeActionReflector} from './before-action-reflector.js';
|
5
5
|
|
6
6
|
const MIDDLEWARE_1 = () => undefined;
|
7
7
|
const MIDDLEWARE_2 = () => undefined;
|
8
8
|
const MIDDLEWARE_3 = () => undefined;
|
9
9
|
|
10
|
-
describe('
|
10
|
+
describe('beforeAction', function () {
|
11
11
|
describe('class target', function () {
|
12
12
|
it('sets given middleware to the target metadata', function () {
|
13
|
-
@
|
13
|
+
@beforeAction(MIDDLEWARE_1)
|
14
14
|
class Target {
|
15
15
|
method() {}
|
16
16
|
}
|
17
|
-
const res =
|
17
|
+
const res = BeforeActionReflector.getMetadata(Target);
|
18
18
|
expect(res).to.be.eql([{middleware: MIDDLEWARE_1}]);
|
19
19
|
});
|
20
20
|
|
21
|
-
it('sets
|
22
|
-
@
|
21
|
+
it('sets multiple middlewares to the target metadata', function () {
|
22
|
+
@beforeAction([MIDDLEWARE_1, MIDDLEWARE_2])
|
23
23
|
class Target {
|
24
24
|
method() {}
|
25
25
|
}
|
26
|
-
const res =
|
26
|
+
const res = BeforeActionReflector.getMetadata(Target);
|
27
27
|
expect(res).to.be.eql([{middleware: [MIDDLEWARE_1, MIDDLEWARE_2]}]);
|
28
28
|
});
|
29
29
|
|
30
30
|
it('allows to use the decorator multiple times', function () {
|
31
|
-
@
|
32
|
-
@
|
31
|
+
@beforeAction(MIDDLEWARE_1)
|
32
|
+
@beforeAction([MIDDLEWARE_2, MIDDLEWARE_3])
|
33
33
|
class Target {
|
34
34
|
method() {}
|
35
35
|
}
|
36
|
-
const res =
|
36
|
+
const res = BeforeActionReflector.getMetadata(Target);
|
37
37
|
expect(res).to.be.eql([
|
38
38
|
{middleware: MIDDLEWARE_1},
|
39
39
|
{middleware: [MIDDLEWARE_2, MIDDLEWARE_3]},
|
@@ -44,10 +44,10 @@ describe('before', function () {
|
|
44
44
|
describe('method target', function () {
|
45
45
|
it('sets given middleware to the target metadata', function () {
|
46
46
|
class Target {
|
47
|
-
@
|
47
|
+
@beforeAction(MIDDLEWARE_1)
|
48
48
|
method() {}
|
49
49
|
}
|
50
|
-
const res =
|
50
|
+
const res = BeforeActionReflector.getMetadata(Target, 'method');
|
51
51
|
expect(res).to.be.eql([
|
52
52
|
{
|
53
53
|
propertyKey: 'method',
|
@@ -56,12 +56,12 @@ describe('before', function () {
|
|
56
56
|
]);
|
57
57
|
});
|
58
58
|
|
59
|
-
it('sets
|
59
|
+
it('sets multiple middlewares to the target metadata', function () {
|
60
60
|
class Target {
|
61
|
-
@
|
61
|
+
@beforeAction([MIDDLEWARE_1, MIDDLEWARE_2])
|
62
62
|
method() {}
|
63
63
|
}
|
64
|
-
const res =
|
64
|
+
const res = BeforeActionReflector.getMetadata(Target, 'method');
|
65
65
|
expect(res).to.be.eql([
|
66
66
|
{
|
67
67
|
propertyKey: 'method',
|
@@ -72,11 +72,11 @@ describe('before', function () {
|
|
72
72
|
|
73
73
|
it('allows to use the decorator multiple times', function () {
|
74
74
|
class Target {
|
75
|
-
@
|
76
|
-
@
|
75
|
+
@beforeAction(MIDDLEWARE_1)
|
76
|
+
@beforeAction([MIDDLEWARE_2, MIDDLEWARE_3])
|
77
77
|
method() {}
|
78
78
|
}
|
79
|
-
const res =
|
79
|
+
const res = BeforeActionReflector.getMetadata(Target, 'method');
|
80
80
|
expect(res).to.be.eql([
|
81
81
|
{
|
82
82
|
propertyKey: 'method',
|
package/src/decorators/{after/after-decorator.ts → before-action/before-action-decorator.ts}
RENAMED
@@ -1,17 +1,17 @@
|
|
1
1
|
import {Prototype} from '../../types.js';
|
2
2
|
import {Constructor} from '../../types.js';
|
3
|
-
import {AfterMetadata} from './after-metadata.js';
|
4
|
-
import {AfterReflector} from './after-reflector.js';
|
5
3
|
import {DecoratorTargetType} from '@e22m4u/ts-reflector';
|
6
4
|
import {getDecoratorTargetType} from '@e22m4u/ts-reflector';
|
5
|
+
import {BeforeActionMetadata} from './before-action-metadata.js';
|
6
|
+
import {BeforeActionReflector} from './before-action-reflector.js';
|
7
7
|
|
8
8
|
/**
|
9
|
-
*
|
9
|
+
* Before action decorator.
|
10
10
|
*
|
11
|
-
* @param
|
11
|
+
* @param middleware
|
12
12
|
*/
|
13
|
-
export function
|
14
|
-
middleware:
|
13
|
+
export function beforeAction<T extends object>(
|
14
|
+
middleware: BeforeActionMetadata['middleware'],
|
15
15
|
) {
|
16
16
|
return function (
|
17
17
|
target: Constructor<T> | Prototype<T>,
|
@@ -24,16 +24,16 @@ export function after<T extends object>(
|
|
24
24
|
descriptor,
|
25
25
|
);
|
26
26
|
if (decoratorType === DecoratorTargetType.CONSTRUCTOR) {
|
27
|
-
|
27
|
+
BeforeActionReflector.addMetadata({middleware}, target as Constructor<T>);
|
28
28
|
} else if (decoratorType === DecoratorTargetType.INSTANCE_METHOD) {
|
29
|
-
|
29
|
+
BeforeActionReflector.addMetadata(
|
30
30
|
{propertyKey, middleware},
|
31
31
|
target.constructor as Constructor<T>,
|
32
32
|
propertyKey,
|
33
33
|
);
|
34
34
|
} else {
|
35
35
|
throw new Error(
|
36
|
-
'@
|
36
|
+
'@beforeAction decorator is only supported on a class or an instance method.',
|
37
37
|
);
|
38
38
|
}
|
39
39
|
};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import {MetadataKey} from '@e22m4u/ts-reflector';
|
2
|
+
import {RoutePreHandler} from '@e22m4u/js-trie-router';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Before action metadata.
|
6
|
+
*/
|
7
|
+
export type BeforeActionMetadata = {
|
8
|
+
propertyKey?: string;
|
9
|
+
middleware: RoutePreHandler | RoutePreHandler[];
|
10
|
+
};
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Before action metadata key.
|
14
|
+
*/
|
15
|
+
export const BEFORE_ACTION_METADATA_KEY = new MetadataKey<
|
16
|
+
BeforeActionMetadata[]
|
17
|
+
>('beforeActionMetadataKey');
|
@@ -1,23 +1,26 @@
|
|
1
1
|
import {expect} from 'chai';
|
2
2
|
import {describe} from 'mocha';
|
3
3
|
import {Reflector} from '@e22m4u/ts-reflector';
|
4
|
-
import {
|
5
|
-
import {
|
4
|
+
import {BeforeActionReflector} from './before-action-reflector.js';
|
5
|
+
import {BEFORE_ACTION_METADATA_KEY} from './before-action-metadata.js';
|
6
6
|
|
7
7
|
const MIDDLEWARE_1 = () => undefined;
|
8
8
|
const MIDDLEWARE_2 = () => undefined;
|
9
9
|
const MIDDLEWARE_3 = () => undefined;
|
10
10
|
|
11
|
-
describe('
|
11
|
+
describe('BeforeActionReflector', function () {
|
12
12
|
describe('class target', function () {
|
13
13
|
describe('addMetadata', function () {
|
14
14
|
it('adds a given value to the target metadata', function () {
|
15
15
|
class Target {}
|
16
16
|
const md1 = {middleware: MIDDLEWARE_1};
|
17
17
|
const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
|
18
|
-
|
19
|
-
|
20
|
-
const res = Reflector.getOwnMetadata(
|
18
|
+
BeforeActionReflector.addMetadata(md1, Target);
|
19
|
+
BeforeActionReflector.addMetadata(md2, Target);
|
20
|
+
const res = Reflector.getOwnMetadata(
|
21
|
+
BEFORE_ACTION_METADATA_KEY,
|
22
|
+
Target,
|
23
|
+
);
|
21
24
|
expect(res).to.be.eql([md2, md1]);
|
22
25
|
});
|
23
26
|
});
|
@@ -25,7 +28,7 @@ describe('AfterReflector', function () {
|
|
25
28
|
describe('getMetadata', function () {
|
26
29
|
it('returns an empty array if no metadata', function () {
|
27
30
|
class Target {}
|
28
|
-
const res =
|
31
|
+
const res = BeforeActionReflector.getMetadata(Target);
|
29
32
|
expect(res).to.be.eql([]);
|
30
33
|
});
|
31
34
|
|
@@ -34,8 +37,8 @@ describe('AfterReflector', function () {
|
|
34
37
|
const md1 = {middleware: MIDDLEWARE_1};
|
35
38
|
const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
|
36
39
|
const mdArray = [md1, md2];
|
37
|
-
Reflector.defineMetadata(
|
38
|
-
const res =
|
40
|
+
Reflector.defineMetadata(BEFORE_ACTION_METADATA_KEY, mdArray, Target);
|
41
|
+
const res = BeforeActionReflector.getMetadata(Target);
|
39
42
|
expect(res).to.be.eql(mdArray);
|
40
43
|
});
|
41
44
|
});
|
@@ -47,10 +50,10 @@ describe('AfterReflector', function () {
|
|
47
50
|
class Target {}
|
48
51
|
const md1 = {middleware: MIDDLEWARE_1};
|
49
52
|
const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
|
50
|
-
|
51
|
-
|
53
|
+
BeforeActionReflector.addMetadata(md1, Target, 'prop');
|
54
|
+
BeforeActionReflector.addMetadata(md2, Target, 'prop');
|
52
55
|
const res = Reflector.getOwnMetadata(
|
53
|
-
|
56
|
+
BEFORE_ACTION_METADATA_KEY,
|
54
57
|
Target,
|
55
58
|
'prop',
|
56
59
|
);
|
@@ -61,7 +64,7 @@ describe('AfterReflector', function () {
|
|
61
64
|
describe('getMetadata', function () {
|
62
65
|
it('returns an empty array if no metadata', function () {
|
63
66
|
class Target {}
|
64
|
-
const res =
|
67
|
+
const res = BeforeActionReflector.getMetadata(Target, 'prop');
|
65
68
|
expect(res).to.be.eql([]);
|
66
69
|
});
|
67
70
|
|
@@ -70,8 +73,13 @@ describe('AfterReflector', function () {
|
|
70
73
|
const md1 = {middleware: MIDDLEWARE_1};
|
71
74
|
const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
|
72
75
|
const mdArray = [md1, md2];
|
73
|
-
Reflector.defineMetadata(
|
74
|
-
|
76
|
+
Reflector.defineMetadata(
|
77
|
+
BEFORE_ACTION_METADATA_KEY,
|
78
|
+
mdArray,
|
79
|
+
Target,
|
80
|
+
'prop',
|
81
|
+
);
|
82
|
+
const res = BeforeActionReflector.getMetadata(Target, 'prop');
|
75
83
|
expect(res).to.be.eql(mdArray);
|
76
84
|
});
|
77
85
|
});
|
@@ -82,10 +90,14 @@ describe('AfterReflector', function () {
|
|
82
90
|
class Target {}
|
83
91
|
const md1 = {middleware: MIDDLEWARE_1};
|
84
92
|
const md2 = {middleware: MIDDLEWARE_2};
|
85
|
-
|
86
|
-
|
87
|
-
const res1 = Reflector.getOwnMetadata(
|
88
|
-
const res2 = Reflector.getOwnMetadata(
|
93
|
+
BeforeActionReflector.addMetadata(md1, Target);
|
94
|
+
BeforeActionReflector.addMetadata(md2, Target, 'prop');
|
95
|
+
const res1 = Reflector.getOwnMetadata(BEFORE_ACTION_METADATA_KEY, Target);
|
96
|
+
const res2 = Reflector.getOwnMetadata(
|
97
|
+
BEFORE_ACTION_METADATA_KEY,
|
98
|
+
Target,
|
99
|
+
'prop',
|
100
|
+
);
|
89
101
|
expect(res1).to.be.eql([md1]);
|
90
102
|
expect(res2).to.be.eql([md2]);
|
91
103
|
});
|
@@ -96,10 +108,15 @@ describe('AfterReflector', function () {
|
|
96
108
|
class Target {}
|
97
109
|
const md1 = {middleware: MIDDLEWARE_1};
|
98
110
|
const md2 = {middleware: MIDDLEWARE_2};
|
99
|
-
Reflector.defineMetadata(
|
100
|
-
Reflector.defineMetadata(
|
101
|
-
|
102
|
-
|
111
|
+
Reflector.defineMetadata(BEFORE_ACTION_METADATA_KEY, [md1], Target);
|
112
|
+
Reflector.defineMetadata(
|
113
|
+
BEFORE_ACTION_METADATA_KEY,
|
114
|
+
[md2],
|
115
|
+
Target,
|
116
|
+
'prop',
|
117
|
+
);
|
118
|
+
const res1 = BeforeActionReflector.getMetadata(Target);
|
119
|
+
const res2 = BeforeActionReflector.getMetadata(Target, 'prop');
|
103
120
|
expect(res1).to.be.eql([md1]);
|
104
121
|
expect(res2).to.be.eql([md2]);
|
105
122
|
});
|
package/src/decorators/{after/after-reflector.ts → before-action/before-action-reflector.ts}
RENAMED
@@ -1,12 +1,12 @@
|
|
1
1
|
import {Constructor} from '../../types.js';
|
2
2
|
import {Reflector} from '@e22m4u/ts-reflector';
|
3
|
-
import {
|
4
|
-
import {
|
3
|
+
import {BeforeActionMetadata} from './before-action-metadata.js';
|
4
|
+
import {BEFORE_ACTION_METADATA_KEY} from './before-action-metadata.js';
|
5
5
|
|
6
6
|
/**
|
7
|
-
*
|
7
|
+
* Before action reflector.
|
8
8
|
*/
|
9
|
-
export class
|
9
|
+
export class BeforeActionReflector {
|
10
10
|
/**
|
11
11
|
* Set metadata.
|
12
12
|
*
|
@@ -15,14 +15,23 @@ export class AfterReflector {
|
|
15
15
|
* @param propertyKey
|
16
16
|
*/
|
17
17
|
static addMetadata(
|
18
|
-
metadata:
|
18
|
+
metadata: BeforeActionMetadata,
|
19
19
|
target: Constructor,
|
20
20
|
propertyKey?: string,
|
21
21
|
) {
|
22
22
|
const oldArray =
|
23
|
-
Reflector.getOwnMetadata(
|
23
|
+
Reflector.getOwnMetadata(
|
24
|
+
BEFORE_ACTION_METADATA_KEY,
|
25
|
+
target,
|
26
|
+
propertyKey,
|
27
|
+
) ?? [];
|
24
28
|
const newArray = [metadata, ...oldArray];
|
25
|
-
Reflector.defineMetadata(
|
29
|
+
Reflector.defineMetadata(
|
30
|
+
BEFORE_ACTION_METADATA_KEY,
|
31
|
+
newArray,
|
32
|
+
target,
|
33
|
+
propertyKey,
|
34
|
+
);
|
26
35
|
}
|
27
36
|
|
28
37
|
/**
|
@@ -34,9 +43,9 @@ export class AfterReflector {
|
|
34
43
|
static getMetadata(
|
35
44
|
target: Constructor,
|
36
45
|
propertyKey?: string,
|
37
|
-
):
|
46
|
+
): BeforeActionMetadata[] {
|
38
47
|
const metadata = Reflector.getOwnMetadata(
|
39
|
-
|
48
|
+
BEFORE_ACTION_METADATA_KEY,
|
40
49
|
target,
|
41
50
|
propertyKey,
|
42
51
|
);
|
package/src/decorators/index.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
export * from './
|
2
|
-
export * from './before/index.js';
|
3
|
-
export * from './action/index.js';
|
4
|
-
export * from './controller/index.js';
|
1
|
+
export * from './rest-action/index.js';
|
5
2
|
export * from './request-data/index.js';
|
3
|
+
export * from './after-action/index.js';
|
4
|
+
export * from './before-action/index.js';
|
5
|
+
export * from './rest-controller/index.js';
|
6
6
|
export * from './request-context/index.js';
|
@@ -1,9 +1,16 @@
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2
2
|
import {expect} from 'chai';
|
3
|
+
import {httpRequest} from './request-context-decorator.js';
|
4
|
+
import {httpResponse} from './request-context-decorator.js';
|
3
5
|
import {requestContext} from './request-context-decorator.js';
|
4
6
|
import {RequestContextReflector} from './request-context-reflector.js';
|
5
7
|
|
6
8
|
describe('requestContext', function () {
|
9
|
+
it('has aliases', function () {
|
10
|
+
expect(httpRequest).to.be.instanceOf(Function);
|
11
|
+
expect(httpResponse).to.be.instanceOf(Function);
|
12
|
+
});
|
13
|
+
|
7
14
|
it('does not require options', function () {
|
8
15
|
class Target {
|
9
16
|
method(
|
@@ -38,15 +38,15 @@ export function requestContext<T extends object>(
|
|
38
38
|
}
|
39
39
|
|
40
40
|
/**
|
41
|
-
*
|
41
|
+
* HttpRequest decorator.
|
42
42
|
*/
|
43
|
-
export function
|
43
|
+
export function httpRequest() {
|
44
44
|
return requestContext('req');
|
45
45
|
}
|
46
46
|
|
47
47
|
/**
|
48
|
-
*
|
48
|
+
* HttpResponse decorator.
|
49
49
|
*/
|
50
|
-
export function
|
50
|
+
export function httpResponse() {
|
51
51
|
return requestContext('res');
|
52
52
|
}
|
@@ -1,21 +1,34 @@
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2
2
|
import {expect} from 'chai';
|
3
3
|
import {DataType} from '@e22m4u/ts-data-schema';
|
4
|
-
import {body} from './request-data-decorator.js';
|
5
|
-
import {param} from './request-data-decorator.js';
|
6
|
-
import {query} from './request-data-decorator.js';
|
7
|
-
import {field} from './request-data-decorator.js';
|
8
|
-
import {cookie} from './request-data-decorator.js';
|
9
|
-
import {header} from './request-data-decorator.js';
|
10
|
-
import {params} from './request-data-decorator.js';
|
11
|
-
import {queries} from './request-data-decorator.js';
|
12
|
-
import {cookies} from './request-data-decorator.js';
|
13
|
-
import {headers} from './request-data-decorator.js';
|
14
4
|
import {requestData} from './request-data-decorator.js';
|
5
|
+
import {requestBody} from './request-data-decorator.js';
|
6
|
+
import {requestField} from './request-data-decorator.js';
|
7
|
+
import {requestParam} from './request-data-decorator.js';
|
8
|
+
import {requestQuery} from './request-data-decorator.js';
|
9
|
+
import {requestCookie} from './request-data-decorator.js';
|
10
|
+
import {requestHeader} from './request-data-decorator.js';
|
11
|
+
import {requestParams} from './request-data-decorator.js';
|
12
|
+
import {requestQueries} from './request-data-decorator.js';
|
13
|
+
import {requestHeaders} from './request-data-decorator.js';
|
14
|
+
import {requestCookies} from './request-data-decorator.js';
|
15
15
|
import {RequestDataSource} from './request-data-metadata.js';
|
16
16
|
import {RequestDataReflector} from './request-data-reflector.js';
|
17
17
|
|
18
18
|
describe('requestData', function () {
|
19
|
+
it('has aliases', function () {
|
20
|
+
expect(requestParams).to.be.instanceOf(Function);
|
21
|
+
expect(requestParam).to.be.instanceOf(Function);
|
22
|
+
expect(requestQueries).to.be.instanceOf(Function);
|
23
|
+
expect(requestQuery).to.be.instanceOf(Function);
|
24
|
+
expect(requestHeaders).to.be.instanceOf(Function);
|
25
|
+
expect(requestHeader).to.be.instanceOf(Function);
|
26
|
+
expect(requestCookies).to.be.instanceOf(Function);
|
27
|
+
expect(requestCookie).to.be.instanceOf(Function);
|
28
|
+
expect(requestField).to.be.instanceOf(Function);
|
29
|
+
expect(requestBody).to.be.instanceOf(Function);
|
30
|
+
});
|
31
|
+
|
19
32
|
it('sets given options to the target metadata', function () {
|
20
33
|
const options = {
|
21
34
|
source: RequestDataSource.BODY,
|
@@ -38,7 +51,7 @@ describe('requestData', function () {
|
|
38
51
|
it('sets metadata with specified source and schema', function () {
|
39
52
|
class Target {
|
40
53
|
myMethod(
|
41
|
-
@
|
54
|
+
@requestParams()
|
42
55
|
prop: unknown,
|
43
56
|
) {}
|
44
57
|
}
|
@@ -54,7 +67,7 @@ describe('requestData', function () {
|
|
54
67
|
it('sets metadata with specified source and schema', function () {
|
55
68
|
class Target {
|
56
69
|
myMethod(
|
57
|
-
@
|
70
|
+
@requestQueries()
|
58
71
|
prop: unknown,
|
59
72
|
) {}
|
60
73
|
}
|
@@ -70,7 +83,7 @@ describe('requestData', function () {
|
|
70
83
|
it('sets metadata with specified source and schema', function () {
|
71
84
|
class Target {
|
72
85
|
myMethod(
|
73
|
-
@
|
86
|
+
@requestHeaders()
|
74
87
|
prop: unknown,
|
75
88
|
) {}
|
76
89
|
}
|
@@ -86,7 +99,7 @@ describe('requestData', function () {
|
|
86
99
|
it('sets metadata with specified source and schema', function () {
|
87
100
|
class Target {
|
88
101
|
myMethod(
|
89
|
-
@
|
102
|
+
@requestCookies()
|
90
103
|
prop: unknown,
|
91
104
|
) {}
|
92
105
|
}
|
@@ -102,7 +115,7 @@ describe('requestData', function () {
|
|
102
115
|
it('sets metadata with specified source and schema', function () {
|
103
116
|
class Target {
|
104
117
|
myMethod(
|
105
|
-
@
|
118
|
+
@requestBody()
|
106
119
|
prop: unknown,
|
107
120
|
) {}
|
108
121
|
}
|
@@ -116,7 +129,7 @@ describe('requestData', function () {
|
|
116
129
|
it('sets a given DataType to the target metadata', function () {
|
117
130
|
class Target {
|
118
131
|
myMethod(
|
119
|
-
@
|
132
|
+
@requestBody(DataType.STRING)
|
120
133
|
prop: unknown,
|
121
134
|
) {}
|
122
135
|
}
|
@@ -131,7 +144,7 @@ describe('requestData', function () {
|
|
131
144
|
const schema = {type: DataType.STRING, required: true};
|
132
145
|
class Target {
|
133
146
|
myMethod(
|
134
|
-
@
|
147
|
+
@requestBody(schema)
|
135
148
|
prop: unknown,
|
136
149
|
) {}
|
137
150
|
}
|
@@ -149,7 +162,7 @@ describe('requestData', function () {
|
|
149
162
|
it('sets a given "propertyKey" to the target metadata', function () {
|
150
163
|
class Target {
|
151
164
|
myMethod(
|
152
|
-
@
|
165
|
+
@requestParam('myPropertyKey')
|
153
166
|
prop: unknown,
|
154
167
|
) {}
|
155
168
|
}
|
@@ -166,7 +179,7 @@ describe('requestData', function () {
|
|
166
179
|
const propertyType = DataType.STRING;
|
167
180
|
class Target {
|
168
181
|
myMethod(
|
169
|
-
@
|
182
|
+
@requestParam(propertyKey, propertyType)
|
170
183
|
prop: unknown,
|
171
184
|
) {}
|
172
185
|
}
|
@@ -193,7 +206,7 @@ describe('requestData', function () {
|
|
193
206
|
const propertyKey = 'myPropertyKey';
|
194
207
|
class Target {
|
195
208
|
myMethod(
|
196
|
-
@
|
209
|
+
@requestParam(propertyKey, schema)
|
197
210
|
prop: unknown,
|
198
211
|
) {}
|
199
212
|
}
|
@@ -215,7 +228,7 @@ describe('requestData', function () {
|
|
215
228
|
it('sets a given "propertyKey" to the target metadata', function () {
|
216
229
|
class Target {
|
217
230
|
myMethod(
|
218
|
-
@
|
231
|
+
@requestQuery('myPropertyKey')
|
219
232
|
prop: unknown,
|
220
233
|
) {}
|
221
234
|
}
|
@@ -232,7 +245,7 @@ describe('requestData', function () {
|
|
232
245
|
const propertyType = DataType.STRING;
|
233
246
|
class Target {
|
234
247
|
myMethod(
|
235
|
-
@
|
248
|
+
@requestQuery(propertyKey, propertyType)
|
236
249
|
prop: unknown,
|
237
250
|
) {}
|
238
251
|
}
|
@@ -259,7 +272,7 @@ describe('requestData', function () {
|
|
259
272
|
const propertyKey = 'myPropertyKey';
|
260
273
|
class Target {
|
261
274
|
myMethod(
|
262
|
-
@
|
275
|
+
@requestQuery(propertyKey, schema)
|
263
276
|
prop: unknown,
|
264
277
|
) {}
|
265
278
|
}
|
@@ -281,7 +294,7 @@ describe('requestData', function () {
|
|
281
294
|
it('sets a given "propertyKey" to the target metadata', function () {
|
282
295
|
class Target {
|
283
296
|
myMethod(
|
284
|
-
@
|
297
|
+
@requestHeader('myPropertyKey')
|
285
298
|
prop: unknown,
|
286
299
|
) {}
|
287
300
|
}
|
@@ -298,7 +311,7 @@ describe('requestData', function () {
|
|
298
311
|
const propertyType = DataType.STRING;
|
299
312
|
class Target {
|
300
313
|
myMethod(
|
301
|
-
@
|
314
|
+
@requestHeader(propertyKey, propertyType)
|
302
315
|
prop: unknown,
|
303
316
|
) {}
|
304
317
|
}
|
@@ -325,7 +338,7 @@ describe('requestData', function () {
|
|
325
338
|
const propertyKey = 'myPropertyKey';
|
326
339
|
class Target {
|
327
340
|
myMethod(
|
328
|
-
@
|
341
|
+
@requestHeader(propertyKey, schema)
|
329
342
|
prop: unknown,
|
330
343
|
) {}
|
331
344
|
}
|
@@ -347,7 +360,7 @@ describe('requestData', function () {
|
|
347
360
|
it('sets a given "propertyKey" to the target metadata', function () {
|
348
361
|
class Target {
|
349
362
|
myMethod(
|
350
|
-
@
|
363
|
+
@requestCookie('myPropertyKey')
|
351
364
|
prop: unknown,
|
352
365
|
) {}
|
353
366
|
}
|
@@ -364,7 +377,7 @@ describe('requestData', function () {
|
|
364
377
|
const propertyType = DataType.STRING;
|
365
378
|
class Target {
|
366
379
|
myMethod(
|
367
|
-
@
|
380
|
+
@requestCookie(propertyKey, propertyType)
|
368
381
|
prop: unknown,
|
369
382
|
) {}
|
370
383
|
}
|
@@ -391,7 +404,7 @@ describe('requestData', function () {
|
|
391
404
|
const propertyKey = 'myPropertyKey';
|
392
405
|
class Target {
|
393
406
|
myMethod(
|
394
|
-
@
|
407
|
+
@requestCookie(propertyKey, schema)
|
395
408
|
prop: unknown,
|
396
409
|
) {}
|
397
410
|
}
|
@@ -413,7 +426,7 @@ describe('requestData', function () {
|
|
413
426
|
it('sets a given "propertyKey" to the target metadata', function () {
|
414
427
|
class Target {
|
415
428
|
myMethod(
|
416
|
-
@
|
429
|
+
@requestField('myPropertyKey')
|
417
430
|
prop: unknown,
|
418
431
|
) {}
|
419
432
|
}
|
@@ -430,7 +443,7 @@ describe('requestData', function () {
|
|
430
443
|
const propertyType = DataType.STRING;
|
431
444
|
class Target {
|
432
445
|
myMethod(
|
433
|
-
@
|
446
|
+
@requestField(propertyKey, propertyType)
|
434
447
|
prop: unknown,
|
435
448
|
) {}
|
436
449
|
}
|
@@ -457,7 +470,7 @@ describe('requestData', function () {
|
|
457
470
|
const propertyKey = 'myPropertyKey';
|
458
471
|
class Target {
|
459
472
|
myMethod(
|
460
|
-
@
|
473
|
+
@requestField(propertyKey, schema)
|
461
474
|
prop: unknown,
|
462
475
|
) {}
|
463
476
|
}
|