@e22m4u/ts-rest-router 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. package/.c8rc +9 -0
  2. package/.commitlintrc +5 -0
  3. package/.editorconfig +13 -0
  4. package/.husky/commit-msg +1 -0
  5. package/.husky/pre-commit +6 -0
  6. package/.mocharc.json +5 -0
  7. package/.prettierrc +7 -0
  8. package/LICENSE +21 -0
  9. package/README-ru.md +41 -0
  10. package/README.md +41 -0
  11. package/build-cjs.js +16 -0
  12. package/dist/cjs/index.cjs +692 -0
  13. package/dist/esm/controller-registry.d.ts +65 -0
  14. package/dist/esm/controller-registry.js +281 -0
  15. package/dist/esm/controller-registry.spec.d.ts +1 -0
  16. package/dist/esm/controller-registry.spec.js +719 -0
  17. package/dist/esm/debuggable-service.d.ts +18 -0
  18. package/dist/esm/debuggable-service.js +23 -0
  19. package/dist/esm/debuggable-service.spec.d.ts +1 -0
  20. package/dist/esm/debuggable-service.spec.js +16 -0
  21. package/dist/esm/decorators/action/action-decorator.d.ts +53 -0
  22. package/dist/esm/decorators/action/action-decorator.js +66 -0
  23. package/dist/esm/decorators/action/action-decorator.spec.d.ts +1 -0
  24. package/dist/esm/decorators/action/action-decorator.spec.js +59 -0
  25. package/dist/esm/decorators/action/action-metadata.d.ts +23 -0
  26. package/dist/esm/decorators/action/action-metadata.js +5 -0
  27. package/dist/esm/decorators/action/action-reflector.d.ts +22 -0
  28. package/dist/esm/decorators/action/action-reflector.js +29 -0
  29. package/dist/esm/decorators/action/action-reflector.spec.d.ts +1 -0
  30. package/dist/esm/decorators/action/action-reflector.spec.js +84 -0
  31. package/dist/esm/decorators/action/index.d.ts +3 -0
  32. package/dist/esm/decorators/action/index.js +3 -0
  33. package/dist/esm/decorators/controller/controller-decorator.d.ts +13 -0
  34. package/dist/esm/decorators/controller/controller-decorator.js +20 -0
  35. package/dist/esm/decorators/controller/controller-decorator.spec.d.ts +1 -0
  36. package/dist/esm/decorators/controller/controller-decorator.spec.js +53 -0
  37. package/dist/esm/decorators/controller/controller-metadata.d.ts +17 -0
  38. package/dist/esm/decorators/controller/controller-metadata.js +5 -0
  39. package/dist/esm/decorators/controller/controller-reflector.d.ts +20 -0
  40. package/dist/esm/decorators/controller/controller-reflector.js +24 -0
  41. package/dist/esm/decorators/controller/controller-reflector.spec.d.ts +1 -0
  42. package/dist/esm/decorators/controller/controller-reflector.spec.js +45 -0
  43. package/dist/esm/decorators/controller/index.d.ts +3 -0
  44. package/dist/esm/decorators/controller/index.js +3 -0
  45. package/dist/esm/decorators/index.d.ts +4 -0
  46. package/dist/esm/decorators/index.js +4 -0
  47. package/dist/esm/decorators/request-context/index.d.ts +3 -0
  48. package/dist/esm/decorators/request-context/index.js +3 -0
  49. package/dist/esm/decorators/request-context/request-context-decorator.d.ts +17 -0
  50. package/dist/esm/decorators/request-context/request-context-decorator.js +32 -0
  51. package/dist/esm/decorators/request-context/request-context-decorator.spec.d.ts +1 -0
  52. package/dist/esm/decorators/request-context/request-context-decorator.spec.js +59 -0
  53. package/dist/esm/decorators/request-context/request-context-metadata.d.ts +17 -0
  54. package/dist/esm/decorators/request-context/request-context-metadata.js +5 -0
  55. package/dist/esm/decorators/request-context/request-context-reflector.d.ts +24 -0
  56. package/dist/esm/decorators/request-context/request-context-reflector.js +31 -0
  57. package/dist/esm/decorators/request-context/request-context-reflector.spec.d.ts +1 -0
  58. package/dist/esm/decorators/request-context/request-context-reflector.spec.js +59 -0
  59. package/dist/esm/decorators/request-data/index.d.ts +3 -0
  60. package/dist/esm/decorators/request-data/index.js +3 -0
  61. package/dist/esm/decorators/request-data/request-data-decorator.d.ts +28 -0
  62. package/dist/esm/decorators/request-data/request-data-decorator.js +84 -0
  63. package/dist/esm/decorators/request-data/request-data-decorator.spec.d.ts +1 -0
  64. package/dist/esm/decorators/request-data/request-data-decorator.spec.js +534 -0
  65. package/dist/esm/decorators/request-data/request-data-metadata.d.ts +29 -0
  66. package/dist/esm/decorators/request-data/request-data-metadata.js +16 -0
  67. package/dist/esm/decorators/request-data/request-data-reflector.d.ts +24 -0
  68. package/dist/esm/decorators/request-data/request-data-reflector.js +31 -0
  69. package/dist/esm/decorators/request-data/request-data-reflector.spec.d.ts +1 -0
  70. package/dist/esm/decorators/request-data/request-data-reflector.spec.js +60 -0
  71. package/dist/esm/errors/index.d.ts +1 -0
  72. package/dist/esm/errors/index.js +1 -0
  73. package/dist/esm/errors/not-a-controller-error.d.ts +12 -0
  74. package/dist/esm/errors/not-a-controller-error.js +14 -0
  75. package/dist/esm/index.d.ts +5 -0
  76. package/dist/esm/index.js +5 -0
  77. package/dist/esm/rest-router.d.ts +19 -0
  78. package/dist/esm/rest-router.js +24 -0
  79. package/dist/esm/types.d.ts +57 -0
  80. package/dist/esm/types.js +2 -0
  81. package/dist/esm/utils/capitalize.d.ts +6 -0
  82. package/dist/esm/utils/capitalize.js +8 -0
  83. package/dist/esm/utils/capitalize.spec.d.ts +1 -0
  84. package/dist/esm/utils/capitalize.spec.js +8 -0
  85. package/dist/esm/utils/create-debugger.d.ts +11 -0
  86. package/dist/esm/utils/create-debugger.js +15 -0
  87. package/dist/esm/utils/create-debugger.spec.d.ts +1 -0
  88. package/dist/esm/utils/create-debugger.spec.js +8 -0
  89. package/dist/esm/utils/create-error.d.ts +10 -0
  90. package/dist/esm/utils/create-error.js +13 -0
  91. package/dist/esm/utils/create-error.spec.d.ts +1 -0
  92. package/dist/esm/utils/create-error.spec.js +8 -0
  93. package/dist/esm/utils/index.d.ts +4 -0
  94. package/dist/esm/utils/index.js +4 -0
  95. package/dist/esm/utils/to-camel-case.d.ts +6 -0
  96. package/dist/esm/utils/to-camel-case.js +11 -0
  97. package/dist/esm/utils/to-camel-case.spec.d.ts +1 -0
  98. package/dist/esm/utils/to-camel-case.spec.js +10 -0
  99. package/dist/tsconfig.tsbuildinfo +1 -0
  100. package/eslint.config.js +43 -0
  101. package/package.json +74 -0
  102. package/src/controller-registry.spec.ts +592 -0
  103. package/src/controller-registry.ts +355 -0
  104. package/src/debuggable-service.spec.ts +18 -0
  105. package/src/debuggable-service.ts +27 -0
  106. package/src/decorators/action/action-decorator.spec.ts +42 -0
  107. package/src/decorators/action/action-decorator.ts +100 -0
  108. package/src/decorators/action/action-metadata.ts +28 -0
  109. package/src/decorators/action/action-reflector.spec.ts +84 -0
  110. package/src/decorators/action/action-reflector.ts +38 -0
  111. package/src/decorators/action/index.ts +3 -0
  112. package/src/decorators/controller/controller-decorator.spec.ts +41 -0
  113. package/src/decorators/controller/controller-decorator.ts +29 -0
  114. package/src/decorators/controller/controller-metadata.ts +21 -0
  115. package/src/decorators/controller/controller-reflector.spec.ts +45 -0
  116. package/src/decorators/controller/controller-reflector.ts +28 -0
  117. package/src/decorators/controller/index.ts +3 -0
  118. package/src/decorators/index.ts +4 -0
  119. package/src/decorators/request-context/index.ts +3 -0
  120. package/src/decorators/request-context/request-context-decorator.spec.ts +41 -0
  121. package/src/decorators/request-context/request-context-decorator.ts +57 -0
  122. package/src/decorators/request-context/request-context-metadata.ts +21 -0
  123. package/src/decorators/request-context/request-context-reflector.spec.ts +77 -0
  124. package/src/decorators/request-context/request-context-reflector.ts +57 -0
  125. package/src/decorators/request-data/index.ts +3 -0
  126. package/src/decorators/request-data/request-data-decorator.spec.ts +477 -0
  127. package/src/decorators/request-data/request-data-decorator.ts +106 -0
  128. package/src/decorators/request-data/request-data-metadata.ts +34 -0
  129. package/src/decorators/request-data/request-data-reflector.spec.ts +78 -0
  130. package/src/decorators/request-data/request-data-reflector.ts +57 -0
  131. package/src/errors/index.ts +1 -0
  132. package/src/errors/not-a-controller-error.ts +15 -0
  133. package/src/index.ts +5 -0
  134. package/src/rest-router.ts +31 -0
  135. package/src/types.ts +59 -0
  136. package/src/utils/capitalize.spec.ts +9 -0
  137. package/src/utils/capitalize.ts +8 -0
  138. package/src/utils/create-debugger.spec.ts +9 -0
  139. package/src/utils/create-debugger.ts +21 -0
  140. package/src/utils/create-error.spec.ts +9 -0
  141. package/src/utils/create-error.ts +19 -0
  142. package/src/utils/index.ts +4 -0
  143. package/src/utils/to-camel-case.spec.ts +11 -0
  144. package/src/utils/to-camel-case.ts +11 -0
  145. package/tsconfig.json +17 -0
@@ -0,0 +1,3 @@
1
+ export * from './action-metadata.js';
2
+ export * from './action-reflector.js';
3
+ export * from './action-decorator.js';
@@ -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,3 @@
1
+ export * from './controller-metadata.js';
2
+ export * from './controller-decorator.js';
3
+ export * from './controller-reflector.js';
@@ -0,0 +1,4 @@
1
+ export * from './action/index.js';
2
+ export * from './controller/index.js';
3
+ export * from './request-data/index.js';
4
+ export * from './request-context/index.js';
@@ -0,0 +1,3 @@
1
+ export * from './request-context-metadata.js';
2
+ export * from './request-context-decorator.js';
3
+ export * from './request-context-reflector.js';
@@ -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
+ }
@@ -0,0 +1,3 @@
1
+ export * from './request-data-metadata.js';
2
+ export * from './request-data-decorator.js';
3
+ export * from './request-data-reflector.js';