@e22m4u/ts-rest-router 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -118
- package/dist/cjs/index.cjs +346 -360
- package/dist/esm/controller-registry.js +28 -28
- 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/{after/after-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/{before/before-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 -23
- 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/{before/before-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/{after/after-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 -23
- 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-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/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 -13
- 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 -12
- 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/package.json +19 -19
- package/src/controller-registry.spec.ts +122 -122
- package/src/controller-registry.ts +33 -35
- package/src/debuggable-service.ts +17 -4
- package/src/decorators/{after/after-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 -23
- 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/{before/before-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 -23
- 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-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/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 -13
- 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 -12
- 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/README-ru.md +0 -268
- 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,15 @@
|
|
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 { BeforeActionReflector } from './decorators/index.js';
|
12
|
+
import { RestControllerReflector } from './decorators/index.js';
|
13
13
|
import { RequestContextReflector } from './decorators/index.js';
|
14
14
|
/**
|
15
15
|
* Controller registry.
|
@@ -26,7 +26,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
26
26
|
* @param options
|
27
27
|
*/
|
28
28
|
addController(ctor, options) {
|
29
|
-
const debug = this.
|
29
|
+
const debug = this.getDebuggerFor(this.addController);
|
30
30
|
// проверка повторной регистрации помогает
|
31
31
|
// заметить ошибку в коде, который использует
|
32
32
|
// интерфейс данного сервиса
|
@@ -35,7 +35,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
35
35
|
// так как контроллером может быть любой
|
36
36
|
// класс, выполняется проверка на наличие
|
37
37
|
// метаданных применяемых декоратором
|
38
|
-
const controllerMd =
|
38
|
+
const controllerMd = RestControllerReflector.getMetadata(ctor);
|
39
39
|
if (!controllerMd)
|
40
40
|
throw new NotAControllerError(ctor);
|
41
41
|
debug('Adding controller %s.', ctor.name);
|
@@ -63,7 +63,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
63
63
|
debug('Controller has %v post-handlers.', postHandlers.length);
|
64
64
|
// обход всех операций контроллера
|
65
65
|
// для определения маршрутов
|
66
|
-
const actionsMd =
|
66
|
+
const actionsMd = RestActionReflector.getMetadata(ctor);
|
67
67
|
debug('%v actions found.', actionsMd.size);
|
68
68
|
const router = this.getService(TrieRouter);
|
69
69
|
actionsMd.forEach((actionMd, actionName) => {
|
@@ -116,7 +116,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
116
116
|
* @param options
|
117
117
|
*/
|
118
118
|
getPathPrefixFromControllerRootOptions(options) {
|
119
|
-
const debug = this.
|
119
|
+
const debug = this.getDebuggerFor(this.getPathPrefixFromControllerRootOptions);
|
120
120
|
debug('Getting path prefix from controller root options.');
|
121
121
|
const res = options?.pathPrefix || '';
|
122
122
|
debug('Controller path prefix is %v.', res);
|
@@ -128,10 +128,10 @@ export class ControllerRegistry extends DebuggableService {
|
|
128
128
|
* @param ctor
|
129
129
|
*/
|
130
130
|
getPathPrefixFromControllerMetadata(ctor) {
|
131
|
-
const debug = this.
|
132
|
-
debug('Getting path prefix from @
|
131
|
+
const debug = this.getDebuggerFor(this.getPathPrefixFromControllerMetadata);
|
132
|
+
debug('Getting path prefix from @restController metadata.');
|
133
133
|
debug('Metadata target is %s.', ctor.name);
|
134
|
-
const md =
|
134
|
+
const md = RestControllerReflector.getMetadata(ctor);
|
135
135
|
if (!md)
|
136
136
|
throw new Errorf('Controller %v has no metadata.', ctor);
|
137
137
|
const res = md.path || '';
|
@@ -144,7 +144,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
144
144
|
* @param options
|
145
145
|
*/
|
146
146
|
getPreHandlersFromControllerRootOptions(options) {
|
147
|
-
const debug = this.
|
147
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromControllerRootOptions);
|
148
148
|
debug('Getting pre-handlers from controller root options.');
|
149
149
|
let res = [];
|
150
150
|
if (options?.before)
|
@@ -158,7 +158,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
158
158
|
* @param options
|
159
159
|
*/
|
160
160
|
getPostHandlersFromControllerRootOptions(options) {
|
161
|
-
const debug = this.
|
161
|
+
const debug = this.getDebuggerFor(this.getPostHandlersFromControllerRootOptions);
|
162
162
|
debug('Getting post-handlers from controller root options.');
|
163
163
|
let res = [];
|
164
164
|
if (options?.after)
|
@@ -173,7 +173,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
173
173
|
* @param actionName
|
174
174
|
*/
|
175
175
|
getPreHandlersFromBeforeMetadata(ctor, actionName) {
|
176
|
-
const debug = this.
|
176
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromBeforeMetadata);
|
177
177
|
debug('Getting pre-handlers from @before metadata.');
|
178
178
|
if (actionName) {
|
179
179
|
debug('Target is %s.%s.', ctor.name, actionName);
|
@@ -182,7 +182,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
182
182
|
debug('Target is %s.', ctor.name);
|
183
183
|
}
|
184
184
|
let preHandlers = [];
|
185
|
-
const mdArray =
|
185
|
+
const mdArray = BeforeActionReflector.getMetadata(ctor, actionName);
|
186
186
|
mdArray.forEach(md => {
|
187
187
|
if (Array.isArray(md.middleware)) {
|
188
188
|
preHandlers = [...preHandlers, ...md.middleware];
|
@@ -206,7 +206,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
206
206
|
* @param actionName
|
207
207
|
*/
|
208
208
|
getPostHandlersFromAfterMetadata(ctor, actionName) {
|
209
|
-
const debug = this.
|
209
|
+
const debug = this.getDebuggerFor(this.getPostHandlersFromAfterMetadata);
|
210
210
|
debug('Getting post-handlers from @after metadata.');
|
211
211
|
if (actionName) {
|
212
212
|
debug('Target is %s.%s.', ctor.name, actionName);
|
@@ -215,7 +215,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
215
215
|
debug('Target is %s.', ctor.name);
|
216
216
|
}
|
217
217
|
let res = [];
|
218
|
-
const mdArray =
|
218
|
+
const mdArray = AfterActionReflector.getMetadata(ctor, actionName);
|
219
219
|
mdArray.forEach(md => {
|
220
220
|
if (Array.isArray(md.middleware)) {
|
221
221
|
res = [...res, ...md.middleware];
|
@@ -238,10 +238,10 @@ export class ControllerRegistry extends DebuggableService {
|
|
238
238
|
* @param ctor
|
239
239
|
*/
|
240
240
|
getPreHandlersFromControllerMetadata(ctor) {
|
241
|
-
const debug = this.
|
242
|
-
debug('Getting pre-handlers from @
|
241
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromControllerMetadata);
|
242
|
+
debug('Getting pre-handlers from @restController metadata.');
|
243
243
|
debug('Target is %s.', ctor.name);
|
244
|
-
const md =
|
244
|
+
const md = RestControllerReflector.getMetadata(ctor);
|
245
245
|
if (!md)
|
246
246
|
throw new Errorf('Controller %v has no metadata.', ctor);
|
247
247
|
let res = [];
|
@@ -256,9 +256,9 @@ export class ControllerRegistry extends DebuggableService {
|
|
256
256
|
* @param ctor
|
257
257
|
*/
|
258
258
|
getPostHandlersFromControllerMetadata(ctor) {
|
259
|
-
const debug = this.
|
260
|
-
debug('Getting post-handlers from @
|
261
|
-
const md =
|
259
|
+
const debug = this.getDebuggerFor(this.getPostHandlersFromControllerMetadata);
|
260
|
+
debug('Getting post-handlers from @restController metadata.');
|
261
|
+
const md = RestControllerReflector.getMetadata(ctor);
|
262
262
|
if (!md)
|
263
263
|
throw new Errorf('Controller %v has no metadata.', ctor);
|
264
264
|
let res = [];
|
@@ -274,9 +274,9 @@ export class ControllerRegistry extends DebuggableService {
|
|
274
274
|
* @param actionName
|
275
275
|
*/
|
276
276
|
getPreHandlersFromActionMetadata(ctor, actionName) {
|
277
|
-
const debug = this.
|
277
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromActionMetadata);
|
278
278
|
debug('Getting pre-handlers from @action metadata.');
|
279
|
-
const actionsMd =
|
279
|
+
const actionsMd = RestActionReflector.getMetadata(ctor);
|
280
280
|
const actionMd = actionsMd.get(actionName);
|
281
281
|
if (!actionMd)
|
282
282
|
throw new Errorf('Action %s.%s has no metadata.', ctor.name, actionName);
|
@@ -295,9 +295,9 @@ export class ControllerRegistry extends DebuggableService {
|
|
295
295
|
* @param actionName
|
296
296
|
*/
|
297
297
|
getPostHandlersFromActionMetadata(ctor, actionName) {
|
298
|
-
const debug = this.
|
298
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromActionMetadata);
|
299
299
|
debug('Getting post-handlers from @action metadata.');
|
300
|
-
const actionsMd =
|
300
|
+
const actionsMd = RestActionReflector.getMetadata(ctor);
|
301
301
|
const actionMd = actionsMd.get(actionName);
|
302
302
|
if (!actionMd)
|
303
303
|
throw new Errorf('Action %s.%s has no metadata.', ctor.name, actionName);
|
@@ -315,7 +315,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
315
315
|
* @protected
|
316
316
|
*/
|
317
317
|
createRouteHandler(controllerCtor, actionName) {
|
318
|
-
const debug = this.
|
318
|
+
const debug = this.getDebuggerFor(this.createRouteHandler);
|
319
319
|
debug('Creating route handler for %s.%s.', controllerCtor.name, actionName);
|
320
320
|
const requestContextMetadataMap = RequestContextReflector.getMetadata(controllerCtor, actionName);
|
321
321
|
const requestDataMetadataMap = RequestDataReflector.getMetadata(controllerCtor, actionName);
|
@@ -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/{after/after-decorator.js → after-action/after-action-decorator.js}
RENAMED
@@ -1,22 +1,22 @@
|
|
1
|
-
import { AfterReflector } from './after-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
|
-
* After decorator.
|
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/{before/before-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
|
-
*
|
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 BeforeReflector {
|
|
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 BeforeReflector {
|
|
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,12 @@
|
|
1
1
|
import { expect } from 'chai';
|
2
2
|
import { describe } from 'mocha';
|
3
3
|
import { Reflector } from '@e22m4u/ts-reflector';
|
4
|
-
import {
|
5
|
-
import {
|
4
|
+
import { AfterActionReflector } from './after-action-reflector.js';
|
5
|
+
import { AFTER_ACTION_METADATA_KEY } from './after-action-metadata.js';
|
6
6
|
const MIDDLEWARE_1 = () => undefined;
|
7
7
|
const MIDDLEWARE_2 = () => undefined;
|
8
8
|
const MIDDLEWARE_3 = () => undefined;
|
9
|
-
describe('
|
9
|
+
describe('AfterActionReflector', function () {
|
10
10
|
describe('class target', function () {
|
11
11
|
describe('addMetadata', function () {
|
12
12
|
it('adds a given value to the target metadata', function () {
|
@@ -14,9 +14,9 @@ describe('BeforeReflector', function () {
|
|
14
14
|
}
|
15
15
|
const md1 = { middleware: MIDDLEWARE_1 };
|
16
16
|
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
17
|
-
|
18
|
-
|
19
|
-
const res = Reflector.getOwnMetadata(
|
17
|
+
AfterActionReflector.addMetadata(md1, Target);
|
18
|
+
AfterActionReflector.addMetadata(md2, Target);
|
19
|
+
const res = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
|
20
20
|
expect(res).to.be.eql([md2, md1]);
|
21
21
|
});
|
22
22
|
});
|
@@ -24,7 +24,7 @@ describe('BeforeReflector', function () {
|
|
24
24
|
it('returns an empty array if no metadata', function () {
|
25
25
|
class Target {
|
26
26
|
}
|
27
|
-
const res =
|
27
|
+
const res = AfterActionReflector.getMetadata(Target);
|
28
28
|
expect(res).to.be.eql([]);
|
29
29
|
});
|
30
30
|
it('returns existing metadata by the target', function () {
|
@@ -33,8 +33,8 @@ describe('BeforeReflector', function () {
|
|
33
33
|
const md1 = { middleware: MIDDLEWARE_1 };
|
34
34
|
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
35
35
|
const mdArray = [md1, md2];
|
36
|
-
Reflector.defineMetadata(
|
37
|
-
const res =
|
36
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, mdArray, Target);
|
37
|
+
const res = AfterActionReflector.getMetadata(Target);
|
38
38
|
expect(res).to.be.eql(mdArray);
|
39
39
|
});
|
40
40
|
});
|
@@ -46,9 +46,9 @@ describe('BeforeReflector', function () {
|
|
46
46
|
}
|
47
47
|
const md1 = { middleware: MIDDLEWARE_1 };
|
48
48
|
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
49
|
-
|
50
|
-
|
51
|
-
const res = Reflector.getOwnMetadata(
|
49
|
+
AfterActionReflector.addMetadata(md1, Target, 'prop');
|
50
|
+
AfterActionReflector.addMetadata(md2, Target, 'prop');
|
51
|
+
const res = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target, 'prop');
|
52
52
|
expect(res).to.be.eql([md2, md1]);
|
53
53
|
});
|
54
54
|
});
|
@@ -56,7 +56,7 @@ describe('BeforeReflector', function () {
|
|
56
56
|
it('returns an empty array if no metadata', function () {
|
57
57
|
class Target {
|
58
58
|
}
|
59
|
-
const res =
|
59
|
+
const res = AfterActionReflector.getMetadata(Target, 'prop');
|
60
60
|
expect(res).to.be.eql([]);
|
61
61
|
});
|
62
62
|
it('returns existing metadata by the target', function () {
|
@@ -65,8 +65,8 @@ describe('BeforeReflector', function () {
|
|
65
65
|
const md1 = { middleware: MIDDLEWARE_1 };
|
66
66
|
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
67
67
|
const mdArray = [md1, md2];
|
68
|
-
Reflector.defineMetadata(
|
69
|
-
const res =
|
68
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, mdArray, Target, 'prop');
|
69
|
+
const res = AfterActionReflector.getMetadata(Target, 'prop');
|
70
70
|
expect(res).to.be.eql(mdArray);
|
71
71
|
});
|
72
72
|
});
|
@@ -77,10 +77,10 @@ describe('BeforeReflector', function () {
|
|
77
77
|
}
|
78
78
|
const md1 = { middleware: MIDDLEWARE_1 };
|
79
79
|
const md2 = { middleware: MIDDLEWARE_2 };
|
80
|
-
|
81
|
-
|
82
|
-
const res1 = Reflector.getOwnMetadata(
|
83
|
-
const res2 = Reflector.getOwnMetadata(
|
80
|
+
AfterActionReflector.addMetadata(md1, Target);
|
81
|
+
AfterActionReflector.addMetadata(md2, Target, 'prop');
|
82
|
+
const res1 = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
|
83
|
+
const res2 = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target, 'prop');
|
84
84
|
expect(res1).to.be.eql([md1]);
|
85
85
|
expect(res2).to.be.eql([md2]);
|
86
86
|
});
|
@@ -91,10 +91,10 @@ describe('BeforeReflector', function () {
|
|
91
91
|
}
|
92
92
|
const md1 = { middleware: MIDDLEWARE_1 };
|
93
93
|
const md2 = { middleware: MIDDLEWARE_2 };
|
94
|
-
Reflector.defineMetadata(
|
95
|
-
Reflector.defineMetadata(
|
96
|
-
const res1 =
|
97
|
-
const res2 =
|
94
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, [md1], Target);
|
95
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, [md2], Target, 'prop');
|
96
|
+
const res1 = AfterActionReflector.getMetadata(Target);
|
97
|
+
const res2 = AfterActionReflector.getMetadata(Target, 'prop');
|
98
98
|
expect(res1).to.be.eql([md1]);
|
99
99
|
expect(res2).to.be.eql([md2]);
|
100
100
|
});
|
@@ -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;
|
package/dist/esm/decorators/{before/before-decorator.js → before-action/before-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 { BeforeActionReflector } from './before-action-reflector.js';
|
4
4
|
/**
|
5
|
-
* Before decorator.
|
5
|
+
* Before action decorator.
|
6
6
|
*
|
7
|
-
* @param
|
7
|
+
* @param middleware
|
8
8
|
*/
|
9
|
-
export function
|
9
|
+
export function beforeAction(middleware) {
|
10
10
|
return function (target, propertyKey, descriptor) {
|
11
11
|
const decoratorType = getDecoratorTargetType(target, propertyKey, descriptor);
|
12
12
|
if (decoratorType === DecoratorTargetType.CONSTRUCTOR) {
|
13
|
-
|
13
|
+
BeforeActionReflector.addMetadata({ middleware }, target);
|
14
14
|
}
|
15
15
|
else if (decoratorType === DecoratorTargetType.INSTANCE_METHOD) {
|
16
|
-
|
16
|
+
BeforeActionReflector.addMetadata({ propertyKey, middleware }, target.constructor, propertyKey);
|
17
17
|
}
|
18
18
|
else {
|
19
|
-
throw new Error('@
|
19
|
+
throw new Error('@beforeAction decorator is only supported on a class or an instance method.');
|
20
20
|
}
|
21
21
|
};
|
22
22
|
}
|