@e22m4u/ts-rest-router 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README-ru.md +227 -0
  2. package/README.md +227 -0
  3. package/dist/cjs/index.cjs +448 -153
  4. package/dist/esm/controller-registry.d.ts +53 -11
  5. package/dist/esm/controller-registry.js +242 -104
  6. package/dist/esm/debuggable-service.js +1 -1
  7. package/dist/esm/decorators/action/action-decorator.d.ts +7 -8
  8. package/dist/esm/decorators/action/action-decorator.js +1 -5
  9. package/dist/esm/decorators/action/action-decorator.spec.js +16 -16
  10. package/dist/esm/decorators/action/action-metadata.d.ts +0 -1
  11. package/dist/esm/decorators/after/after-decorator.d.ts +9 -0
  12. package/dist/esm/decorators/after/after-decorator.js +22 -0
  13. package/dist/esm/decorators/after/after-decorator.spec.d.ts +1 -0
  14. package/dist/esm/decorators/after/after-decorator.spec.js +115 -0
  15. package/dist/esm/decorators/after/after-metadata.d.ts +13 -0
  16. package/dist/esm/decorators/after/after-metadata.js +5 -0
  17. package/dist/esm/decorators/after/after-reflector.d.ts +22 -0
  18. package/dist/esm/decorators/after/after-reflector.js +29 -0
  19. package/dist/esm/decorators/after/after-reflector.spec.d.ts +1 -0
  20. package/dist/esm/decorators/after/after-reflector.spec.js +102 -0
  21. package/dist/esm/decorators/after/index.d.ts +3 -0
  22. package/dist/esm/decorators/after/index.js +3 -0
  23. package/dist/esm/decorators/before/before-decorator.d.ts +9 -0
  24. package/dist/esm/decorators/before/before-decorator.js +22 -0
  25. package/dist/esm/decorators/before/before-decorator.spec.d.ts +1 -0
  26. package/dist/esm/decorators/before/before-decorator.spec.js +115 -0
  27. package/dist/esm/decorators/before/before-metadata.d.ts +13 -0
  28. package/dist/esm/decorators/before/before-metadata.js +5 -0
  29. package/dist/esm/decorators/before/before-reflector.d.ts +22 -0
  30. package/dist/esm/decorators/before/before-reflector.js +29 -0
  31. package/dist/esm/decorators/before/before-reflector.spec.d.ts +1 -0
  32. package/dist/esm/decorators/before/before-reflector.spec.js +102 -0
  33. package/dist/esm/decorators/before/index.d.ts +3 -0
  34. package/dist/esm/decorators/before/index.js +3 -0
  35. package/dist/esm/decorators/controller/controller-decorator.d.ts +2 -1
  36. package/dist/esm/decorators/controller/controller-decorator.js +27 -6
  37. package/dist/esm/decorators/controller/controller-decorator.spec.js +37 -15
  38. package/dist/esm/decorators/controller/controller-metadata.d.ts +0 -1
  39. package/dist/esm/decorators/index.d.ts +2 -0
  40. package/dist/esm/decorators/index.js +2 -0
  41. package/dist/esm/decorators/request-context/request-context-decorator.d.ts +2 -3
  42. package/dist/esm/decorators/request-context/request-context-decorator.js +3 -6
  43. package/dist/esm/decorators/request-context/request-context-decorator.spec.js +2 -17
  44. package/dist/esm/decorators/request-context/request-context-metadata.d.ts +0 -1
  45. package/dist/esm/decorators/request-data/request-data-decorator.d.ts +7 -3
  46. package/dist/esm/decorators/request-data/request-data-decorator.js +16 -16
  47. package/dist/esm/decorators/request-data/request-data-decorator.spec.js +12 -10
  48. package/dist/esm/decorators/request-data/request-data-metadata.d.ts +0 -1
  49. package/dist/esm/utils/create-debugger.d.ts +35 -2
  50. package/dist/esm/utils/create-debugger.js +71 -5
  51. package/package.json +17 -17
  52. package/src/controller-registry.spec.ts +601 -275
  53. package/src/controller-registry.ts +263 -128
  54. package/src/debuggable-service.ts +1 -1
  55. package/src/decorators/action/action-decorator.spec.ts +16 -16
  56. package/src/decorators/action/action-decorator.ts +10 -12
  57. package/src/decorators/action/action-metadata.ts +0 -1
  58. package/src/decorators/after/after-decorator.spec.ts +92 -0
  59. package/src/decorators/after/after-decorator.ts +40 -0
  60. package/src/decorators/after/after-metadata.ts +17 -0
  61. package/src/decorators/after/after-reflector.spec.ts +107 -0
  62. package/src/decorators/after/after-reflector.ts +45 -0
  63. package/src/decorators/after/index.ts +3 -0
  64. package/src/decorators/before/before-decorator.spec.ts +92 -0
  65. package/src/decorators/before/before-decorator.ts +40 -0
  66. package/src/decorators/before/before-metadata.ts +17 -0
  67. package/src/decorators/before/before-reflector.spec.ts +111 -0
  68. package/src/decorators/before/before-reflector.ts +50 -0
  69. package/src/decorators/before/index.ts +3 -0
  70. package/src/decorators/controller/controller-decorator.spec.ts +33 -16
  71. package/src/decorators/controller/controller-decorator.ts +33 -6
  72. package/src/decorators/controller/controller-metadata.ts +0 -1
  73. package/src/decorators/index.ts +2 -0
  74. package/src/decorators/request-context/request-context-decorator.spec.ts +2 -15
  75. package/src/decorators/request-context/request-context-decorator.ts +3 -8
  76. package/src/decorators/request-context/request-context-metadata.ts +0 -1
  77. package/src/decorators/request-data/request-data-decorator.spec.ts +12 -11
  78. package/src/decorators/request-data/request-data-decorator.ts +41 -16
  79. package/src/decorators/request-data/request-data-metadata.ts +0 -1
  80. package/src/utils/create-debugger.ts +84 -7
