@e22m4u/ts-rest-router 0.1.2 → 0.2.1

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 (145) hide show
  1. package/README.md +171 -74
  2. package/dist/cjs/index.cjs +352 -362
  3. package/dist/esm/controller-registry.js +37 -32
  4. package/dist/esm/debuggable-service.d.ts +10 -1
  5. package/dist/esm/debuggable-service.js +14 -3
  6. package/dist/esm/decorators/after-action/after-action-decorator.d.ts +9 -0
  7. package/dist/esm/decorators/{before/before-decorator.js → after-action/after-action-decorator.js} +7 -7
  8. package/dist/esm/decorators/{before/before-decorator.spec.js → after-action/after-action-decorator.spec.js} +19 -19
  9. package/dist/esm/decorators/{after/after-metadata.d.ts → after-action/after-action-metadata.d.ts} +4 -4
  10. package/dist/esm/decorators/after-action/after-action-metadata.js +5 -0
  11. package/dist/esm/decorators/{after/after-reflector.d.ts → after-action/after-action-reflector.d.ts} +5 -5
  12. package/dist/esm/decorators/{after/after-reflector.js → after-action/after-action-reflector.js} +6 -6
  13. package/dist/esm/decorators/{before/before-reflector.spec.js → after-action/after-action-reflector.spec.js} +23 -24
  14. package/dist/esm/decorators/after-action/index.d.ts +3 -0
  15. package/dist/esm/decorators/after-action/index.js +3 -0
  16. package/dist/esm/decorators/before-action/before-action-decorator.d.ts +9 -0
  17. package/dist/esm/decorators/{after/after-decorator.js → before-action/before-action-decorator.js} +7 -7
  18. package/dist/esm/decorators/{after/after-decorator.spec.js → before-action/before-action-decorator.spec.js} +19 -19
  19. package/dist/esm/decorators/{before/before-metadata.d.ts → before-action/before-action-metadata.d.ts} +4 -4
  20. package/dist/esm/decorators/before-action/before-action-metadata.js +5 -0
  21. package/dist/esm/decorators/{before/before-reflector.d.ts → before-action/before-action-reflector.d.ts} +5 -5
  22. package/dist/esm/decorators/{before/before-reflector.js → before-action/before-action-reflector.js} +6 -6
  23. package/dist/esm/decorators/{after/after-reflector.spec.js → before-action/before-action-reflector.spec.js} +23 -24
  24. package/dist/esm/decorators/before-action/index.d.ts +3 -0
  25. package/dist/esm/decorators/before-action/index.js +3 -0
  26. package/dist/esm/decorators/index.d.ts +4 -4
  27. package/dist/esm/decorators/index.js +4 -4
  28. package/dist/esm/decorators/request-context/request-context-decorator.d.ts +4 -4
  29. package/dist/esm/decorators/request-context/request-context-decorator.js +4 -4
  30. package/dist/esm/decorators/request-context/request-context-decorator.spec.js +6 -0
  31. package/dist/esm/decorators/request-context/request-context-reflector.spec.js +0 -1
  32. package/dist/esm/decorators/request-data/request-data-decorator.d.ts +10 -10
  33. package/dist/esm/decorators/request-data/request-data-decorator.js +10 -10
  34. package/dist/esm/decorators/request-data/request-data-decorator.spec.js +44 -32
  35. package/dist/esm/decorators/request-data/request-data-metadata.d.ts +1 -1
  36. package/dist/esm/decorators/request-data/request-data-reflector.spec.js +0 -1
  37. package/dist/esm/decorators/rest-action/index.d.ts +3 -0
  38. package/dist/esm/decorators/rest-action/index.js +3 -0
  39. package/dist/esm/decorators/rest-action/rest-action-decorator.d.ts +57 -0
  40. package/dist/esm/decorators/rest-action/rest-action-decorator.js +52 -0
  41. package/dist/esm/decorators/rest-action/rest-action-decorator.spec.js +401 -0
  42. package/dist/esm/decorators/{action/action-metadata.d.ts → rest-action/rest-action-metadata.d.ts} +6 -6
  43. package/dist/esm/decorators/rest-action/rest-action-metadata.js +5 -0
  44. package/dist/esm/decorators/rest-action/rest-action-reflector.d.ts +22 -0
  45. package/dist/esm/decorators/{action/action-reflector.js → rest-action/rest-action-reflector.js} +6 -6
  46. package/dist/esm/decorators/{action/action-reflector.spec.js → rest-action/rest-action-reflector.spec.js} +13 -14
  47. package/dist/esm/decorators/rest-controller/index.d.ts +3 -0
  48. package/dist/esm/decorators/rest-controller/index.js +3 -0
  49. package/dist/esm/decorators/rest-controller/rest-controller-decorator.d.ts +14 -0
  50. package/dist/esm/decorators/{controller/controller-decorator.js → rest-controller/rest-controller-decorator.js} +5 -5
  51. package/dist/esm/decorators/{controller/controller-decorator.spec.js → rest-controller/rest-controller-decorator.spec.js} +15 -15
  52. package/dist/esm/decorators/{controller/controller-metadata.d.ts → rest-controller/rest-controller-metadata.d.ts} +5 -5
  53. package/dist/esm/decorators/rest-controller/rest-controller-metadata.js +5 -0
  54. package/dist/esm/decorators/rest-controller/rest-controller-reflector.d.ts +20 -0
  55. package/dist/esm/decorators/rest-controller/rest-controller-reflector.js +24 -0
  56. package/dist/esm/decorators/{controller/controller-reflector.spec.js → rest-controller/rest-controller-reflector.spec.js} +12 -13
  57. package/dist/esm/errors/not-a-controller-error.js +1 -1
  58. package/dist/esm/types.d.ts +0 -10
  59. package/dist/esm/utils/index.d.ts +0 -1
  60. package/dist/esm/utils/index.js +0 -1
  61. package/eslint.config.js +1 -1
  62. package/package.json +15 -15
  63. package/src/controller-registry.spec.ts +192 -134
  64. package/src/controller-registry.ts +46 -39
  65. package/src/debuggable-service.spec.ts +0 -1
  66. package/src/debuggable-service.ts +17 -4
  67. package/src/decorators/{before/before-decorator.spec.ts → after-action/after-action-decorator.spec.ts} +19 -19
  68. package/src/decorators/{before/before-decorator.ts → after-action/after-action-decorator.ts} +9 -9
  69. package/src/decorators/{after/after-metadata.ts → after-action/after-action-metadata.ts} +5 -5
  70. package/src/decorators/{before/before-reflector.spec.ts → after-action/after-action-reflector.spec.ts} +33 -24
  71. package/src/decorators/{before/before-reflector.ts → after-action/after-action-reflector.ts} +13 -9
  72. package/src/decorators/after-action/index.ts +3 -0
  73. package/src/decorators/{after/after-decorator.spec.ts → before-action/before-action-decorator.spec.ts} +19 -19
  74. package/src/decorators/{after/after-decorator.ts → before-action/before-action-decorator.ts} +9 -9
  75. package/src/decorators/before-action/before-action-metadata.ts +17 -0
  76. package/src/decorators/{after/after-reflector.spec.ts → before-action/before-action-reflector.spec.ts} +40 -24
  77. package/src/decorators/{after/after-reflector.ts → before-action/before-action-reflector.ts} +18 -9
  78. package/src/decorators/before-action/index.ts +3 -0
  79. package/src/decorators/index.ts +4 -4
  80. package/src/decorators/request-context/request-context-decorator.spec.ts +7 -0
  81. package/src/decorators/request-context/request-context-decorator.ts +4 -4
  82. package/src/decorators/request-context/request-context-reflector.spec.ts +0 -1
  83. package/src/decorators/request-data/request-data-decorator.spec.ts +45 -32
  84. package/src/decorators/request-data/request-data-decorator.ts +10 -10
  85. package/src/decorators/request-data/request-data-metadata.ts +1 -1
  86. package/src/decorators/request-data/request-data-reflector.spec.ts +0 -1
  87. package/src/decorators/rest-action/index.ts +3 -0
  88. package/src/decorators/rest-action/rest-action-decorator.spec.ts +325 -0
  89. package/src/decorators/rest-action/rest-action-decorator.ts +166 -0
  90. package/src/decorators/{action/action-metadata.ts → rest-action/rest-action-metadata.ts} +7 -7
  91. package/src/decorators/{action/action-reflector.spec.ts → rest-action/rest-action-reflector.spec.ts} +13 -14
  92. package/src/decorators/rest-action/rest-action-reflector.ts +41 -0
  93. package/src/decorators/rest-controller/index.ts +3 -0
  94. package/src/decorators/{controller/controller-decorator.spec.ts → rest-controller/rest-controller-decorator.spec.ts} +16 -16
  95. package/src/decorators/{controller/controller-decorator.ts → rest-controller/rest-controller-decorator.ts} +14 -10
  96. package/src/decorators/{controller/controller-metadata.ts → rest-controller/rest-controller-metadata.ts} +6 -7
  97. package/src/decorators/{controller/controller-reflector.spec.ts → rest-controller/rest-controller-reflector.spec.ts} +21 -13
  98. package/src/decorators/rest-controller/rest-controller-reflector.ts +32 -0
  99. package/src/errors/not-a-controller-error.ts +4 -1
  100. package/src/types.ts +0 -10
  101. package/src/utils/index.ts +0 -1
  102. package/dist/esm/decorators/action/action-decorator.d.ts +0 -52
  103. package/dist/esm/decorators/action/action-decorator.js +0 -62
  104. package/dist/esm/decorators/action/action-decorator.spec.js +0 -59
  105. package/dist/esm/decorators/action/action-metadata.js +0 -5
  106. package/dist/esm/decorators/action/action-reflector.d.ts +0 -22
  107. package/dist/esm/decorators/action/index.d.ts +0 -3
  108. package/dist/esm/decorators/action/index.js +0 -3
  109. package/dist/esm/decorators/after/after-decorator.d.ts +0 -9
  110. package/dist/esm/decorators/after/after-metadata.js +0 -5
  111. package/dist/esm/decorators/after/index.d.ts +0 -3
  112. package/dist/esm/decorators/after/index.js +0 -3
  113. package/dist/esm/decorators/before/before-decorator.d.ts +0 -9
  114. package/dist/esm/decorators/before/before-metadata.js +0 -5
  115. package/dist/esm/decorators/before/index.d.ts +0 -3
  116. package/dist/esm/decorators/before/index.js +0 -3
  117. package/dist/esm/decorators/controller/controller-decorator.d.ts +0 -14
  118. package/dist/esm/decorators/controller/controller-metadata.js +0 -5
  119. package/dist/esm/decorators/controller/controller-reflector.d.ts +0 -20
  120. package/dist/esm/decorators/controller/controller-reflector.js +0 -24
  121. package/dist/esm/decorators/controller/index.d.ts +0 -3
  122. package/dist/esm/decorators/controller/index.js +0 -3
  123. package/dist/esm/utils/create-debugger.d.ts +0 -44
  124. package/dist/esm/utils/create-debugger.js +0 -81
  125. package/dist/esm/utils/create-debugger.spec.d.ts +0 -1
  126. package/dist/esm/utils/create-debugger.spec.js +0 -8
  127. package/src/decorators/action/action-decorator.spec.ts +0 -42
  128. package/src/decorators/action/action-decorator.ts +0 -98
  129. package/src/decorators/action/action-reflector.ts +0 -38
  130. package/src/decorators/action/index.ts +0 -3
  131. package/src/decorators/after/index.ts +0 -3
  132. package/src/decorators/before/before-metadata.ts +0 -17
  133. package/src/decorators/before/index.ts +0 -3
  134. package/src/decorators/controller/controller-reflector.ts +0 -28
  135. package/src/decorators/controller/index.ts +0 -3
  136. package/src/utils/create-debugger.spec.ts +0 -9
  137. package/src/utils/create-debugger.ts +0 -98
  138. /package/dist/esm/decorators/{action/action-decorator.spec.d.ts → after-action/after-action-decorator.spec.d.ts} +0 -0
  139. /package/dist/esm/decorators/{action/action-reflector.spec.d.ts → after-action/after-action-reflector.spec.d.ts} +0 -0
  140. /package/dist/esm/decorators/{after/after-decorator.spec.d.ts → before-action/before-action-decorator.spec.d.ts} +0 -0
  141. /package/dist/esm/decorators/{after/after-reflector.spec.d.ts → before-action/before-action-reflector.spec.d.ts} +0 -0
  142. /package/dist/esm/decorators/{before/before-decorator.spec.d.ts → rest-action/rest-action-decorator.spec.d.ts} +0 -0
  143. /package/dist/esm/decorators/{before/before-reflector.spec.d.ts → rest-action/rest-action-reflector.spec.d.ts} +0 -0
  144. /package/dist/esm/decorators/{controller/controller-decorator.spec.d.ts → rest-controller/rest-controller-decorator.spec.d.ts} +0 -0
  145. /package/dist/esm/decorators/{controller/controller-reflector.spec.d.ts → rest-controller/rest-controller-reflector.spec.d.ts} +0 -0
