@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,534 @@
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 { DataType } from '@e22m4u/ts-data-schema';
16
+ import { body } from './request-data-decorator.js';
17
+ import { param } from './request-data-decorator.js';
18
+ import { query } from './request-data-decorator.js';
19
+ import { cookie } from './request-data-decorator.js';
20
+ import { header } from './request-data-decorator.js';
21
+ import { params } from './request-data-decorator.js';
22
+ import { queries } from './request-data-decorator.js';
23
+ import { cookies } from './request-data-decorator.js';
24
+ import { headers } from './request-data-decorator.js';
25
+ import { bodyParam } from './request-data-decorator.js';
26
+ import { requestData } from './request-data-decorator.js';
27
+ import { RequestDataSource } from './request-data-metadata.js';
28
+ import { RequestDataReflector } from './request-data-reflector.js';
29
+ describe('requestData', function () {
30
+ it('sets a given argument to the target metadata', function () {
31
+ const md = {
32
+ source: RequestDataSource.PARAMS,
33
+ customOption: 'myOption',
34
+ };
35
+ class Target {
36
+ myMethod(prop) { }
37
+ }
38
+ __decorate([
39
+ __param(0, requestData(md)),
40
+ __metadata("design:type", Function),
41
+ __metadata("design:paramtypes", [Object]),
42
+ __metadata("design:returntype", void 0)
43
+ ], Target.prototype, "myMethod", null);
44
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
45
+ expect(res.get(0)).to.be.eql(md);
46
+ });
47
+ describe('request data by a given source', function () {
48
+ describe('params', function () {
49
+ it('sets metadata with specified source and schema', function () {
50
+ class Target {
51
+ myMethod(prop) { }
52
+ }
53
+ __decorate([
54
+ __param(0, params()),
55
+ __metadata("design:type", Function),
56
+ __metadata("design:paramtypes", [Object]),
57
+ __metadata("design:returntype", void 0)
58
+ ], Target.prototype, "myMethod", null);
59
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
60
+ expect(res.get(0)).to.be.eql({
61
+ source: RequestDataSource.PARAMS,
62
+ schema: { type: DataType.OBJECT },
63
+ });
64
+ });
65
+ });
66
+ describe('queries', function () {
67
+ it('sets metadata with specified source and schema', function () {
68
+ class Target {
69
+ myMethod(prop) { }
70
+ }
71
+ __decorate([
72
+ __param(0, queries()),
73
+ __metadata("design:type", Function),
74
+ __metadata("design:paramtypes", [Object]),
75
+ __metadata("design:returntype", void 0)
76
+ ], Target.prototype, "myMethod", null);
77
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
78
+ expect(res.get(0)).to.be.eql({
79
+ source: RequestDataSource.QUERY,
80
+ schema: { type: DataType.OBJECT },
81
+ });
82
+ });
83
+ });
84
+ describe('headers', function () {
85
+ it('sets metadata with specified source and schema', function () {
86
+ class Target {
87
+ myMethod(prop) { }
88
+ }
89
+ __decorate([
90
+ __param(0, headers()),
91
+ __metadata("design:type", Function),
92
+ __metadata("design:paramtypes", [Object]),
93
+ __metadata("design:returntype", void 0)
94
+ ], Target.prototype, "myMethod", null);
95
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
96
+ expect(res.get(0)).to.be.eql({
97
+ source: RequestDataSource.HEADERS,
98
+ schema: { type: DataType.OBJECT },
99
+ });
100
+ });
101
+ });
102
+ describe('cookies', function () {
103
+ it('sets metadata with specified source and schema', function () {
104
+ class Target {
105
+ myMethod(prop) { }
106
+ }
107
+ __decorate([
108
+ __param(0, cookies()),
109
+ __metadata("design:type", Function),
110
+ __metadata("design:paramtypes", [Object]),
111
+ __metadata("design:returntype", void 0)
112
+ ], Target.prototype, "myMethod", null);
113
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
114
+ expect(res.get(0)).to.be.eql({
115
+ source: RequestDataSource.COOKIE,
116
+ schema: { type: DataType.OBJECT },
117
+ });
118
+ });
119
+ });
120
+ describe('body', function () {
121
+ it('sets metadata with specified source and schema', function () {
122
+ class Target {
123
+ myMethod(prop) { }
124
+ }
125
+ __decorate([
126
+ __param(0, body()),
127
+ __metadata("design:type", Function),
128
+ __metadata("design:paramtypes", [Object]),
129
+ __metadata("design:returntype", void 0)
130
+ ], Target.prototype, "myMethod", null);
131
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
132
+ expect(res.get(0)).to.be.eql({
133
+ source: RequestDataSource.BODY,
134
+ schema: { type: DataType.ANY },
135
+ });
136
+ });
137
+ it('sets a given DataType to the target metadata', function () {
138
+ class Target {
139
+ myMethod(prop) { }
140
+ }
141
+ __decorate([
142
+ __param(0, body(DataType.STRING)),
143
+ __metadata("design:type", Function),
144
+ __metadata("design:paramtypes", [Object]),
145
+ __metadata("design:returntype", void 0)
146
+ ], Target.prototype, "myMethod", null);
147
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
148
+ expect(res.get(0)).to.be.eql({
149
+ source: RequestDataSource.BODY,
150
+ schema: { type: DataType.STRING },
151
+ });
152
+ });
153
+ it('set a given DataSchema to the target metadata', function () {
154
+ const schema = { type: DataType.STRING, required: true };
155
+ class Target {
156
+ myMethod(prop) { }
157
+ }
158
+ __decorate([
159
+ __param(0, body(schema)),
160
+ __metadata("design:type", Function),
161
+ __metadata("design:paramtypes", [Object]),
162
+ __metadata("design:returntype", void 0)
163
+ ], Target.prototype, "myMethod", null);
164
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
165
+ expect(res.get(0)).to.be.eql({
166
+ source: RequestDataSource.BODY,
167
+ schema,
168
+ });
169
+ });
170
+ });
171
+ });
172
+ describe('request data piece by a given property key', function () {
173
+ describe('param', function () {
174
+ it('sets a given "propertyKey" to the target metadata', function () {
175
+ class Target {
176
+ myMethod(prop) { }
177
+ }
178
+ __decorate([
179
+ __param(0, param('myPropertyKey')),
180
+ __metadata("design:type", Function),
181
+ __metadata("design:paramtypes", [Object]),
182
+ __metadata("design:returntype", void 0)
183
+ ], Target.prototype, "myMethod", null);
184
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
185
+ expect(res.get(0)).to.be.eql({
186
+ source: RequestDataSource.PARAMS,
187
+ schema: { type: DataType.OBJECT },
188
+ property: 'myPropertyKey',
189
+ });
190
+ });
191
+ it('sets a given DataType as property type', function () {
192
+ const propertyKey = 'myPropertyKey';
193
+ const propertyType = DataType.STRING;
194
+ class Target {
195
+ myMethod(prop) { }
196
+ }
197
+ __decorate([
198
+ __param(0, param(propertyKey, propertyType)),
199
+ __metadata("design:type", Function),
200
+ __metadata("design:paramtypes", [Object]),
201
+ __metadata("design:returntype", void 0)
202
+ ], Target.prototype, "myMethod", null);
203
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
204
+ expect(res.get(0)).to.be.eql({
205
+ source: RequestDataSource.PARAMS,
206
+ schema: {
207
+ type: DataType.OBJECT,
208
+ properties: {
209
+ [propertyKey]: {
210
+ type: propertyType,
211
+ },
212
+ },
213
+ },
214
+ property: propertyKey,
215
+ });
216
+ });
217
+ it('sets a given DataSchema as property schema', function () {
218
+ const schema = {
219
+ type: DataType.STRING,
220
+ required: true,
221
+ };
222
+ const propertyKey = 'myPropertyKey';
223
+ class Target {
224
+ myMethod(prop) { }
225
+ }
226
+ __decorate([
227
+ __param(0, param(propertyKey, schema)),
228
+ __metadata("design:type", Function),
229
+ __metadata("design:paramtypes", [Object]),
230
+ __metadata("design:returntype", void 0)
231
+ ], Target.prototype, "myMethod", null);
232
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
233
+ expect(res.get(0)).to.be.eql({
234
+ source: RequestDataSource.PARAMS,
235
+ schema: {
236
+ type: DataType.OBJECT,
237
+ properties: {
238
+ [propertyKey]: schema,
239
+ },
240
+ },
241
+ property: propertyKey,
242
+ });
243
+ });
244
+ });
245
+ describe('query', function () {
246
+ it('sets a given "propertyKey" to the target metadata', function () {
247
+ class Target {
248
+ myMethod(prop) { }
249
+ }
250
+ __decorate([
251
+ __param(0, query('myPropertyKey')),
252
+ __metadata("design:type", Function),
253
+ __metadata("design:paramtypes", [Object]),
254
+ __metadata("design:returntype", void 0)
255
+ ], Target.prototype, "myMethod", null);
256
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
257
+ expect(res.get(0)).to.be.eql({
258
+ source: RequestDataSource.QUERY,
259
+ schema: { type: DataType.OBJECT },
260
+ property: 'myPropertyKey',
261
+ });
262
+ });
263
+ it('sets a given DataType as property type', function () {
264
+ const propertyKey = 'myPropertyKey';
265
+ const propertyType = DataType.STRING;
266
+ class Target {
267
+ myMethod(prop) { }
268
+ }
269
+ __decorate([
270
+ __param(0, query(propertyKey, propertyType)),
271
+ __metadata("design:type", Function),
272
+ __metadata("design:paramtypes", [Object]),
273
+ __metadata("design:returntype", void 0)
274
+ ], Target.prototype, "myMethod", null);
275
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
276
+ expect(res.get(0)).to.be.eql({
277
+ source: RequestDataSource.QUERY,
278
+ schema: {
279
+ type: DataType.OBJECT,
280
+ properties: {
281
+ [propertyKey]: {
282
+ type: propertyType,
283
+ },
284
+ },
285
+ },
286
+ property: propertyKey,
287
+ });
288
+ });
289
+ it('sets a given DataSchema as property schema', function () {
290
+ const schema = {
291
+ type: DataType.STRING,
292
+ required: true,
293
+ };
294
+ const propertyKey = 'myPropertyKey';
295
+ class Target {
296
+ myMethod(prop) { }
297
+ }
298
+ __decorate([
299
+ __param(0, query(propertyKey, schema)),
300
+ __metadata("design:type", Function),
301
+ __metadata("design:paramtypes", [Object]),
302
+ __metadata("design:returntype", void 0)
303
+ ], Target.prototype, "myMethod", null);
304
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
305
+ expect(res.get(0)).to.be.eql({
306
+ source: RequestDataSource.QUERY,
307
+ schema: {
308
+ type: DataType.OBJECT,
309
+ properties: {
310
+ [propertyKey]: schema,
311
+ },
312
+ },
313
+ property: propertyKey,
314
+ });
315
+ });
316
+ });
317
+ describe('header', function () {
318
+ it('sets a given "propertyKey" to the target metadata', function () {
319
+ class Target {
320
+ myMethod(prop) { }
321
+ }
322
+ __decorate([
323
+ __param(0, header('myPropertyKey')),
324
+ __metadata("design:type", Function),
325
+ __metadata("design:paramtypes", [Object]),
326
+ __metadata("design:returntype", void 0)
327
+ ], Target.prototype, "myMethod", null);
328
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
329
+ expect(res.get(0)).to.be.eql({
330
+ source: RequestDataSource.HEADERS,
331
+ schema: { type: DataType.OBJECT },
332
+ property: 'myPropertyKey',
333
+ });
334
+ });
335
+ it('sets a given DataType as property type', function () {
336
+ const propertyKey = 'myPropertyKey';
337
+ const propertyType = DataType.STRING;
338
+ class Target {
339
+ myMethod(prop) { }
340
+ }
341
+ __decorate([
342
+ __param(0, header(propertyKey, propertyType)),
343
+ __metadata("design:type", Function),
344
+ __metadata("design:paramtypes", [Object]),
345
+ __metadata("design:returntype", void 0)
346
+ ], Target.prototype, "myMethod", null);
347
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
348
+ expect(res.get(0)).to.be.eql({
349
+ source: RequestDataSource.HEADERS,
350
+ schema: {
351
+ type: DataType.OBJECT,
352
+ properties: {
353
+ [propertyKey]: {
354
+ type: propertyType,
355
+ },
356
+ },
357
+ },
358
+ property: propertyKey,
359
+ });
360
+ });
361
+ it('sets a given DataSchema as property schema', function () {
362
+ const schema = {
363
+ type: DataType.STRING,
364
+ required: true,
365
+ };
366
+ const propertyKey = 'myPropertyKey';
367
+ class Target {
368
+ myMethod(prop) { }
369
+ }
370
+ __decorate([
371
+ __param(0, header(propertyKey, schema)),
372
+ __metadata("design:type", Function),
373
+ __metadata("design:paramtypes", [Object]),
374
+ __metadata("design:returntype", void 0)
375
+ ], Target.prototype, "myMethod", null);
376
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
377
+ expect(res.get(0)).to.be.eql({
378
+ source: RequestDataSource.HEADERS,
379
+ schema: {
380
+ type: DataType.OBJECT,
381
+ properties: {
382
+ [propertyKey]: schema,
383
+ },
384
+ },
385
+ property: propertyKey,
386
+ });
387
+ });
388
+ });
389
+ describe('cookie', function () {
390
+ it('sets a given "propertyKey" to the target metadata', function () {
391
+ class Target {
392
+ myMethod(prop) { }
393
+ }
394
+ __decorate([
395
+ __param(0, cookie('myPropertyKey')),
396
+ __metadata("design:type", Function),
397
+ __metadata("design:paramtypes", [Object]),
398
+ __metadata("design:returntype", void 0)
399
+ ], Target.prototype, "myMethod", null);
400
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
401
+ expect(res.get(0)).to.be.eql({
402
+ source: RequestDataSource.COOKIE,
403
+ schema: { type: DataType.OBJECT },
404
+ property: 'myPropertyKey',
405
+ });
406
+ });
407
+ it('sets a given DataType as property type', function () {
408
+ const propertyKey = 'myPropertyKey';
409
+ const propertyType = DataType.STRING;
410
+ class Target {
411
+ myMethod(prop) { }
412
+ }
413
+ __decorate([
414
+ __param(0, cookie(propertyKey, propertyType)),
415
+ __metadata("design:type", Function),
416
+ __metadata("design:paramtypes", [Object]),
417
+ __metadata("design:returntype", void 0)
418
+ ], Target.prototype, "myMethod", null);
419
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
420
+ expect(res.get(0)).to.be.eql({
421
+ source: RequestDataSource.COOKIE,
422
+ schema: {
423
+ type: DataType.OBJECT,
424
+ properties: {
425
+ [propertyKey]: {
426
+ type: propertyType,
427
+ },
428
+ },
429
+ },
430
+ property: propertyKey,
431
+ });
432
+ });
433
+ it('sets a given DataSchema as property schema', function () {
434
+ const schema = {
435
+ type: DataType.STRING,
436
+ required: true,
437
+ };
438
+ const propertyKey = 'myPropertyKey';
439
+ class Target {
440
+ myMethod(prop) { }
441
+ }
442
+ __decorate([
443
+ __param(0, cookie(propertyKey, schema)),
444
+ __metadata("design:type", Function),
445
+ __metadata("design:paramtypes", [Object]),
446
+ __metadata("design:returntype", void 0)
447
+ ], Target.prototype, "myMethod", null);
448
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
449
+ expect(res.get(0)).to.be.eql({
450
+ source: RequestDataSource.COOKIE,
451
+ schema: {
452
+ type: DataType.OBJECT,
453
+ properties: {
454
+ [propertyKey]: schema,
455
+ },
456
+ },
457
+ property: propertyKey,
458
+ });
459
+ });
460
+ });
461
+ describe('bodyParam', function () {
462
+ it('sets a given "propertyKey" to the target metadata', function () {
463
+ class Target {
464
+ myMethod(prop) { }
465
+ }
466
+ __decorate([
467
+ __param(0, bodyParam('myPropertyKey')),
468
+ __metadata("design:type", Function),
469
+ __metadata("design:paramtypes", [Object]),
470
+ __metadata("design:returntype", void 0)
471
+ ], Target.prototype, "myMethod", null);
472
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
473
+ expect(res.get(0)).to.be.eql({
474
+ source: RequestDataSource.BODY,
475
+ schema: { type: DataType.OBJECT },
476
+ property: 'myPropertyKey',
477
+ });
478
+ });
479
+ it('sets a given DataType as property type', function () {
480
+ const propertyKey = 'myPropertyKey';
481
+ const propertyType = DataType.STRING;
482
+ class Target {
483
+ myMethod(prop) { }
484
+ }
485
+ __decorate([
486
+ __param(0, bodyParam(propertyKey, propertyType)),
487
+ __metadata("design:type", Function),
488
+ __metadata("design:paramtypes", [Object]),
489
+ __metadata("design:returntype", void 0)
490
+ ], Target.prototype, "myMethod", null);
491
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
492
+ expect(res.get(0)).to.be.eql({
493
+ source: RequestDataSource.BODY,
494
+ schema: {
495
+ type: DataType.OBJECT,
496
+ properties: {
497
+ [propertyKey]: {
498
+ type: propertyType,
499
+ },
500
+ },
501
+ },
502
+ property: propertyKey,
503
+ });
504
+ });
505
+ it('sets a given DataSchema as property schema', function () {
506
+ const schema = {
507
+ type: DataType.STRING,
508
+ required: true,
509
+ };
510
+ const propertyKey = 'myPropertyKey';
511
+ class Target {
512
+ myMethod(prop) { }
513
+ }
514
+ __decorate([
515
+ __param(0, bodyParam(propertyKey, schema)),
516
+ __metadata("design:type", Function),
517
+ __metadata("design:paramtypes", [Object]),
518
+ __metadata("design:returntype", void 0)
519
+ ], Target.prototype, "myMethod", null);
520
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
521
+ expect(res.get(0)).to.be.eql({
522
+ source: RequestDataSource.BODY,
523
+ schema: {
524
+ type: DataType.OBJECT,
525
+ properties: {
526
+ [propertyKey]: schema,
527
+ },
528
+ },
529
+ property: propertyKey,
530
+ });
531
+ });
532
+ });
533
+ });
534
+ });
@@ -0,0 +1,29 @@
1
+ import { DataSchema } from '@e22m4u/ts-data-schema';
2
+ import { MetadataKey } from '@e22m4u/ts-reflector';
3
+ /**
4
+ * Request data source.
5
+ */
6
+ export declare enum RequestDataSource {
7
+ PARAMS = "params",
8
+ QUERY = "query",
9
+ HEADERS = "headers",
10
+ COOKIE = "cookie",
11
+ BODY = "body"
12
+ }
13
+ /**
14
+ * Request data metadata.
15
+ */
16
+ export type RequestDataMetadata = {
17
+ source: RequestDataSource;
18
+ schema?: DataSchema;
19
+ property?: string;
20
+ [option: string]: unknown | undefined;
21
+ };
22
+ /**
23
+ * Request data metadata map.
24
+ */
25
+ export type RequestDataMetadataMap = Map<number, RequestDataMetadata>;
26
+ /**
27
+ * Request data metadata key.
28
+ */
29
+ export declare const REQUEST_DATA_METADATA_KEY: MetadataKey<RequestDataMetadataMap>;
@@ -0,0 +1,16 @@
1
+ import { MetadataKey } from '@e22m4u/ts-reflector';
2
+ /**
3
+ * Request data source.
4
+ */
5
+ export var RequestDataSource;
6
+ (function (RequestDataSource) {
7
+ RequestDataSource["PARAMS"] = "params";
8
+ RequestDataSource["QUERY"] = "query";
9
+ RequestDataSource["HEADERS"] = "headers";
10
+ RequestDataSource["COOKIE"] = "cookie";
11
+ RequestDataSource["BODY"] = "body";
12
+ })(RequestDataSource || (RequestDataSource = {}));
13
+ /**
14
+ * Request data metadata key.
15
+ */
16
+ export const REQUEST_DATA_METADATA_KEY = new MetadataKey('requestDataMetadataKey');
@@ -0,0 +1,24 @@
1
+ import { Constructor } from '../../types.js';
2
+ import { RequestDataMetadata } from './request-data-metadata.js';
3
+ import { RequestDataMetadataMap } from './request-data-metadata.js';
4
+ /**
5
+ * Request data reflector.
6
+ */
7
+ export declare class RequestDataReflector {
8
+ /**
9
+ * Set metadata.
10
+ *
11
+ * @param metadata
12
+ * @param target
13
+ * @param index
14
+ * @param propertyKey
15
+ */
16
+ static setMetadata(metadata: RequestDataMetadata, 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): RequestDataMetadataMap;
24
+ }
@@ -0,0 +1,31 @@
1
+ import { Reflector } from '@e22m4u/ts-reflector';
2
+ import { REQUEST_DATA_METADATA_KEY } from './request-data-metadata.js';
3
+ /**
4
+ * Request data reflector.
5
+ */
6
+ export class RequestDataReflector {
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_DATA_METADATA_KEY, target, propertyKey);
17
+ const newMap = new Map(oldMap);
18
+ newMap.set(index, metadata);
19
+ Reflector.defineMetadata(REQUEST_DATA_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_DATA_METADATA_KEY, target, propertyKey);
29
+ return metadata ?? new Map();
30
+ }
31
+ }