@@ -0,0 +1,102 @@
1
+ import { expect } from 'chai';
2
+ import { describe } from 'mocha';
3
+ import { Reflector } from '@e22m4u/ts-reflector';
4
+ import { BeforeReflector } from './before-reflector.js';
5
+ import { BEFORE_METADATA_KEY } from './before-metadata.js';
6
+ const MIDDLEWARE_1 = () => undefined;
7
+ const MIDDLEWARE_2 = () => undefined;
8
+ const MIDDLEWARE_3 = () => undefined;
9
+ describe('BeforeReflector', function () {
10
+ describe('class target', function () {
11
+ describe('addMetadata', function () {
12
+ it('adds a given value to the target metadata', function () {
13
+ class Target {
14
+ }
15
+ const md1 = { middleware: MIDDLEWARE_1 };
16
+ const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
17
+ BeforeReflector.addMetadata(md1, Target);
18
+ BeforeReflector.addMetadata(md2, Target);
19
+ const res = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target);
20
+ expect(res).to.be.eql([md2, md1]);
21
+ });
22
+ });
23
+ describe('getMetadata', function () {
24
+ it('returns an empty array if no metadata', function () {
25
+ class Target {
26
+ }
27
+ const res = BeforeReflector.getMetadata(Target);
28
+ expect(res).to.be.eql([]);
29
+ });
30
+ it('returns existing metadata by the target', function () {
31
+ class Target {
32
+ }
33
+ const md1 = { middleware: MIDDLEWARE_1 };
34
+ const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
35
+ const mdArray = [md1, md2];
36
+ Reflector.defineMetadata(BEFORE_METADATA_KEY, mdArray, Target);
37
+ const res = BeforeReflector.getMetadata(Target);
38
+ expect(res).to.be.eql(mdArray);
39
+ });
40
+ });
41
+ });
42
+ describe('method target', function () {
43
+ describe('addMetadata', function () {
44
+ it('adds a given value to the target metadata', function () {
45
+ class Target {
46
+ }
47
+ const md1 = { middleware: MIDDLEWARE_1 };
48
+ const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
49
+ BeforeReflector.addMetadata(md1, Target, 'prop');
50
+ BeforeReflector.addMetadata(md2, Target, 'prop');
51
+ const res = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target, 'prop');
52
+ expect(res).to.be.eql([md2, md1]);
53
+ });
54
+ });
55
+ describe('getMetadata', function () {
56
+ it('returns an empty array if no metadata', function () {
57
+ class Target {
58
+ }
59
+ const res = BeforeReflector.getMetadata(Target, 'prop');
60
+ expect(res).to.be.eql([]);
61
+ });
62
+ it('returns existing metadata by the target', function () {
63
+ class Target {
64
+ }
65
+ const md1 = { middleware: MIDDLEWARE_1 };
66
+ const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
67
+ const mdArray = [md1, md2];
68
+ Reflector.defineMetadata(BEFORE_METADATA_KEY, mdArray, Target, 'prop');
69
+ const res = BeforeReflector.getMetadata(Target, 'prop');
70
+ expect(res).to.be.eql(mdArray);
71
+ });
72
+ });
73
+ });
74
+ describe('addMetadata', function () {
75
+ it('can distinguish class and method metadata', function () {
76
+ class Target {
77
+ }
78
+ const md1 = { middleware: MIDDLEWARE_1 };
79
+ const md2 = { middleware: MIDDLEWARE_2 };
80
+ BeforeReflector.addMetadata(md1, Target);
81
+ BeforeReflector.addMetadata(md2, Target, 'prop');
82
+ const res1 = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target);
83
+ const res2 = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target, 'prop');
84
+ expect(res1).to.be.eql([md1]);
85
+ expect(res2).to.be.eql([md2]);
86
+ });
87
+ });
88
+ describe('getMetadata', function () {
89
+ it('can distinguish class and method metadata', function () {
90
+ class Target {
91
+ }
92
+ const md1 = { middleware: MIDDLEWARE_1 };
93
+ const md2 = { middleware: MIDDLEWARE_2 };
94
+ Reflector.defineMetadata(BEFORE_METADATA_KEY, [md1], Target);
95
+ Reflector.defineMetadata(BEFORE_METADATA_KEY, [md2], Target, 'prop');
96
+ const res1 = BeforeReflector.getMetadata(Target);
97
+ const res2 = BeforeReflector.getMetadata(Target, 'prop');
98
+ expect(res1).to.be.eql([md1]);
99
+ expect(res2).to.be.eql([md2]);
100
+ });
101
+ });
102
+ });
@@ -0,0 +1,3 @@
1
+ export * from './before-metadata.js';
2
+ export * from './before-reflector.js';
3
+ export * from './before-decorator.js';
@@ -0,0 +1,3 @@
1
+ export * from './before-metadata.js';
2
+ export * from './before-reflector.js';
3
+ export * from './before-decorator.js';
@@ -8,6 +8,7 @@ export type ControllerOptions = Flatten<Omit<ControllerMetadata, 'className'>>;
8
8
  /**
9
9
  * Controller decorator.
10
10
  *
11
+ * @param pathOrOptions
11
12
  * @param options
12
13
  */
