@e22m4u/ts-rest-router 0.5.1 → 0.5.3
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 +489 -305
- package/dist/cjs/index.cjs +14 -14
- package/dist/esm/controller-registry.js +8 -8
- package/dist/esm/decorators/after-action/after-action-decorator.d.ts +2 -2
- package/dist/esm/decorators/after-action/after-action-decorator.js +4 -4
- package/dist/esm/decorators/after-action/after-action-decorator.spec.js +22 -25
- package/dist/esm/decorators/after-action/after-action-metadata.d.ts +1 -1
- package/dist/esm/decorators/after-action/after-action-reflector.spec.js +15 -15
- package/dist/esm/decorators/before-action/before-action-decorator.d.ts +2 -2
- package/dist/esm/decorators/before-action/before-action-decorator.js +4 -4
- package/dist/esm/decorators/before-action/before-action-decorator.spec.js +22 -25
- package/dist/esm/decorators/before-action/before-action-metadata.d.ts +1 -1
- package/dist/esm/decorators/before-action/before-action-reflector.spec.js +15 -15
- package/package.json +9 -9
- package/src/controller-registry.spec.ts +21 -21
- package/src/controller-registry.ts +8 -8
- package/src/decorators/after-action/after-action-decorator.spec.ts +22 -25
- package/src/decorators/after-action/after-action-decorator.ts +4 -4
- package/src/decorators/after-action/after-action-metadata.ts +1 -1
- package/src/decorators/after-action/after-action-reflector.spec.ts +15 -15
- package/src/decorators/before-action/before-action-decorator.spec.ts +22 -25
- package/src/decorators/before-action/before-action-decorator.ts +4 -4
- package/src/decorators/before-action/before-action-metadata.ts +1 -1
- package/src/decorators/before-action/before-action-reflector.spec.ts +15 -15
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@e22m4u/ts-rest-router",
|
3
|
-
"version": "0.5.
|
4
|
-
"description": "REST
|
3
|
+
"version": "0.5.3",
|
4
|
+
"description": "Декларативный REST-маршрутизатор на основе контроллеров для TypeScript",
|
5
5
|
"author": "Mikhail Evstropov <e22m4u@yandex.ru>",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
@@ -41,18 +41,18 @@
|
|
41
41
|
"prepare": "husky"
|
42
42
|
},
|
43
43
|
"dependencies": {
|
44
|
-
"@e22m4u/js-debug": "~0.2.
|
44
|
+
"@e22m4u/js-debug": "~0.2.3",
|
45
45
|
"@e22m4u/js-format": "~0.2.0",
|
46
|
-
"@e22m4u/js-service": "~0.3.
|
47
|
-
"@e22m4u/js-trie-router": "~0.1
|
48
|
-
"@e22m4u/ts-data-schema": "~0.
|
46
|
+
"@e22m4u/js-service": "~0.3.8",
|
47
|
+
"@e22m4u/js-trie-router": "~0.2.1",
|
48
|
+
"@e22m4u/ts-data-schema": "~0.4.2",
|
49
49
|
"@e22m4u/ts-reflector": "~0.1.7",
|
50
50
|
"http-errors": "~2.0.0"
|
51
51
|
},
|
52
52
|
"devDependencies": {
|
53
53
|
"@commitlint/cli": "~19.8.1",
|
54
54
|
"@commitlint/config-conventional": "~19.8.1",
|
55
|
-
"@eslint/js": "~9.
|
55
|
+
"@eslint/js": "~9.36.0",
|
56
56
|
"@types/chai": "~5.2.2",
|
57
57
|
"@types/debug": "~4.1.12",
|
58
58
|
"@types/http-errors": "~2.0.5",
|
@@ -61,7 +61,7 @@
|
|
61
61
|
"c8": "~10.1.3",
|
62
62
|
"chai": "~6.0.1",
|
63
63
|
"esbuild": "~0.25.10",
|
64
|
-
"eslint": "~9.
|
64
|
+
"eslint": "~9.36.0",
|
65
65
|
"eslint-config-prettier": "~10.1.8",
|
66
66
|
"eslint-plugin-chai-expect": "~3.1.0",
|
67
67
|
"eslint-plugin-mocha": "~11.1.0",
|
@@ -71,6 +71,6 @@
|
|
71
71
|
"rimraf": "~6.0.1",
|
72
72
|
"tsx": "~4.20.5",
|
73
73
|
"typescript": "~5.9.2",
|
74
|
-
"typescript-eslint": "~8.44.
|
74
|
+
"typescript-eslint": "~8.44.1"
|
75
75
|
}
|
76
76
|
}
|
@@ -2,7 +2,7 @@
|
|
2
2
|
import {
|
3
3
|
createRequestMock,
|
4
4
|
createResponseMock,
|
5
|
-
|
5
|
+
HookType,
|
6
6
|
HttpMethod,
|
7
7
|
ParsedCookie,
|
8
8
|
ParsedHeaders,
|
@@ -180,7 +180,7 @@ describe('ControllerRegistry', function () {
|
|
180
180
|
});
|
181
181
|
const matching = routeReg.matchRouteByRequest(req);
|
182
182
|
expect(matching).to.be.not.empty;
|
183
|
-
const res = matching!.route.hookRegistry.getHooks(
|
183
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
184
184
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
185
185
|
});
|
186
186
|
|
@@ -200,7 +200,7 @@ describe('ControllerRegistry', function () {
|
|
200
200
|
});
|
201
201
|
const matching = routeReg.matchRouteByRequest(req);
|
202
202
|
expect(matching).to.be.not.empty;
|
203
|
-
const res = matching!.route.hookRegistry.getHooks(
|
203
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
204
204
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
205
205
|
});
|
206
206
|
|
@@ -220,7 +220,7 @@ describe('ControllerRegistry', function () {
|
|
220
220
|
});
|
221
221
|
const matching = routeReg.matchRouteByRequest(req);
|
222
222
|
expect(matching).to.be.not.empty;
|
223
|
-
const res = matching!.route.hookRegistry.getHooks(
|
223
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
224
224
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
225
225
|
});
|
226
226
|
|
@@ -239,7 +239,7 @@ describe('ControllerRegistry', function () {
|
|
239
239
|
});
|
240
240
|
const matching = routeReg.matchRouteByRequest(req);
|
241
241
|
expect(matching).to.be.not.empty;
|
242
|
-
const res = matching!.route.hookRegistry.getHooks(
|
242
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
243
243
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
244
244
|
});
|
245
245
|
|
@@ -258,7 +258,7 @@ describe('ControllerRegistry', function () {
|
|
258
258
|
});
|
259
259
|
const matching = routeReg.matchRouteByRequest(req);
|
260
260
|
expect(matching).to.be.not.empty;
|
261
|
-
const res = matching!.route.hookRegistry.getHooks(
|
261
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
262
262
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
263
263
|
});
|
264
264
|
});
|
@@ -281,7 +281,7 @@ describe('ControllerRegistry', function () {
|
|
281
281
|
});
|
282
282
|
const matching = routeReg.matchRouteByRequest(req);
|
283
283
|
expect(matching).to.be.not.empty;
|
284
|
-
const res = matching!.route.hookRegistry.getHooks(
|
284
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
285
285
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
286
286
|
});
|
287
287
|
|
@@ -301,7 +301,7 @@ describe('ControllerRegistry', function () {
|
|
301
301
|
});
|
302
302
|
const matching = routeReg.matchRouteByRequest(req);
|
303
303
|
expect(matching).to.be.not.empty;
|
304
|
-
const res = matching!.route.hookRegistry.getHooks(
|
304
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
305
305
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
306
306
|
});
|
307
307
|
|
@@ -321,7 +321,7 @@ describe('ControllerRegistry', function () {
|
|
321
321
|
});
|
322
322
|
const matching = routeReg.matchRouteByRequest(req);
|
323
323
|
expect(matching).to.be.not.empty;
|
324
|
-
const res = matching!.route.hookRegistry.getHooks(
|
324
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
325
325
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
326
326
|
});
|
327
327
|
|
@@ -342,7 +342,7 @@ describe('ControllerRegistry', function () {
|
|
342
342
|
});
|
343
343
|
const matching = routeReg.matchRouteByRequest(req);
|
344
344
|
expect(matching).to.be.not.empty;
|
345
|
-
const res = matching!.route.hookRegistry.getHooks(
|
345
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
346
346
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
347
347
|
});
|
348
348
|
|
@@ -363,7 +363,7 @@ describe('ControllerRegistry', function () {
|
|
363
363
|
});
|
364
364
|
const matching = routeReg.matchRouteByRequest(req);
|
365
365
|
expect(matching).to.be.not.empty;
|
366
|
-
const res = matching!.route.hookRegistry.getHooks(
|
366
|
+
const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
|
367
367
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
368
368
|
});
|
369
369
|
});
|
@@ -385,7 +385,7 @@ describe('ControllerRegistry', function () {
|
|
385
385
|
const matching = routeReg.matchRouteByRequest(req);
|
386
386
|
expect(matching).to.be.not.empty;
|
387
387
|
const res = matching!.route.hookRegistry.getHooks(
|
388
|
-
|
388
|
+
HookType.POST_HANDLER,
|
389
389
|
);
|
390
390
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
391
391
|
});
|
@@ -407,7 +407,7 @@ describe('ControllerRegistry', function () {
|
|
407
407
|
const matching = routeReg.matchRouteByRequest(req);
|
408
408
|
expect(matching).to.be.not.empty;
|
409
409
|
const res = matching!.route.hookRegistry.getHooks(
|
410
|
-
|
410
|
+
HookType.POST_HANDLER,
|
411
411
|
);
|
412
412
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
413
413
|
});
|
@@ -429,7 +429,7 @@ describe('ControllerRegistry', function () {
|
|
429
429
|
const matching = routeReg.matchRouteByRequest(req);
|
430
430
|
expect(matching).to.be.not.empty;
|
431
431
|
const res = matching!.route.hookRegistry.getHooks(
|
432
|
-
|
432
|
+
HookType.POST_HANDLER,
|
433
433
|
);
|
434
434
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
435
435
|
});
|
@@ -450,7 +450,7 @@ describe('ControllerRegistry', function () {
|
|
450
450
|
const matching = routeReg.matchRouteByRequest(req);
|
451
451
|
expect(matching).to.be.not.empty;
|
452
452
|
const res = matching!.route.hookRegistry.getHooks(
|
453
|
-
|
453
|
+
HookType.POST_HANDLER,
|
454
454
|
);
|
455
455
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
456
456
|
});
|
@@ -471,7 +471,7 @@ describe('ControllerRegistry', function () {
|
|
471
471
|
const matching = routeReg.matchRouteByRequest(req);
|
472
472
|
expect(matching).to.be.not.empty;
|
473
473
|
const res = matching!.route.hookRegistry.getHooks(
|
474
|
-
|
474
|
+
HookType.POST_HANDLER,
|
475
475
|
);
|
476
476
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
477
477
|
});
|
@@ -496,7 +496,7 @@ describe('ControllerRegistry', function () {
|
|
496
496
|
const matching = routeReg.matchRouteByRequest(req);
|
497
497
|
expect(matching).to.be.not.empty;
|
498
498
|
const res = matching!.route.hookRegistry.getHooks(
|
499
|
-
|
499
|
+
HookType.POST_HANDLER,
|
500
500
|
);
|
501
501
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
502
502
|
});
|
@@ -518,7 +518,7 @@ describe('ControllerRegistry', function () {
|
|
518
518
|
const matching = routeReg.matchRouteByRequest(req);
|
519
519
|
expect(matching).to.be.not.empty;
|
520
520
|
const res = matching!.route.hookRegistry.getHooks(
|
521
|
-
|
521
|
+
HookType.POST_HANDLER,
|
522
522
|
);
|
523
523
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
524
524
|
});
|
@@ -540,7 +540,7 @@ describe('ControllerRegistry', function () {
|
|
540
540
|
const matching = routeReg.matchRouteByRequest(req);
|
541
541
|
expect(matching).to.be.not.empty;
|
542
542
|
const res = matching!.route.hookRegistry.getHooks(
|
543
|
-
|
543
|
+
HookType.POST_HANDLER,
|
544
544
|
);
|
545
545
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
546
546
|
});
|
@@ -563,7 +563,7 @@ describe('ControllerRegistry', function () {
|
|
563
563
|
const matching = routeReg.matchRouteByRequest(req);
|
564
564
|
expect(matching).to.be.not.empty;
|
565
565
|
const res = matching!.route.hookRegistry.getHooks(
|
566
|
-
|
566
|
+
HookType.POST_HANDLER,
|
567
567
|
);
|
568
568
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
569
569
|
});
|
@@ -586,7 +586,7 @@ describe('ControllerRegistry', function () {
|
|
586
586
|
const matching = routeReg.matchRouteByRequest(req);
|
587
587
|
expect(matching).to.be.not.empty;
|
588
588
|
const res = matching!.route.hookRegistry.getHooks(
|
589
|
-
|
589
|
+
HookType.POST_HANDLER,
|
590
590
|
);
|
591
591
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
592
592
|
});
|
@@ -62,7 +62,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
62
62
|
// заметить ошибку в коде, который использует
|
63
63
|
// интерфейс данного сервиса
|
64
64
|
if (this.hasController(ctor))
|
65
|
-
throw new Errorf('The controller %v is already registered.');
|
65
|
+
throw new Errorf('The controller %v is already registered.', ctor.name);
|
66
66
|
// так как контроллером может быть любой
|
67
67
|
// класс, выполняется проверка на наличие
|
68
68
|
// метаданных применяемых декоратором
|
@@ -237,10 +237,10 @@ export class ControllerRegistry extends DebuggableService {
|
|
237
237
|
let preHandlers: RoutePreHandler[] = [];
|
238
238
|
const mdArray = BeforeActionReflector.getMetadata(ctor, actionName);
|
239
239
|
mdArray.forEach(md => {
|
240
|
-
if (Array.isArray(md.
|
241
|
-
preHandlers = [...preHandlers, ...md.
|
240
|
+
if (Array.isArray(md.hook)) {
|
241
|
+
preHandlers = [...preHandlers, ...md.hook];
|
242
242
|
} else {
|
243
|
-
preHandlers.push(md.
|
243
|
+
preHandlers.push(md.hook);
|
244
244
|
}
|
245
245
|
});
|
246
246
|
if (mdArray.length) {
|
@@ -271,10 +271,10 @@ export class ControllerRegistry extends DebuggableService {
|
|
271
271
|
let res: RoutePostHandler[] = [];
|
272
272
|
const mdArray = AfterActionReflector.getMetadata(ctor, actionName);
|
273
273
|
mdArray.forEach(md => {
|
274
|
-
if (Array.isArray(md.
|
275
|
-
res = [...res, ...md.
|
274
|
+
if (Array.isArray(md.hook)) {
|
275
|
+
res = [...res, ...md.hook];
|
276
276
|
} else {
|
277
|
-
res.push(md.
|
277
|
+
res.push(md.hook);
|
278
278
|
}
|
279
279
|
});
|
280
280
|
if (mdArray.length) {
|
@@ -366,7 +366,7 @@ export class ControllerRegistry extends DebuggableService {
|
|
366
366
|
let res: RoutePostHandler[] = [];
|
367
367
|
if (actionMd.after)
|
368
368
|
res = Array.isArray(actionMd.after) ? actionMd.after : [actionMd.after];
|
369
|
-
debug('%v
|
369
|
+
debug('%v post-handlers found.', res.length);
|
370
370
|
return res;
|
371
371
|
}
|
372
372
|
|
@@ -3,88 +3,85 @@ import {expect} from 'chai';
|
|
3
3
|
import {afterAction} from './after-action-decorator.js';
|
4
4
|
import {AfterActionReflector} from './after-action-reflector.js';
|
5
5
|
|
6
|
-
const
|
7
|
-
const
|
8
|
-
const
|
6
|
+
const HOOK_1 = () => undefined;
|
7
|
+
const HOOK_2 = () => undefined;
|
8
|
+
const HOOK_3 = () => undefined;
|
9
9
|
|
10
10
|
describe('afterAction', function () {
|
11
11
|
describe('class target', function () {
|
12
|
-
it('sets given
|
13
|
-
@afterAction(
|
12
|
+
it('sets given hook to the target metadata', function () {
|
13
|
+
@afterAction(HOOK_1)
|
14
14
|
class Target {
|
15
15
|
method() {}
|
16
16
|
}
|
17
17
|
const res = AfterActionReflector.getMetadata(Target);
|
18
|
-
expect(res).to.be.eql([{
|
18
|
+
expect(res).to.be.eql([{hook: HOOK_1}]);
|
19
19
|
});
|
20
20
|
|
21
|
-
it('sets multiple
|
22
|
-
@afterAction([
|
21
|
+
it('sets multiple hooks to the target metadata', function () {
|
22
|
+
@afterAction([HOOK_1, HOOK_2])
|
23
23
|
class Target {
|
24
24
|
method() {}
|
25
25
|
}
|
26
26
|
const res = AfterActionReflector.getMetadata(Target);
|
27
|
-
expect(res).to.be.eql([{
|
27
|
+
expect(res).to.be.eql([{hook: [HOOK_1, HOOK_2]}]);
|
28
28
|
});
|
29
29
|
|
30
30
|
it('allows to use the decorator multiple times', function () {
|
31
|
-
@afterAction(
|
32
|
-
@afterAction([
|
31
|
+
@afterAction(HOOK_1)
|
32
|
+
@afterAction([HOOK_2, HOOK_3])
|
33
33
|
class Target {
|
34
34
|
method() {}
|
35
35
|
}
|
36
36
|
const res = AfterActionReflector.getMetadata(Target);
|
37
|
-
expect(res).to.be.eql([
|
38
|
-
{middleware: MIDDLEWARE_1},
|
39
|
-
{middleware: [MIDDLEWARE_2, MIDDLEWARE_3]},
|
40
|
-
]);
|
37
|
+
expect(res).to.be.eql([{hook: HOOK_1}, {hook: [HOOK_2, HOOK_3]}]);
|
41
38
|
});
|
42
39
|
});
|
43
40
|
|
44
41
|
describe('method target', function () {
|
45
|
-
it('sets given
|
42
|
+
it('sets given hook to the target metadata', function () {
|
46
43
|
class Target {
|
47
|
-
@afterAction(
|
44
|
+
@afterAction(HOOK_1)
|
48
45
|
method() {}
|
49
46
|
}
|
50
47
|
const res = AfterActionReflector.getMetadata(Target, 'method');
|
51
48
|
expect(res).to.be.eql([
|
52
49
|
{
|
53
50
|
propertyKey: 'method',
|
54
|
-
|
51
|
+
hook: HOOK_1,
|
55
52
|
},
|
56
53
|
]);
|
57
54
|
});
|
58
55
|
|
59
|
-
it('sets multiple
|
56
|
+
it('sets multiple hooks to the target metadata', function () {
|
60
57
|
class Target {
|
61
|
-
@afterAction([
|
58
|
+
@afterAction([HOOK_1, HOOK_2])
|
62
59
|
method() {}
|
63
60
|
}
|
64
61
|
const res = AfterActionReflector.getMetadata(Target, 'method');
|
65
62
|
expect(res).to.be.eql([
|
66
63
|
{
|
67
64
|
propertyKey: 'method',
|
68
|
-
|
65
|
+
hook: [HOOK_1, HOOK_2],
|
69
66
|
},
|
70
67
|
]);
|
71
68
|
});
|
72
69
|
|
73
70
|
it('allows to use the decorator multiple times', function () {
|
74
71
|
class Target {
|
75
|
-
@afterAction(
|
76
|
-
@afterAction([
|
72
|
+
@afterAction(HOOK_1)
|
73
|
+
@afterAction([HOOK_2, HOOK_3])
|
77
74
|
method() {}
|
78
75
|
}
|
79
76
|
const res = AfterActionReflector.getMetadata(Target, 'method');
|
80
77
|
expect(res).to.be.eql([
|
81
78
|
{
|
82
79
|
propertyKey: 'method',
|
83
|
-
|
80
|
+
hook: HOOK_1,
|
84
81
|
},
|
85
82
|
{
|
86
83
|
propertyKey: 'method',
|
87
|
-
|
84
|
+
hook: [HOOK_2, HOOK_3],
|
88
85
|
},
|
89
86
|
]);
|
90
87
|
});
|
@@ -8,10 +8,10 @@ import {AfterActionReflector} from './after-action-reflector.js';
|
|
8
8
|
/**
|
9
9
|
* After action decorator.
|
10
10
|
*
|
11
|
-
* @param
|
11
|
+
* @param hook
|
12
12
|
*/
|
13
13
|
export function afterAction<T extends object>(
|
14
|
-
|
14
|
+
hook: AfterActionMetadata['hook'],
|
15
15
|
) {
|
16
16
|
return function (
|
17
17
|
target: Constructor<T> | Prototype<T>,
|
@@ -24,10 +24,10 @@ export function afterAction<T extends object>(
|
|
24
24
|
descriptor,
|
25
25
|
);
|
26
26
|
if (decoratorType === DecoratorTargetType.CONSTRUCTOR) {
|
27
|
-
AfterActionReflector.addMetadata({
|
27
|
+
AfterActionReflector.addMetadata({hook}, target as Constructor<T>);
|
28
28
|
} else if (decoratorType === DecoratorTargetType.INSTANCE_METHOD) {
|
29
29
|
AfterActionReflector.addMetadata(
|
30
|
-
{propertyKey,
|
30
|
+
{propertyKey, hook},
|
31
31
|
target.constructor as Constructor<T>,
|
32
32
|
propertyKey,
|
33
33
|
);
|
@@ -3,17 +3,17 @@ import {Reflector} from '@e22m4u/ts-reflector';
|
|
3
3
|
import {AfterActionReflector} from './after-action-reflector.js';
|
4
4
|
import {AFTER_ACTION_METADATA_KEY} from './after-action-metadata.js';
|
5
5
|
|
6
|
-
const
|
7
|
-
const
|
8
|
-
const
|
6
|
+
const HOOK_1 = () => undefined;
|
7
|
+
const HOOK_2 = () => undefined;
|
8
|
+
const HOOK_3 = () => undefined;
|
9
9
|
|
10
10
|
describe('AfterActionReflector', function () {
|
11
11
|
describe('class target', function () {
|
12
12
|
describe('addMetadata', function () {
|
13
13
|
it('adds a given value to the target metadata', function () {
|
14
14
|
class Target {}
|
15
|
-
const md1 = {
|
16
|
-
const md2 = {
|
15
|
+
const md1 = {hook: HOOK_1};
|
16
|
+
const md2 = {hook: [HOOK_2, HOOK_3]};
|
17
17
|
AfterActionReflector.addMetadata(md1, Target);
|
18
18
|
AfterActionReflector.addMetadata(md2, Target);
|
19
19
|
const res = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
|
@@ -30,8 +30,8 @@ describe('AfterActionReflector', function () {
|
|
30
30
|
|
31
31
|
it('returns existing metadata by the target', function () {
|
32
32
|
class Target {}
|
33
|
-
const md1 = {
|
34
|
-
const md2 = {
|
33
|
+
const md1 = {hook: HOOK_1};
|
34
|
+
const md2 = {hook: [HOOK_2, HOOK_3]};
|
35
35
|
const mdArray = [md1, md2];
|
36
36
|
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, mdArray, Target);
|
37
37
|
const res = AfterActionReflector.getMetadata(Target);
|
@@ -44,8 +44,8 @@ describe('AfterActionReflector', function () {
|
|
44
44
|
describe('addMetadata', function () {
|
45
45
|
it('adds a given value to the target metadata', function () {
|
46
46
|
class Target {}
|
47
|
-
const md1 = {
|
48
|
-
const md2 = {
|
47
|
+
const md1 = {hook: HOOK_1};
|
48
|
+
const md2 = {hook: [HOOK_2, HOOK_3]};
|
49
49
|
AfterActionReflector.addMetadata(md1, Target, 'prop');
|
50
50
|
AfterActionReflector.addMetadata(md2, Target, 'prop');
|
51
51
|
const res = Reflector.getOwnMetadata(
|
@@ -66,8 +66,8 @@ describe('AfterActionReflector', function () {
|
|
66
66
|
|
67
67
|
it('returns existing metadata by the target', function () {
|
68
68
|
class Target {}
|
69
|
-
const md1 = {
|
70
|
-
const md2 = {
|
69
|
+
const md1 = {hook: HOOK_1};
|
70
|
+
const md2 = {hook: [HOOK_2, HOOK_3]};
|
71
71
|
const mdArray = [md1, md2];
|
72
72
|
Reflector.defineMetadata(
|
73
73
|
AFTER_ACTION_METADATA_KEY,
|
@@ -84,8 +84,8 @@ describe('AfterActionReflector', function () {
|
|
84
84
|
describe('addMetadata', function () {
|
85
85
|
it('can distinguish class and method metadata', function () {
|
86
86
|
class Target {}
|
87
|
-
const md1 = {
|
88
|
-
const md2 = {
|
87
|
+
const md1 = {hook: HOOK_1};
|
88
|
+
const md2 = {hook: HOOK_2};
|
89
89
|
AfterActionReflector.addMetadata(md1, Target);
|
90
90
|
AfterActionReflector.addMetadata(md2, Target, 'prop');
|
91
91
|
const res1 = Reflector.getOwnMetadata(AFTER_ACTION_METADATA_KEY, Target);
|
@@ -102,8 +102,8 @@ describe('AfterActionReflector', function () {
|
|
102
102
|
describe('getMetadata', function () {
|
103
103
|
it('can distinguish class and method metadata', function () {
|
104
104
|
class Target {}
|
105
|
-
const md1 = {
|
106
|
-
const md2 = {
|
105
|
+
const md1 = {hook: HOOK_1};
|
106
|
+
const md2 = {hook: HOOK_2};
|
107
107
|
Reflector.defineMetadata(AFTER_ACTION_METADATA_KEY, [md1], Target);
|
108
108
|
Reflector.defineMetadata(
|
109
109
|
AFTER_ACTION_METADATA_KEY,
|
@@ -3,88 +3,85 @@ import {expect} from 'chai';
|
|
3
3
|
import {beforeAction} from './before-action-decorator.js';
|
4
4
|
import {BeforeActionReflector} from './before-action-reflector.js';
|
5
5
|
|
6
|
-
const
|
7
|
-
const
|
8
|
-
const
|
6
|
+
const HOOK_1 = () => undefined;
|
7
|
+
const HOOK_2 = () => undefined;
|
8
|
+
const HOOK_3 = () => undefined;
|
9
9
|
|
10
10
|
describe('beforeAction', function () {
|
11
11
|
describe('class target', function () {
|
12
|
-
it('sets given
|
13
|
-
@beforeAction(
|
12
|
+
it('sets given hook to the target metadata', function () {
|
13
|
+
@beforeAction(HOOK_1)
|
14
14
|
class Target {
|
15
15
|
method() {}
|
16
16
|
}
|
17
17
|
const res = BeforeActionReflector.getMetadata(Target);
|
18
|
-
expect(res).to.be.eql([{
|
18
|
+
expect(res).to.be.eql([{hook: HOOK_1}]);
|
19
19
|
});
|
20
20
|
|
21
|
-
it('sets multiple
|
22
|
-
@beforeAction([
|
21
|
+
it('sets multiple hooks to the target metadata', function () {
|
22
|
+
@beforeAction([HOOK_1, HOOK_2])
|
23
23
|
class Target {
|
24
24
|
method() {}
|
25
25
|
}
|
26
26
|
const res = BeforeActionReflector.getMetadata(Target);
|
27
|
-
expect(res).to.be.eql([{
|
27
|
+
expect(res).to.be.eql([{hook: [HOOK_1, HOOK_2]}]);
|
28
28
|
});
|
29
29
|
|
30
30
|
it('allows to use the decorator multiple times', function () {
|
31
|
-
@beforeAction(
|
32
|
-
@beforeAction([
|
31
|
+
@beforeAction(HOOK_1)
|
32
|
+
@beforeAction([HOOK_2, HOOK_3])
|
33
33
|
class Target {
|
34
34
|
method() {}
|
35
35
|
}
|
36
36
|
const res = BeforeActionReflector.getMetadata(Target);
|
37
|
-
expect(res).to.be.eql([
|
38
|
-
{middleware: MIDDLEWARE_1},
|
39
|
-
{middleware: [MIDDLEWARE_2, MIDDLEWARE_3]},
|
40
|
-
]);
|
37
|
+
expect(res).to.be.eql([{hook: HOOK_1}, {hook: [HOOK_2, HOOK_3]}]);
|
41
38
|
});
|
42
39
|
});
|
43
40
|
|
44
41
|
describe('method target', function () {
|
45
|
-
it('sets given
|
42
|
+
it('sets given hook to the target metadata', function () {
|
46
43
|
class Target {
|
47
|
-
@beforeAction(
|
44
|
+
@beforeAction(HOOK_1)
|
48
45
|
method() {}
|
49
46
|
}
|
50
47
|
const res = BeforeActionReflector.getMetadata(Target, 'method');
|
51
48
|
expect(res).to.be.eql([
|
52
49
|
{
|
53
50
|
propertyKey: 'method',
|
54
|
-
|
51
|
+
hook: HOOK_1,
|
55
52
|
},
|
56
53
|
]);
|
57
54
|
});
|
58
55
|
|
59
|
-
it('sets multiple
|
56
|
+
it('sets multiple hooks to the target metadata', function () {
|
60
57
|
class Target {
|
61
|
-
@beforeAction([
|
58
|
+
@beforeAction([HOOK_1, HOOK_2])
|
62
59
|
method() {}
|
63
60
|
}
|
64
61
|
const res = BeforeActionReflector.getMetadata(Target, 'method');
|
65
62
|
expect(res).to.be.eql([
|
66
63
|
{
|
67
64
|
propertyKey: 'method',
|
68
|
-
|
65
|
+
hook: [HOOK_1, HOOK_2],
|
69
66
|
},
|
70
67
|
]);
|
71
68
|
});
|
72
69
|
|
73
70
|
it('allows to use the decorator multiple times', function () {
|
74
71
|
class Target {
|
75
|
-
@beforeAction(
|
76
|
-
@beforeAction([
|
72
|
+
@beforeAction(HOOK_1)
|
73
|
+
@beforeAction([HOOK_2, HOOK_3])
|
77
74
|
method() {}
|
78
75
|
}
|
79
76
|
const res = BeforeActionReflector.getMetadata(Target, 'method');
|
80
77
|
expect(res).to.be.eql([
|
81
78
|
{
|
82
79
|
propertyKey: 'method',
|
83
|
-
|
80
|
+
hook: HOOK_1,
|
84
81
|
},
|
85
82
|
{
|
86
83
|
propertyKey: 'method',
|
87
|
-
|
84
|
+
hook: [HOOK_2, HOOK_3],
|
88
85
|
},
|
89
86
|
]);
|
90
87
|
});
|
@@ -8,10 +8,10 @@ import {BeforeActionReflector} from './before-action-reflector.js';
|
|
8
8
|
/**
|
9
9
|
* Before action decorator.
|
10
10
|
*
|
11
|
-
* @param
|
11
|
+
* @param hook
|
12
12
|
*/
|
13
13
|
export function beforeAction<T extends object>(
|
14
|
-
|
14
|
+
hook: BeforeActionMetadata['hook'],
|
15
15
|
) {
|
16
16
|
return function (
|
17
17
|
target: Constructor<T> | Prototype<T>,
|
@@ -24,10 +24,10 @@ export function beforeAction<T extends object>(
|
|
24
24
|
descriptor,
|
25
25
|
);
|
26
26
|
if (decoratorType === DecoratorTargetType.CONSTRUCTOR) {
|
27
|
-
BeforeActionReflector.addMetadata({
|
27
|
+
BeforeActionReflector.addMetadata({hook}, target as Constructor<T>);
|
28
28
|
} else if (decoratorType === DecoratorTargetType.INSTANCE_METHOD) {
|
29
29
|
BeforeActionReflector.addMetadata(
|
30
|
-
{propertyKey,
|
30
|
+
{propertyKey, hook},
|
31
31
|
target.constructor as Constructor<T>,
|
32
32
|
propertyKey,
|
33
33
|
);
|