@@ -4,18 +4,19 @@ import {Errorf} from '@e22m4u/js-format';
4
4
  import {TrieRouter} from '@e22m4u/js-trie-router';
5
5
  import {RouteHandler} from '@e22m4u/js-trie-router';
6
6
  import {DataValidator} from '@e22m4u/ts-data-schema';
7
- import {AfterReflector} from './decorators/index.js';
8
7
  import {DataTypeCaster} from '@e22m4u/ts-data-schema';
9
- import {ActionReflector} from './decorators/index.js';
10
- import {BeforeReflector} from './decorators/index.js';
11
8
  import {NotAControllerError} from './errors/index.js';
12
9
  import {RequestContext} from '@e22m4u/js-trie-router';
13
10
  import {RoutePreHandler} from '@e22m4u/js-trie-router';
14
11
  import {RoutePostHandler} from '@e22m4u/js-trie-router';
15
12
  import {RequestDataSource} from './decorators/index.js';
16
13
  import {DebuggableService} from './debuggable-service.js';
17
- import {ControllerReflector} from './decorators/index.js';
14
+ import {RestActionReflector} from './decorators/index.js';
18
15
  import {RequestDataReflector} from './decorators/index.js';
16
+ import {AfterActionReflector} from './decorators/index.js';
17
+ import {DefaultValuesApplier} from '@e22m4u/ts-data-schema';
18
+ import {BeforeActionReflector} from './decorators/index.js';
19
+ import {RestControllerReflector} from './decorators/index.js';
19
20
  import {RequestContextReflector} from './decorators/index.js';