13
- export declare function controller<T extends object>(options?: ControllerOptions): (target: Constructor<T>) => void;
14
+ export declare function controller<T extends object>(pathOrOptions?: string | ControllerOptions, options?: ControllerOptions): (target: Constructor<T>) => void;
@@ -4,17 +4,38 @@ import { ControllerReflector } from './controller-reflector.js';
4
4
  /**
5
5
  * Controller decorator.
6
6
  *
7
+ * @param pathOrOptions
7
8
  * @param options
8
9
  */
9
- export function controller(options) {
10
+ export function controller(pathOrOptions, options) {
10
11
  return function (target) {
11
12
  const decoratorType = getDecoratorTargetType(target);
12
13
  if (decoratorType !== DecoratorTargetType.CONSTRUCTOR)
13
14
  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);
15
+ // если первый аргумент является строкой,
16
+ // то значение используется в качестве
17
+ // базового пути контроллера
18
+ if (typeof pathOrOptions === 'string') {
19
+ // если второй аргумент не определен,
20
+ // то создается новый объект опций,
21
+ // который включает базовый путь
22
+ if (!options) {
23
+ options = { path: pathOrOptions };
24
+ }
25
+ // если второй аргумент определен,
26
+ // то базовый путь из первого аргумента
27
+ // передается в объект опций декоратора
28
+ else {
29
+ options.path = pathOrOptions;
30
+ }
31
+ }
32
+ // если первый аргумент является объектом,
33
+ // то его значение используется в качестве
34
+ // объекта опций декоратора, а второй
35
+ // аргумент игнорируется
36
+ else if (typeof pathOrOptions === 'object') {
37
+ options = pathOrOptions;
38
+ }
39
+ ControllerReflector.setMetadata({ ...options, className: target.name }, target);
19
40
  };
20
41
  }
