@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.
Files changed (37) hide show
  1. package/README-ru.md +204 -0
  2. package/README.md +206 -0
  3. package/dist/cjs/index.cjs +22 -31
  4. package/dist/esm/decorators/action/action-decorator.d.ts +7 -8
  5. package/dist/esm/decorators/action/action-decorator.js +1 -5
  6. package/dist/esm/decorators/action/action-decorator.spec.js +16 -16
  7. package/dist/esm/decorators/action/action-metadata.d.ts +0 -1
  8. package/dist/esm/decorators/controller/controller-decorator.js +1 -5
  9. package/dist/esm/decorators/controller/controller-decorator.spec.js +9 -15
  10. package/dist/esm/decorators/controller/controller-metadata.d.ts +0 -1
  11. package/dist/esm/decorators/request-context/request-context-decorator.d.ts +2 -3
  12. package/dist/esm/decorators/request-context/request-context-decorator.js +3 -6
  13. package/dist/esm/decorators/request-context/request-context-decorator.spec.js +2 -17
  14. package/dist/esm/decorators/request-context/request-context-metadata.d.ts +0 -1
  15. package/dist/esm/decorators/request-data/request-data-decorator.d.ts +6 -2
  16. package/dist/esm/decorators/request-data/request-data-decorator.js +16 -16
  17. package/dist/esm/decorators/request-data/request-data-decorator.spec.js +7 -5
  18. package/dist/esm/decorators/request-data/request-data-metadata.d.ts +0 -1
  19. package/package.json +18 -17
  20. package/src/decorators/action/action-decorator.spec.ts +16 -16
  21. package/src/decorators/action/action-decorator.ts +10 -12
  22. package/src/decorators/action/action-metadata.ts +0 -1
  23. package/src/decorators/controller/controller-decorator.spec.ts +11 -16
  24. package/src/decorators/controller/controller-decorator.ts +4 -5
  25. package/src/decorators/controller/controller-metadata.ts +0 -1
  26. package/src/decorators/request-context/request-context-decorator.spec.ts +2 -15
  27. package/src/decorators/request-context/request-context-decorator.ts +3 -8
  28. package/src/decorators/request-context/request-context-metadata.ts +0 -1
  29. package/src/decorators/request-data/request-data-decorator.spec.ts +7 -6
  30. package/src/decorators/request-data/request-data-decorator.ts +41 -16
  31. package/src/decorators/request-data/request-data-metadata.ts +0 -1
  32. package/tsconfig.json +2 -1
  33. package/dist/esm/controller-registry.spec.d.ts +0 -1
  34. package/dist/esm/controller-registry.spec.js +0 -719
  35. package/dist/esm/debuggable-service.spec.d.ts +0 -1
  36. package/dist/esm/debuggable-service.spec.js +0 -16
  37. 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