20
21
 
21
22
  /**
@@ -46,7 +47,7 @@ export class ControllerRegistry extends DebuggableService {
46
47
  ctor: Constructor<T>,
47
48
  options?: ControllerRootOptions,
48
49
  ): this {
49
- const debug = this.debug.bind(this.addController.name);
50
+ const debug = this.getDebuggerFor(this.addController);
50
51
  // проверка повторной регистрации помогает
51
52
  // заметить ошибку в коде, который использует
52
53
  // интерфейс данного сервиса
@@ -55,7 +56,7 @@ export class ControllerRegistry extends DebuggableService {
55
56
  // так как контроллером может быть любой
56
57
  // класс, выполняется проверка на наличие
57
58
  // метаданных применяемых декоратором
58
- const controllerMd = ControllerReflector.getMetadata(ctor);
59
+ const controllerMd = RestControllerReflector.getMetadata(ctor);
59
60
  if (!controllerMd) throw new NotAControllerError(ctor);
60
61
  debug('Adding controller %s.', ctor.name);
61
62
  // определение префикса применяемого
@@ -82,7 +83,7 @@ export class ControllerRegistry extends DebuggableService {
82
83
  debug('Controller has %v post-handlers.', postHandlers.length);
83
84
  // обход всех операций контроллера
84
85
  // для определения маршрутов
85
- const actionsMd = ActionReflector.getMetadata(ctor);
86
+ const actionsMd = RestActionReflector.getMetadata(ctor);
86
87
  debug('%v actions found.', actionsMd.size);
87
88
  const router = this.getService(TrieRouter);
88
89
  actionsMd.forEach((actionMd, actionName) => {
@@ -143,8 +144,8 @@ export class ControllerRegistry extends DebuggableService {
143
144
  protected getPathPrefixFromControllerRootOptions(
144
145
  options?: ControllerRootOptions,
145
146
  ) {
146
- const debug = this.debug.bind(
147
- this.getPathPrefixFromControllerRootOptions.name,
147
+ const debug = this.getDebuggerFor(
148
+ this.getPathPrefixFromControllerRootOptions,
148
149
  );
149
150
  debug('Getting path prefix from controller root options.');
150
151
  const res = options?.pathPrefix || '';
@@ -158,12 +159,10 @@ export class ControllerRegistry extends DebuggableService {
158
159
  * @param ctor
159
160
  */
