@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
@@ -4,18 +4,18 @@ 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 {
|
14
|
+
import {RestActionReflector} from './decorators/index.js';
|
18
15
|
import {RequestDataReflector} from './decorators/index.js';
|
16
|
+
import {AfterActionReflector} from './decorators/index.js';
|
17
|
+
import {BeforeActionReflector} from './decorators/index.js';
|
18
|
+
import {RestControllerReflector} from './decorators/index.js';
|
19
19
|
import {RequestContextReflector} from './decorators/index.js';
|
20
20
|
|
21
21
|
/**
|
@@ -46,7 +46,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
46
46
|
ctor: Constructor<T>,
|
47
47
|
options?: ControllerRootOptions,
|
48
48
|
): this {
|
49
|
-
const debug = this.
|
49
|
+
const debug = this.getDebuggerFor(this.addController);
|
50
50
|
// проверка повторной регистрации помогает
|
51
51
|
// заметить ошибку в коде, который использует
|
52
52
|
// интерфейс данного сервиса
|
@@ -55,7 +55,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
55
55
|
// так как контроллером может быть любой
|
56
56
|
// класс, выполняется проверка на наличие
|
57
57
|
// метаданных применяемых декоратором
|
58
|
-
const controllerMd =
|
58
|
+
const controllerMd = RestControllerReflector.getMetadata(ctor);
|
59
59
|
if (!controllerMd) throw new NotAControllerError(ctor);
|
60
60
|
debug('Adding controller %s.', ctor.name);
|
61
61
|
// определение префикса применяемого
|
@@ -82,7 +82,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
82
82
|
debug('Controller has %v post-handlers.', postHandlers.length);
|
83
83
|
// обход всех операций контроллера
|
84
84
|
// для определения маршрутов
|
85
|
-
const actionsMd =
|
85
|
+
const actionsMd = RestActionReflector.getMetadata(ctor);
|
86
86
|
debug('%v actions found.', actionsMd.size);
|
87
87
|
const router = this.getService(TrieRouter);
|
88
88
|
actionsMd.forEach((actionMd, actionName) => {
|
@@ -143,8 +143,8 @@ export class ControllerRegistry extends DebuggableService {
|
|
143
143
|
protected getPathPrefixFromControllerRootOptions(
|
144
144
|
options?: ControllerRootOptions,
|
145
145
|
) {
|
146
|
-
const debug = this.
|
147
|
-
this.getPathPrefixFromControllerRootOptions
|
146
|
+
const debug = this.getDebuggerFor(
|
147
|
+
this.getPathPrefixFromControllerRootOptions,
|
148
148
|
);
|
149
149
|
debug('Getting path prefix from controller root options.');
|
150
150
|
const res = options?.pathPrefix || '';
|
@@ -158,12 +158,10 @@ export class ControllerRegistry extends DebuggableService {
|
|
158
158
|
* @param ctor
|
159
159
|
*/
|
160
160
|
protected getPathPrefixFromControllerMetadata<T>(ctor: Constructor<T>) {
|
161
|
-
const debug = this.
|
162
|
-
|
163
|
-
);
|
164
|
-
debug('Getting path prefix from @controller metadata.');
|
161
|
+
const debug = this.getDebuggerFor(this.getPathPrefixFromControllerMetadata);
|
162
|
+
debug('Getting path prefix from @restController metadata.');
|
165
163
|
debug('Metadata target is %s.', ctor.name);
|
166
|
-
const md =
|
164
|
+
const md = RestControllerReflector.getMetadata(ctor);
|
167
165
|
if (!md) throw new Errorf('Controller %v has no metadata.', ctor);
|
168
166
|
const res = md.path || '';
|
169
167
|
debug('Controller path prefix is %v.', res);
|
@@ -178,8 +176,8 @@ export class ControllerRegistry extends DebuggableService {
|
|
178
176
|
protected getPreHandlersFromControllerRootOptions(
|
179
177
|
options?: ControllerRootOptions,
|
180
178
|
) {
|
181
|
-
const debug = this.
|
182
|
-
this.getPreHandlersFromControllerRootOptions
|
179
|
+
const debug = this.getDebuggerFor(
|
180
|
+
this.getPreHandlersFromControllerRootOptions,
|
183
181
|
);
|
184
182
|
debug('Getting pre-handlers from controller root options.');
|
185
183
|
let res: RoutePreHandler[] = [];
|
@@ -197,8 +195,8 @@ export class ControllerRegistry extends DebuggableService {
|
|
197
195
|
protected getPostHandlersFromControllerRootOptions(
|
198
196
|
options?: ControllerRootOptions,
|
199
197
|
) {
|
200
|
-
const debug = this.
|
201
|
-
this.getPostHandlersFromControllerRootOptions
|
198
|
+
const debug = this.getDebuggerFor(
|
199
|
+
this.getPostHandlersFromControllerRootOptions,
|
202
200
|
);
|
203
201
|
debug('Getting post-handlers from controller root options.');
|
204
202
|
let res: RoutePostHandler[] = [];
|
@@ -218,7 +216,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
218
216
|
ctor: Constructor<T>,
|
219
217
|
actionName?: string,
|
220
218
|
) {
|
221
|
-
const debug = this.
|
219
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromBeforeMetadata);
|
222
220
|
debug('Getting pre-handlers from @before metadata.');
|
223
221
|
if (actionName) {
|
224
222
|
debug('Target is %s.%s.', ctor.name, actionName);
|
@@ -226,7 +224,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
226
224
|
debug('Target is %s.', ctor.name);
|
227
225
|
}
|
228
226
|
let preHandlers: RoutePreHandler[] = [];
|
229
|
-
const mdArray =
|
227
|
+
const mdArray = BeforeActionReflector.getMetadata(ctor, actionName);
|
230
228
|
mdArray.forEach(md => {
|
231
229
|
if (Array.isArray(md.middleware)) {
|
232
230
|
preHandlers = [...preHandlers, ...md.middleware];
|
@@ -252,7 +250,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
252
250
|
ctor: Constructor<T>,
|
253
251
|
actionName?: string,
|
254
252
|
) {
|
255
|
-
const debug = this.
|
253
|
+
const debug = this.getDebuggerFor(this.getPostHandlersFromAfterMetadata);
|
256
254
|
debug('Getting post-handlers from @after metadata.');
|
257
255
|
if (actionName) {
|
258
256
|
debug('Target is %s.%s.', ctor.name, actionName);
|
@@ -260,7 +258,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
260
258
|
debug('Target is %s.', ctor.name);
|
261
259
|
}
|
262
260
|
let res: RoutePostHandler[] = [];
|
263
|
-
const mdArray =
|
261
|
+
const mdArray = AfterActionReflector.getMetadata(ctor, actionName);
|
264
262
|
mdArray.forEach(md => {
|
265
263
|
if (Array.isArray(md.middleware)) {
|
266
264
|
res = [...res, ...md.middleware];
|
@@ -282,12 +280,12 @@ export class ControllerRegistry extends DebuggableService {
|
|
282
280
|
* @param ctor
|
283
281
|
*/
|
284
282
|
protected getPreHandlersFromControllerMetadata<T>(ctor: Constructor<T>) {
|
285
|
-
const debug = this.
|
286
|
-
this.getPreHandlersFromControllerMetadata
|
283
|
+
const debug = this.getDebuggerFor(
|
284
|
+
this.getPreHandlersFromControllerMetadata,
|
287
285
|
);
|
288
|
-
debug('Getting pre-handlers from @
|
286
|
+
debug('Getting pre-handlers from @restController metadata.');
|
289
287
|
debug('Target is %s.', ctor.name);
|
290
|
-
const md =
|
288
|
+
const md = RestControllerReflector.getMetadata(ctor);
|
291
289
|
if (!md) throw new Errorf('Controller %v has no metadata.', ctor);
|
292
290
|
let res: RoutePreHandler[] = [];
|
293
291
|
if (md.before) res = Array.isArray(md.before) ? md.before : [md.before];
|
@@ -301,11 +299,11 @@ export class ControllerRegistry extends DebuggableService {
|
|
301
299
|
* @param ctor
|
302
300
|
*/
|
303
301
|
protected getPostHandlersFromControllerMetadata<T>(ctor: Constructor<T>) {
|
304
|
-
const debug = this.
|
305
|
-
this.getPostHandlersFromControllerMetadata
|
302
|
+
const debug = this.getDebuggerFor(
|
303
|
+
this.getPostHandlersFromControllerMetadata,
|
306
304
|
);
|
307
|
-
debug('Getting post-handlers from @
|
308
|
-
const md =
|
305
|
+
debug('Getting post-handlers from @restController metadata.');
|
306
|
+
const md = RestControllerReflector.getMetadata(ctor);
|
309
307
|
if (!md) throw new Errorf('Controller %v has no metadata.', ctor);
|
310
308
|
let res: RoutePostHandler[] = [];
|
311
309
|
if (md.after) res = Array.isArray(md.after) ? md.after : [md.after];
|
@@ -323,9 +321,9 @@ export class ControllerRegistry extends DebuggableService {
|
|
323
321
|
ctor: Constructor<T>,
|
324
322
|
actionName: string,
|
325
323
|
) {
|
326
|
-
const debug = this.
|
324
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromActionMetadata);
|
327
325
|
debug('Getting pre-handlers from @action metadata.');
|
328
|
-
const actionsMd =
|
326
|
+
const actionsMd = RestActionReflector.getMetadata(ctor);
|
329
327
|
const actionMd = actionsMd.get(actionName);
|
330
328
|
if (!actionMd)
|
331
329
|
throw new Errorf('Action %s.%s has no metadata.', ctor.name, actionName);
|
@@ -348,9 +346,9 @@ export class ControllerRegistry extends DebuggableService {
|
|
348
346
|
ctor: Constructor<T>,
|
349
347
|
actionName: string,
|
350
348
|
) {
|
351
|
-
const debug = this.
|
349
|
+
const debug = this.getDebuggerFor(this.getPreHandlersFromActionMetadata);
|
352
350
|
debug('Getting post-handlers from @action metadata.');
|
353
|
-
const actionsMd =
|
351
|
+
const actionsMd = RestActionReflector.getMetadata(ctor);
|
354
352
|
const actionMd = actionsMd.get(actionName);
|
355
353
|
if (!actionMd)
|
356
354
|
throw new Errorf('Action %s.%s has no metadata.', ctor.name, actionName);
|
@@ -372,7 +370,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
372
370
|
controllerCtor: Constructor<T>,
|
373
371
|
actionName: string,
|
374
372
|
): RouteHandler {
|
375
|
-
const debug = this.
|
373
|
+
const debug = this.getDebuggerFor(this.createRouteHandler);
|
376
374
|
debug('Creating route handler for %s.%s.', controllerCtor.name, actionName);
|
377
375
|
const requestContextMetadataMap = RequestContextReflector.getMetadata(
|
378
376
|
controllerCtor,
|
@@ -1,7 +1,8 @@
|
|
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 {toCamelCase} from './utils/index.js';
|
4
|
-
import {createDebugger} from '
|
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.
|
36
|
+
this.debug = createDebugger('tsRestRouter', serviceName);
|
37
|
+
const debug = this.debug.withNs('constructor').withHash();
|
38
|
+
debug('Service created.');
|
26
39
|
}
|
27
40
|
}
|
package/src/decorators/{after/after-decorator.spec.ts → after-action/after-action-decorator.spec.ts}
RENAMED
@@ -1,39 +1,39 @@
|
|
1
1
|
/* eslint mocha/no-sibling-hooks: 0 */
|
2
2
|
import {expect} from 'chai';
|
3
|
-
import {
|
4
|
-
import {
|
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('
|
10
|
+
describe('afterAction', function () {
|
11
11
|
describe('class target', function () {
|
12
12
|
it('sets given middleware to the target metadata', function () {
|
13
|
-
@
|
13
|
+
@afterAction(MIDDLEWARE_1)
|
14
14
|
class Target {
|
15
15
|
method() {}
|
16
16
|
}
|
17
|
-
const res =
|
17
|
+
const res = AfterActionReflector.getMetadata(Target);
|
18
18
|
expect(res).to.be.eql([{middleware: MIDDLEWARE_1}]);
|
19
19
|
});
|
20
20
|
|
21
|
-
it('sets
|
22
|
-
@
|
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 =
|
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
|
-
@
|
32
|
-
@
|
31
|
+
@afterAction(MIDDLEWARE_1)
|
32
|
+
@afterAction([MIDDLEWARE_2, MIDDLEWARE_3])
|
33
33
|
class Target {
|
34
34
|
method() {}
|
35
35
|
}
|
36
|
-
const res =
|
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('after', function () {
|
|
44
44
|
describe('method target', function () {
|
45
45
|
it('sets given middleware to the target metadata', function () {
|
46
46
|
class Target {
|
47
|
-
@
|
47
|
+
@afterAction(MIDDLEWARE_1)
|
48
48
|
method() {}
|
49
49
|
}
|
50
|
-
const res =
|
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('after', function () {
|
|
56
56
|
]);
|
57
57
|
});
|
58
58
|
|
59
|
-
it('sets
|
59
|
+
it('sets multiple middlewares to the target metadata', function () {
|
60
60
|
class Target {
|
61
|
-
@
|
61
|
+
@afterAction([MIDDLEWARE_1, MIDDLEWARE_2])
|
62
62
|
method() {}
|
63
63
|
}
|
64
|
-
const res =
|
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('after', function () {
|
|
72
72
|
|
73
73
|
it('allows to use the decorator multiple times', function () {
|
74
74
|
class Target {
|
75
|
-
@
|
76
|
-
@
|
75
|
+
@afterAction(MIDDLEWARE_1)
|
76
|
+
@afterAction([MIDDLEWARE_2, MIDDLEWARE_3])
|
77
77
|
method() {}
|
78
78
|
}
|
79
|
-
const res =
|
79
|
+
const res = AfterActionReflector.getMetadata(Target, 'method');
|
80
80
|
expect(res).to.be.eql([
|
81
81
|
{
|
82
82
|
propertyKey: 'method',
|
package/src/decorators/{before/before-decorator.ts → after-action/after-action-decorator.ts}
RENAMED
@@ -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
|
-
*
|
9
|
+
* After action decorator.
|
10
10
|
*
|
11
|
-
* @param
|
11
|
+
* @param middleware
|
12
12
|
*/
|
13
|
-
export function
|
14
|
-
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
|
-
|
27
|
+
AfterActionReflector.addMetadata({middleware}, target as Constructor<T>);
|
28
28
|
} else if (decoratorType === DecoratorTargetType.INSTANCE_METHOD) {
|
29
|
-
|
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
|
-
'@
|
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
|
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
|
16
|
-
'
|
15
|
+
export const AFTER_ACTION_METADATA_KEY = new MetadataKey<AfterActionMetadata[]>(
|
16
|
+
'afterActionMetadataKey',
|
17
17
|
);
|
@@ -1,23 +1,23 @@
|
|
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
|
|
7
7
|
const MIDDLEWARE_1 = () => undefined;
|
8
8
|
const MIDDLEWARE_2 = () => undefined;
|
9
9
|
const MIDDLEWARE_3 = () => undefined;
|
10
10
|
|
11
|
-
describe('
|
11
|
+
describe('AfterActionReflector', function () {
|
12
12
|
describe('class target', function () {
|
13
13
|
describe('addMetadata', function () {
|
14
14
|
it('adds a given value to the target metadata', function () {
|
15
15
|
class Target {}
|
16
16
|
const md1 = {middleware: MIDDLEWARE_1};
|
17
17
|
const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
|
18
|
-
|
19
|
-
|
20
|
-
const res = Reflector.getOwnMetadata(
|
18
|
+
AfterActionReflector.addMetadata(md1, Target);
|
19
|
+
AfterActionReflector.addMetadata(md2, Target);
|
20
|
+
const res = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
|
21
21
|
expect(res).to.be.eql([md2, md1]);
|
22
22
|
});
|
23
23
|
});
|
@@ -25,7 +25,7 @@ describe('BeforeReflector', function () {
|
|
25
25
|
describe('getMetadata', function () {
|
26
26
|
it('returns an empty array if no metadata', function () {
|
27
27
|
class Target {}
|
28
|
-
const res =
|
28
|
+
const res = AfterActionReflector.getMetadata(Target);
|
29
29
|
expect(res).to.be.eql([]);
|
30
30
|
});
|
31
31
|
|
@@ -34,8 +34,8 @@ describe('BeforeReflector', function () {
|
|
34
34
|
const md1 = {middleware: MIDDLEWARE_1};
|
35
35
|
const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
|
36
36
|
const mdArray = [md1, md2];
|
37
|
-
Reflector.defineMetadata(
|
38
|
-
const res =
|
37
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, mdArray, Target);
|
38
|
+
const res = AfterActionReflector.getMetadata(Target);
|
39
39
|
expect(res).to.be.eql(mdArray);
|
40
40
|
});
|
41
41
|
});
|
@@ -47,10 +47,10 @@ describe('BeforeReflector', function () {
|
|
47
47
|
class Target {}
|
48
48
|
const md1 = {middleware: MIDDLEWARE_1};
|
49
49
|
const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
|
50
|
-
|
51
|
-
|
50
|
+
AfterActionReflector.addMetadata(md1, Target, 'prop');
|
51
|
+
AfterActionReflector.addMetadata(md2, Target, 'prop');
|
52
52
|
const res = Reflector.getOwnMetadata(
|
53
|
-
|
53
|
+
AFTER_ACTION_METADATA_KEY,
|
54
54
|
Target,
|
55
55
|
'prop',
|
56
56
|
);
|
@@ -61,7 +61,7 @@ describe('BeforeReflector', function () {
|
|
61
61
|
describe('getMetadata', function () {
|
62
62
|
it('returns an empty array if no metadata', function () {
|
63
63
|
class Target {}
|
64
|
-
const res =
|
64
|
+
const res = AfterActionReflector.getMetadata(Target, 'prop');
|
65
65
|
expect(res).to.be.eql([]);
|
66
66
|
});
|
67
67
|
|
@@ -70,8 +70,13 @@ describe('BeforeReflector', function () {
|
|
70
70
|
const md1 = {middleware: MIDDLEWARE_1};
|
71
71
|
const md2 = {middleware: [MIDDLEWARE_2, MIDDLEWARE_3]};
|
72
72
|
const mdArray = [md1, md2];
|
73
|
-
Reflector.defineMetadata(
|
74
|
-
|
73
|
+
Reflector.defineMetadata(
|
74
|
+
AFTER_ACTION_METADATA_KEY,
|
75
|
+
mdArray,
|
76
|
+
Target,
|
77
|
+
'prop',
|
78
|
+
);
|
79
|
+
const res = AfterActionReflector.getMetadata(Target, 'prop');
|
75
80
|
expect(res).to.be.eql(mdArray);
|
76
81
|
});
|
77
82
|
});
|
@@ -82,11 +87,11 @@ describe('BeforeReflector', function () {
|
|
82
87
|
class Target {}
|
83
88
|
const md1 = {middleware: MIDDLEWARE_1};
|
84
89
|
const md2 = {middleware: MIDDLEWARE_2};
|
85
|
-
|
86
|
-
|
87
|
-
const res1 = Reflector.getOwnMetadata(
|
90
|
+
AfterActionReflector.addMetadata(md1, Target);
|
91
|
+
AfterActionReflector.addMetadata(md2, Target, 'prop');
|
92
|
+
const res1 = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
|
88
93
|
const res2 = Reflector.getOwnMetadata(
|
89
|
-
|
94
|
+
AFTER_ACTION_METADATA_KEY,
|
90
95
|
Target,
|
91
96
|
'prop',
|
92
97
|
);
|
@@ -100,10 +105,15 @@ describe('BeforeReflector', function () {
|
|
100
105
|
class Target {}
|
101
106
|
const md1 = {middleware: MIDDLEWARE_1};
|
102
107
|
const md2 = {middleware: MIDDLEWARE_2};
|
103
|
-
Reflector.defineMetadata(
|
104
|
-
Reflector.defineMetadata(
|
105
|
-
|
106
|
-
|
108
|
+
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, [md1], Target);
|
109
|
+
Reflector.defineMetadata(
|
110
|
+
AFTER_ACTION_METADATA_KEY,
|
111
|
+
[md2],
|
112
|
+
Target,
|
113
|
+
'prop',
|
114
|
+
);
|
115
|
+
const res1 = AfterActionReflector.getMetadata(Target);
|
116
|
+
const res2 = AfterActionReflector.getMetadata(Target, 'prop');
|
107
117
|
expect(res1).to.be.eql([md1]);
|
108
118
|
expect(res2).to.be.eql([md2]);
|
109
119
|
});
|
package/src/decorators/{before/before-reflector.ts → after-action/after-action-reflector.ts}
RENAMED
@@ -1,12 +1,12 @@
|
|
1
1
|
import {Constructor} from '../../types.js';
|
2
2
|
import {Reflector} from '@e22m4u/ts-reflector';
|
3
|
-
import {
|
4
|
-
import {
|
3
|
+
import {AfterActionMetadata} from './after-action-metadata.js';
|
4
|
+
import {AFTER_ACTION_METADATA_KEY} from './after-action-metadata.js';
|
5
5
|
|
6
6
|
/**
|
7
|
-
*
|
7
|
+
* After action reflector.
|
8
8
|
*/
|
9
|
-
export class
|
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:
|
18
|
+
metadata: AfterActionMetadata,
|
19
19
|
target: Constructor,
|
20
20
|
propertyKey?: string,
|
21
21
|
) {
|
22
22
|
const oldArray =
|
23
|
-
Reflector.getOwnMetadata(
|
23
|
+
Reflector.getOwnMetadata(
|
24
|
+
AFTER_ACTION_METADATA_KEY,
|
25
|
+
target,
|
26
|
+
propertyKey,
|
27
|
+
) ?? [];
|
24
28
|
const newArray = [metadata, ...oldArray];
|
25
29
|
Reflector.defineMetadata(
|
26
|
-
|
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
|
-
):
|
46
|
+
): AfterActionMetadata[] {
|
43
47
|
const metadata = Reflector.getOwnMetadata(
|
44
|
-
|
48
|
+
AFTER_ACTION_METADATA_KEY,
|
45
49
|
target,
|
46
50
|
propertyKey,
|
47
51
|
);
|