@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.
- package/README.md +171 -74
- package/dist/cjs/index.cjs +352 -362
- package/dist/esm/controller-registry.js +37 -32
- package/dist/esm/debuggable-service.d.ts +10 -1
- package/dist/esm/debuggable-service.js +14 -3
- package/dist/esm/decorators/after-action/after-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{before/before-decorator.js → after-action/after-action-decorator.js} +7 -7
- package/dist/esm/decorators/{before/before-decorator.spec.js → after-action/after-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{after/after-metadata.d.ts → after-action/after-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/after-action/after-action-metadata.js +5 -0
- package/dist/esm/decorators/{after/after-reflector.d.ts → after-action/after-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{after/after-reflector.js → after-action/after-action-reflector.js} +6 -6
- package/dist/esm/decorators/{before/before-reflector.spec.js → after-action/after-action-reflector.spec.js} +23 -24
- package/dist/esm/decorators/after-action/index.d.ts +3 -0
- package/dist/esm/decorators/after-action/index.js +3 -0
- package/dist/esm/decorators/before-action/before-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{after/after-decorator.js → before-action/before-action-decorator.js} +7 -7
- package/dist/esm/decorators/{after/after-decorator.spec.js → before-action/before-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{before/before-metadata.d.ts → before-action/before-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/before-action/before-action-metadata.js +5 -0
- package/dist/esm/decorators/{before/before-reflector.d.ts → before-action/before-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{before/before-reflector.js → before-action/before-action-reflector.js} +6 -6
- package/dist/esm/decorators/{after/after-reflector.spec.js → before-action/before-action-reflector.spec.js} +23 -24
- package/dist/esm/decorators/before-action/index.d.ts +3 -0
- package/dist/esm/decorators/before-action/index.js +3 -0
- package/dist/esm/decorators/index.d.ts +4 -4
- package/dist/esm/decorators/index.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +6 -0
- package/dist/esm/decorators/request-context/request-context-reflector.spec.js +0 -1
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.js +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +44 -32
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +1 -1
- package/dist/esm/decorators/request-data/request-data-reflector.spec.js +0 -1
- package/dist/esm/decorators/rest-action/index.d.ts +3 -0
- package/dist/esm/decorators/rest-action/index.js +3 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.d.ts +57 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.js +52 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.spec.js +401 -0
- package/dist/esm/decorators/{action/action-metadata.d.ts → rest-action/rest-action-metadata.d.ts} +6 -6
- package/dist/esm/decorators/rest-action/rest-action-metadata.js +5 -0
- package/dist/esm/decorators/rest-action/rest-action-reflector.d.ts +22 -0
- package/dist/esm/decorators/{action/action-reflector.js → rest-action/rest-action-reflector.js} +6 -6
- package/dist/esm/decorators/{action/action-reflector.spec.js → rest-action/rest-action-reflector.spec.js} +13 -14
- package/dist/esm/decorators/rest-controller/index.d.ts +3 -0
- package/dist/esm/decorators/rest-controller/index.js +3 -0
- package/dist/esm/decorators/rest-controller/rest-controller-decorator.d.ts +14 -0
- package/dist/esm/decorators/{controller/controller-decorator.js → rest-controller/rest-controller-decorator.js} +5 -5
- package/dist/esm/decorators/{controller/controller-decorator.spec.js → rest-controller/rest-controller-decorator.spec.js} +15 -15
- package/dist/esm/decorators/{controller/controller-metadata.d.ts → rest-controller/rest-controller-metadata.d.ts} +5 -5
- package/dist/esm/decorators/rest-controller/rest-controller-metadata.js +5 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.d.ts +20 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.js +24 -0
- package/dist/esm/decorators/{controller/controller-reflector.spec.js → rest-controller/rest-controller-reflector.spec.js} +12 -13
- package/dist/esm/errors/not-a-controller-error.js +1 -1
- package/dist/esm/types.d.ts +0 -10
- package/dist/esm/utils/index.d.ts +0 -1
- package/dist/esm/utils/index.js +0 -1
- package/eslint.config.js +1 -1
- package/package.json +15 -15
- package/src/controller-registry.spec.ts +192 -134
- package/src/controller-registry.ts +46 -39
- package/src/debuggable-service.spec.ts +0 -1
- package/src/debuggable-service.ts +17 -4
- package/src/decorators/{before/before-decorator.spec.ts → after-action/after-action-decorator.spec.ts} +19 -19
- package/src/decorators/{before/before-decorator.ts → after-action/after-action-decorator.ts} +9 -9
- package/src/decorators/{after/after-metadata.ts → after-action/after-action-metadata.ts} +5 -5
- package/src/decorators/{before/before-reflector.spec.ts → after-action/after-action-reflector.spec.ts} +33 -24
- package/src/decorators/{before/before-reflector.ts → after-action/after-action-reflector.ts} +13 -9
- package/src/decorators/after-action/index.ts +3 -0
- package/src/decorators/{after/after-decorator.spec.ts → before-action/before-action-decorator.spec.ts} +19 -19
- package/src/decorators/{after/after-decorator.ts → before-action/before-action-decorator.ts} +9 -9
- package/src/decorators/before-action/before-action-metadata.ts +17 -0
- package/src/decorators/{after/after-reflector.spec.ts → before-action/before-action-reflector.spec.ts} +40 -24
- package/src/decorators/{after/after-reflector.ts → before-action/before-action-reflector.ts} +18 -9
- package/src/decorators/before-action/index.ts +3 -0
- package/src/decorators/index.ts +4 -4
- package/src/decorators/request-context/request-context-decorator.spec.ts +7 -0
- package/src/decorators/request-context/request-context-decorator.ts +4 -4
- package/src/decorators/request-context/request-context-reflector.spec.ts +0 -1
- package/src/decorators/request-data/request-data-decorator.spec.ts +45 -32
- package/src/decorators/request-data/request-data-decorator.ts +10 -10
- package/src/decorators/request-data/request-data-metadata.ts +1 -1
- package/src/decorators/request-data/request-data-reflector.spec.ts +0 -1
- package/src/decorators/rest-action/index.ts +3 -0
- package/src/decorators/rest-action/rest-action-decorator.spec.ts +325 -0
- package/src/decorators/rest-action/rest-action-decorator.ts +166 -0
- package/src/decorators/{action/action-metadata.ts → rest-action/rest-action-metadata.ts} +7 -7
- package/src/decorators/{action/action-reflector.spec.ts → rest-action/rest-action-reflector.spec.ts} +13 -14
- package/src/decorators/rest-action/rest-action-reflector.ts +41 -0
- package/src/decorators/rest-controller/index.ts +3 -0
- package/src/decorators/{controller/controller-decorator.spec.ts → rest-controller/rest-controller-decorator.spec.ts} +16 -16
- package/src/decorators/{controller/controller-decorator.ts → rest-controller/rest-controller-decorator.ts} +14 -10
- package/src/decorators/{controller/controller-metadata.ts → rest-controller/rest-controller-metadata.ts} +6 -7
- package/src/decorators/{controller/controller-reflector.spec.ts → rest-controller/rest-controller-reflector.spec.ts} +21 -13
- package/src/decorators/rest-controller/rest-controller-reflector.ts +32 -0
- package/src/errors/not-a-controller-error.ts +4 -1
- package/src/types.ts +0 -10
- package/src/utils/index.ts +0 -1
- package/dist/esm/decorators/action/action-decorator.d.ts +0 -52
- package/dist/esm/decorators/action/action-decorator.js +0 -62
- package/dist/esm/decorators/action/action-decorator.spec.js +0 -59
- package/dist/esm/decorators/action/action-metadata.js +0 -5
- package/dist/esm/decorators/action/action-reflector.d.ts +0 -22
- package/dist/esm/decorators/action/index.d.ts +0 -3
- package/dist/esm/decorators/action/index.js +0 -3
- package/dist/esm/decorators/after/after-decorator.d.ts +0 -9
- package/dist/esm/decorators/after/after-metadata.js +0 -5
- package/dist/esm/decorators/after/index.d.ts +0 -3
- package/dist/esm/decorators/after/index.js +0 -3
- package/dist/esm/decorators/before/before-decorator.d.ts +0 -9
- package/dist/esm/decorators/before/before-metadata.js +0 -5
- package/dist/esm/decorators/before/index.d.ts +0 -3
- package/dist/esm/decorators/before/index.js +0 -3
- package/dist/esm/decorators/controller/controller-decorator.d.ts +0 -14
- package/dist/esm/decorators/controller/controller-metadata.js +0 -5
- package/dist/esm/decorators/controller/controller-reflector.d.ts +0 -20
- package/dist/esm/decorators/controller/controller-reflector.js +0 -24
- package/dist/esm/decorators/controller/index.d.ts +0 -3
- package/dist/esm/decorators/controller/index.js +0 -3
- package/dist/esm/utils/create-debugger.d.ts +0 -44
- package/dist/esm/utils/create-debugger.js +0 -81
- package/dist/esm/utils/create-debugger.spec.d.ts +0 -1
- package/dist/esm/utils/create-debugger.spec.js +0 -8
- package/src/decorators/action/action-decorator.spec.ts +0 -42
- package/src/decorators/action/action-decorator.ts +0 -98
- package/src/decorators/action/action-reflector.ts +0 -38
- package/src/decorators/action/index.ts +0 -3
- package/src/decorators/after/index.ts +0 -3
- package/src/decorators/before/before-metadata.ts +0 -17
- package/src/decorators/before/index.ts +0 -3
- package/src/decorators/controller/controller-reflector.ts +0 -28
- package/src/decorators/controller/index.ts +0 -3
- package/src/utils/create-debugger.spec.ts +0 -9
- package/src/utils/create-debugger.ts +0 -98
- /package/dist/esm/decorators/{action/action-decorator.spec.d.ts → after-action/after-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{action/action-reflector.spec.d.ts → after-action/after-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-decorator.spec.d.ts → before-action/before-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-reflector.spec.d.ts → before-action/before-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-decorator.spec.d.ts → rest-action/rest-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-reflector.spec.d.ts → rest-action/rest-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-decorator.spec.d.ts → rest-controller/rest-controller-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-reflector.spec.d.ts → rest-controller/rest-controller-reflector.spec.d.ts} +0 -0
@@ -1,15 +1,16 @@
|
|
1
1
|
import { Errorf } from '@e22m4u/js-format';
|
2
2
|
import { TrieRouter } from '@e22m4u/js-trie-router';
|
3
3
|
import { DataValidator } from '@e22m4u/ts-data-schema';
|
4
|
-
import { AfterReflector } from './decorators/index.js';
|
5
4
|
import { DataTypeCaster } from '@e22m4u/ts-data-schema';
|
6
|
-
import { ActionReflector } from './decorators/index.js';
|
7
|
-
import { BeforeReflector } from './decorators/index.js';
|
8
5
|
import { NotAControllerError } from './errors/index.js';
|
9
6
|
import { RequestDataSource } from './decorators/index.js';
|
10
7
|
import { DebuggableService } from './debuggable-service.js';
|
11
|
-
import {
|
8
|
+
import { RestActionReflector } from './decorators/index.js';
|
12
9
|
import { RequestDataReflector } from './decorators/index.js';
|
10
|
+
import { AfterActionReflector } from './decorators/index.js';
|
11
|
+
import { DefaultValuesApplier } from '@e22m4u/ts-data-schema';
|
12
|
+
import { BeforeActionReflector } from './decorators/index.js';
|
13
|
+
import { RestControllerReflector } from './decorators/index.js';
|
13
14
|
import { RequestContextReflector } from './decorators/index.js';
|
14
15
|
/**
|
15
16
|
* Controller registry.
|
@@ -26,7 +27,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
26
27
|
* @param options
|
27
28
|
*/
|
28
29
|
addController(ctor, options) {
|
29
|
-
const debug = this.
|
30
|
+
const debug = this.getDebuggerFor(this.addController);
|
30
31
|
// проверка повторной регистрации помогает
|
31
32
|
// заметить ошибку в коде, который использует
|
32
33
|
// интерфейс данного сервиса
|
@@ -35,7 +36,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
35
36
|
// так как контроллером может быть любой
|
36
37
|
// класс, выполняется проверка на наличие
|
37
38
|
// метаданных применяемых декоратором
|
38
|
-
const controllerMd =
|
39
|
+
const controllerMd = RestControllerReflector.getMetadata(ctor);
|
39
40
|
if (!controllerMd)
|
40
41
|
throw new NotAControllerError(ctor);
|
41
42
|
debug('Adding controller %s.', ctor.name);
|
@@ -63,7 +64,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
63
64
|
debug('Controller has %v post-handlers.', postHandlers.length);
|
64
65
|
// обход всех операций контроллера
|
65
66
|
// для определения маршрутов
|
66
|
-
const actionsMd =
|
67
|
+
const actionsMd = RestActionReflector.getMetadata(ctor);
|
67
68
|
debug('%v actions found.', actionsMd.size);
|
68
69
|
const router = this.getService(TrieRouter);
|
69
70
|
actionsMd.forEach((actionMd, actionName) => {
|
@@ -116,7 +117,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
116
117
|
* @param options
|
117
118
|
*/
|
118
119
|
getPathPrefixFromControllerRootOptions(options) {
|
119
|
-
const debug = this.
|
120
|
+
const debug = this.getDebuggerFor(this.getPathPrefixFromControllerRootOptions);
|
120
121
|
debug('Getting path prefix from controller root options.');
|
121
122
|
const res = options?.pathPrefix || '';
|
122
123
|
debug('Controller path prefix is %v.', res);
|
@@ -128,10 +129,10 @@ export class ControllerRegistry extends DebuggableService {
|
|
128
129
|
* @param ctor
|
129
130
|
*/
|
130
131
|
getPathPrefixFromControllerMetadata(ctor) {
|
131
|
-
const debug = this.
|
132
|
-
debug('Getting path prefix from @
|
132
|
+
const debug = this.getDebuggerFor(this.getPathPrefixFromControllerMetadata);
|
133
|
+
debug('Getting path prefix from @restController metadata.');
|
133
134
|
debug('Metadata target is %s.', ctor.name);
|
134
|
-
const md =
|
135
|
+
const md = RestControllerReflector.getMetadata(ctor);
|
135
136
|
if (!md)
|
136
137
|
throw new Errorf('Controller %v has no metadata.', ctor);
|
137
138
|
const res = md.path || '';
|
@@ -144,7 +145,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
144
145
|
* @param options
|
145
146
|
*/
|
146
147
|
getPreHandlersFromControllerRootOptions(options) {
|
147
|
-
const debug = this.
|
148
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromControllerRootOptions);
|
148
149
|
debug('Getting pre-handlers from controller root options.');
|
149
150
|
let res = [];
|
150
151
|
if (options?.before)
|
@@ -158,7 +159,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
158
159
|
* @param options
|
159
160
|
*/
|
160
161
|
getPostHandlersFromControllerRootOptions(options) {
|
161
|
-
const debug = this.
|
162
|
+
const debug = this.getDebuggerFor(this.getPostHandlersFromControllerRootOptions);
|
162
163
|
debug('Getting post-handlers from controller root options.');
|
163
164
|
let res = [];
|
164
165
|
if (options?.after)
|
@@ -173,7 +174,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
173
174
|
* @param actionName
|
174
175
|
*/
|
175
176
|
getPreHandlersFromBeforeMetadata(ctor, actionName) {
|
176
|
-
const debug = this.
|
177
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromBeforeMetadata);
|
177
178
|
debug('Getting pre-handlers from @before metadata.');
|
178
179
|
if (actionName) {
|
179
180
|
debug('Target is %s.%s.', ctor.name, actionName);
|
@@ -182,7 +183,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
182
183
|
debug('Target is %s.', ctor.name);
|
183
184
|
}
|
184
185
|
let preHandlers = [];
|
185
|
-
const mdArray =
|
186
|
+
const mdArray = BeforeActionReflector.getMetadata(ctor, actionName);
|
186
187
|
mdArray.forEach(md => {
|
187
188
|
if (Array.isArray(md.middleware)) {
|
188
189
|
preHandlers = [...preHandlers, ...md.middleware];
|
@@ -206,7 +207,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
206
207
|
* @param actionName
|
207
208
|
*/
|
208
209
|
getPostHandlersFromAfterMetadata(ctor, actionName) {
|
209
|
-
const debug = this.
|
210
|
+
const debug = this.getDebuggerFor(this.getPostHandlersFromAfterMetadata);
|
210
211
|
debug('Getting post-handlers from @after metadata.');
|
211
212
|
if (actionName) {
|
212
213
|
debug('Target is %s.%s.', ctor.name, actionName);
|
@@ -215,7 +216,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
215
216
|
debug('Target is %s.', ctor.name);
|
216
217
|
}
|
217
218
|
let res = [];
|
218
|
-
const mdArray =
|
219
|
+
const mdArray = AfterActionReflector.getMetadata(ctor, actionName);
|
219
220
|
mdArray.forEach(md => {
|
220
221
|
if (Array.isArray(md.middleware)) {
|
221
222
|
res = [...res, ...md.middleware];
|
@@ -238,10 +239,10 @@ export class ControllerRegistry extends DebuggableService {
|
|
238
239
|
* @param ctor
|
239
240
|
*/
|
240
241
|
getPreHandlersFromControllerMetadata(ctor) {
|
241
|
-
const debug = this.
|
242
|
-
debug('Getting pre-handlers from @
|
242
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromControllerMetadata);
|
243
|
+
debug('Getting pre-handlers from @restController metadata.');
|
243
244
|
debug('Target is %s.', ctor.name);
|
244
|
-
const md =
|
245
|
+
const md = RestControllerReflector.getMetadata(ctor);
|
245
246
|
if (!md)
|
246
247
|
throw new Errorf('Controller %v has no metadata.', ctor);
|
247
248
|
let res = [];
|
@@ -256,9 +257,9 @@ export class ControllerRegistry extends DebuggableService {
|
|
256
257
|
* @param ctor
|
257
258
|
*/
|
258
259
|
getPostHandlersFromControllerMetadata(ctor) {
|
259
|
-
const debug = this.
|
260
|
-
debug('Getting post-handlers from @
|
261
|
-
const md =
|
260
|
+
const debug = this.getDebuggerFor(this.getPostHandlersFromControllerMetadata);
|
261
|
+
debug('Getting post-handlers from @restController metadata.');
|
262
|
+
const md = RestControllerReflector.getMetadata(ctor);
|
262
263
|
if (!md)
|
263
264
|
throw new Errorf('Controller %v has no metadata.', ctor);
|
264
265
|
let res = [];
|
@@ -274,9 +275,9 @@ export class ControllerRegistry extends DebuggableService {
|
|
274
275
|
* @param actionName
|
275
276
|
*/
|
276
277
|
getPreHandlersFromActionMetadata(ctor, actionName) {
|
277
|
-
const debug = this.
|
278
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromActionMetadata);
|
278
279
|
debug('Getting pre-handlers from @action metadata.');
|
279
|
-
const actionsMd =
|
280
|
+
const actionsMd = RestActionReflector.getMetadata(ctor);
|
280
281
|
const actionMd = actionsMd.get(actionName);
|
281
282
|
if (!actionMd)
|
282
283
|
throw new Errorf('Action %s.%s has no metadata.', ctor.name, actionName);
|
@@ -295,9 +296,9 @@ export class ControllerRegistry extends DebuggableService {
|
|
295
296
|
* @param actionName
|
296
297
|
*/
|
297
298
|
getPostHandlersFromActionMetadata(ctor, actionName) {
|
298
|
-
const debug = this.
|
299
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromActionMetadata);
|
299
300
|
debug('Getting post-handlers from @action metadata.');
|
300
|
-
const actionsMd =
|
301
|
+
const actionsMd = RestActionReflector.getMetadata(ctor);
|
301
302
|
const actionMd = actionsMd.get(actionName);
|
302
303
|
if (!actionMd)
|
303
304
|
throw new Errorf('Action %s.%s has no metadata.', ctor.name, actionName);
|
@@ -315,11 +316,12 @@ export class ControllerRegistry extends DebuggableService {
|
|
315
316
|
* @protected
|
316
317
|
*/
|
317
318
|
createRouteHandler(controllerCtor, actionName) {
|
318
|
-
const debug = this.
|
319
|
+
const debug = this.getDebuggerFor(this.createRouteHandler);
|
319
320
|
debug('Creating route handler for %s.%s.', controllerCtor.name, actionName);
|
320
321
|
const requestContextMetadataMap = RequestContextReflector.getMetadata(controllerCtor, actionName);
|
321
322
|
const requestDataMetadataMap = RequestDataReflector.getMetadata(controllerCtor, actionName);
|
322
323
|
const argsNumber = controllerCtor.prototype[actionName].length;
|
324
|
+
const defaultsApplier = this.getService(DefaultValuesApplier);
|
323
325
|
const dataTypeCaster = this.getService(DataTypeCaster);
|
324
326
|
const dataValidator = this.getService(DataValidator);
|
325
327
|
return (requestContext) => {
|
@@ -378,16 +380,19 @@ export class ControllerRegistry extends DebuggableService {
|
|
378
380
|
break;
|
379
381
|
}
|
380
382
|
debug('Request data source is %v.', requestDataMd.source);
|
381
|
-
// при наличии схемы данных
|
382
|
-
//
|
383
|
+
// при наличии схемы данных применяются значения
|
384
|
+
// по умолчанию, выполняется конвертация входящего
|
385
|
+
// значения и валидация согласно схеме
|
383
386
|
if (requestDataMd.schema) {
|
387
|
+
data = defaultsApplier.applyDefaultValuesIfNeeded(data, requestDataMd.schema, requestDataMd.source);
|
388
|
+
debug('Default values applied.');
|
384
389
|
data = dataTypeCaster.cast(data, requestDataMd.schema, {
|
385
390
|
noTypeCastError: true,
|
386
391
|
sourcePath: requestDataMd.source,
|
387
392
|
});
|
388
|
-
debug('Data type casting
|
393
|
+
debug('Data type casting applied.');
|
389
394
|
dataValidator.validate(data, requestDataMd.schema, requestDataMd.source);
|
390
|
-
debug('Data validation
|
395
|
+
debug('Data validation passed.');
|
391
396
|
}
|
392
397
|
// если свойство данных не определено,
|
393
398
|
// то используем весь объекта данных
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import {
|
1
|
+
import { Callable } from './types.js';
|
2
|
+
import { Debugger } from '@e22m4u/js-debug';
|
2
3
|
import { Service } from '@e22m4u/js-service';
|
3
4
|
import { ServiceContainer } from '@e22m4u/js-service';
|
4
5
|
/**
|
@@ -9,6 +10,14 @@ export declare class DebuggableService extends Service {
|
|
9
10
|
* Debug.
|
10
11
|
*/
|
11
12
|
debug: Debugger;
|
13
|
+
/**
|
14
|
+
* Возвращает функцию-отладчик с сегментом пространства имен
|
15
|
+
* указанного в параметре метода.
|
16
|
+
*
|
17
|
+
* @param method
|
18
|
+
* @protected
|
19
|
+
*/
|
20
|
+
protected getDebuggerFor(method: Callable): Debugger;
|
12
21
|
/**
|
13
22
|
* Constructor.
|
14
23
|
*
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { Service } from '@e22m4u/js-service';
|
2
2
|
import { toCamelCase } from './utils/index.js';
|
3
|
-
import { createDebugger } from '
|
3
|
+
import { createDebugger } from '@e22m4u/js-debug';
|
4
4
|
/**
|
5
5
|
* Service.
|
6
6
|
*/
|
@@ -9,6 +9,16 @@ export class DebuggableService extends Service {
|
|
9
9
|
* Debug.
|
10
10
|
*/
|
11
11
|
debug;
|
12
|
+
/**
|
13
|
+
* Возвращает функцию-отладчик с сегментом пространства имен
|
14
|
+
* указанного в параметре метода.
|
15
|
+
*
|
16
|
+
* @param method
|
17
|
+
* @protected
|
18
|
+
*/
|
19
|
+
getDebuggerFor(method) {
|
20
|
+
return this.debug.withHash().withNs(method.name);
|
21
|
+
}
|
12
22
|
/**
|
13
23
|
* Constructor.
|
14
24
|
*
|
@@ -17,7 +27,8 @@ export class DebuggableService extends Service {
|
|
17
27
|
constructor(container) {
|
18
28
|
super(container);
|
19
29
|
const serviceName = toCamelCase(this.constructor.name);
|
20
|
-
this.debug = createDebugger(serviceName);
|
21
|
-
this.debug.
|
30
|
+
this.debug = createDebugger('tsRestRouter', serviceName);
|
31
|
+
const debug = this.debug.withNs('constructor').withHash();
|
32
|
+
debug('Service created.');
|
22
33
|
}
|
23
34
|
}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { Prototype } from '../../types.js';
|
2
|
+
import { Constructor } from '../../types.js';
|
3
|
+
import { AfterActionMetadata } from './after-action-metadata.js';
|
4
|
+
/**
|
5
|
+
* After action decorator.
|
6
|
+
*
|
7
|
+
* @param middleware
|
8
|
+
*/
|
9
|
+
export declare function afterAction<T extends object>(middleware: AfterActionMetadata['middleware']): (target: Constructor<T> | Prototype<T>, propertyKey?: string, descriptor?: PropertyDescriptor) => void;
|
package/dist/esm/decorators/{before/before-decorator.js → after-action/after-action-decorator.js}
RENAMED
@@ -1,22 +1,22 @@
|
|
1
|
-
import { BeforeReflector } from './before-reflector.js';
|
2
1
|
import { DecoratorTargetType } from '@e22m4u/ts-reflector';
|
3
2
|
import { getDecoratorTargetType } from '@e22m4u/ts-reflector';
|
3
|
+
import { AfterActionReflector } from './after-action-reflector.js';
|
4
4
|
/**
|
5
|
-
*
|
5
|
+
* After action decorator.
|
6
6
|
*
|
7
|
-
* @param
|
7
|
+
* @param middleware
|
8
8
|
*/
|
9
|
-
export function
|
9
|
+
export function afterAction(middleware) {
|
10
10
|
return function (target, propertyKey, descriptor) {
|
11
11
|
const decoratorType = getDecoratorTargetType(target, propertyKey, descriptor);
|
12
12
|
if (decoratorType === DecoratorTargetType.CONSTRUCTOR) {
|
13
|
-
|
13
|
+
AfterActionReflector.addMetadata({ middleware }, target);
|
14
14
|
}
|
15
15
|
else if (decoratorType === DecoratorTargetType.INSTANCE_METHOD) {
|
16
|
-
|
16
|
+
AfterActionReflector.addMetadata({ propertyKey, middleware }, target.constructor, propertyKey);
|
17
17
|
}
|
18
18
|
else {
|
19
|
-
throw new Error('@
|
19
|
+
throw new Error('@afterAction decorator is only supported on a class or an instance method.');
|
20
20
|
}
|
21
21
|
};
|
22
22
|
}
|
@@ -9,31 +9,31 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
9
|
};
|
10
10
|
/* eslint mocha/no-sibling-hooks: 0 */
|
11
11
|
import { expect } from 'chai';
|
12
|
-
import {
|
13
|
-
import {
|
12
|
+
import { afterAction } from './after-action-decorator.js';
|
13
|
+
import { AfterActionReflector } from './after-action-reflector.js';
|
14
14
|
const MIDDLEWARE_1 = () => undefined;
|
15
15
|
const MIDDLEWARE_2 = () => undefined;
|
16
16
|
const MIDDLEWARE_3 = () => undefined;
|
17
|
-
describe('
|
17
|
+
describe('afterAction', function () {
|
18
18
|
describe('class target', function () {
|
19
19
|
it('sets given middleware to the target metadata', function () {
|
20
20
|
let Target = class Target {
|
21
21
|
method() { }
|
22
22
|
};
|
23
23
|
Target = __decorate([
|
24
|
-
|
24
|
+
afterAction(MIDDLEWARE_1)
|
25
25
|
], Target);
|
26
|
-
const res =
|
26
|
+
const res = AfterActionReflector.getMetadata(Target);
|
27
27
|
expect(res).to.be.eql([{ middleware: MIDDLEWARE_1 }]);
|
28
28
|
});
|
29
|
-
it('sets
|
29
|
+
it('sets multiple middlewares to the target metadata', function () {
|
30
30
|
let Target = class Target {
|
31
31
|
method() { }
|
32
32
|
};
|
33
33
|
Target = __decorate([
|
34
|
-
|
34
|
+
afterAction([MIDDLEWARE_1, MIDDLEWARE_2])
|
35
35
|
], Target);
|
36
|
-
const res =
|
36
|
+
const res = AfterActionReflector.getMetadata(Target);
|
37
37
|
expect(res).to.be.eql([{ middleware: [MIDDLEWARE_1, MIDDLEWARE_2] }]);
|
38
38
|
});
|
39
39
|
it('allows to use the decorator multiple times', function () {
|
@@ -41,10 +41,10 @@ describe('before', function () {
|
|
41
41
|
method() { }
|
42
42
|
};
|
43
43
|
Target = __decorate([
|
44
|
-
|
45
|
-
|
44
|
+
afterAction(MIDDLEWARE_1),
|
45
|
+
afterAction([MIDDLEWARE_2, MIDDLEWARE_3])
|
46
46
|
], Target);
|
47
|
-
const res =
|
47
|
+
const res = AfterActionReflector.getMetadata(Target);
|
48
48
|
expect(res).to.be.eql([
|
49
49
|
{ middleware: MIDDLEWARE_1 },
|
50
50
|
{ middleware: [MIDDLEWARE_2, MIDDLEWARE_3] },
|
@@ -57,12 +57,12 @@ describe('before', function () {
|
|
57
57
|
method() { }
|
58
58
|
}
|
59
59
|
__decorate([
|
60
|
-
|
60
|
+
afterAction(MIDDLEWARE_1),
|
61
61
|
__metadata("design:type", Function),
|
62
62
|
__metadata("design:paramtypes", []),
|
63
63
|
__metadata("design:returntype", void 0)
|
64
64
|
], Target.prototype, "method", null);
|
65
|
-
const res =
|
65
|
+
const res = AfterActionReflector.getMetadata(Target, 'method');
|
66
66
|
expect(res).to.be.eql([
|
67
67
|
{
|
68
68
|
propertyKey: 'method',
|
@@ -70,17 +70,17 @@ describe('before', function () {
|
|
70
70
|
},
|
71
71
|
]);
|
72
72
|
});
|
73
|
-
it('sets
|
73
|
+
it('sets multiple middlewares to the target metadata', function () {
|
74
74
|
class Target {
|
75
75
|
method() { }
|
76
76
|
}
|
77
77
|
__decorate([
|
78
|
-
|
78
|
+
afterAction([MIDDLEWARE_1, MIDDLEWARE_2]),
|
79
79
|
__metadata("design:type", Function),
|
80
80
|
__metadata("design:paramtypes", []),
|
81
81
|
__metadata("design:returntype", void 0)
|
82
82
|
], Target.prototype, "method", null);
|
83
|
-
const res =
|
83
|
+
const res = AfterActionReflector.getMetadata(Target, 'method');
|
84
84
|
expect(res).to.be.eql([
|
85
85
|
{
|
86
86
|
propertyKey: 'method',
|
@@ -93,13 +93,13 @@ describe('before', function () {
|
|
93
93
|
method() { }
|
94
94
|
}
|
95
95
|
__decorate([
|
96
|
-
|
97
|
-
|
96
|
+
afterAction(MIDDLEWARE_1),
|
97
|
+
afterAction([MIDDLEWARE_2, MIDDLEWARE_3]),
|
98
98
|
__metadata("design:type", Function),
|
99
99
|
__metadata("design:paramtypes", []),
|
100
100
|
__metadata("design:returntype", void 0)
|
101
101
|
], Target.prototype, "method", null);
|
102
|
-
const res =
|
102
|
+
const res = AfterActionReflector.getMetadata(Target, 'method');
|
103
103
|
expect(res).to.be.eql([
|
104
104
|
{
|
105
105
|
propertyKey: 'method',
|
package/dist/esm/decorators/{after/after-metadata.d.ts → after-action/after-action-metadata.d.ts}
RENAMED
@@ -1,13 +1,13 @@
|
|
1
1
|
import { MetadataKey } from '@e22m4u/ts-reflector';
|
2
2
|
import { RoutePostHandler } from '@e22m4u/js-trie-router';
|
3
3
|
/**
|
4
|
-
* After metadata.
|
4
|
+
* After action metadata.
|
5
5
|
*/
|
6
|
-
export type
|
6
|
+
export type AfterActionMetadata = {
|
7
7
|
propertyKey?: string;
|
8
8
|
middleware: RoutePostHandler | RoutePostHandler[];
|
9
9
|
};
|
10
10
|
/**
|
11
|
-
* After metadata key.
|
11
|
+
* After action metadata key.
|
12
12
|
*/
|
13
|
-
export declare const
|
13
|
+
export declare const AFTER_ACTION_METADATA_KEY: MetadataKey<AfterActionMetadata[]>;
|
package/dist/esm/decorators/{after/after-reflector.d.ts → after-action/after-action-reflector.d.ts}
RENAMED
@@ -1,9 +1,9 @@
|
|
1
1
|
import { Constructor } from '../../types.js';
|
2
|
-
import {
|
2
|
+
import { AfterActionMetadata } from './after-action-metadata.js';
|
3
3
|
/**
|
4
|
-
* After reflector.
|
4
|
+
* After action reflector.
|
5
5
|
*/
|
6
|
-
export declare class
|
6
|
+
export declare class AfterActionReflector {
|
7
7
|
/**
|
8
8
|
* Set metadata.
|
9
9
|
*
|
@@ -11,12 +11,12 @@ export declare class AfterReflector {
|
|
11
11
|
* @param target
|
12
12
|
* @param propertyKey
|
13
13
|
*/
|
14
|
-
static addMetadata(metadata:
|
14
|
+
static addMetadata(metadata: AfterActionMetadata, target: Constructor, propertyKey?: string): void;
|
15
15
|
/**
|
16
16
|
* Get metadata.
|
17
17
|
*
|
18
18
|
* @param target
|
19
19
|
* @param propertyKey
|
20
20
|
*/
|
21
|
-
static getMetadata(target: Constructor, propertyKey?: string):
|
21
|
+
static getMetadata(target: Constructor, propertyKey?: string): AfterActionMetadata[];
|
22
22
|
}
|
package/dist/esm/decorators/{after/after-reflector.js → after-action/after-action-reflector.js}
RENAMED
@@ -1,9 +1,9 @@
|
|
1
1
|
import { Reflector } from '@e22m4u/ts-reflector';
|
2
|
-
import {
|
2
|
+
import { AFTER_ACTION_METADATA_KEY } from './after-action-metadata.js';
|
3
3
|
/**
|
4
|
-
* After reflector.
|
4
|
+
* After action reflector.
|
5
5
|
*/
|
6
|
-
export class
|
6
|
+
export class AfterActionReflector {
|
7
7
|
/**
|
8
8
|
* Set metadata.
|
9
9
|
*
|
@@ -12,9 +12,9 @@ export class AfterReflector {
|
|
12
12
|
* @param propertyKey
|
13
13
|
*/
|
14
14
|
static addMetadata(metadata, target, propertyKey) {
|
15
|
-
const oldArray = Reflector.getOwnMetadata(
|
15
|
+
const oldArray = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, target, propertyKey) ?? [];
|
16
16
|
const newArray = [metadata, ...oldArray];
|
17
|
-
Reflector.defineMetadata(
|
17
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, newArray, target, propertyKey);
|
18
18
|
}
|
19
19
|
/**
|
20
20
|
* Get metadata.
|
@@ -23,7 +23,7 @@ export class AfterReflector {
|
|
23
23
|
* @param propertyKey
|
24
24
|
*/
|
25
25
|
static getMetadata(target, propertyKey) {
|
26
|
-
const metadata = Reflector.getOwnMetadata(
|
26
|
+
const metadata = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, target, propertyKey);
|
27
27
|
return metadata ?? [];
|
28
28
|
}
|
29
29
|
}
|
@@ -1,12 +1,11 @@
|
|
1
1
|
import { expect } from 'chai';
|
2
|
-
import { describe } from 'mocha';
|
3
2
|
import { Reflector } from '@e22m4u/ts-reflector';
|
4
|
-
import {
|
5
|
-
import {
|
3
|
+
import { AfterActionReflector } from './after-action-reflector.js';
|
4
|
+
import { AFTER_ACTION_METADATA_KEY } from './after-action-metadata.js';
|
6
5
|
const MIDDLEWARE_1 = () => undefined;
|
7
6
|
const MIDDLEWARE_2 = () => undefined;
|
8
7
|
const MIDDLEWARE_3 = () => undefined;
|
9
|
-
describe('
|
8
|
+
describe('AfterActionReflector', function () {
|
10
9
|
describe('class target', function () {
|
11
10
|
describe('addMetadata', function () {
|
12
11
|
it('adds a given value to the target metadata', function () {
|
@@ -14,9 +13,9 @@ describe('BeforeReflector', function () {
|
|
14
13
|
}
|
15
14
|
const md1 = { middleware: MIDDLEWARE_1 };
|
16
15
|
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
17
|
-
|
18
|
-
|
19
|
-
const res = Reflector.getOwnMetadata(
|
16
|
+
AfterActionReflector.addMetadata(md1, Target);
|
17
|
+
AfterActionReflector.addMetadata(md2, Target);
|
18
|
+
const res = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
|
20
19
|
expect(res).to.be.eql([md2, md1]);
|
21
20
|
});
|
22
21
|
});
|
@@ -24,7 +23,7 @@ describe('BeforeReflector', function () {
|
|
24
23
|
it('returns an empty array if no metadata', function () {
|
25
24
|
class Target {
|
26
25
|
}
|
27
|
-
const res =
|
26
|
+
const res = AfterActionReflector.getMetadata(Target);
|
28
27
|
expect(res).to.be.eql([]);
|
29
28
|
});
|
30
29
|
it('returns existing metadata by the target', function () {
|
@@ -33,8 +32,8 @@ describe('BeforeReflector', function () {
|
|
33
32
|
const md1 = { middleware: MIDDLEWARE_1 };
|
34
33
|
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
35
34
|
const mdArray = [md1, md2];
|
36
|
-
Reflector.defineMetadata(
|
37
|
-
const res =
|
35
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, mdArray, Target);
|
36
|
+
const res = AfterActionReflector.getMetadata(Target);
|
38
37
|
expect(res).to.be.eql(mdArray);
|
39
38
|
});
|
40
39
|
});
|
@@ -46,9 +45,9 @@ describe('BeforeReflector', function () {
|
|
46
45
|
}
|
47
46
|
const md1 = { middleware: MIDDLEWARE_1 };
|
48
47
|
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
49
|
-
|
50
|
-
|
51
|
-
const res = Reflector.getOwnMetadata(
|
48
|
+
AfterActionReflector.addMetadata(md1, Target, 'prop');
|
49
|
+
AfterActionReflector.addMetadata(md2, Target, 'prop');
|
50
|
+
const res = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target, 'prop');
|
52
51
|
expect(res).to.be.eql([md2, md1]);
|
53
52
|
});
|
54
53
|
});
|
@@ -56,7 +55,7 @@ describe('BeforeReflector', function () {
|
|
56
55
|
it('returns an empty array if no metadata', function () {
|
57
56
|
class Target {
|
58
57
|
}
|
59
|
-
const res =
|
58
|
+
const res = AfterActionReflector.getMetadata(Target, 'prop');
|
60
59
|
expect(res).to.be.eql([]);
|
61
60
|
});
|
62
61
|
it('returns existing metadata by the target', function () {
|
@@ -65,8 +64,8 @@ describe('BeforeReflector', function () {
|
|
65
64
|
const md1 = { middleware: MIDDLEWARE_1 };
|
66
65
|
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
67
66
|
const mdArray = [md1, md2];
|
68
|
-
Reflector.defineMetadata(
|
69
|
-
const res =
|
67
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, mdArray, Target, 'prop');
|
68
|
+
const res = AfterActionReflector.getMetadata(Target, 'prop');
|
70
69
|
expect(res).to.be.eql(mdArray);
|
71
70
|
});
|
72
71
|
});
|
@@ -77,10 +76,10 @@ describe('BeforeReflector', function () {
|
|
77
76
|
}
|
78
77
|
const md1 = { middleware: MIDDLEWARE_1 };
|
79
78
|
const md2 = { middleware: MIDDLEWARE_2 };
|
80
|
-
|
81
|
-
|
82
|
-
const res1 = Reflector.getOwnMetadata(
|
83
|
-
const res2 = Reflector.getOwnMetadata(
|
79
|
+
AfterActionReflector.addMetadata(md1, Target);
|
80
|
+
AfterActionReflector.addMetadata(md2, Target, 'prop');
|
81
|
+
const res1 = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
|
82
|
+
const res2 = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target, 'prop');
|
84
83
|
expect(res1).to.be.eql([md1]);
|
85
84
|
expect(res2).to.be.eql([md2]);
|
86
85
|
});
|
@@ -91,10 +90,10 @@ describe('BeforeReflector', function () {
|
|
91
90
|
}
|
92
91
|
const md1 = { middleware: MIDDLEWARE_1 };
|
93
92
|
const md2 = { middleware: MIDDLEWARE_2 };
|
94
|
-
Reflector.defineMetadata(
|
95
|
-
Reflector.defineMetadata(
|
96
|
-
const res1 =
|
97
|
-
const res2 =
|
93
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, [md1], Target);
|
94
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, [md2], Target, 'prop');
|
95
|
+
const res1 = AfterActionReflector.getMetadata(Target);
|
96
|
+
const res2 = AfterActionReflector.getMetadata(Target, 'prop');
|
98
97
|
expect(res1).to.be.eql([md1]);
|
99
98
|
expect(res2).to.be.eql([md2]);
|
100
99
|
});
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { Prototype } from '../../types.js';
|
2
|
+
import { Constructor } from '../../types.js';
|
3
|
+
import { BeforeActionMetadata } from './before-action-metadata.js';
|
4
|
+
/**
|
5
|
+
* Before action decorator.
|
6
|
+
*
|
7
|
+
* @param middleware
|
8
|
+
*/
|
9
|
+
export declare function beforeAction<T extends object>(middleware: BeforeActionMetadata['middleware']): (target: Constructor<T> | Prototype<T>, propertyKey?: string, descriptor?: PropertyDescriptor) => void;
|