160
161
  protected getPathPrefixFromControllerMetadata<T>(ctor: Constructor<T>) {
161
- const debug = this.debug.bind(
162
- this.getPathPrefixFromControllerMetadata.name,
163
- );
164
- debug('Getting path prefix from @controller metadata.');
162
+ const debug = this.getDebuggerFor(this.getPathPrefixFromControllerMetadata);
163
+ debug('Getting path prefix from @restController metadata.');
165
164
  debug('Metadata target is %s.', ctor.name);
166
- const md = ControllerReflector.getMetadata(ctor);
165
+ const md = RestControllerReflector.getMetadata(ctor);
167
166
  if (!md) throw new Errorf('Controller %v has no metadata.', ctor);
168
167
  const res = md.path || '';
169
168
  debug('Controller path prefix is %v.', res);
@@ -178,8 +177,8 @@ export class ControllerRegistry extends DebuggableService {
178
177
  protected getPreHandlersFromControllerRootOptions(
179
178
  options?: ControllerRootOptions,
180
179
  ) {
181
- const debug = this.debug.bind(
182
- this.getPreHandlersFromControllerRootOptions.name,
180
+ const debug = this.getDebuggerFor(
181
+ this.getPreHandlersFromControllerRootOptions,
183
182
  );
184
183
  debug('Getting pre-handlers from controller root options.');
185
184
  let res: RoutePreHandler[] = [];
@@ -197,8 +196,8 @@ export class ControllerRegistry extends DebuggableService {
197
196
  protected getPostHandlersFromControllerRootOptions(
198
197
  options?: ControllerRootOptions,
199
198
  ) {
200
- const debug = this.debug.bind(
201
- this.getPostHandlersFromControllerRootOptions.name,
199
+ const debug = this.getDebuggerFor(
200
+ this.getPostHandlersFromControllerRootOptions,
202
201
  );
203
202
  debug('Getting post-handlers from controller root options.');
204
203
  let res: RoutePostHandler[] = [];
@@ -218,7 +217,7 @@ export class ControllerRegistry extends DebuggableService {
218
217
  ctor: Constructor<T>,
219
218
  actionName?: string,
220
219
  ) {
221
- const debug = this.debug.bind(this.getPreHandlersFromBeforeMetadata.name);
220
+ const debug = this.getDebuggerFor(this.getPreHandlersFromBeforeMetadata);
222
221
  debug('Getting pre-handlers from @before metadata.');
223
222
  if (actionName) {
224
223
  debug('Target is %s.%s.', ctor.name, actionName);
@@ -226,7 +225,7 @@ export class ControllerRegistry extends DebuggableService {
226
225
  debug('Target is %s.', ctor.name);
227
226
  }
228
227
  let preHandlers: RoutePreHandler[] = [];
229
- const mdArray = BeforeReflector.getMetadata(ctor, actionName);
228
+ const mdArray = BeforeActionReflector.getMetadata(ctor, actionName);
230
229
  mdArray.forEach(md => {
231
230
  if (Array.isArray(md.middleware)) {
232
231
  preHandlers = [...preHandlers, ...md.middleware];
@@ -252,7 +251,7 @@ export class ControllerRegistry extends DebuggableService {
252
251
  ctor: Constructor<T>,
253
252
  actionName?: string,
254
253
  ) {
255
- const debug = this.debug.bind(this.getPostHandlersFromAfterMetadata.name);
254
+ const debug = this.getDebuggerFor(this.getPostHandlersFromAfterMetadata);
256
255
  debug('Getting post-handlers from @after metadata.');
257
256
  if (actionName) {
258
257
  debug('Target is %s.%s.', ctor.name, actionName);
@@ -260,7 +259,7 @@ export class ControllerRegistry extends DebuggableService {
260
259
  debug('Target is %s.', ctor.name);
261
260
  }
262
261
  let res: RoutePostHandler[] = [];
263
- const mdArray = AfterReflector.getMetadata(ctor, actionName);
262
+ const mdArray = AfterActionReflector.getMetadata(ctor, actionName);
264
263
  mdArray.forEach(md => {
265
264
  if (Array.isArray(md.middleware)) {
266
265
  res = [...res, ...md.middleware];
@@ -282,12 +281,12 @@ export class ControllerRegistry extends DebuggableService {
282
281
  * @param ctor
283
282
  */
284
283
  protected getPreHandlersFromControllerMetadata<T>(ctor: Constructor<T>) {
285
- const debug = this.debug.bind(
286
- this.getPreHandlersFromControllerMetadata.name,
284
+ const debug = this.getDebuggerFor(
285
+ this.getPreHandlersFromControllerMetadata,
287
286
  );
288
- debug('Getting pre-handlers from @controller metadata.');
287
+ debug('Getting pre-handlers from @restController metadata.');
289
288
  debug('Target is %s.', ctor.name);
290
- const md = ControllerReflector.getMetadata(ctor);
289
+ const md = RestControllerReflector.getMetadata(ctor);
291
290
  if (!md) throw new Errorf('Controller %v has no metadata.', ctor);
292
291
  let res: RoutePreHandler[] = [];
293
292
  if (md.before) res = Array.isArray(md.before) ? md.before : [md.before];
@@ -301,11 +300,11 @@ export class ControllerRegistry extends DebuggableService {
301
300
  * @param ctor
302
301
  */
303
302
  protected getPostHandlersFromControllerMetadata<T>(ctor: Constructor<T>) {
304
- const debug = this.debug.bind(
305
- this.getPostHandlersFromControllerMetadata.name,
303
+ const debug = this.getDebuggerFor(
304
+ this.getPostHandlersFromControllerMetadata,
306
305
  );
307
- debug('Getting post-handlers from @controller metadata.');
308
- const md = ControllerReflector.getMetadata(ctor);
306
+ debug('Getting post-handlers from @restController metadata.');
307
+ const md = RestControllerReflector.getMetadata(ctor);
309
308
  if (!md) throw new Errorf('Controller %v has no metadata.', ctor);
310
309
  let res: RoutePostHandler[] = [];
311
310
  if (md.after) res = Array.isArray(md.after) ? md.after : [md.after];
@@ -323,9 +322,9 @@ export class ControllerRegistry extends DebuggableService {
323
322
  ctor: Constructor<T>,
324
323
  actionName: string,
325
324
  ) {
326
- const debug = this.debug.bind(this.getPreHandlersFromActionMetadata.name);
325
+ const debug = this.getDebuggerFor(this.getPreHandlersFromActionMetadata);
327
326
  debug('Getting pre-handlers from @action metadata.');
328
- const actionsMd = ActionReflector.getMetadata(ctor);
327
+ const actionsMd = RestActionReflector.getMetadata(ctor);
329
328
  const actionMd = actionsMd.get(actionName);
330
329
  if (!actionMd)
331
330
  throw new Errorf('Action %s.%s has no metadata.', ctor.name, actionName);
@@ -348,9 +347,9 @@ export class ControllerRegistry extends DebuggableService {
348
347
  ctor: Constructor<T>,
349
348
  actionName: string,
350
349
  ) {
351
- const debug = this.debug.bind(this.getPreHandlersFromActionMetadata.name);
350
+ const debug = this.getDebuggerFor(this.getPreHandlersFromActionMetadata);
352
351
  debug('Getting post-handlers from @action metadata.');
353
- const actionsMd = ActionReflector.getMetadata(ctor);
352
+ const actionsMd = RestActionReflector.getMetadata(ctor);
354
353
  const actionMd = actionsMd.get(actionName);
355
354
  if (!actionMd)
356
355
  throw new Errorf('Action %s.%s has no metadata.', ctor.name, actionName);
@@ -372,7 +371,7 @@ export class ControllerRegistry extends DebuggableService {
372
371
  controllerCtor: Constructor<T>,
373
372
  actionName: string,
374
373
  ): RouteHandler {
375
- const debug = this.debug.bind(this.createRouteHandler.name);
374
+ const debug = this.getDebuggerFor(this.createRouteHandler);
376
375
  debug('Creating route handler for %s.%s.', controllerCtor.name, actionName);
377
376
  const requestContextMetadataMap = RequestContextReflector.getMetadata(
378
377
  controllerCtor,
@@ -383,6 +382,7 @@ export class ControllerRegistry extends DebuggableService {
383
382
  actionName,
384
383
  );
385
384
  const argsNumber = controllerCtor.prototype[actionName].length;
385
+ const defaultsApplier = this.getService(DefaultValuesApplier);
386
386
  const dataTypeCaster = this.getService(DataTypeCaster);
387
387
  const dataValidator = this.getService(DataValidator);
388
388
  return (requestContext: RequestContext) => {
@@ -447,20 +447,27 @@ export class ControllerRegistry extends DebuggableService {
447
447
  break;
448
448
  }
449
449
  debug('Request data source is %v.', requestDataMd.source);
450
- // при наличии схемы данных выполняется
451
- // их конвертация и валидация
450
+ // при наличии схемы данных применяются значения
451
+ // по умолчанию, выполняется конвертация входящего
452
+ // значения и валидация согласно схеме
452
453
  if (requestDataMd.schema) {
454
+ data = defaultsApplier.applyDefaultValuesIfNeeded(
455
+ data,
456
+ requestDataMd.schema,
457
+ requestDataMd.source,
458
+ );
459
+ debug('Default values applied.');
453
460
  data = dataTypeCaster.cast(data, requestDataMd.schema, {
454
461
  noTypeCastError: true,
455
462
  sourcePath: requestDataMd.source,
456
463
  });
457
- debug('Data type casting is passed.');
464
+ debug('Data type casting applied.');
458
465
  dataValidator.validate(
459
466
  data,
460
467
  requestDataMd.schema,
461
468
  requestDataMd.source,
462
469
  );
463
- debug('Data validation is passed.');
470
+ debug('Data validation passed.');
464
471
  }
465
472
  // если свойство данных не определено,
466
473
  // то используем весь объекта данных
@@ -1,5 +1,4 @@
1
1
  import {expect} from 'chai';
2
- import {describe} from 'mocha';
3
2
  import {Service} from '@e22m4u/js-service';
4
3
  import {DebuggableService} from './debuggable-service.js';
5
4
 
@@ -1,7 +1,8 @@
1
- import {Debugger} from './utils/index.js';
1
+ import {Callable} from './types.js';
2
+ import {Debugger} from '@e22m4u/js-debug';
2
3
  import {Service} from '@e22m4u/js-service';
3
4
  import {toCamelCase} from './utils/index.js';
4
- import {createDebugger} from './utils/index.js';
5
+ import {createDebugger} from '@e22m4u/js-debug';
5
6
  import {ServiceContainer} from '@e22m4u/js-service';
6
7
 
7
8
  /**
@@ -13,6 +14,17 @@ export class DebuggableService extends Service {
13
14
  */
14
15
  debug: Debugger;
15
16
 
17
+ /**
18
+ * Возвращает функцию-отладчик с сегментом пространства имен
19
+ * указанного в параметре метода.
20
+ *
21
+ * @param method
22
+ * @protected
23
+ */
24
+ protected getDebuggerFor(method: Callable) {
25
+ return this.debug.withHash().withNs(method.name);
26
+ }
27
+
16
28
  /**
17
29
  * Constructor.
18
30
  *
@@ -21,7 +33,8 @@ export class DebuggableService extends Service {
21
33
  constructor(container?: ServiceContainer) {
22
34
  super(container);
23
35
  const serviceName = toCamelCase(this.constructor.name);
24
- this.debug = createDebugger(serviceName);
25
- this.debug.bind('constructor')('Service created.');
36
+ this.debug = createDebugger('tsRestRouter', serviceName);
37
+ const debug = this.debug.withNs('constructor').withHash();
38
+ debug('Service created.');
26
39
  }
27
40
  }
@@ -1,39 +1,39 @@
1
1
  /* eslint mocha/no-sibling-hooks: 0 */
2
2
  import {expect} from 'chai';
3
- import {before} from './before-decorator.js';
4
- import {BeforeReflector} from './before-reflector.js';
3
+ import {afterAction} from './after-action-decorator.js';
4
+ import {AfterActionReflector} from './after-action-reflector.js';
5
5
 
6
6
  const MIDDLEWARE_1 = () => undefined;
7
7
  const MIDDLEWARE_2 = () => undefined;
8
8
  const MIDDLEWARE_3 = () => undefined;
9
9
 
10
- describe('before', function () {
10
+ describe('afterAction', function () {
11
11
  describe('class target', function () {
12
12
  it('sets given middleware to the target metadata', function () {
13
- @before(MIDDLEWARE_1)
13
+ @afterAction(MIDDLEWARE_1)
14
14
  class Target {
15
15
  method() {}
16
16
  }
17
- const res = BeforeReflector.getMetadata(Target);
17
+ const res = AfterActionReflector.getMetadata(Target);
18
18
  expect(res).to.be.eql([{middleware: MIDDLEWARE_1}]);
19
19
  });
20
20
 
21
- it('sets miltiple middlewares to the target metadata', function () {
22
- @before([MIDDLEWARE_1, MIDDLEWARE_2])
21
+ it('sets multiple middlewares to the target metadata', function () {
22
+ @afterAction([MIDDLEWARE_1, MIDDLEWARE_2])
23
23
  class Target {
24
24
  method() {}
25
25
  }
26
- const res = BeforeReflector.getMetadata(Target);
26
+ const res = AfterActionReflector.getMetadata(Target);
27
27
  expect(res).to.be.eql([{middleware: [MIDDLEWARE_1, MIDDLEWARE_2]}]);
28
28
  });
29
29
 
30
30
  it('allows to use the decorator multiple times', function () {
31
- @before(MIDDLEWARE_1)
32
- @before([MIDDLEWARE_2, MIDDLEWARE_3])
31
+ @afterAction(MIDDLEWARE_1)
32
+ @afterAction([MIDDLEWARE_2, MIDDLEWARE_3])
33
33
  class Target {
34
34
  method() {}
35
35
  }
36
- const res = BeforeReflector.getMetadata(Target);
36
+ const res = AfterActionReflector.getMetadata(Target);
37
37
  expect(res).to.be.eql([
38
38
  {middleware: MIDDLEWARE_1},
39
39
  {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]},
@@ -44,10 +44,10 @@ describe('before', function () {
44
44
  describe('method target', function () {
45
45
  it('sets given middleware to the target metadata', function () {
46
46
  class Target {
47
- @before(MIDDLEWARE_1)
47
+ @afterAction(MIDDLEWARE_1)
48
48
  method() {}
49
49
  }
50
- const res = BeforeReflector.getMetadata(Target, 'method');
50
+ const res = AfterActionReflector.getMetadata(Target, 'method');
51
51
  expect(res).to.be.eql([
52
52
  {
53
53
  propertyKey: 'method',
@@ -56,12 +56,12 @@ describe('before', function () {
56
56
  ]);
57
57
  });
58
58
 
59
- it('sets miltiple middlewares to the target metadata', function () {
59
+ it('sets multiple middlewares to the target metadata', function () {
60
60
  class Target {
61
- @before([MIDDLEWARE_1, MIDDLEWARE_2])
61
+ @afterAction([MIDDLEWARE_1, MIDDLEWARE_2])
62
62
  method() {}
63
63
  }
64
- const res = BeforeReflector.getMetadata(Target, 'method');
64
+ const res = AfterActionReflector.getMetadata(Target, 'method');
65
65
  expect(res).to.be.eql([
66
66
  {
67
67
  propertyKey: 'method',
@@ -72,11 +72,11 @@ describe('before', function () {
72
72
 
73
73
  it('allows to use the decorator multiple times', function () {
74
74
  class Target {
75
- @before(MIDDLEWARE_1)
76
- @before([MIDDLEWARE_2, MIDDLEWARE_3])
75
+ @afterAction(MIDDLEWARE_1)
76
+ @afterAction([MIDDLEWARE_2, MIDDLEWARE_3])
77
77
  method() {}
78
78
  }
79
- const res = BeforeReflector.getMetadata(Target, 'method');
79
+ const res = AfterActionReflector.getMetadata(Target, 'method');
80
80
  expect(res).to.be.eql([
81
81
  {
82
82
  propertyKey: 'method',
@@ -1,17 +1,17 @@
1
1
  import {Prototype} from '../../types.js';
2
2
  import {Constructor} from '../../types.js';
3
- import {BeforeMetadata} from './before-metadata.js';
4
- import {BeforeReflector} from './before-reflector.js';
5
3
  import {DecoratorTargetType} from '@e22m4u/ts-reflector';
6
4
  import {getDecoratorTargetType} from '@e22m4u/ts-reflector';
5
+ import {AfterActionMetadata} from './after-action-metadata.js';
6
+ import {AfterActionReflector} from './after-action-reflector.js';
7
7
 
8
8
  /**
9
- * Before decorator.
9
+ * After action decorator.
10
10
  *
11
- * @param options
11
+ * @param middleware
12
12
  */
13
- export function before<T extends object>(
14
- middleware: BeforeMetadata['middleware'],
13
+ export function afterAction<T extends object>(
14
+ middleware: AfterActionMetadata['middleware'],
15
15
  ) {
16
16
  return function (
17
17
  target: Constructor<T> | Prototype<T>,
@@ -24,16 +24,16 @@ export function before<T extends object>(
24
24
  descriptor,
25
25
  );
26
26
  if (decoratorType === DecoratorTargetType.CONSTRUCTOR) {
27
- BeforeReflector.addMetadata({middleware}, target as Constructor<T>);
27
+ AfterActionReflector.addMetadata({middleware}, target as Constructor<T>);
28
28
  } else if (decoratorType === DecoratorTargetType.INSTANCE_METHOD) {
29
- BeforeReflector.addMetadata(
29
+ AfterActionReflector.addMetadata(
30
30
  {propertyKey, middleware},
31
31
  target.constructor as Constructor<T>,
32
32
  propertyKey,
33
33
  );
34
34
  } else {
35
35
  throw new Error(
36
- '@before decorator is only supported on a class or an instance method.',
36
+ '@afterAction decorator is only supported on a class or an instance method.',
37
37
  );
38
38
  }
39
39
  };
@@ -2,16 +2,16 @@ import {MetadataKey} from '@e22m4u/ts-reflector';
2
2
  import {RoutePostHandler} from '@e22m4u/js-trie-router';
3
3
 
4
4
  /**
5
- * After metadata.
5
+ * After action metadata.
6
6
  */
7
- export type AfterMetadata = {
7
+ export type AfterActionMetadata = {
8
8
  propertyKey?: string;
9
9
  middleware: RoutePostHandler | RoutePostHandler[];
10
10
  };
11
11
 
12
12
  /**
13
- * After metadata key.
13
+ * After action metadata key.
14
14
  */
15
- export const AFTER_METADATA_KEY = new MetadataKey<AfterMetadata[]>(
16
- 'afterMetadataKey',
15
+ export const AFTER_ACTION_METADATA_KEY = new MetadataKey<AfterActionMetadata[]>(
16
+ 'afterActionMetadataKey',
17
17
  );
@@ -1,23 +1,22 @@
1
1
  import {expect} from 'chai';
2
- import {describe} from 'mocha';
3
2
  import {Reflector} from '@e22m4u/ts-reflector';
4
- import {BeforeReflector} from './before-reflector.js';
5
- import {BEFORE_METADATA_KEY} from './before-metadata.js';
3
+ import {AfterActionReflector} from './after-action-reflector.js';
4
+ import {AFTER_ACTION_METADATA_KEY} from './after-action-metadata.js';
6
5
 
7
6
  const MIDDLEWARE_1 = () => undefined;
8
7
  const MIDDLEWARE_2 = () => undefined;
9
8
  const MIDDLEWARE_3 = () => undefined;
10
9
 
11
- describe('BeforeReflector', function () {
10
+ describe('AfterActionReflector', function () {
12
11
  describe('class target', function () {
13
12
  describe('addMetadata', function () {
14
13
  it('adds a given value to the target metadata', function () {
15
14
  class Target {}
16
15
  const md1 = {middleware: MIDDLEWARE_1};
17
16
  const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
18
- BeforeReflector.addMetadata(md1, Target);
19
- BeforeReflector.addMetadata(md2, Target);
20
- const res = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target);
17
+ AfterActionReflector.addMetadata(md1, Target);
18
+ AfterActionReflector.addMetadata(md2, Target);
19
+ const res = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
21
20
  expect(res).to.be.eql([md2, md1]);
22
21
  });
23
22
  });
@@ -25,7 +24,7 @@ describe('BeforeReflector', function () {
25
24
  describe('getMetadata', function () {
26
25
  it('returns an empty array if no metadata', function () {
27
26
  class Target {}
28
- const res = BeforeReflector.getMetadata(Target);
27
+ const res = AfterActionReflector.getMetadata(Target);
29
28
  expect(res).to.be.eql([]);
30
29
  });
31
30
 
@@ -34,8 +33,8 @@ describe('BeforeReflector', function () {
34
33
  const md1 = {middleware: MIDDLEWARE_1};
35
34
  const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
36
35
  const mdArray = [md1, md2];
37
- Reflector.defineMetadata(BEFORE_METADATA_KEY, mdArray, Target);
38
- const res = BeforeReflector.getMetadata(Target);
36
+ Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, mdArray, Target);
37
+ const res = AfterActionReflector.getMetadata(Target);
39
38
  expect(res).to.be.eql(mdArray);
40
39
  });
41
40
  });
@@ -47,10 +46,10 @@ describe('BeforeReflector', function () {
47
46
  class Target {}
48
47
  const md1 = {middleware: MIDDLEWARE_1};
49
48
  const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
50
- BeforeReflector.addMetadata(md1, Target, 'prop');
51
- BeforeReflector.addMetadata(md2, Target, 'prop');
49
+ AfterActionReflector.addMetadata(md1, Target, 'prop');
50
+ AfterActionReflector.addMetadata(md2, Target, 'prop');
52
51
  const res = Reflector.getOwnMetadata(
53
- BEFORE_METADATA_KEY,
52
+ AFTER_ACTION_METADATA_KEY,
54
53
  Target,
55
54
  'prop',
56
55
  );
@@ -61,7 +60,7 @@ describe('BeforeReflector', function () {
61
60
  describe('getMetadata', function () {
62
61
  it('returns an empty array if no metadata', function () {
63
62
  class Target {}
64
- const res = BeforeReflector.getMetadata(Target, 'prop');
63
+ const res = AfterActionReflector.getMetadata(Target, 'prop');
65
64
  expect(res).to.be.eql([]);
66
65
  });
67
66
 
@@ -70,8 +69,13 @@ describe('BeforeReflector', function () {
70
69
  const md1 = {middleware: MIDDLEWARE_1};
71
70
  const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
72
71
  const mdArray = [md1, md2];
73
- Reflector.defineMetadata(BEFORE_METADATA_KEY, mdArray, Target, 'prop');
74
- const res = BeforeReflector.getMetadata(Target, 'prop');
72
+ Reflector.defineMetadata(
73
+ AFTER_ACTION_METADATA_KEY,
74
+ mdArray,
75
+ Target,
76
+ 'prop',
77
+ );
78
+ const res = AfterActionReflector.getMetadata(Target, 'prop');
75
79
  expect(res).to.be.eql(mdArray);
76
80
  });
77
81
  });
@@ -82,11 +86,11 @@ describe('BeforeReflector', function () {
82
86
  class Target {}
83
87
  const md1 = {middleware: MIDDLEWARE_1};
84
88
  const md2 = {middleware: MIDDLEWARE_2};
85
- BeforeReflector.addMetadata(md1, Target);
86
- BeforeReflector.addMetadata(md2, Target, 'prop');
87
- const res1 = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target);
89
+ AfterActionReflector.addMetadata(md1, Target);
90
+ AfterActionReflector.addMetadata(md2, Target, 'prop');
91
+ const res1 = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
88
92
  const res2 = Reflector.getOwnMetadata(
89
- BEFORE_METADATA_KEY,
93
+ AFTER_ACTION_METADATA_KEY,
90
94
  Target,
91
95
  'prop',
92
96
  );
@@ -100,10 +104,15 @@ describe('BeforeReflector', function () {
100
104
  class Target {}
101
105
  const md1 = {middleware: MIDDLEWARE_1};
102
106
  const md2 = {middleware: MIDDLEWARE_2};
103
- Reflector.defineMetadata(BEFORE_METADATA_KEY, [md1], Target);
104
- Reflector.defineMetadata(BEFORE_METADATA_KEY, [md2], Target, 'prop');
105
- const res1 = BeforeReflector.getMetadata(Target);
106
- const res2 = BeforeReflector.getMetadata(Target, 'prop');
107
+ Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, [md1], Target);
108
+ Reflector.defineMetadata(
109
+ AFTER_ACTION_METADATA_KEY,
110
+ [md2],
111
+ Target,
112
+ 'prop',
113
+ );
114
+ const res1 = AfterActionReflector.getMetadata(Target);
115
+ const res2 = AfterActionReflector.getMetadata(Target, 'prop');
107
116
  expect(res1).to.be.eql([md1]);
108
117
  expect(res2).to.be.eql([md2]);
109
118
  });
@@ -1,12 +1,12 @@
1
1
  import {Constructor} from '../../types.js';
2
2
  import {Reflector} from '@e22m4u/ts-reflector';
3
- import {BeforeMetadata} from './before-metadata.js';
4
- import {BEFORE_METADATA_KEY} from './before-metadata.js';
3
+ import {AfterActionMetadata} from './after-action-metadata.js';
4
+ import {AFTER_ACTION_METADATA_KEY} from './after-action-metadata.js';
5
5
 
6
6
  /**
7
- * Before reflector.
7
+ * After action reflector.
8
8
  */
9
- export class BeforeReflector {
9
+ export class AfterActionReflector {
10
10
  /**
11
11
  * Set metadata.
12
12
  *
@@ -15,15 +15,19 @@ export class BeforeReflector {
15
15
  * @param propertyKey
16
16
  */
17
17
  static addMetadata(
18
- metadata: BeforeMetadata,
18
+ metadata: AfterActionMetadata,
19
19
  target: Constructor,
20
20
  propertyKey?: string,
21
21
  ) {
22
22
  const oldArray =
23
- Reflector.getOwnMetadata(BEFORE_METADATA_KEY, target, propertyKey) ?? [];
23
+ Reflector.getOwnMetadata(
24
+ AFTER_ACTION_METADATA_KEY,
25
+ target,
26
+ propertyKey,
27
+ ) ?? [];
24
28
  const newArray = [metadata, ...oldArray];
25
29
  Reflector.defineMetadata(
26
- BEFORE_METADATA_KEY,
30
+ AFTER_ACTION_METADATA_KEY,
27
31
  newArray,
28
32
  target,
29
33
  propertyKey,
@@ -39,9 +43,9 @@ export class BeforeReflector {
39
43
  static getMetadata(
40
44
  target: Constructor,
41
45
  propertyKey?: string,
42
- ): BeforeMetadata[] {
46
+ ): AfterActionMetadata[] {
43
47
  const metadata = Reflector.getOwnMetadata(
44
- BEFORE_METADATA_KEY,
48
+ AFTER_ACTION_METADATA_KEY,
45
49
  target,
46
50
  propertyKey,
47
51
  );
@@ -0,0 +1,3 @@
1
+ export * from './after-action-metadata.js';
2
+ export * from './after-action-reflector.js';
3
+ export * from './after-action-decorator.js';