- const metadata = {
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 requires options', function () {
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 path = 'myPath';
22
- const before = () => undefined;
23
- const after = () => undefined;
24
- const extraOption = 'extraOption';
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 () {
@@ -9,7 +9,6 @@ export type ControllerMetadata = {
9
9
  path?: string;
10
10
  before?: RoutePreHandler | RoutePreHandler[];
11
11
  after?: RoutePostHandler | RoutePostHandler[];
12
- [option: string]: unknown | undefined;
13
12
  };
14
13
  /**
15
14
  * Controller metadata key.
@@ -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 propertyOrMetadata
6
+ * @param propertyName
8
7
  */
9
- export declare function requestContext<T extends object>(propertyOrMetadata?: keyof RequestContext | RequestContextMetadata): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
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 propertyOrMetadata
7
+ * @param propertyName
8
8
  */
9
- export function requestContext(propertyOrMetadata) {
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
- const metadata = typeof propertyOrMetadata !== 'object'
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 requires options', function () {
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 key of RequestContext as "propertyKey" of the target metadata', function () {
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
  });
@@ -5,7 +5,6 @@ import { RequestContext } from '@e22m4u/js-trie-router';
5
5
  */
6
6
  export type RequestContextMetadata = {
7
7
  property?: keyof RequestContext;
8
- [option: string]: unknown | undefined;
9
8
  };
10
9
  /**
11
10
  * Request context metadata map.
@@ -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 metadata
12
+ * @param options
9
13
  */
10
- export declare function requestData<T extends object>(metadata: RequestDataMetadata): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
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 metadata
9
+ * @param options
10
10
  */
11
- export function requestData(metadata) {
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(metadata, target.constructor, indexOrDescriptor, propertyKey);
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 createDataDecorator(source) {
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 createPropertyDecorator(source) {
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 = createDataDecorator(RequestDataSource.PARAMS);
59
- export const param = createPropertyDecorator(RequestDataSource.PARAMS);
60
- export const queries = createDataDecorator(RequestDataSource.QUERY);
61
- export const query = createPropertyDecorator(RequestDataSource.QUERY);
62
- export const headers = createDataDecorator(RequestDataSource.HEADERS);
63
- export const header = createPropertyDecorator(RequestDataSource.HEADERS);
64
- export const cookies = createDataDecorator(RequestDataSource.COOKIE);
65
- export const cookie = createPropertyDecorator(RequestDataSource.COOKIE);
66
- export const bodyParam = createPropertyDecorator(RequestDataSource.BODY);
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 a given argument to the target metadata', function () {
31
- const md = {
32
- source: RequestDataSource.PARAMS,
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(md)),
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(md);
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 () {
@@ -17,7 +17,6 @@ export type RequestDataMetadata = {
17
17
  source: RequestDataSource;
18
18
  schema?: DataSchema;
19
19
  property?: string;
20
- [option: string]: unknown | undefined;
21
20
  };
22
21
  /**
23
22
  * Request data metadata map.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/ts-rest-router",
3
- "version": "0.0.5",
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.x",
43
- "@e22m4u/js-service": "0.1.x",
44
- "@e22m4u/js-trie-router": "0.0.x",
45
- "@e22m4u/ts-data-schema": "0.0.x",
46
- "@e22m4u/ts-reflector": "0.1.x",
47
- "debug": "~4.3.7",
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.0",
52
+ "@commitlint/cli": "~19.6.1",
52
53
  "@commitlint/config-conventional": "~19.6.0",
53
- "@eslint/js": "~9.15.0",
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.9.3",
59
- "c8": "~10.1.2",
59
+ "@types/node": "~22.10.2",
60
+ "c8": "~10.1.3",
60
61
  "chai": "~5.1.2",
61
- "esbuild": "~0.24.0",
62
- "eslint": "~9.15.0",
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": "~10.8.2",
68
- "prettier": "~3.3.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.6.3",
72
- "typescript-eslint": "~8.15.0"
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 method = HttpMethod.GET;
9
- const path = 'myPath';
10
- const before = () => undefined;
11
- const after = () => undefined;
12
- const customOption = 'customOption';
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({method, path, before, after, customOption})
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 method = HttpMethod.GET;
30
- const path = 'myPath';
27
+ const options = {
28
+ propertyKey: 'myMethod',
29
+ method: HttpMethod.GET,
30
+ path: 'myPath',
31
+ };
31
32
  class Target {
32
- @action({propertyKey: 'myMethod', method, path})
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
- metadata,
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 alias options.
44
+ * Action method options.
49
45
  */
50
- type ActionAliasOptions = Flatten<Omit<ActionOptions, 'method' | 'path'>>;
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?: ActionAliasOptions) => {
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?: ActionAliasOptions) => {
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?: ActionAliasOptions) => {
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?: ActionAliasOptions) => {
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?: ActionAliasOptions) => {
96
+ export const del = (path: string, options?: ActionMethodOptions) => {
99
97
  return action({...options, path, method: HttpMethod.DELETE});
100
98
  };
@@ -12,7 +12,6 @@ export type ActionMetadata = {
12
12
  path: string;
13
13
  before?: RoutePreHandler | RoutePreHandler[];
14
14
  after?: RoutePostHandler | RoutePostHandler[];
15
- [option: string]: unknown | undefined;
16
15
  };
17
16
 
18
17
  /**
@@ -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 requires options', function () {
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 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
- })
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
- const metadata = {
24
- ...options,
25
- className: target.name,
26
- };
27
- ControllerReflector.setMetadata(metadata, target);
23
+ ControllerReflector.setMetadata(
24
+ {...options, className: target.name},
25
+ target,
26
+ );
28
27
  };
29
28
  }
@@ -10,7 +10,6 @@ export type ControllerMetadata = {
10
10
  path?: string;
11
11
  before?: RoutePreHandler | RoutePreHandler[];
12
12
  after?: RoutePostHandler | RoutePostHandler[];
13
- [option: string]: unknown | undefined;
14
13
  };
15
14
 
16
15
  /**
@@ -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 requires options', function () {
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 key of RequestContext as "propertyKey" of the target metadata', function () {
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 propertyOrMetadata
11
+ * @param propertyName
13
12
  */
14
13
  export function requestContext<T extends object>(
15
- propertyOrMetadata?: keyof RequestContext | RequestContextMetadata,
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
- metadata,
32
+ {property: propertyName},
38
33
  target.constructor as Constructor<T>,
39
34
  indexOrDescriptor,
40
35
  propertyKey,
@@ -6,7 +6,6 @@ import {RequestContext} from '@e22m4u/js-trie-router';
6
6
  */
7
7
  export type RequestContextMetadata = {
8
8
  property?: keyof RequestContext;
9
- [option: string]: unknown | undefined;
10
9
  };
11
10
 
12
11
  /**