@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 './request-context-metadata.js';
2
+ export * from './request-context-decorator.js';
3
+ export * from './request-context-reflector.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,17 @@
1
+ import { Prototype } from '../../types.js';
2
+ import { RequestContext } from '@e22m4u/js-trie-router';
3
+ import { RequestContextMetadata } from './request-context-metadata.js';
4
+ /**
5
+ * Request context decorator.
6
+ *
7
+ * @param propertyOrMetadata
8
+ */
9
+ export declare function requestContext<T extends object>(propertyOrMetadata?: keyof RequestContext | RequestContextMetadata): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
10
+ /**
11
+ * Request decorator.
12
+ */
13
+ export declare function request(): (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
14
+ /**
15
+ * Response decorator.
16
+ */
17
+ export declare function response(): (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
@@ -0,0 +1,32 @@
1
+ import { DecoratorTargetType } from '@e22m4u/ts-reflector';
2
+ import { getDecoratorTargetType } from '@e22m4u/ts-reflector';
3
+ import { RequestContextReflector } from './request-context-reflector.js';
4
+ /**
5
+ * Request context decorator.
6
+ *
7
+ * @param propertyOrMetadata
8
+ */
9
+ export function requestContext(propertyOrMetadata) {
10
+ return function (target, propertyKey, indexOrDescriptor) {
11
+ const decoratorType = getDecoratorTargetType(target, propertyKey, indexOrDescriptor);
12
+ if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
13
+ throw new Error('@requestContext decorator is only supported ' +
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);
19
+ };
20
+ }
21
+ /**
22
+ * Request decorator.
23
+ */
24
+ export function request() {
25
+ return requestContext('req');
26
+ }
27
+ /**
28
+ * Response decorator.
29
+ */
30
+ export function response() {
31
+ return requestContext('res');
32
+ }
@@ -0,0 +1,59 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
11
+ return function (target, key) { decorator(target, key, paramIndex); }
12
+ };
13
+ /* eslint-disable @typescript-eslint/no-unused-vars */
14
+ import { expect } from 'chai';
15
+ import { requestContext } from './request-context-decorator.js';
16
+ import { RequestContextReflector } from './request-context-reflector.js';
17
+ describe('requestContext', function () {
18
+ it('does not requires options', function () {
19
+ class Target {
20
+ method(prop) { }
21
+ }
22
+ __decorate([
23
+ __param(0, requestContext()),
24
+ __metadata("design:type", Function),
25
+ __metadata("design:paramtypes", [Object]),
26
+ __metadata("design:returntype", void 0)
27
+ ], Target.prototype, "method", null);
28
+ const res = RequestContextReflector.getMetadata(Target, 'method');
29
+ expect(res.get(0)).to.be.eql({ property: undefined });
30
+ });
31
+ it('sets a given key of RequestContext as "propertyKey" of the target metadata', function () {
32
+ class Target {
33
+ method(prop) { }
34
+ }
35
+ __decorate([
36
+ __param(0, requestContext('res')),
37
+ __metadata("design:type", Function),
38
+ __metadata("design:paramtypes", [Object]),
39
+ __metadata("design:returntype", void 0)
40
+ ], Target.prototype, "method", null);
41
+ const res = RequestContextReflector.getMetadata(Target, 'method');
42
+ expect(res.get(0)).to.be.eql({ property: 'res' });
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
+ });
@@ -0,0 +1,17 @@
1
+ import { MetadataKey } from '@e22m4u/ts-reflector';
2
+ import { RequestContext } from '@e22m4u/js-trie-router';
3
+ /**
4
+ * Request context metadata.
5
+ */
6
+ export type RequestContextMetadata = {
7
+ property?: keyof RequestContext;
8
+ [option: string]: unknown | undefined;
9
+ };
10
+ /**
11
+ * Request context metadata map.
12
+ */
13
+ export type RequestContextMetadataMap = Map<number, RequestContextMetadata>;
14
+ /**
15
+ * Request context metadata key.
16
+ */
17
+ export declare const REQUEST_CONTEXT_METADATA_KEY: MetadataKey<RequestContextMetadataMap>;
@@ -0,0 +1,5 @@
1
+ import { MetadataKey } from '@e22m4u/ts-reflector';
2
+ /**
3
+ * Request context metadata key.
4
+ */
5
+ export const REQUEST_CONTEXT_METADATA_KEY = new MetadataKey('requestContextMetadataKey');
@@ -0,0 +1,24 @@
1
+ import { Constructor } from '../../types.js';
2
+ import { RequestContextMetadata } from './request-context-metadata.js';
3
+ import { RequestContextMetadataMap } from './request-context-metadata.js';
4
+ /**
5
+ * Request context reflector.
6
+ */
7
+ export declare class RequestContextReflector {
8
+ /**
9
+ * Set metadata.
10
+ *
11
+ * @param metadata
12
+ * @param target
13
+ * @param index
14
+ * @param propertyKey
15
+ */
16
+ static setMetadata(metadata: RequestContextMetadata, target: Constructor, index: number, propertyKey: string): void;
17
+ /**
18
+ * Get metadata.
19
+ *
20
+ * @param target
21
+ * @param propertyKey
22
+ */
23
+ static getMetadata(target: Constructor, propertyKey: string): RequestContextMetadataMap;
24
+ }
@@ -0,0 +1,31 @@
1
+ import { Reflector } from '@e22m4u/ts-reflector';
2
+ import { REQUEST_CONTEXT_METADATA_KEY } from './request-context-metadata.js';
3
+ /**
4
+ * Request context reflector.
5
+ */
6
+ export class RequestContextReflector {
7
+ /**
8
+ * Set metadata.
9
+ *
10
+ * @param metadata
11
+ * @param target
12
+ * @param index
13
+ * @param propertyKey
14
+ */
15
+ static setMetadata(metadata, target, index, propertyKey) {
16
+ const oldMap = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, target, propertyKey);
17
+ const newMap = new Map(oldMap);
18
+ newMap.set(index, metadata);
19
+ Reflector.defineMetadata(REQUEST_CONTEXT_METADATA_KEY, newMap, target, propertyKey);
20
+ }
21
+ /**
22
+ * Get metadata.
23
+ *
24
+ * @param target
25
+ * @param propertyKey
26
+ */
27
+ static getMetadata(target, propertyKey) {
28
+ const metadata = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, target, propertyKey);
29
+ return metadata ?? new Map();
30
+ }
31
+ }
@@ -0,0 +1,59 @@
1
+ import { expect } from 'chai';
2
+ import { describe } from 'mocha';
3
+ import { Reflector } from '@e22m4u/ts-reflector';
4
+ import { RequestContextReflector } from './request-context-reflector.js';
5
+ import { REQUEST_CONTEXT_METADATA_KEY } from './request-context-metadata.js';
6
+ describe('RequestContextReflector', function () {
7
+ describe('setMetadata', function () {
8
+ it('sets a given value as target metadata', function () {
9
+ class Target {
10
+ }
11
+ const md1 = { property: 'req' };
12
+ const md2 = { property: 'res' };
13
+ RequestContextReflector.setMetadata(md1, Target, 0, 'propertyKey');
14
+ RequestContextReflector.setMetadata(md2, Target, 1, 'propertyKey');
15
+ const res = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, Target, 'propertyKey');
16
+ expect(res).to.be.instanceof(Map);
17
+ expect(res.get(0)).to.be.eq(md1);
18
+ expect(res.get(1)).to.be.eq(md2);
19
+ });
20
+ it('overrides existing metadata', function () {
21
+ class Target {
22
+ }
23
+ const md1 = { property: 'req' };
24
+ const md2 = { property: 'res' };
25
+ RequestContextReflector.setMetadata(md1, Target, 0, 'propertyKey');
26
+ const res1 = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, Target, 'propertyKey');
27
+ expect(res1).to.be.instanceof(Map);
28
+ expect(res1.get(0)).to.be.eq(md1);
29
+ RequestContextReflector.setMetadata(md2, Target, 0, 'propertyKey');
30
+ const res2 = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, Target, 'propertyKey');
31
+ expect(res2).to.be.instanceof(Map);
32
+ expect(res2.get(0)).to.be.eq(md2);
33
+ });
34
+ });
35
+ describe('getMetadata', function () {
36
+ it('returns an existing metadata of the target', function () {
37
+ class Target {
38
+ }
39
+ const md1 = { property: 'req' };
40
+ const md2 = { property: 'res' };
41
+ const mdMap = new Map([
42
+ [0, md1],
43
+ [1, md2],
44
+ ]);
45
+ Reflector.defineMetadata(REQUEST_CONTEXT_METADATA_KEY, mdMap, Target, 'propertyKey');
46
+ const res = RequestContextReflector.getMetadata(Target, 'propertyKey');
47
+ expect(res).to.be.instanceof(Map);
48
+ expect(res.get(0)).to.be.eq(md1);
49
+ expect(res.get(1)).to.be.eq(md2);
50
+ });
51
+ it('returns an empty map if no metadata', function () {
52
+ class Target {
53
+ }
54
+ const res = RequestContextReflector.getMetadata(Target, 'propertyKey');
55
+ expect(res).to.be.instanceof(Map);
56
+ expect(res).to.be.empty;
57
+ });
58
+ });
59
+ });
@@ -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';
@@ -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';
@@ -0,0 +1,28 @@
1
+ import { Prototype } from '../../types.js';
2
+ import { DataType } from '@e22m4u/ts-data-schema';
3
+ import { DataSchema } from '@e22m4u/ts-data-schema';
4
+ import { RequestDataMetadata } from './request-data-metadata.js';
5
+ /**
6
+ * Request data decorator.
7
+ *
8
+ * @param metadata
9
+ */
10
+ export declare function requestData<T extends object>(metadata: RequestDataMetadata): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
11
+ /**
12
+ * Decorator aliases.
13
+ */
14
+ export declare const params: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
15
+ export declare const param: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
16
+ export declare const queries: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
17
+ export declare const query: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
18
+ export declare const headers: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
19
+ export declare const header: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
20
+ export declare const cookies: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
21
+ 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;
23
+ /**
24
+ * Request body decorator.
25
+ *
26
+ * @param schemaOrType
27
+ */
28
+ export declare function body(schemaOrType?: DataSchema | DataType): (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
@@ -0,0 +1,84 @@
1
+ import { DataType } from '@e22m4u/ts-data-schema';
2
+ import { DecoratorTargetType } from '@e22m4u/ts-reflector';
3
+ import { getDecoratorTargetType } from '@e22m4u/ts-reflector';
4
+ import { RequestDataSource } from './request-data-metadata.js';
5
+ import { RequestDataReflector } from './request-data-reflector.js';
6
+ /**
7
+ * Request data decorator.
8
+ *
9
+ * @param metadata
10
+ */
11
+ export function requestData(metadata) {
12
+ return function (target, propertyKey, indexOrDescriptor) {
13
+ const decoratorType = getDecoratorTargetType(target, propertyKey, indexOrDescriptor);
14
+ if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
15
+ throw new Error('@requestData decorator is only supported ' +
16
+ 'on an instance method parameter.');
17
+ RequestDataReflector.setMetadata(metadata, target.constructor, indexOrDescriptor, propertyKey);
18
+ };
19
+ }
20
+ /**
21
+ * Create data decorator.
22
+ *
23
+ * @param source
24
+ */
25
+ function createDataDecorator(source) {
26
+ return function () {
27
+ const schema = { type: DataType.OBJECT };
28
+ return requestData({ schema, source });
29
+ };
30
+ }
31
+ /**
32
+ * Create property decorator.
33
+ *
34
+ * @param source
35
+ */
36
+ function createPropertyDecorator(source) {
37
+ return function (propertyKey, schemaOrType) {
38
+ const properties = {};
39
+ const rootSchema = { type: DataType.OBJECT };
40
+ if (typeof schemaOrType === 'object') {
41
+ properties[propertyKey] = schemaOrType;
42
+ rootSchema.properties = properties;
43
+ }
44
+ else if (typeof schemaOrType === 'string') {
45
+ properties[propertyKey] = { type: schemaOrType };
46
+ rootSchema.properties = properties;
47
+ }
48
+ return requestData({
49
+ source: source,
50
+ schema: rootSchema,
51
+ property: propertyKey,
52
+ });
53
+ };
54
+ }
55
+ /**
56
+ * Decorator aliases.
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);
67
+ /**
68
+ * Request body decorator.
69
+ *
70
+ * @param schemaOrType
71
+ */
72
+ export function body(schemaOrType) {
73
+ let schema;
74
+ if (typeof schemaOrType === 'object') {
75
+ schema = schemaOrType;
76
+ }
77
+ else if (typeof schemaOrType === 'string') {
78
+ schema = { type: schemaOrType };
79
+ }
80
+ else {
81
+ schema = { type: DataType.ANY };
82
+ }
83
+ return requestData({ schema, source: RequestDataSource.BODY });
84
+ }