@e22m4u/ts-rest-router 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/.c8rc +9 -0
- package/.commitlintrc +5 -0
- package/.editorconfig +13 -0
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +6 -0
- package/.mocharc.json +5 -0
- package/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/README-ru.md +41 -0
- package/README.md +41 -0
- package/build-cjs.js +16 -0
- package/dist/cjs/index.cjs +692 -0
- package/dist/esm/controller-registry.d.ts +65 -0
- package/dist/esm/controller-registry.js +281 -0
- package/dist/esm/controller-registry.spec.d.ts +1 -0
- package/dist/esm/controller-registry.spec.js +719 -0
- package/dist/esm/debuggable-service.d.ts +18 -0
- package/dist/esm/debuggable-service.js +23 -0
- package/dist/esm/debuggable-service.spec.d.ts +1 -0
- package/dist/esm/debuggable-service.spec.js +16 -0
- package/dist/esm/decorators/action/action-decorator.d.ts +53 -0
- package/dist/esm/decorators/action/action-decorator.js +66 -0
- package/dist/esm/decorators/action/action-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/action/action-decorator.spec.js +59 -0
- package/dist/esm/decorators/action/action-metadata.d.ts +23 -0
- package/dist/esm/decorators/action/action-metadata.js +5 -0
- package/dist/esm/decorators/action/action-reflector.d.ts +22 -0
- package/dist/esm/decorators/action/action-reflector.js +29 -0
- package/dist/esm/decorators/action/action-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/action/action-reflector.spec.js +84 -0
- package/dist/esm/decorators/action/index.d.ts +3 -0
- package/dist/esm/decorators/action/index.js +3 -0
- package/dist/esm/decorators/controller/controller-decorator.d.ts +13 -0
- package/dist/esm/decorators/controller/controller-decorator.js +20 -0
- package/dist/esm/decorators/controller/controller-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/controller/controller-decorator.spec.js +53 -0
- package/dist/esm/decorators/controller/controller-metadata.d.ts +17 -0
- package/dist/esm/decorators/controller/controller-metadata.js +5 -0
- package/dist/esm/decorators/controller/controller-reflector.d.ts +20 -0
- package/dist/esm/decorators/controller/controller-reflector.js +24 -0
- package/dist/esm/decorators/controller/controller-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/controller/controller-reflector.spec.js +45 -0
- package/dist/esm/decorators/controller/index.d.ts +3 -0
- package/dist/esm/decorators/controller/index.js +3 -0
- package/dist/esm/decorators/index.d.ts +4 -0
- package/dist/esm/decorators/index.js +4 -0
- package/dist/esm/decorators/request-context/index.d.ts +3 -0
- package/dist/esm/decorators/request-context/index.js +3 -0
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +17 -0
- package/dist/esm/decorators/request-context/request-context-decorator.js +32 -0
- package/dist/esm/decorators/request-context/request-context-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +59 -0
- package/dist/esm/decorators/request-context/request-context-metadata.d.ts +17 -0
- package/dist/esm/decorators/request-context/request-context-metadata.js +5 -0
- package/dist/esm/decorators/request-context/request-context-reflector.d.ts +24 -0
- package/dist/esm/decorators/request-context/request-context-reflector.js +31 -0
- package/dist/esm/decorators/request-context/request-context-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/request-context/request-context-reflector.spec.js +59 -0
- package/dist/esm/decorators/request-data/index.d.ts +3 -0
- package/dist/esm/decorators/request-data/index.js +3 -0
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +28 -0
- package/dist/esm/decorators/request-data/request-data-decorator.js +84 -0
- package/dist/esm/decorators/request-data/request-data-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +534 -0
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +29 -0
- package/dist/esm/decorators/request-data/request-data-metadata.js +16 -0
- package/dist/esm/decorators/request-data/request-data-reflector.d.ts +24 -0
- package/dist/esm/decorators/request-data/request-data-reflector.js +31 -0
- package/dist/esm/decorators/request-data/request-data-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/request-data/request-data-reflector.spec.js +60 -0
- package/dist/esm/errors/index.d.ts +1 -0
- package/dist/esm/errors/index.js +1 -0
- package/dist/esm/errors/not-a-controller-error.d.ts +12 -0
- package/dist/esm/errors/not-a-controller-error.js +14 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/rest-router.d.ts +19 -0
- package/dist/esm/rest-router.js +24 -0
- package/dist/esm/types.d.ts +57 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/utils/capitalize.d.ts +6 -0
- package/dist/esm/utils/capitalize.js +8 -0
- package/dist/esm/utils/capitalize.spec.d.ts +1 -0
- package/dist/esm/utils/capitalize.spec.js +8 -0
- package/dist/esm/utils/create-debugger.d.ts +11 -0
- package/dist/esm/utils/create-debugger.js +15 -0
- package/dist/esm/utils/create-debugger.spec.d.ts +1 -0
- package/dist/esm/utils/create-debugger.spec.js +8 -0
- package/dist/esm/utils/create-error.d.ts +10 -0
- package/dist/esm/utils/create-error.js +13 -0
- package/dist/esm/utils/create-error.spec.d.ts +1 -0
- package/dist/esm/utils/create-error.spec.js +8 -0
- package/dist/esm/utils/index.d.ts +4 -0
- package/dist/esm/utils/index.js +4 -0
- package/dist/esm/utils/to-camel-case.d.ts +6 -0
- package/dist/esm/utils/to-camel-case.js +11 -0
- package/dist/esm/utils/to-camel-case.spec.d.ts +1 -0
- package/dist/esm/utils/to-camel-case.spec.js +10 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/eslint.config.js +43 -0
- package/package.json +74 -0
- package/src/controller-registry.spec.ts +592 -0
- package/src/controller-registry.ts +355 -0
- package/src/debuggable-service.spec.ts +18 -0
- package/src/debuggable-service.ts +27 -0
- package/src/decorators/action/action-decorator.spec.ts +42 -0
- package/src/decorators/action/action-decorator.ts +100 -0
- package/src/decorators/action/action-metadata.ts +28 -0
- package/src/decorators/action/action-reflector.spec.ts +84 -0
- package/src/decorators/action/action-reflector.ts +38 -0
- package/src/decorators/action/index.ts +3 -0
- package/src/decorators/controller/controller-decorator.spec.ts +41 -0
- package/src/decorators/controller/controller-decorator.ts +29 -0
- package/src/decorators/controller/controller-metadata.ts +21 -0
- package/src/decorators/controller/controller-reflector.spec.ts +45 -0
- package/src/decorators/controller/controller-reflector.ts +28 -0
- package/src/decorators/controller/index.ts +3 -0
- package/src/decorators/index.ts +4 -0
- package/src/decorators/request-context/index.ts +3 -0
- package/src/decorators/request-context/request-context-decorator.spec.ts +41 -0
- package/src/decorators/request-context/request-context-decorator.ts +57 -0
- package/src/decorators/request-context/request-context-metadata.ts +21 -0
- package/src/decorators/request-context/request-context-reflector.spec.ts +77 -0
- package/src/decorators/request-context/request-context-reflector.ts +57 -0
- package/src/decorators/request-data/index.ts +3 -0
- package/src/decorators/request-data/request-data-decorator.spec.ts +477 -0
- package/src/decorators/request-data/request-data-decorator.ts +106 -0
- package/src/decorators/request-data/request-data-metadata.ts +34 -0
- package/src/decorators/request-data/request-data-reflector.spec.ts +78 -0
- package/src/decorators/request-data/request-data-reflector.ts +57 -0
- package/src/errors/index.ts +1 -0
- package/src/errors/not-a-controller-error.ts +15 -0
- package/src/index.ts +5 -0
- package/src/rest-router.ts +31 -0
- package/src/types.ts +59 -0
- package/src/utils/capitalize.spec.ts +9 -0
- package/src/utils/capitalize.ts +8 -0
- package/src/utils/create-debugger.spec.ts +9 -0
- package/src/utils/create-debugger.ts +21 -0
- package/src/utils/create-error.spec.ts +9 -0
- package/src/utils/create-error.ts +19 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/to-camel-case.spec.ts +11 -0
- package/src/utils/to-camel-case.ts +11 -0
- package/tsconfig.json +17 -0
@@ -0,0 +1,41 @@
|
|
1
|
+
import {expect} from 'chai';
|
2
|
+
import {controller} from './controller-decorator.js';
|
3
|
+
import {ControllerReflector} from './controller-reflector.js';
|
4
|
+
|
5
|
+
describe('controller', function () {
|
6
|
+
it('does not requires options', function () {
|
7
|
+
@controller()
|
8
|
+
class Target {}
|
9
|
+
const res = ControllerReflector.getMetadata(Target);
|
10
|
+
expect(res).to.be.eql({className: 'Target'});
|
11
|
+
});
|
12
|
+
|
13
|
+
it('sets given options to the target metadata', function () {
|
14
|
+
const path = 'myPath';
|
15
|
+
const before = () => undefined;
|
16
|
+
const after = () => undefined;
|
17
|
+
const extraOption = 'extraOption';
|
18
|
+
@controller({
|
19
|
+
path,
|
20
|
+
before,
|
21
|
+
after,
|
22
|
+
extraOption,
|
23
|
+
})
|
24
|
+
class Target {}
|
25
|
+
const res = ControllerReflector.getMetadata(Target);
|
26
|
+
expect(res).to.be.eql({
|
27
|
+
className: 'Target',
|
28
|
+
path,
|
29
|
+
before,
|
30
|
+
after,
|
31
|
+
extraOption,
|
32
|
+
});
|
33
|
+
});
|
34
|
+
|
35
|
+
it('overrides given "className" option by the target class name', function () {
|
36
|
+
@controller({className: 'myClassName'})
|
37
|
+
class Target {}
|
38
|
+
const res = ControllerReflector.getMetadata(Target);
|
39
|
+
expect(res).to.be.eql({className: 'Target'});
|
40
|
+
});
|
41
|
+
});
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import {Flatten} from '../../types.js';
|
2
|
+
import {Constructor} from '../../types.js';
|
3
|
+
import {DecoratorTargetType} from '@e22m4u/ts-reflector';
|
4
|
+
import {ControllerMetadata} from './controller-metadata.js';
|
5
|
+
import {getDecoratorTargetType} from '@e22m4u/ts-reflector';
|
6
|
+
import {ControllerReflector} from './controller-reflector.js';
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Controller options.
|
10
|
+
*/
|
11
|
+
export type ControllerOptions = Flatten<Omit<ControllerMetadata, 'className'>>;
|
12
|
+
|
13
|
+
/**
|
14
|
+
* Controller decorator.
|
15
|
+
*
|
16
|
+
* @param options
|
17
|
+
*/
|
18
|
+
export function controller<T extends object>(options?: ControllerOptions) {
|
19
|
+
return function (target: Constructor<T>) {
|
20
|
+
const decoratorType = getDecoratorTargetType(target);
|
21
|
+
if (decoratorType !== DecoratorTargetType.CONSTRUCTOR)
|
22
|
+
throw new Error('@controller decorator is only supported on a class.');
|
23
|
+
const metadata = {
|
24
|
+
...options,
|
25
|
+
className: target.name,
|
26
|
+
};
|
27
|
+
ControllerReflector.setMetadata(metadata, target);
|
28
|
+
};
|
29
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import {RoutePreHandler} from '@e22m4u/js-trie-router';
|
2
|
+
import {RoutePostHandler} from '@e22m4u/js-trie-router';
|
3
|
+
import {MetadataKey} from '@e22m4u/ts-reflector';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Controller metadata.
|
7
|
+
*/
|
8
|
+
export type ControllerMetadata = {
|
9
|
+
className: string;
|
10
|
+
path?: string;
|
11
|
+
before?: RoutePreHandler | RoutePreHandler[];
|
12
|
+
after?: RoutePostHandler | RoutePostHandler[];
|
13
|
+
[option: string]: unknown | undefined;
|
14
|
+
};
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Controller metadata key.
|
18
|
+
*/
|
19
|
+
export const CONTROLLER_METADATA_KEY = new MetadataKey<ControllerMetadata>(
|
20
|
+
'controllerMetadataKey',
|
21
|
+
);
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import {expect} from 'chai';
|
2
|
+
import {describe} from 'mocha';
|
3
|
+
import {Reflector} from '@e22m4u/ts-reflector';
|
4
|
+
import {ControllerReflector} from './controller-reflector.js';
|
5
|
+
import {CONTROLLER_METADATA_KEY} from './controller-metadata.js';
|
6
|
+
|
7
|
+
describe('ControllerReflector', function () {
|
8
|
+
describe('setMetadata', function () {
|
9
|
+
it('sets a given value as target metadata', function () {
|
10
|
+
class Target {}
|
11
|
+
const md = {className: 'Target'};
|
12
|
+
ControllerReflector.setMetadata(md, Target);
|
13
|
+
const res = Reflector.getOwnMetadata(CONTROLLER_METADATA_KEY, Target);
|
14
|
+
expect(res).to.be.eq(md);
|
15
|
+
});
|
16
|
+
|
17
|
+
it('overrides existing metadata', function () {
|
18
|
+
class Target {}
|
19
|
+
const md1 = {className: 'Target', path: 'path1'};
|
20
|
+
const md2 = {className: 'Target', path: 'path2'};
|
21
|
+
ControllerReflector.setMetadata(md1, Target);
|
22
|
+
const res1 = Reflector.getOwnMetadata(CONTROLLER_METADATA_KEY, Target);
|
23
|
+
expect(res1).to.be.eq(md1);
|
24
|
+
ControllerReflector.setMetadata(md2, Target);
|
25
|
+
const res2 = Reflector.getOwnMetadata(CONTROLLER_METADATA_KEY, Target);
|
26
|
+
expect(res2).to.be.eq(md2);
|
27
|
+
});
|
28
|
+
});
|
29
|
+
|
30
|
+
describe('getMetadata', function () {
|
31
|
+
it('returns an existing metadata of the target', function () {
|
32
|
+
class Target {}
|
33
|
+
const md = {className: 'Target'};
|
34
|
+
Reflector.defineMetadata(CONTROLLER_METADATA_KEY, md, Target);
|
35
|
+
const res = ControllerReflector.getMetadata(Target);
|
36
|
+
expect(res).to.be.eq(md);
|
37
|
+
});
|
38
|
+
|
39
|
+
it('returns undefined if no metadata', function () {
|
40
|
+
class Target {}
|
41
|
+
const res = ControllerReflector.getMetadata(Target);
|
42
|
+
expect(res).to.be.undefined;
|
43
|
+
});
|
44
|
+
});
|
45
|
+
});
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import {Constructor} from '../../types.js';
|
2
|
+
import {Reflector} from '@e22m4u/ts-reflector';
|
3
|
+
import {ControllerMetadata} from './controller-metadata.js';
|
4
|
+
import {CONTROLLER_METADATA_KEY} from './controller-metadata.js';
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Controller reflector.
|
8
|
+
*/
|
9
|
+
export class ControllerReflector {
|
10
|
+
/**
|
11
|
+
* Set metadata.
|
12
|
+
*
|
13
|
+
* @param metadata
|
14
|
+
* @param target
|
15
|
+
*/
|
16
|
+
static setMetadata(metadata: ControllerMetadata, target: Constructor) {
|
17
|
+
return Reflector.defineMetadata(CONTROLLER_METADATA_KEY, metadata, target);
|
18
|
+
}
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Get metadata.
|
22
|
+
*
|
23
|
+
* @param target
|
24
|
+
*/
|
25
|
+
static getMetadata(target: Constructor): ControllerMetadata | undefined {
|
26
|
+
return Reflector.getOwnMetadata(CONTROLLER_METADATA_KEY, target);
|
27
|
+
}
|
28
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2
|
+
import {expect} from 'chai';
|
3
|
+
import {requestContext} from './request-context-decorator.js';
|
4
|
+
import {RequestContextReflector} from './request-context-reflector.js';
|
5
|
+
|
6
|
+
describe('requestContext', function () {
|
7
|
+
it('does not requires options', function () {
|
8
|
+
class Target {
|
9
|
+
method(
|
10
|
+
@requestContext()
|
11
|
+
prop: unknown,
|
12
|
+
) {}
|
13
|
+
}
|
14
|
+
const res = RequestContextReflector.getMetadata(Target, 'method');
|
15
|
+
expect(res.get(0)).to.be.eql({property: undefined});
|
16
|
+
});
|
17
|
+
|
18
|
+
it('sets a given key of RequestContext as "propertyKey" of the target metadata', function () {
|
19
|
+
class Target {
|
20
|
+
method(
|
21
|
+
@requestContext('res')
|
22
|
+
prop: unknown,
|
23
|
+
) {}
|
24
|
+
}
|
25
|
+
const res = RequestContextReflector.getMetadata(Target, 'method');
|
26
|
+
expect(res.get(0)).to.be.eql({property: 'res'});
|
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
|
+
});
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import {Prototype} from '../../types.js';
|
2
|
+
import {Constructor} from '../../types.js';
|
3
|
+
import {RequestContext} from '@e22m4u/js-trie-router';
|
4
|
+
import {DecoratorTargetType} from '@e22m4u/ts-reflector';
|
5
|
+
import {getDecoratorTargetType} from '@e22m4u/ts-reflector';
|
6
|
+
import {RequestContextReflector} from './request-context-reflector.js';
|
7
|
+
import {RequestContextMetadata} from './request-context-metadata.js';
|
8
|
+
|
9
|
+
/**
|
10
|
+
* Request context decorator.
|
11
|
+
*
|
12
|
+
* @param propertyOrMetadata
|
13
|
+
*/
|
14
|
+
export function requestContext<T extends object>(
|
15
|
+
propertyOrMetadata?: keyof RequestContext | RequestContextMetadata,
|
16
|
+
) {
|
17
|
+
return function (
|
18
|
+
target: Prototype<T>,
|
19
|
+
propertyKey: string,
|
20
|
+
indexOrDescriptor: number,
|
21
|
+
) {
|
22
|
+
const decoratorType = getDecoratorTargetType(
|
23
|
+
target,
|
24
|
+
propertyKey,
|
25
|
+
indexOrDescriptor,
|
26
|
+
);
|
27
|
+
if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
|
28
|
+
throw new Error(
|
29
|
+
'@requestContext decorator is only supported ' +
|
30
|
+
'on an instance method parameter.',
|
31
|
+
);
|
32
|
+
const metadata =
|
33
|
+
typeof propertyOrMetadata !== 'object'
|
34
|
+
? {property: propertyOrMetadata}
|
35
|
+
: propertyOrMetadata;
|
36
|
+
RequestContextReflector.setMetadata(
|
37
|
+
metadata,
|
38
|
+
target.constructor as Constructor<T>,
|
39
|
+
indexOrDescriptor,
|
40
|
+
propertyKey,
|
41
|
+
);
|
42
|
+
};
|
43
|
+
}
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Request decorator.
|
47
|
+
*/
|
48
|
+
export function request() {
|
49
|
+
return requestContext('req');
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Response decorator.
|
54
|
+
*/
|
55
|
+
export function response() {
|
56
|
+
return requestContext('res');
|
57
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import {MetadataKey} from '@e22m4u/ts-reflector';
|
2
|
+
import {RequestContext} from '@e22m4u/js-trie-router';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Request context metadata.
|
6
|
+
*/
|
7
|
+
export type RequestContextMetadata = {
|
8
|
+
property?: keyof RequestContext;
|
9
|
+
[option: string]: unknown | undefined;
|
10
|
+
};
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Request context metadata map.
|
14
|
+
*/
|
15
|
+
export type RequestContextMetadataMap = Map<number, RequestContextMetadata>;
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Request context metadata key.
|
19
|
+
*/
|
20
|
+
export const REQUEST_CONTEXT_METADATA_KEY =
|
21
|
+
new MetadataKey<RequestContextMetadataMap>('requestContextMetadataKey');
|
@@ -0,0 +1,77 @@
|
|
1
|
+
import {expect} from 'chai';
|
2
|
+
import {describe} from 'mocha';
|
3
|
+
import {Reflector} from '@e22m4u/ts-reflector';
|
4
|
+
import {RequestContextMetadata} from './request-context-metadata.js';
|
5
|
+
import {RequestContextReflector} from './request-context-reflector.js';
|
6
|
+
import {REQUEST_CONTEXT_METADATA_KEY} from './request-context-metadata.js';
|
7
|
+
|
8
|
+
describe('RequestContextReflector', function () {
|
9
|
+
describe('setMetadata', function () {
|
10
|
+
it('sets a given value as target metadata', function () {
|
11
|
+
class Target {}
|
12
|
+
const md1 = {property: 'req' as const};
|
13
|
+
const md2 = {property: 'res' as const};
|
14
|
+
RequestContextReflector.setMetadata(md1, Target, 0, 'propertyKey');
|
15
|
+
RequestContextReflector.setMetadata(md2, Target, 1, 'propertyKey');
|
16
|
+
const res = Reflector.getOwnMetadata(
|
17
|
+
REQUEST_CONTEXT_METADATA_KEY,
|
18
|
+
Target,
|
19
|
+
'propertyKey',
|
20
|
+
);
|
21
|
+
expect(res).to.be.instanceof(Map);
|
22
|
+
expect(res!.get(0)).to.be.eq(md1);
|
23
|
+
expect(res!.get(1)).to.be.eq(md2);
|
24
|
+
});
|
25
|
+
|
26
|
+
it('overrides existing metadata', function () {
|
27
|
+
class Target {}
|
28
|
+
const md1 = {property: 'req' as const};
|
29
|
+
const md2 = {property: 'res' as const};
|
30
|
+
RequestContextReflector.setMetadata(md1, Target, 0, 'propertyKey');
|
31
|
+
const res1 = Reflector.getOwnMetadata(
|
32
|
+
REQUEST_CONTEXT_METADATA_KEY,
|
33
|
+
Target,
|
34
|
+
'propertyKey',
|
35
|
+
);
|
36
|
+
expect(res1).to.be.instanceof(Map);
|
37
|
+
expect(res1!.get(0)).to.be.eq(md1);
|
38
|
+
RequestContextReflector.setMetadata(md2, Target, 0, 'propertyKey');
|
39
|
+
const res2 = Reflector.getOwnMetadata(
|
40
|
+
REQUEST_CONTEXT_METADATA_KEY,
|
41
|
+
Target,
|
42
|
+
'propertyKey',
|
43
|
+
);
|
44
|
+
expect(res2).to.be.instanceof(Map);
|
45
|
+
expect(res2!.get(0)).to.be.eq(md2);
|
46
|
+
});
|
47
|
+
});
|
48
|
+
|
49
|
+
describe('getMetadata', function () {
|
50
|
+
it('returns an existing metadata of the target', function () {
|
51
|
+
class Target {}
|
52
|
+
const md1 = {property: 'req' as const};
|
53
|
+
const md2 = {property: 'res' as const};
|
54
|
+
const mdMap = new Map<number, RequestContextMetadata>([
|
55
|
+
[0, md1],
|
56
|
+
[1, md2],
|
57
|
+
]);
|
58
|
+
Reflector.defineMetadata(
|
59
|
+
REQUEST_CONTEXT_METADATA_KEY,
|
60
|
+
mdMap,
|
61
|
+
Target,
|
62
|
+
'propertyKey',
|
63
|
+
);
|
64
|
+
const res = RequestContextReflector.getMetadata(Target, 'propertyKey');
|
65
|
+
expect(res).to.be.instanceof(Map);
|
66
|
+
expect(res!.get(0)).to.be.eq(md1);
|
67
|
+
expect(res!.get(1)).to.be.eq(md2);
|
68
|
+
});
|
69
|
+
|
70
|
+
it('returns an empty map if no metadata', function () {
|
71
|
+
class Target {}
|
72
|
+
const res = RequestContextReflector.getMetadata(Target, 'propertyKey');
|
73
|
+
expect(res).to.be.instanceof(Map);
|
74
|
+
expect(res).to.be.empty;
|
75
|
+
});
|
76
|
+
});
|
77
|
+
});
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import {Constructor} from '../../types.js';
|
2
|
+
import {Reflector} from '@e22m4u/ts-reflector';
|
3
|
+
import {RequestContextMetadata} from './request-context-metadata.js';
|
4
|
+
import {RequestContextMetadataMap} from './request-context-metadata.js';
|
5
|
+
import {REQUEST_CONTEXT_METADATA_KEY} from './request-context-metadata.js';
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Request context reflector.
|
9
|
+
*/
|
10
|
+
export class RequestContextReflector {
|
11
|
+
/**
|
12
|
+
* Set metadata.
|
13
|
+
*
|
14
|
+
* @param metadata
|
15
|
+
* @param target
|
16
|
+
* @param index
|
17
|
+
* @param propertyKey
|
18
|
+
*/
|
19
|
+
static setMetadata(
|
20
|
+
metadata: RequestContextMetadata,
|
21
|
+
target: Constructor,
|
22
|
+
index: number,
|
23
|
+
propertyKey: string,
|
24
|
+
) {
|
25
|
+
const oldMap = Reflector.getOwnMetadata(
|
26
|
+
REQUEST_CONTEXT_METADATA_KEY,
|
27
|
+
target,
|
28
|
+
propertyKey,
|
29
|
+
);
|
30
|
+
const newMap = new Map(oldMap);
|
31
|
+
newMap.set(index, metadata);
|
32
|
+
Reflector.defineMetadata(
|
33
|
+
REQUEST_CONTEXT_METADATA_KEY,
|
34
|
+
newMap,
|
35
|
+
target,
|
36
|
+
propertyKey,
|
37
|
+
);
|
38
|
+
}
|
39
|
+
|
40
|
+
/**
|
41
|
+
* Get metadata.
|
42
|
+
*
|
43
|
+
* @param target
|
44
|
+
* @param propertyKey
|
45
|
+
*/
|
46
|
+
static getMetadata(
|
47
|
+
target: Constructor,
|
48
|
+
propertyKey: string,
|
49
|
+
): RequestContextMetadataMap {
|
50
|
+
const metadata = Reflector.getOwnMetadata(
|
51
|
+
REQUEST_CONTEXT_METADATA_KEY,
|
52
|
+
target,
|
53
|
+
propertyKey,
|
54
|
+
);
|
55
|
+
return metadata ?? new Map();
|
56
|
+
}
|
57
|
+
}
|