@e22m4u/ts-rest-router 0.0.5 → 0.0.7
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-ru.md +204 -0
- package/README.md +206 -0
- package/dist/cjs/index.cjs +22 -31
- package/dist/esm/decorators/action/action-decorator.d.ts +7 -8
- package/dist/esm/decorators/action/action-decorator.js +1 -5
- package/dist/esm/decorators/action/action-decorator.spec.js +16 -16
- package/dist/esm/decorators/action/action-metadata.d.ts +0 -1
- package/dist/esm/decorators/controller/controller-decorator.js +1 -5
- package/dist/esm/decorators/controller/controller-decorator.spec.js +9 -15
- package/dist/esm/decorators/controller/controller-metadata.d.ts +0 -1
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +2 -3
- package/dist/esm/decorators/request-context/request-context-decorator.js +3 -6
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +2 -17
- package/dist/esm/decorators/request-context/request-context-metadata.d.ts +0 -1
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +6 -2
- package/dist/esm/decorators/request-data/request-data-decorator.js +16 -16
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +7 -5
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +0 -1
- package/package.json +18 -17
- package/src/decorators/action/action-decorator.spec.ts +16 -16
- package/src/decorators/action/action-decorator.ts +10 -12
- package/src/decorators/action/action-metadata.ts +0 -1
- package/src/decorators/controller/controller-decorator.spec.ts +11 -16
- package/src/decorators/controller/controller-decorator.ts +4 -5
- package/src/decorators/controller/controller-metadata.ts +0 -1
- package/src/decorators/request-context/request-context-decorator.spec.ts +2 -15
- package/src/decorators/request-context/request-context-decorator.ts +3 -8
- package/src/decorators/request-context/request-context-metadata.ts +0 -1
- package/src/decorators/request-data/request-data-decorator.spec.ts +7 -6
- package/src/decorators/request-data/request-data-decorator.ts +41 -16
- package/src/decorators/request-data/request-data-metadata.ts +0 -1
- package/tsconfig.json +2 -1
- package/dist/esm/controller-registry.spec.d.ts +0 -1
- package/dist/esm/controller-registry.spec.js +0 -719
- package/dist/esm/debuggable-service.spec.d.ts +0 -1
- package/dist/esm/debuggable-service.spec.js +0 -16
- package/dist/tsconfig.tsbuildinfo +0 -1
@@ -11,10 +11,6 @@ export function controller(options) {
|
|
11
11
|
const decoratorType = getDecoratorTargetType(target);
|
12
12
|
if (decoratorType !== DecoratorTargetType.CONSTRUCTOR)
|
13
13
|
throw new Error('@controller decorator is only supported on a class.');
|
14
|
-
|
15
|
-
...options,
|
16
|
-
className: target.name,
|
17
|
-
};
|
18
|
-
ControllerReflector.setMetadata(metadata, target);
|
14
|
+
ControllerReflector.setMetadata({ ...options, className: target.name }, target);
|
19
15
|
};
|
20
16
|
}
|
@@ -8,7 +8,7 @@ import { expect } from 'chai';
|
|
8
8
|
import { controller } from './controller-decorator.js';
|
9
9
|
import { ControllerReflector } from './controller-reflector.js';
|
10
10
|
describe('controller', function () {
|
11
|
-
it('does not
|
11
|
+
it('does not require options', function () {
|
12
12
|
let Target = class Target {
|
13
13
|
};
|
14
14
|
Target = __decorate([
|
@@ -18,27 +18,21 @@ describe('controller', function () {
|
|
18
18
|
expect(res).to.be.eql({ className: 'Target' });
|
19
19
|
});
|
20
20
|
it('sets given options to the target metadata', function () {
|
21
|
-
const
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
const options = {
|
22
|
+
path: 'myPath',
|
23
|
+
before: () => undefined,
|
24
|
+
after: () => undefined,
|
25
|
+
extraOption: 'extraOption',
|
26
|
+
};
|
25
27
|
let Target = class Target {
|
26
28
|
};
|
27
29
|
Target = __decorate([
|
28
|
-
controller(
|
29
|
-
path,
|
30
|
-
before,
|
31
|
-
after,
|
32
|
-
extraOption,
|
33
|
-
})
|
30
|
+
controller(options)
|
34
31
|
], Target);
|
35
32
|
const res = ControllerReflector.getMetadata(Target);
|
36
33
|
expect(res).to.be.eql({
|
34
|
+
...options,
|
37
35
|
className: 'Target',
|
38
|
-
path,
|
39
|
-
before,
|
40
|
-
after,
|
41
|
-
extraOption,
|
42
36
|
});
|
43
37
|
});
|
44
38
|
it('overrides given "className" option by the target class name', function () {
|
@@ -1,12 +1,11 @@
|
|
1
1
|
import { Prototype } from '../../types.js';
|
2
2
|
import { RequestContext } from '@e22m4u/js-trie-router';
|
3
|
-
import { RequestContextMetadata } from './request-context-metadata.js';
|
4
3
|
/**
|
5
4
|
* Request context decorator.
|
6
5
|
*
|
7
|
-
* @param
|
6
|
+
* @param propertyName
|
8
7
|
*/
|
9
|
-
export declare function requestContext<T extends object>(
|
8
|
+
export declare function requestContext<T extends object>(propertyName?: keyof RequestContext): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
|
10
9
|
/**
|
11
10
|
* Request decorator.
|
12
11
|
*/
|
@@ -4,18 +4,15 @@ import { RequestContextReflector } from './request-context-reflector.js';
|
|
4
4
|
/**
|
5
5
|
* Request context decorator.
|
6
6
|
*
|
7
|
-
* @param
|
7
|
+
* @param propertyName
|
8
8
|
*/
|
9
|
-
export function requestContext(
|
9
|
+
export function requestContext(propertyName) {
|
10
10
|
return function (target, propertyKey, indexOrDescriptor) {
|
11
11
|
const decoratorType = getDecoratorTargetType(target, propertyKey, indexOrDescriptor);
|
12
12
|
if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
|
13
13
|
throw new Error('@requestContext decorator is only supported ' +
|
14
14
|
'on an instance method parameter.');
|
15
|
-
|
16
|
-
? { property: propertyOrMetadata }
|
17
|
-
: propertyOrMetadata;
|
18
|
-
RequestContextReflector.setMetadata(metadata, target.constructor, indexOrDescriptor, propertyKey);
|
15
|
+
RequestContextReflector.setMetadata({ property: propertyName }, target.constructor, indexOrDescriptor, propertyKey);
|
19
16
|
};
|
20
17
|
}
|
21
18
|
/**
|
@@ -15,7 +15,7 @@ import { expect } from 'chai';
|
|
15
15
|
import { requestContext } from './request-context-decorator.js';
|
16
16
|
import { RequestContextReflector } from './request-context-reflector.js';
|
17
17
|
describe('requestContext', function () {
|
18
|
-
it('does not
|
18
|
+
it('does not require options', function () {
|
19
19
|
class Target {
|
20
20
|
method(prop) { }
|
21
21
|
}
|
@@ -28,7 +28,7 @@ describe('requestContext', function () {
|
|
28
28
|
const res = RequestContextReflector.getMetadata(Target, 'method');
|
29
29
|
expect(res.get(0)).to.be.eql({ property: undefined });
|
30
30
|
});
|
31
|
-
it('sets a given
|
31
|
+
it('sets a given property to the target metadata', function () {
|
32
32
|
class Target {
|
33
33
|
method(prop) { }
|
34
34
|
}
|
@@ -41,19 +41,4 @@ describe('requestContext', function () {
|
|
41
41
|
const res = RequestContextReflector.getMetadata(Target, 'method');
|
42
42
|
expect(res.get(0)).to.be.eql({ property: 'res' });
|
43
43
|
});
|
44
|
-
it('sets a given RequestContextMetadata as the target metadata', function () {
|
45
|
-
const property = 'res';
|
46
|
-
const customOption = 'customOption';
|
47
|
-
class Target {
|
48
|
-
method(prop) { }
|
49
|
-
}
|
50
|
-
__decorate([
|
51
|
-
__param(0, requestContext({ property, customOption })),
|
52
|
-
__metadata("design:type", Function),
|
53
|
-
__metadata("design:paramtypes", [Object]),
|
54
|
-
__metadata("design:returntype", void 0)
|
55
|
-
], Target.prototype, "method", null);
|
56
|
-
const res = RequestContextReflector.getMetadata(Target, 'method');
|
57
|
-
expect(res.get(0)).to.be.eql({ property, customOption });
|
58
|
-
});
|
59
44
|
});
|
@@ -2,12 +2,16 @@ import { Prototype } from '../../types.js';
|
|
2
2
|
import { DataType } from '@e22m4u/ts-data-schema';
|
3
3
|
import { DataSchema } from '@e22m4u/ts-data-schema';
|
4
4
|
import { RequestDataMetadata } from './request-data-metadata.js';
|
5
|
+
/**
|
6
|
+
* Request data options.
|
7
|
+
*/
|
8
|
+
export type RequestDataOptions = RequestDataMetadata;
|
5
9
|
/**
|
6
10
|
* Request data decorator.
|
7
11
|
*
|
8
|
-
* @param
|
12
|
+
* @param options
|
9
13
|
*/
|
10
|
-
export declare function requestData<T extends object>(
|
14
|
+
export declare function requestData<T extends object>(options: RequestDataOptions): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
|
11
15
|
/**
|
12
16
|
* Decorator aliases.
|
13
17
|
*/
|
@@ -6,34 +6,34 @@ import { RequestDataReflector } from './request-data-reflector.js';
|
|
6
6
|
/**
|
7
7
|
* Request data decorator.
|
8
8
|
*
|
9
|
-
* @param
|
9
|
+
* @param options
|
10
10
|
*/
|
11
|
-
export function requestData(
|
11
|
+
export function requestData(options) {
|
12
12
|
return function (target, propertyKey, indexOrDescriptor) {
|
13
13
|
const decoratorType = getDecoratorTargetType(target, propertyKey, indexOrDescriptor);
|
14
14
|
if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
|
15
15
|
throw new Error('@requestData decorator is only supported ' +
|
16
16
|
'on an instance method parameter.');
|
17
|
-
RequestDataReflector.setMetadata(
|
17
|
+
RequestDataReflector.setMetadata(options, target.constructor, indexOrDescriptor, propertyKey);
|
18
18
|
};
|
19
19
|
}
|
20
20
|
/**
|
21
|
-
* Create data decorator.
|
21
|
+
* Create request data decorator with source.
|
22
22
|
*
|
23
23
|
* @param source
|
24
24
|
*/
|
25
|
-
function
|
25
|
+
function createRequestDataDecoratorWithSource(source) {
|
26
26
|
return function () {
|
27
27
|
const schema = { type: DataType.OBJECT };
|
28
28
|
return requestData({ schema, source });
|
29
29
|
};
|
30
30
|
}
|
31
31
|
/**
|
32
|
-
* Create property decorator.
|
32
|
+
* Create request data property decorator with source.
|
33
33
|
*
|
34
34
|
* @param source
|
35
35
|
*/
|
36
|
-
function
|
36
|
+
function createRequestDataPropertyDecoratorWithSource(source) {
|
37
37
|
return function (propertyKey, schemaOrType) {
|
38
38
|
const properties = {};
|
39
39
|
const rootSchema = { type: DataType.OBJECT };
|
@@ -55,15 +55,15 @@ function createPropertyDecorator(source) {
|
|
55
55
|
/**
|
56
56
|
* Decorator aliases.
|
57
57
|
*/
|
58
|
-
export const params =
|
59
|
-
export const param =
|
60
|
-
export const queries =
|
61
|
-
export const query =
|
62
|
-
export const headers =
|
63
|
-
export const header =
|
64
|
-
export const cookies =
|
65
|
-
export const cookie =
|
66
|
-
export const bodyParam =
|
58
|
+
export const params = createRequestDataDecoratorWithSource(RequestDataSource.PARAMS);
|
59
|
+
export const param = createRequestDataPropertyDecoratorWithSource(RequestDataSource.PARAMS);
|
60
|
+
export const queries = createRequestDataDecoratorWithSource(RequestDataSource.QUERY);
|
61
|
+
export const query = createRequestDataPropertyDecoratorWithSource(RequestDataSource.QUERY);
|
62
|
+
export const headers = createRequestDataDecoratorWithSource(RequestDataSource.HEADERS);
|
63
|
+
export const header = createRequestDataPropertyDecoratorWithSource(RequestDataSource.HEADERS);
|
64
|
+
export const cookies = createRequestDataDecoratorWithSource(RequestDataSource.COOKIE);
|
65
|
+
export const cookie = createRequestDataPropertyDecoratorWithSource(RequestDataSource.COOKIE);
|
66
|
+
export const bodyParam = createRequestDataPropertyDecoratorWithSource(RequestDataSource.BODY);
|
67
67
|
/**
|
68
68
|
* Request body decorator.
|
69
69
|
*
|
@@ -27,22 +27,24 @@ import { requestData } from './request-data-decorator.js';
|
|
27
27
|
import { RequestDataSource } from './request-data-metadata.js';
|
28
28
|
import { RequestDataReflector } from './request-data-reflector.js';
|
29
29
|
describe('requestData', function () {
|
30
|
-
it('sets
|
31
|
-
const
|
32
|
-
source: RequestDataSource.
|
30
|
+
it('sets given options to the target metadata', function () {
|
31
|
+
const options = {
|
32
|
+
source: RequestDataSource.BODY,
|
33
|
+
schema: { type: DataType.STRING },
|
34
|
+
property: 'prop',
|
33
35
|
customOption: 'myOption',
|
34
36
|
};
|
35
37
|
class Target {
|
36
38
|
myMethod(prop) { }
|
37
39
|
}
|
38
40
|
__decorate([
|
39
|
-
__param(0, requestData(
|
41
|
+
__param(0, requestData(options)),
|
40
42
|
__metadata("design:type", Function),
|
41
43
|
__metadata("design:paramtypes", [Object]),
|
42
44
|
__metadata("design:returntype", void 0)
|
43
45
|
], Target.prototype, "myMethod", null);
|
44
46
|
const res = RequestDataReflector.getMetadata(Target, 'myMethod');
|
45
|
-
expect(res.get(0)).to.be.eql(
|
47
|
+
expect(res.get(0)).to.be.eql(options);
|
46
48
|
});
|
47
49
|
describe('request data by a given source', function () {
|
48
50
|
describe('params', function () {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@e22m4u/ts-rest-router",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.7",
|
4
4
|
"description": "Controllers-based REST router implementation for TypeScript",
|
5
5
|
"author": "e22m4u <e22m4u@yandex.ru>",
|
6
6
|
"keywords": [
|
@@ -31,6 +31,7 @@
|
|
31
31
|
"build:esm": "tsc --build",
|
32
32
|
"build:cjs": "rimraf ./dist/cjs && node --no-warnings=ExperimentalWarning build-cjs.js",
|
33
33
|
"build": "rimraf dist && npm run build:esm && npm run build:cjs",
|
34
|
+
"postbuild": "rimraf ./dist/**/*.spec.* --glob ./dist/tsconfig.tsbuildinfo",
|
34
35
|
"lint": "eslint ./src",
|
35
36
|
"lint:fix": "eslint ./src --fix",
|
36
37
|
"format": "prettier --write \"./src/**/*.ts\"",
|
@@ -39,37 +40,37 @@
|
|
39
40
|
"prepare": "husky"
|
40
41
|
},
|
41
42
|
"dependencies": {
|
42
|
-
"@e22m4u/js-format": "0.1.
|
43
|
-
"@e22m4u/js-service": "0.
|
44
|
-
"@e22m4u/js-trie-router": "0.0.
|
45
|
-
"@e22m4u/ts-data-schema": "0.0.
|
46
|
-
"@e22m4u/ts-reflector": "0.1.
|
47
|
-
"debug": "~4.
|
43
|
+
"@e22m4u/js-format": "~0.1.0",
|
44
|
+
"@e22m4u/js-service": "~0.2.0",
|
45
|
+
"@e22m4u/js-trie-router": "~0.0.1",
|
46
|
+
"@e22m4u/ts-data-schema": "~0.0.1",
|
47
|
+
"@e22m4u/ts-reflector": "~0.1.0",
|
48
|
+
"debug": "~4.4.0",
|
48
49
|
"http-errors": "~2.0.0"
|
49
50
|
},
|
50
51
|
"devDependencies": {
|
51
|
-
"@commitlint/cli": "~19.6.
|
52
|
+
"@commitlint/cli": "~19.6.1",
|
52
53
|
"@commitlint/config-conventional": "~19.6.0",
|
53
|
-
"@eslint/js": "~9.
|
54
|
+
"@eslint/js": "~9.17.0",
|
54
55
|
"@types/chai": "~5.0.1",
|
55
56
|
"@types/debug": "~4.1.12",
|
56
57
|
"@types/http-errors": "~2.0.4",
|
57
58
|
"@types/mocha": "~10.0.10",
|
58
|
-
"@types/node": "~22.
|
59
|
-
"c8": "~10.1.
|
59
|
+
"@types/node": "~22.10.2",
|
60
|
+
"c8": "~10.1.3",
|
60
61
|
"chai": "~5.1.2",
|
61
|
-
"esbuild": "~0.24.
|
62
|
-
"eslint": "~9.
|
62
|
+
"esbuild": "~0.24.2",
|
63
|
+
"eslint": "~9.17.0",
|
63
64
|
"eslint-config-prettier": "~9.1.0",
|
64
65
|
"eslint-plugin-chai-expect": "~3.1.0",
|
65
66
|
"eslint-plugin-mocha": "~10.5.0",
|
66
67
|
"husky": "~9.1.7",
|
67
|
-
"mocha": "~
|
68
|
-
"prettier": "~3.
|
68
|
+
"mocha": "~11.0.1",
|
69
|
+
"prettier": "~3.4.2",
|
69
70
|
"rimraf": "~6.0.1",
|
70
71
|
"tsx": "~4.19.2",
|
71
|
-
"typescript": "~5.
|
72
|
-
"typescript-eslint": "~8.
|
72
|
+
"typescript": "~5.7.2",
|
73
|
+
"typescript-eslint": "~8.18.2"
|
73
74
|
},
|
74
75
|
"license": "MIT"
|
75
76
|
}
|
@@ -5,38 +5,38 @@ import {ActionReflector} from './action-reflector.js';
|
|
5
5
|
|
6
6
|
describe('action', function () {
|
7
7
|
it('sets given options to the target metadata', function () {
|
8
|
-
const
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
const options = {
|
9
|
+
method: HttpMethod.GET,
|
10
|
+
path: 'myPath',
|
11
|
+
before: () => undefined,
|
12
|
+
after: () => undefined,
|
13
|
+
customOption: 'customOption',
|
14
|
+
};
|
13
15
|
class Target {
|
14
|
-
@action(
|
16
|
+
@action(options)
|
15
17
|
method() {}
|
16
18
|
}
|
17
19
|
const res = ActionReflector.getMetadata(Target);
|
18
20
|
expect(res.get('method')).to.be.eql({
|
21
|
+
...options,
|
19
22
|
propertyKey: 'method',
|
20
|
-
method,
|
21
|
-
path,
|
22
|
-
before,
|
23
|
-
after,
|
24
|
-
customOption,
|
25
23
|
});
|
26
24
|
});
|
27
25
|
|
28
26
|
it('overrides a given "propertyKey" option by the target method name', function () {
|
29
|
-
const
|
30
|
-
|
27
|
+
const options = {
|
28
|
+
propertyKey: 'myMethod',
|
29
|
+
method: HttpMethod.GET,
|
30
|
+
path: 'myPath',
|
31
|
+
};
|
31
32
|
class Target {
|
32
|
-
@action(
|
33
|
+
@action(options)
|
33
34
|
method() {}
|
34
35
|
}
|
35
36
|
const res = ActionReflector.getMetadata(Target);
|
36
37
|
expect(res.get('method')).to.be.eql({
|
38
|
+
...options,
|
37
39
|
propertyKey: 'method',
|
38
|
-
method,
|
39
|
-
path,
|
40
40
|
});
|
41
41
|
});
|
42
42
|
});
|
@@ -32,12 +32,8 @@ export function action<T extends object>(options: ActionOptions) {
|
|
32
32
|
throw new Error(
|
33
33
|
'@action decorator is only supported on an instance method.',
|
34
34
|
);
|
35
|
-
const metadata = {
|
36
|
-
...options,
|
37
|
-
propertyKey,
|
38
|
-
} as ActionMetadata;
|
39
35
|
ActionReflector.setMetadata(
|
40
|
-
|
36
|
+
{...options, propertyKey},
|
41
37
|
target.constructor as Constructor<T>,
|
42
38
|
propertyKey,
|
43
39
|
);
|
@@ -45,9 +41,11 @@ export function action<T extends object>(options: ActionOptions) {
|
|
45
41
|
}
|
46
42
|
|
47
43
|
/**
|
48
|
-
* Action
|
44
|
+
* Action method options.
|
49
45
|
*/
|
50
|
-
type
|
46
|
+
export type ActionMethodOptions = Flatten<
|
47
|
+
Omit<ActionOptions, 'method' | 'path'>
|
48
|
+
>;
|
51
49
|
|
52
50
|
/**
|
53
51
|
* Get decorator.
|
@@ -55,7 +53,7 @@ type ActionAliasOptions = Flatten<Omit<ActionOptions, 'method' | 'path'>>;
|
|
55
53
|
* @param path
|
56
54
|
* @param options
|
57
55
|
*/
|
58
|
-
export const get = (path: string, options?:
|
56
|
+
export const get = (path: string, options?: ActionMethodOptions) => {
|
59
57
|
return action({...options, path, method: HttpMethod.GET});
|
60
58
|
};
|
61
59
|
|
@@ -65,7 +63,7 @@ export const get = (path: string, options?: ActionAliasOptions) => {
|
|
65
63
|
* @param path
|
66
64
|
* @param options
|
67
65
|
*/
|
68
|
-
export const post = (path: string, options?:
|
66
|
+
export const post = (path: string, options?: ActionMethodOptions) => {
|
69
67
|
return action({...options, path, method: HttpMethod.POST});
|
70
68
|
};
|
71
69
|
|
@@ -75,7 +73,7 @@ export const post = (path: string, options?: ActionAliasOptions) => {
|
|
75
73
|
* @param path
|
76
74
|
* @param options
|
77
75
|
*/
|
78
|
-
export const put = (path: string, options?:
|
76
|
+
export const put = (path: string, options?: ActionMethodOptions) => {
|
79
77
|
return action({...options, path, method: HttpMethod.PUT});
|
80
78
|
};
|
81
79
|
|
@@ -85,7 +83,7 @@ export const put = (path: string, options?: ActionAliasOptions) => {
|
|
85
83
|
* @param path
|
86
84
|
* @param options
|
87
85
|
*/
|
88
|
-
export const patch = (path: string, options?:
|
86
|
+
export const patch = (path: string, options?: ActionMethodOptions) => {
|
89
87
|
return action({...options, path, method: HttpMethod.PATCH});
|
90
88
|
};
|
91
89
|
|
@@ -95,6 +93,6 @@ export const patch = (path: string, options?: ActionAliasOptions) => {
|
|
95
93
|
* @param path
|
96
94
|
* @param options
|
97
95
|
*/
|
98
|
-
export const del = (path: string, options?:
|
96
|
+
export const del = (path: string, options?: ActionMethodOptions) => {
|
99
97
|
return action({...options, path, method: HttpMethod.DELETE});
|
100
98
|
};
|
@@ -1,9 +1,10 @@
|
|
1
1
|
import {expect} from 'chai';
|
2
2
|
import {controller} from './controller-decorator.js';
|
3
|
+
import {ControllerOptions} from './controller-decorator.js';
|
3
4
|
import {ControllerReflector} from './controller-reflector.js';
|
4
5
|
|
5
6
|
describe('controller', function () {
|
6
|
-
it('does not
|
7
|
+
it('does not require options', function () {
|
7
8
|
@controller()
|
8
9
|
class Target {}
|
9
10
|
const res = ControllerReflector.getMetadata(Target);
|
@@ -11,29 +12,23 @@ describe('controller', function () {
|
|
11
12
|
});
|
12
13
|
|
13
14
|
it('sets given options to the target metadata', function () {
|
14
|
-
const
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
after,
|
22
|
-
extraOption,
|
23
|
-
})
|
15
|
+
const options = {
|
16
|
+
path: 'myPath',
|
17
|
+
before: () => undefined,
|
18
|
+
after: () => undefined,
|
19
|
+
extraOption: 'extraOption',
|
20
|
+
};
|
21
|
+
@controller(options)
|
24
22
|
class Target {}
|
25
23
|
const res = ControllerReflector.getMetadata(Target);
|
26
24
|
expect(res).to.be.eql({
|
25
|
+
...options,
|
27
26
|
className: 'Target',
|
28
|
-
path,
|
29
|
-
before,
|
30
|
-
after,
|
31
|
-
extraOption,
|
32
27
|
});
|
33
28
|
});
|
34
29
|
|
35
30
|
it('overrides given "className" option by the target class name', function () {
|
36
|
-
@controller({className: 'myClassName'})
|
31
|
+
@controller({className: 'myClassName'} as ControllerOptions)
|
37
32
|
class Target {}
|
38
33
|
const res = ControllerReflector.getMetadata(Target);
|
39
34
|
expect(res).to.be.eql({className: 'Target'});
|
@@ -20,10 +20,9 @@ export function controller<T extends object>(options?: ControllerOptions) {
|
|
20
20
|
const decoratorType = getDecoratorTargetType(target);
|
21
21
|
if (decoratorType !== DecoratorTargetType.CONSTRUCTOR)
|
22
22
|
throw new Error('@controller decorator is only supported on a class.');
|
23
|
-
|
24
|
-
...options,
|
25
|
-
|
26
|
-
|
27
|
-
ControllerReflector.setMetadata(metadata, target);
|
23
|
+
ControllerReflector.setMetadata(
|
24
|
+
{...options, className: target.name},
|
25
|
+
target,
|
26
|
+
);
|
28
27
|
};
|
29
28
|
}
|
@@ -4,7 +4,7 @@ import {requestContext} from './request-context-decorator.js';
|
|
4
4
|
import {RequestContextReflector} from './request-context-reflector.js';
|
5
5
|
|
6
6
|
describe('requestContext', function () {
|
7
|
-
it('does not
|
7
|
+
it('does not require options', function () {
|
8
8
|
class Target {
|
9
9
|
method(
|
10
10
|
@requestContext()
|
@@ -15,7 +15,7 @@ describe('requestContext', function () {
|
|
15
15
|
expect(res.get(0)).to.be.eql({property: undefined});
|
16
16
|
});
|
17
17
|
|
18
|
-
it('sets a given
|
18
|
+
it('sets a given property to the target metadata', function () {
|
19
19
|
class Target {
|
20
20
|
method(
|
21
21
|
@requestContext('res')
|
@@ -25,17 +25,4 @@ describe('requestContext', function () {
|
|
25
25
|
const res = RequestContextReflector.getMetadata(Target, 'method');
|
26
26
|
expect(res.get(0)).to.be.eql({property: 'res'});
|
27
27
|
});
|
28
|
-
|
29
|
-
it('sets a given RequestContextMetadata as the target metadata', function () {
|
30
|
-
const property = 'res';
|
31
|
-
const customOption = 'customOption';
|
32
|
-
class Target {
|
33
|
-
method(
|
34
|
-
@requestContext({property, customOption})
|
35
|
-
prop: unknown,
|
36
|
-
) {}
|
37
|
-
}
|
38
|
-
const res = RequestContextReflector.getMetadata(Target, 'method');
|
39
|
-
expect(res.get(0)).to.be.eql({property, customOption});
|
40
|
-
});
|
41
28
|
});
|
@@ -4,15 +4,14 @@ import {RequestContext} from '@e22m4u/js-trie-router';
|
|
4
4
|
import {DecoratorTargetType} from '@e22m4u/ts-reflector';
|
5
5
|
import {getDecoratorTargetType} from '@e22m4u/ts-reflector';
|
6
6
|
import {RequestContextReflector} from './request-context-reflector.js';
|
7
|
-
import {RequestContextMetadata} from './request-context-metadata.js';
|
8
7
|
|
9
8
|
/**
|
10
9
|
* Request context decorator.
|
11
10
|
*
|
12
|
-
* @param
|
11
|
+
* @param propertyName
|
13
12
|
*/
|
14
13
|
export function requestContext<T extends object>(
|
15
|
-
|
14
|
+
propertyName?: keyof RequestContext,
|
16
15
|
) {
|
17
16
|
return function (
|
18
17
|
target: Prototype<T>,
|
@@ -29,12 +28,8 @@ export function requestContext<T extends object>(
|
|
29
28
|
'@requestContext decorator is only supported ' +
|
30
29
|
'on an instance method parameter.',
|
31
30
|
);
|
32
|
-
const metadata =
|
33
|
-
typeof propertyOrMetadata !== 'object'
|
34
|
-
? {property: propertyOrMetadata}
|
35
|
-
: propertyOrMetadata;
|
36
31
|
RequestContextReflector.setMetadata(
|
37
|
-
|
32
|
+
{property: propertyName},
|
38
33
|
target.constructor as Constructor<T>,
|
39
34
|
indexOrDescriptor,
|
40
35
|
propertyKey,
|