@@ -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 () {
@@ -50,4 +44,32 @@ describe('controller', function () {
50
44
  const res = ControllerReflector.getMetadata(Target);
51
45
  expect(res).to.be.eql({ className: 'Target' });
52
46
  });
47
+ it('allows to pass the path option to the first parameter', function () {
48
+ let Target = class Target {
49
+ };
50
+ Target = __decorate([
51
+ controller('myPath')
52
+ ], Target);
53
+ const res = ControllerReflector.getMetadata(Target);
54
+ expect(res).to.be.eql({ className: 'Target', path: 'myPath' });
55
+ });
56
+ it('merges two given arguments in the target metadata', function () {
57
+ const before = () => undefined;
58
+ let Target = class Target {
59
+ };
60
+ Target = __decorate([
61
+ controller('myPath', { before })
62
+ ], Target);
63
+ const res = ControllerReflector.getMetadata(Target);
64
+ expect(res).to.be.eql({ className: 'Target', path: 'myPath', before });
65
+ });
66
+ it('overrides the path option by the first argument', function () {
67
+ let Target = class Target {
68
+ };
69
+ Target = __decorate([
70
+ controller('myPath1', { path: 'myPath2' })
71
+ ], Target);
72
+ const res = ControllerReflector.getMetadata(Target);
73
+ expect(res).to.be.eql({ className: 'Target', path: 'myPath1' });
74
+ });
53
75
  });
@@ -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,3 +1,5 @@
1
+ export * from './after/index.js';
2
+ export * from './before/index.js';
1
3
  export * from './action/index.js';
2
4
  export * from './controller/index.js';
3
5
  export * from './request-data/index.js';
@@ -1,3 +1,5 @@
1
+ export * from './after/index.js';
2
+ export * from './before/index.js';
1
3
  export * from './action/index.js';
2
4
  export * from './controller/index.js';
3
5
  export * from './request-data/index.js';
@@ -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
  */
