@e22m4u/ts-rest-router 0.6.10 → 0.6.11

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.
@@ -975,6 +975,24 @@ var _RestRouter = class _RestRouter extends DebuggableService {
975
975
  this.getService(import_js_trie_router3.TrieRouter).addHook(type, hook);
976
976
  return this;
977
977
  }
978
+ /**
979
+ * Add pre-handler hook.
980
+ *
981
+ * @param hook
982
+ */
983
+ addPreHandler(hook) {
984
+ this.getService(import_js_trie_router3.TrieRouter).addPreHandler(hook);
985
+ return this;
986
+ }
987
+ /**
988
+ * Add post-handler hook.
989
+ *
990
+ * @param hook
991
+ */
992
+ addPostHandler(hook) {
993
+ this.getService(import_js_trie_router3.TrieRouter).addPostHandler(hook);
994
+ return this;
995
+ }
978
996
  };
979
997
  __name(_RestRouter, "RestRouter");
980
998
  var RestRouter = _RestRouter;
@@ -31,4 +31,16 @@ export declare class RestRouter extends DebuggableService {
31
31
  * @param hook
32
32
  */
33
33
  addHook(type: typeof RouterHookType.POST_HANDLER, hook: PostHandlerHook): this;
34
+ /**
35
+ * Add pre-handler hook.
36
+ *
37
+ * @param hook
38
+ */
39
+ addPreHandler(hook: PreHandlerHook): this;
40
+ /**
41
+ * Add post-handler hook.
42
+ *
43
+ * @param hook
44
+ */
45
+ addPostHandler(hook: PostHandlerHook): this;
34
46
  }
@@ -31,4 +31,22 @@ export class RestRouter extends DebuggableService {
31
31
  this.getService(TrieRouter).addHook(type, hook);
32
32
  return this;
33
33
  }
34
+ /**
35
+ * Add pre-handler hook.
36
+ *
37
+ * @param hook
38
+ */
39
+ addPreHandler(hook) {
40
+ this.getService(TrieRouter).addPreHandler(hook);
41
+ return this;
42
+ }
43
+ /**
44
+ * Add post-handler hook.
45
+ *
46
+ * @param hook
47
+ */
48
+ addPostHandler(hook) {
49
+ this.getService(TrieRouter).addPostHandler(hook);
50
+ return this;
51
+ }
34
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/ts-rest-router",
3
- "version": "0.6.10",
3
+ "version": "0.6.11",
4
4
  "description": "Декларативный REST-маршрутизатор на основе контроллеров для TypeScript",
5
5
  "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
6
6
  "license": "MIT",
@@ -45,7 +45,7 @@
45
45
  "@e22m4u/js-debug": "~0.3.3",
46
46
  "@e22m4u/js-format": "~0.2.1",
47
47
  "@e22m4u/js-service": "~0.4.6",
48
- "@e22m4u/js-trie-router": "~0.4.1",
48
+ "@e22m4u/js-trie-router": "~0.4.3",
49
49
  "@e22m4u/ts-reflector": "~0.1.8",
50
50
  "http-errors": "~2.0.0"
51
51
  },
@@ -1,9 +1,8 @@
1
1
  /* eslint mocha/no-sibling-hooks: 0 */