@@ -19,7 +23,7 @@ export declare const headers: () => (target: Prototype<object>, propertyKey: str
19
23
  export declare const header: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
20
24
  export declare const cookies: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
21
25
  export declare const cookie: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
22
- export declare const bodyParam: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
26
+ export declare const bodyProp: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
23
27
  /**
24
28
  * Request body decorator.
25
29
  *
@@ -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 bodyProp = createRequestDataPropertyDecoratorWithSource(RequestDataSource.BODY);
67
67
  /**
68
68
  * Request body decorator.
69
69
  *
@@ -22,27 +22,29 @@ import { params } from './request-data-decorator.js';
22
22
  import { queries } from './request-data-decorator.js';
23
23
  import { cookies } from './request-data-decorator.js';
24
24
  import { headers } from './request-data-decorator.js';
25
- import { bodyParam } from './request-data-decorator.js';
25
+ import { bodyProp } from './request-data-decorator.js';
26
26
  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 () {
@@ -458,13 +460,13 @@ describe('requestData', function () {
458
460
  });
459
461
  });
460
462
  });
461
- describe('bodyParam', function () {
463
+ describe('bodyProp', function () {
462
464
  it('sets a given "propertyKey" to the target metadata', function () {
463
465
  class Target {
464
466
  myMethod(prop) { }
465
467
  }
466
468
  __decorate([
467
- __param(0, bodyParam('myPropertyKey')),
469
+ __param(0, bodyProp('myPropertyKey')),
468
470
  __metadata("design:type", Function),
469
471
  __metadata("design:paramtypes", [Object]),
470
472
  __metadata("design:returntype", void 0)
@@ -483,7 +485,7 @@ describe('requestData', function () {
483
485
  myMethod(prop) { }
484
486
  }
485
487
  __decorate([
486
- __param(0, bodyParam(propertyKey, propertyType)),
488
+ __param(0, bodyProp(propertyKey, propertyType)),
487
489
  __metadata("design:type", Function),
488
490
  __metadata("design:paramtypes", [Object]),
489
491
  __metadata("design:returntype", void 0)
@@ -512,7 +514,7 @@ describe('requestData', function () {
512
514
  myMethod(prop) { }
513
515
  }
514
516
  __decorate([
515
- __param(0, bodyParam(propertyKey, schema)),
517
+ __param(0, bodyProp(propertyKey, schema)),
516
518
  __metadata("design:type", Function),
517
519
  __metadata("design:paramtypes", [Object]),
518
520
  __metadata("design:returntype", void 0)
@@ -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.
@@ -1,11 +1,44 @@
1
- import { format } from '@e22m4u/js-format';
2
1
  /**
3
2
  * Debugger.
4
3
  */
5
- export type Debugger = (...args: Parameters<typeof format>) => void;
4
+ export type Debugger = (messageOrData: string | unknown, ...args: any[]) => void;
6
5
  /**
7
6
  * Create debugger.
8
7
  *
8
+ * Base usage:
9
+ * ```ts
10
+ * const debug = createDebugger('myService');
11
+ * debug('Service created.');
12
+ * // ujut:myService Service created.
13
+ * ```
14
+ *
15
+ * Nested namespaces:
16
+ * ```ts
17
+ * const debug1 = debug.bind('namespace1');
18
+ * const debug2 = debug.bind('namespace2');
19
+ * debug1('Application started'); // ujut:myService [namespace1] Application started
20
+ * debug2('Connection established'); // ujut:myService [namespace2] Connection established
21
+ * ```
22
+ *
23
+ * Value inspection:
24
+ * ```ts
25
+ * debug({foo: 'lorem', bar: 'ipsum'})
26
+ * // ujut:myService {
27
+ * // ujut:myService "foo": "lorem",
28
+ * // ujut:myService "bar": "ipsum"
29
+ * // ujut:myService }
30
+ * ```
31
+ *
32
+ * Titled inspection output:
33
+ * ```ts
34
+ * debug({foo: 'lorem', bar: 'ipsum'}, 'My awesome output:')
35
+ * // ujut:myService My awesome output:
36
+ * // ujut:myService {
37
+ * // ujut:myService "foo": "lorem",
38
+ * // ujut:myService "bar": "ipsum"
39
+ * // ujut:myService }
40
+ * ```
41
+ *
9
42
  * @param name
10
43
  */
11
44
  export declare function createDebugger(name: string): Debugger;