2
2
  import {
3
- createRequestMock,
4
- createResponseMock,
5
- RouterHookType,
3
+ TrieRouter,
6
4
  HttpMethod,
5
+ RouterHookType,
7
6
  ParsedCookies,
8
7
  ParsedHeaders,
9
8
  ParsedParams,
@@ -11,14 +10,16 @@ import {
11
10
  RequestContext,
12
11
  RequestParser,
13
12
  RouteRegistry,
14
- TrieRouter,
13
+ createRouteMock,
14
+ createRequestMock,
15
+ createResponseMock,
15
16
  } from '@e22m4u/js-trie-router';
16
17
 
17
18
  import {
18
- afterAction,
19
- beforeAction,
20
19
  getAction,
21
20
  postAction,
21
+ afterAction,
22
+ beforeAction,
22
23
  requestBody,
23
24
  requestField,
24
25
  requestCookies,
@@ -637,7 +638,7 @@ describe('ControllerRegistry', function () {
637
638
  const matching = routeReg.matchRouteByRequest(req);
638
639
  expect(matching).to.be.not.empty;
639
640
  const res = createResponseMock();
640
- const ctx = new RequestContext(S.container, req, res);
641
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
641
642
  ctx.params = matching!.params;
642
643
  await matching!.route.handler(ctx);
643
644
  expect(checked).to.be.true;
@@ -666,7 +667,7 @@ describe('ControllerRegistry', function () {
666
667
  const matching = routeReg.matchRouteByRequest(req);
667
668
  expect(matching).to.be.not.empty;
668
669
  const res = createResponseMock();
669
- const ctx = new RequestContext(S.container, req, res);
670
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
670
671
  ctx.params = matching!.params;
671
672
  await matching!.route.handler(ctx);
672
673
  expect(checked).to.be.true;
@@ -695,7 +696,7 @@ describe('ControllerRegistry', function () {
695
696
  const matching = routeReg.matchRouteByRequest(req);
696
697
  expect(matching).to.be.not.empty;
697
698
  const res = createResponseMock();
698
- const ctx = new RequestContext(S.container, req, res);
699
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
699
700
  const reqData = await S.getService(RequestParser).parse(req);
700
701
  Object.assign(ctx, reqData);
701
702
  await matching!.route.handler(ctx);
@@ -725,7 +726,7 @@ describe('ControllerRegistry', function () {
725
726
  const matching = routeReg.matchRouteByRequest(req);
726
727
  expect(matching).to.be.not.empty;
727
728
  const res = createResponseMock();
728
- const ctx = new RequestContext(S.container, req, res);
729
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
729
730
  const reqData = await S.getService(RequestParser).parse(req);
730
731
  Object.assign(ctx, reqData);
731
732
  await matching!.route.handler(ctx);
@@ -760,7 +761,7 @@ describe('ControllerRegistry', function () {
760
761
  const matching = routeReg.matchRouteByRequest(req);
761
762
  expect(matching).to.be.not.empty;
762
763
  const res = createResponseMock();
763
- const ctx = new RequestContext(S.container, req, res);
764
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
764
765
  const reqData = await S.getService(RequestParser).parse(req);
765
766
  Object.assign(ctx, reqData);
766
767
  await matching!.route.handler(ctx);
@@ -791,7 +792,7 @@ describe('ControllerRegistry', function () {
791
792
  const matching = routeReg.matchRouteByRequest(req);
792
793
  expect(matching).to.be.not.empty;
793
794
  const res = createResponseMock();
794
- const ctx = new RequestContext(S.container, req, res);
795
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
795
796
  const reqData = await S.getService(RequestParser).parse(req);
796
797
  Object.assign(ctx, reqData);
797
798
  await matching!.route.handler(ctx);
@@ -822,7 +823,7 @@ describe('ControllerRegistry', function () {
822
823
  const matching = routeReg.matchRouteByRequest(req);
823
824
  expect(matching).to.be.not.empty;
824
825
  const res = createResponseMock();
825
- const ctx = new RequestContext(S.container, req, res);
826
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
826
827
  const reqData = await S.getService(RequestParser).parse(req);
827
828
  Object.assign(ctx, reqData);
828
829
  await matching!.route.handler(ctx);
@@ -853,7 +854,7 @@ describe('ControllerRegistry', function () {
853
854
  const matching = routeReg.matchRouteByRequest(req);
854
855
  expect(matching).to.be.not.empty;
855
856
  const res = createResponseMock();
856
- const ctx = new RequestContext(S.container, req, res);
857
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
857
858
  const reqData = await S.getService(RequestParser).parse(req);
858
859
  Object.assign(ctx, reqData);
859
860
  await matching!.route.handler(ctx);
@@ -885,7 +886,7 @@ describe('ControllerRegistry', function () {
885
886
  const matching = routeReg.matchRouteByRequest(req);
886
887
  expect(matching).to.be.not.empty;
887
888
  const res = createResponseMock();
888
- const ctx = new RequestContext(S.container, req, res);
889
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
889
890
  const reqData = await S.getService(RequestParser).parse(req);
890
891
  Object.assign(ctx, reqData);
891
892
  await matching!.route.handler(ctx);
@@ -917,7 +918,7 @@ describe('ControllerRegistry', function () {
917
918
  const matching = routeReg.matchRouteByRequest(req);
918
919
  expect(matching).to.be.not.empty;
919
920
  const res = createResponseMock();
920
- const ctx = new RequestContext(S.container, req, res);
921
+ const ctx = new RequestContext(S.container, req, res, matching!.route);
921
922
  const reqData = await S.getService(RequestParser).parse(req);
922
923
  Object.assign(ctx, reqData);
923
924
  await matching!.route.handler(ctx);
@@ -963,7 +964,8 @@ describe('ControllerRegistry', function () {
963
964
  const S = new ControllerRegistry();
964
965
  const req = createRequestMock();
965
966
  const res = createResponseMock();
966
- const ctx = new RequestContext(S.container, req, res);
967
+ const route = createRouteMock();
968
+ const ctx = new RequestContext(S.container, req, res, route);
967
969
  const handler = S['createRouteHandler'](MyController, 'myAction');
968
970
  await handler(ctx);
969
971
  expect(invoked).to.be.true;
@@ -988,7 +990,8 @@ describe('ControllerRegistry', function () {
988
990
  const S = new ControllerRegistry();
989
991
  const req = createRequestMock();
990
992
  const res = createResponseMock();
991
- const ctx = new RequestContext(S.container, req, res);
993
+ const route = createRouteMock();
994
+ const ctx = new RequestContext(S.container, req, res, route);
992
995
  const handler = S['createRouteHandler'](MyController, 'myAction');
993
996
  await handler(ctx);
994
997
  expect(invoked).to.be.true;
@@ -1017,13 +1020,13 @@ describe('ControllerRegistry', function () {
1017
1020
  const req1 = createRequestMock();
1018
1021
  const res1 = createResponseMock();
1019
1022
  const cont1 = new ServiceContainer(S.container);
1020
- const ctx1 = new RequestContext(cont1, req1, res1);
1023
+ const ctx1 = new RequestContext(cont1, req1, res1, matching!.route);
1021
1024
  const controllerInstance1 = (await handler(ctx1)) as MyStatefulController;
1022
1025
  // симуляция второго запроса
1023
1026
  const req2 = createRequestMock();
1024
1027
  const res2 = createResponseMock();
1025
1028
  const cont2 = new ServiceContainer(S.container);
1026
- const ctx2 = new RequestContext(cont2, req2, res2);
1029
+ const ctx2 = new RequestContext(cont2, req2, res2, matching!.route);
1027
1030
  const controllerInstance2 = (await handler(ctx2)) as MyStatefulController;
1028
1031
  // проверка, что это два разных экземпляра
1029
1032
  expect(controllerInstance1).to.be.instanceOf(MyStatefulController);
@@ -1057,7 +1060,7 @@ describe('ControllerRegistry', function () {
1057
1060
  // симуляция запроса
1058
1061
  const res = createResponseMock();
1059
1062
  const cont1 = new ServiceContainer(S.container);
1060
- const ctx1 = new RequestContext(cont1, req, res);
1063
+ const ctx1 = new RequestContext(cont1, req, res, matching!.route);
1061
1064
  await handler(ctx1);
1062
1065
  expect(counter).to.be.eq(1);
1063
1066
  });
@@ -1078,8 +1081,9 @@ describe('ControllerRegistry', function () {
1078
1081
  }
1079
1082
  const req = createRequestMock();
1080
1083
  const res = createResponseMock();
1084
+ const route = createRouteMock();
1081
1085
  const S = new ControllerRegistry();
1082
- const ctx = new RequestContext(S.container, req, res);
1086
+ const ctx = new RequestContext(S.container, req, res, route);
1083
1087
  ctx.body = '10';
1084
1088
  const handler = S['createRouteHandler'](MyController, 'myAction');
1085
1089
  await handler(ctx);
@@ -1104,8 +1108,9 @@ describe('ControllerRegistry', function () {
1104
1108
  }
1105
1109
  const req = createRequestMock();
1106
1110
  const res = createResponseMock();
1111
+ const route = createRouteMock();
1107
1112
  const S = new ControllerRegistry();
1108
- const ctx = new RequestContext(S.container, req, res);
1113
+ const ctx = new RequestContext(S.container, req, res, route);
1109
1114
  const handler = S['createRouteHandler'](MyController, 'myAction');
1110
1115
  await handler(ctx);
1111
1116
  expect(invoked).to.be.eq(1);
@@ -1124,8 +1129,9 @@ describe('ControllerRegistry', function () {
1124
1129
  }
1125
1130
  const req = createRequestMock();
1126
1131
  const res = createResponseMock();
1132
+ const route = createRouteMock();
1127
1133
  const S = new ControllerRegistry();
1128
- const ctx = new RequestContext(S.container, req, res);
1134
+ const ctx = new RequestContext(S.container, req, res, route);
1129
1135
  ctx.body = '10';
1130
1136
  const handler = S['createRouteHandler'](MyController, 'myAction');
1131
1137
  await handler(ctx);
@@ -1145,11 +1151,11 @@ describe('ControllerRegistry', function () {
1145
1151
  return body;
1146
1152
  }
1147
1153
  }
1148
-
1149
1154
  const req = createRequestMock();
1150
1155
  const res = createResponseMock();
1156
+ const route = createRouteMock();
1151
1157
  const S = new ControllerRegistry();
1152
- const ctx = new RequestContext(S.container, req, res);
1158
+ const ctx = new RequestContext(S.container, req, res, route);
1153
1159
  const handler = S['createRouteHandler'](MyController, 'myAction');
1154
1160
  const throwable = () => handler(ctx);
1155
1161
  expect(throwable).to.throw(/is required, but undefined was given/);
@@ -1175,7 +1181,8 @@ describe('ControllerRegistry', function () {
1175
1181
  }
1176
1182
  const req = createRequestMock();
1177
1183
  const res = createResponseMock();
1178
- const ctx = new RequestContext(S.container, req, res);
1184
+ const route = createRouteMock();
1185
+ const ctx = new RequestContext(S.container, req, res, route);
1179
1186
  const handler = S['createRouteHandler'](MyController, 'myAction');
1180
1187
  await handler(ctx);
1181
1188
  expect(invoked).to.be.eq(1);
@@ -1199,7 +1206,8 @@ describe('ControllerRegistry', function () {
1199
1206
  }
1200
1207
  const req = createRequestMock();
1201
1208
  const res = createResponseMock();
1202
- const ctx = new RequestContext(S.container, req, res);
1209
+ const route = createRouteMock();
1210
+ const ctx = new RequestContext(S.container, req, res, route);
1203
1211
  ctx.body = '10';
1204
1212
  const handler = S['createRouteHandler'](MyController, 'myAction');
1205
1213
  await handler(ctx);
@@ -1224,7 +1232,8 @@ describe('ControllerRegistry', function () {
1224
1232
 
1225
1233
  const req = createRequestMock();
1226
1234
  const res = createResponseMock();
1227
- const ctx = new RequestContext(S.container, req, res);
1235
+ const route = createRouteMock();
1236
+ const ctx = new RequestContext(S.container, req, res, route);
1228
1237
  const handler = S['createRouteHandler'](MyController, 'myAction');
1229
1238
  const throwable = () => handler(ctx);
1230
1239
  expect(throwable).to.throw(/is required, but undefined was given/);
@@ -68,4 +68,24 @@ export class RestRouter extends DebuggableService {
68
68
  this.getService(TrieRouter).addHook(type, hook);
69
69
  return this;
70
70
  }
71
+
72
+ /**
73
+ * Add pre-handler hook.
74
+ *
75
+ * @param hook
76
+ */
77
+ addPreHandler(hook: PreHandlerHook) {
78
+ this.getService(TrieRouter).addPreHandler(hook);
79
+ return this;
80
+ }
81
+
82
+ /**
83
+ * Add post-handler hook.
84
+ *
85
+ * @param hook
86
+ */
87
+ addPostHandler(hook: PostHandlerHook) {
88
+ this.getService(TrieRouter).addPostHandler(hook);
89
+ return this;
90
+ }
71
91
  }