@e22m4u/ts-rest-router 0.0.6 → 0.1.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.
Files changed (80) hide show
  1. package/README-ru.md +227 -0
  2. package/README.md +227 -0
  3. package/dist/cjs/index.cjs +448 -153
  4. package/dist/esm/controller-registry.d.ts +53 -11
  5. package/dist/esm/controller-registry.js +242 -104
  6. package/dist/esm/debuggable-service.js +1 -1
  7. package/dist/esm/decorators/action/action-decorator.d.ts +7 -8
  8. package/dist/esm/decorators/action/action-decorator.js +1 -5
  9. package/dist/esm/decorators/action/action-decorator.spec.js +16 -16
  10. package/dist/esm/decorators/action/action-metadata.d.ts +0 -1
  11. package/dist/esm/decorators/after/after-decorator.d.ts +9 -0
  12. package/dist/esm/decorators/after/after-decorator.js +22 -0
  13. package/dist/esm/decorators/after/after-decorator.spec.d.ts +1 -0
  14. package/dist/esm/decorators/after/after-decorator.spec.js +115 -0
  15. package/dist/esm/decorators/after/after-metadata.d.ts +13 -0
  16. package/dist/esm/decorators/after/after-metadata.js +5 -0
  17. package/dist/esm/decorators/after/after-reflector.d.ts +22 -0
  18. package/dist/esm/decorators/after/after-reflector.js +29 -0
  19. package/dist/esm/decorators/after/after-reflector.spec.d.ts +1 -0
  20. package/dist/esm/decorators/after/after-reflector.spec.js +102 -0
  21. package/dist/esm/decorators/after/index.d.ts +3 -0
  22. package/dist/esm/decorators/after/index.js +3 -0
  23. package/dist/esm/decorators/before/before-decorator.d.ts +9 -0
  24. package/dist/esm/decorators/before/before-decorator.js +22 -0
  25. package/dist/esm/decorators/before/before-decorator.spec.d.ts +1 -0
  26. package/dist/esm/decorators/before/before-decorator.spec.js +115 -0
  27. package/dist/esm/decorators/before/before-metadata.d.ts +13 -0
  28. package/dist/esm/decorators/before/before-metadata.js +5 -0
  29. package/dist/esm/decorators/before/before-reflector.d.ts +22 -0
  30. package/dist/esm/decorators/before/before-reflector.js +29 -0
  31. package/dist/esm/decorators/before/before-reflector.spec.d.ts +1 -0
  32. package/dist/esm/decorators/before/before-reflector.spec.js +102 -0
  33. package/dist/esm/decorators/before/index.d.ts +3 -0
  34. package/dist/esm/decorators/before/index.js +3 -0
  35. package/dist/esm/decorators/controller/controller-decorator.d.ts +2 -1
  36. package/dist/esm/decorators/controller/controller-decorator.js +27 -6
  37. package/dist/esm/decorators/controller/controller-decorator.spec.js +37 -15
  38. package/dist/esm/decorators/controller/controller-metadata.d.ts +0 -1
  39. package/dist/esm/decorators/index.d.ts +2 -0
  40. package/dist/esm/decorators/index.js +2 -0
  41. package/dist/esm/decorators/request-context/request-context-decorator.d.ts +2 -3
  42. package/dist/esm/decorators/request-context/request-context-decorator.js +3 -6
  43. package/dist/esm/decorators/request-context/request-context-decorator.spec.js +2 -17
  44. package/dist/esm/decorators/request-context/request-context-metadata.d.ts +0 -1
  45. package/dist/esm/decorators/request-data/request-data-decorator.d.ts +7 -3
  46. package/dist/esm/decorators/request-data/request-data-decorator.js +16 -16
  47. package/dist/esm/decorators/request-data/request-data-decorator.spec.js +12 -10
  48. package/dist/esm/decorators/request-data/request-data-metadata.d.ts +0 -1
  49. package/dist/esm/utils/create-debugger.d.ts +35 -2
  50. package/dist/esm/utils/create-debugger.js +71 -5
  51. package/package.json +17 -17
  52. package/src/controller-registry.spec.ts +601 -275
  53. package/src/controller-registry.ts +263 -128
  54. package/src/debuggable-service.ts +1 -1
  55. package/src/decorators/action/action-decorator.spec.ts +16 -16
  56. package/src/decorators/action/action-decorator.ts +10 -12
  57. package/src/decorators/action/action-metadata.ts +0 -1
  58. package/src/decorators/after/after-decorator.spec.ts +92 -0
  59. package/src/decorators/after/after-decorator.ts +40 -0
  60. package/src/decorators/after/after-metadata.ts +17 -0
  61. package/src/decorators/after/after-reflector.spec.ts +107 -0
  62. package/src/decorators/after/after-reflector.ts +45 -0
  63. package/src/decorators/after/index.ts +3 -0
  64. package/src/decorators/before/before-decorator.spec.ts +92 -0
  65. package/src/decorators/before/before-decorator.ts +40 -0
  66. package/src/decorators/before/before-metadata.ts +17 -0
  67. package/src/decorators/before/before-reflector.spec.ts +111 -0
  68. package/src/decorators/before/before-reflector.ts +50 -0
  69. package/src/decorators/before/index.ts +3 -0
  70. package/src/decorators/controller/controller-decorator.spec.ts +33 -16
  71. package/src/decorators/controller/controller-decorator.ts +33 -6
  72. package/src/decorators/controller/controller-metadata.ts +0 -1
  73. package/src/decorators/index.ts +2 -0
  74. package/src/decorators/request-context/request-context-decorator.spec.ts +2 -15
  75. package/src/decorators/request-context/request-context-decorator.ts +3 -8
  76. package/src/decorators/request-context/request-context-metadata.ts +0 -1
  77. package/src/decorators/request-data/request-data-decorator.spec.ts +12 -11
  78. package/src/decorators/request-data/request-data-decorator.ts +41 -16
  79. package/src/decorators/request-data/request-data-metadata.ts +0 -1
  80. package/src/utils/create-debugger.ts +84 -7
@@ -1,59 +1,78 @@
1
+ /* eslint mocha/no-sibling-hooks: 0 */
1
2
  import {expect} from 'chai';
2
3
  import {get} from './decorators/index.js';
3
4
  import {post} from './decorators/index.js';
5
+ import {body} from './decorators/index.js';
6
+ import {after} from './decorators/index.js';
7
+ import {query} from './decorators/index.js';
8
+ import {param} from './decorators/index.js';
9
+ import {cookie} from './decorators/index.js';
10
+ import {params} from './decorators/index.js';
11
+ import {before} from './decorators/index.js';
12
+ import {header} from './decorators/index.js';
13
+ import {cookies} from './decorators/index.js';
14
+ import {queries} from './decorators/index.js';
15
+ import {headers} from './decorators/index.js';
16
+ import {bodyProp} from './decorators/index.js';
4
17
  import {HookName} from '@e22m4u/js-trie-router';
5
18
  import {controller} from './decorators/index.js';
6
19
  import {TrieRouter} from '@e22m4u/js-trie-router';
7
20
  import {HttpMethod} from '@e22m4u/js-trie-router';
21
+ import {ParsedQuery} from '@e22m4u/js-trie-router';
22
+ import {ParsedCookie} from '@e22m4u/js-trie-router';
23
+ import {ParsedParams} from '@e22m4u/js-trie-router';
24
+ import {ParsedHeaders} from '@e22m4u/js-trie-router';
8
25
  import {RouteRegistry} from '@e22m4u/js-trie-router';
26
+ import {RequestParser} from '@e22m4u/js-trie-router';
27
+ import {RequestContext} from '@e22m4u/js-trie-router';
9
28
  import {createRequestMock} from '@e22m4u/js-trie-router';
10
- import {ControllerReflector} from './decorators/index.js';
29
+ import {createResponseMock} from '@e22m4u/js-trie-router';
11
30
  import {ControllerRegistry} from './controller-registry.js';
12
31
 
13
32
  const PRE_HANDLER_1 = () => undefined;
14
33
  const PRE_HANDLER_2 = () => undefined;
15
- const PRE_HANDLER_3 = () => undefined;
16
- const PRE_HANDLER_4 = () => undefined;
34
+ // const PRE_HANDLER_3 = () => undefined;
35
+ // const PRE_HANDLER_4 = () => undefined;
17
36
 
18
37
  const POST_HANDLER_1 = () => undefined;
19
38
  const POST_HANDLER_2 = () => undefined;
20
- const POST_HANDLER_3 = () => undefined;
21
- const POST_HANDLER_4 = () => undefined;
39
+ // const POST_HANDLER_3 = () => undefined;
40
+ // const POST_HANDLER_4 = () => undefined;
22
41
 
23
42
  describe('ControllerRegistry', function () {
24
43
  it('has a public property with set of controllers', function () {
25
- const s = new ControllerRegistry();
26
- expect(s).to.have.property('controllers');
27
- expect(s.controllers).to.be.instanceof(Set);
44
+ const S = new ControllerRegistry();
45
+ expect(S).to.have.property('controllers');
46
+ expect(S.controllers).to.be.instanceof(Set);
28
47
  });
29
48
 
30
49
  describe('addController', function () {
31
50
  it('returns itself', function () {
32
- const s = new ControllerRegistry();
51
+ const S = new ControllerRegistry();
33
52
  @controller()
34
53
  class MyController {}
35
- const res = s.addController(MyController);
36
- expect(res).to.be.eq(s);
54
+ const res = S.addController(MyController);
55
+ expect(res).to.be.eq(S);
37
56
  });
38
57
 
39
58
  it('adds a given controller to controllers set', function () {
40
- const s = new ControllerRegistry();
59
+ const S = new ControllerRegistry();
41
60
  @controller()
42
61
  class MyController {}
43
- expect(s.hasController(MyController)).to.be.false;
44
- s.addController(MyController);
45
- expect(s.hasController(MyController)).to.be.true;
62
+ expect(S.hasController(MyController)).to.be.false;
63
+ S.addController(MyController);
64
+ expect(S.hasController(MyController)).to.be.true;
46
65
  });
47
66
 
48
- it('passes action method and path of a given controller to the route', function () {
49
- const s = new ControllerRegistry();
67
+ it('uses http method and action path for a new route', function () {
68
+ const S = new ControllerRegistry();
50
69
  @controller()
51
70
  class MyController {
52
71
  @get('/myAction')
53
72
  foo() {}
54
73
  }
55
- s.addController(MyController);
56
- const reg = s.getService(TrieRouter).getService(RouteRegistry);
74
+ S.addController(MyController);
75
+ const reg = S.getService(TrieRouter).getService(RouteRegistry);
57
76
  const req = createRequestMock({
58
77
  method: HttpMethod.GET,
59
78
  path: '/myAction',
@@ -64,8 +83,8 @@ describe('ControllerRegistry', function () {
64
83
  expect(matching!.route.path).to.be.eq('/myAction');
65
84
  });
66
85
 
67
- it('creates routes for multiple actions of a given controller', function () {
68
- const s = new ControllerRegistry();
86
+ it('adds multiple routes by the given controller', function () {
87
+ const S = new ControllerRegistry();
69
88
  @controller()
70
89
  class MyController {
71
90
  @get('/foo')
@@ -73,8 +92,8 @@ describe('ControllerRegistry', function () {
73
92
  @post('/bar')
74
93
  bar() {}
75
94
  }
76
- s.addController(MyController);
77
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
95
+ S.addController(MyController);
96
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
78
97
  const fooReq = createRequestMock({
79
98
  method: HttpMethod.GET,
80
99
  path: '/foo',
@@ -89,16 +108,107 @@ describe('ControllerRegistry', function () {
89
108
  expect(barMatching).to.be.not.empty;
90
109
  });
91
110
 
111
+ it('uses path prefix of controller root options', function () {
112
+ const S = new ControllerRegistry();
113
+ @controller()
114
+ class MyController {
115
+ @get('/myAction')
116
+ myAction() {}
117
+ }
118
+ S.addController(MyController, {pathPrefix: '/myPrefix'});
119
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
120
+ const req = createRequestMock({
121
+ method: HttpMethod.GET,
122
+ path: '/myPrefix/myAction',
123
+ });
124
+ const matching = routeReg.matchRouteByRequest(req);
125
+ expect(matching).to.be.not.empty;
126
+ });
127
+
128
+ it('uses path prefix of @controller metadata', function () {
129
+ const S = new ControllerRegistry();
130
+ @controller('/myController')
131
+ class MyController {
132
+ @get('/myAction')
133
+ myAction() {}
134
+ }
135
+ S.addController(MyController);
136
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
137
+ const req = createRequestMock({
138
+ method: HttpMethod.GET,
139
+ path: '/myController/myAction',
140
+ });
141
+ const matching = routeReg.matchRouteByRequest(req);
142
+ expect(matching).to.be.not.empty;
143
+ });
144
+
145
+ it('uses path prefix of controller root options and @controller metadata', function () {
146
+ const S = new ControllerRegistry();
147
+ @controller('/myController')
148
+ class MyController {
149
+ @get('/myAction')
150
+ myAction() {}
151
+ }
152
+ S.addController(MyController, {pathPrefix: '/myPrefix'});
153
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
154
+ const req = createRequestMock({
155
+ method: HttpMethod.GET,
156
+ path: '/myPrefix/myController/myAction',
157
+ });
158
+ const matching = routeReg.matchRouteByRequest(req);
159
+ expect(matching).to.be.not.empty;
160
+ });
161
+
92
162
  describe('single pre-handler', function () {
93
- it('passes pre-handler of a given options to the route', function () {
94
- const s = new ControllerRegistry();
163
+ it('uses pre-handler of controller root options', function () {
164
+ const S = new ControllerRegistry();
165
+ @controller()
166
+ class MyController {
167
+ @get('/myAction')
168
+ myAction() {}
169
+ }
170
+ S.addController(MyController, {before: PRE_HANDLER_1});
171
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
172
+ const req = createRequestMock({
173
+ method: HttpMethod.GET,
174
+ path: '/myAction',
175
+ });
176
+ const matching = routeReg.matchRouteByRequest(req);
177
+ expect(matching).to.be.not.empty;
178
+ const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
179
+ expect(res).to.be.eql([PRE_HANDLER_1]);
180
+ });
181
+
182
+ it('uses pre-handler of @before metadata applied to controller', function () {
183
+ const S = new ControllerRegistry();
184
+ @controller()
185
+ @before(PRE_HANDLER_1)
186
+ class MyController {
187
+ @get('/myAction')
188
+ myAction() {}
189
+ }
190
+ S.addController(MyController);
191
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
192
+ const req = createRequestMock({
193
+ method: HttpMethod.GET,
194
+ path: '/myAction',
195
+ });
196
+ const matching = routeReg.matchRouteByRequest(req);
197
+ expect(matching).to.be.not.empty;
198
+ const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
199
+ expect(res).to.be.eql([PRE_HANDLER_1]);
200
+ });
201
+
202
+ it('uses pre-handler of @before metadata applied to action', function () {
203
+ const S = new ControllerRegistry();
95
204
  @controller()
96
205
  class MyController {
97
206
  @get('/myAction')
207
+ @before(PRE_HANDLER_1)
98
208
  myAction() {}
99
209
  }
100
- s.addController(MyController, {before: PRE_HANDLER_1});
101
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
210
+ S.addController(MyController);
211
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
102
212
  const req = createRequestMock({
103
213
  method: HttpMethod.GET,
104
214
  path: '/myAction',
@@ -109,15 +219,15 @@ describe('ControllerRegistry', function () {
109
219
  expect(res).to.be.eql([PRE_HANDLER_1]);
110
220
  });
111
221
 
112
- it('passes pre-handler of a given controller to the route', function () {
113
- const s = new ControllerRegistry();
222
+ it('uses pre-handler of @controller metadata', function () {
223
+ const S = new ControllerRegistry();
114
224
  @controller({before: PRE_HANDLER_1})
115
225
  class MyController {
116
226
  @get('/myAction')
117
227
  myAction() {}
118
228
  }
119
- s.addController(MyController);
120
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
229
+ S.addController(MyController);
230
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
121
231
  const req = createRequestMock({
122
232
  method: HttpMethod.GET,
123
233
  path: '/myAction',
@@ -128,15 +238,15 @@ describe('ControllerRegistry', function () {
128
238
  expect(res).to.be.eql([PRE_HANDLER_1]);
129
239
  });
130
240
 
131
- it('passes action pre-handler of a given controller to the route', function () {
132
- const s = new ControllerRegistry();
241
+ it('uses pre-handler of @action metadata', function () {
242
+ const S = new ControllerRegistry();
133
243
  @controller()
134
244
  class MyController {
135
245
  @get('/myAction', {before: PRE_HANDLER_1})
136
246
  myAction() {}
137
247
  }
138
- s.addController(MyController);
139
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
248
+ S.addController(MyController);
249
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
140
250
  const req = createRequestMock({
141
251
  method: HttpMethod.GET,
142
252
  path: '/myAction',
@@ -149,17 +259,57 @@ describe('ControllerRegistry', function () {
149
259
  });
150
260
 
151
261
  describe('multiple pre-handlers', function () {
152
- it('passes pre-handlers of a given options to the route', function () {
153
- const s = new ControllerRegistry();
262
+ it('uses pre-handlers of controller root options', function () {
263
+ const S = new ControllerRegistry();
154
264
  @controller()
155
265
  class MyController {
156
266
  @get('/myAction')
157
267
  myAction() {}
158
268
  }
159
- s.addController(MyController, {
269
+ S.addController(MyController, {
160
270
  before: [PRE_HANDLER_1, PRE_HANDLER_2],
161
271
  });
162
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
272
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
273
+ const req = createRequestMock({
274
+ method: HttpMethod.GET,
275
+ path: '/myAction',
276
+ });
277
+ const matching = routeReg.matchRouteByRequest(req);
278
+ expect(matching).to.be.not.empty;
279
+ const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
280
+ expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
281
+ });
282
+
283
+ it('uses pre-handlers of @before metadata applied to controller', function () {
284
+ const S = new ControllerRegistry();
285
+ @controller()
286
+ @before([PRE_HANDLER_1, PRE_HANDLER_2])
287
+ class MyController {
288
+ @get('/myAction')
289
+ myAction() {}
290
+ }
291
+ S.addController(MyController);
292
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
293
+ const req = createRequestMock({
294
+ method: HttpMethod.GET,
295
+ path: '/myAction',
296
+ });
297
+ const matching = routeReg.matchRouteByRequest(req);
298
+ expect(matching).to.be.not.empty;
299
+ const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
300
+ expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
301
+ });
302
+
303
+ it('uses pre-handlers of @before metadata applied to action', function () {
304
+ const S = new ControllerRegistry();
305
+ @controller()
306
+ class MyController {
307
+ @get('/myAction')
308
+ @before([PRE_HANDLER_1, PRE_HANDLER_2])
309
+ myAction() {}
310
+ }
311
+ S.addController(MyController);
312
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
163
313
  const req = createRequestMock({
164
314
  method: HttpMethod.GET,
165
315
  path: '/myAction',
@@ -170,8 +320,8 @@ describe('ControllerRegistry', function () {
170
320
  expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
171
321
  });
172
322
 
173
- it('passes pre-handlers of a given controller to the route', function () {
174
- const s = new ControllerRegistry();
323
+ it('uses pre-handlers of @controller metadata', function () {
324
+ const S = new ControllerRegistry();
175
325
  @controller({
176
326
  before: [PRE_HANDLER_1, PRE_HANDLER_2],
177
327
  })
@@ -179,8 +329,8 @@ describe('ControllerRegistry', function () {
179
329
  @get('/myAction')
180
330
  myAction() {}
181
331
  }
182
- s.addController(MyController);
183
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
332
+ S.addController(MyController);
333
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
184
334
  const req = createRequestMock({
185
335
  method: HttpMethod.GET,
186
336
  path: '/myAction',
@@ -191,8 +341,8 @@ describe('ControllerRegistry', function () {
191
341
  expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
192
342
  });
193
343
 
194
- it('passes action pre-handlers of a given controller to the route', function () {
195
- const s = new ControllerRegistry();
344
+ it('uses pre-handlers of @action metadata', function () {
345
+ const S = new ControllerRegistry();
196
346
  @controller()
197
347
  class MyController {
198
348
  @get('/myAction', {
@@ -200,8 +350,8 @@ describe('ControllerRegistry', function () {
200
350
  })
201
351
  myAction() {}
202
352
  }
203
- s.addController(MyController);
204
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
353
+ S.addController(MyController);
354
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
205
355
  const req = createRequestMock({
206
356
  method: HttpMethod.GET,
207
357
  path: '/myAction',
@@ -214,15 +364,59 @@ describe('ControllerRegistry', function () {
214
364
  });
215
365
 
216
366
  describe('single post-handler', function () {
217
- it('passes post-handler of a given options to the route', function () {
218
- const s = new ControllerRegistry();
367
+ it('uses post-handler of controller root options', function () {
368
+ const S = new ControllerRegistry();
369
+ @controller()
370
+ class MyController {
371
+ @get('/myAction')
372
+ myAction() {}
373
+ }
374
+ S.addController(MyController, {after: POST_HANDLER_1});
375
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
376
+ const req = createRequestMock({
377
+ method: HttpMethod.GET,
378
+ path: '/myAction',
379
+ });
380
+ const matching = routeReg.matchRouteByRequest(req);
381
+ expect(matching).to.be.not.empty;
382
+ const res = matching!.route.hookRegistry.getHooks(
383
+ HookName.POST_HANDLER,
384
+ );
385
+ expect(res).to.be.eql([POST_HANDLER_1]);
386
+ });
387
+
388
+ it('uses pre-handler of @after metadata applied to controller', function () {
389
+ const S = new ControllerRegistry();
390
+ @controller()
391
+ @after(POST_HANDLER_1)
392
+ class MyController {
393
+ @get('/myAction')
394
+ myAction() {}
395
+ }
396
+ S.addController(MyController);
397
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
398
+ const req = createRequestMock({
399
+ method: HttpMethod.GET,
400
+ path: '/myAction',
401
+ });
402
+ const matching = routeReg.matchRouteByRequest(req);
403
+ expect(matching).to.be.not.empty;
404
+ const res = matching!.route.hookRegistry.getHooks(
405
+ HookName.POST_HANDLER,
406
+ );
407
+ expect(res).to.be.eql([POST_HANDLER_1]);
408
+ });
409
+
410
+ it('uses pre-handler of @after metadata applied to action', function () {
411
+ const S = new ControllerRegistry();
219
412
  @controller()
220
413
  class MyController {
221
414
  @get('/myAction')
415
+ @after(POST_HANDLER_1)
222
416
  myAction() {}
223
417
  }
224
- s.addController(MyController, {after: POST_HANDLER_1});
225
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
418
+ S.addController(MyController);
419
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
226
420
  const req = createRequestMock({
227
421
  method: HttpMethod.GET,
228
422
  path: '/myAction',
@@ -235,15 +429,15 @@ describe('ControllerRegistry', function () {
235
429
  expect(res).to.be.eql([POST_HANDLER_1]);
236
430
  });
237
431
 
238
- it('passes post-handler of a given controller to the route', function () {
239
- const s = new ControllerRegistry();
432
+ it('uses post-handler of @controller metadata', function () {
433
+ const S = new ControllerRegistry();
240
434
  @controller({after: POST_HANDLER_1})
241
435
  class MyController {
242
436
  @get('/myAction')
243
437
  myAction() {}
244
438
  }
245
- s.addController(MyController);
246
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
439
+ S.addController(MyController);
440
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
247
441
  const req = createRequestMock({
248
442
  method: HttpMethod.GET,
249
443
  path: '/myAction',
@@ -256,15 +450,15 @@ describe('ControllerRegistry', function () {
256
450
  expect(res).to.be.eql([POST_HANDLER_1]);
257
451
  });
258
452
 
259
- it('passes action post-handler of a given controller to the route', function () {
260
- const s = new ControllerRegistry();
453
+ it('uses post-handler of @action metadata', function () {
454
+ const S = new ControllerRegistry();
261
455
  @controller()
262
456
  class MyController {
263
457
  @get('/myAction', {after: POST_HANDLER_1})
264
458
  myAction() {}
265
459
  }
266
- s.addController(MyController);
267
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
460
+ S.addController(MyController);
461
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
268
462
  const req = createRequestMock({
269
463
  method: HttpMethod.GET,
270
464
  path: '/myAction',
@@ -279,17 +473,61 @@ describe('ControllerRegistry', function () {
279
473
  });
280
474
 
281
475
  describe('multiple post-handlers', function () {
282
- it('passes post-handlers of a given options to the route', function () {
283
- const s = new ControllerRegistry();
476
+ it('uses post-handlers of controller root options', function () {
477
+ const S = new ControllerRegistry();
284
478
  @controller()
285
479
  class MyController {
286
480
  @get('/myAction')
287
481
  myAction() {}
288
482
  }
289
- s.addController(MyController, {
483
+ S.addController(MyController, {
290
484
  after: [POST_HANDLER_1, POST_HANDLER_2],
291
485
  });
292
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
486
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
487
+ const req = createRequestMock({
488
+ method: HttpMethod.GET,
489
+ path: '/myAction',
490
+ });
491
+ const matching = routeReg.matchRouteByRequest(req);
492
+ expect(matching).to.be.not.empty;
493
+ const res = matching!.route.hookRegistry.getHooks(
494
+ HookName.POST_HANDLER,
495
+ );
496
+ expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
497
+ });
498
+
499
+ it('uses pre-handlers of @after metadata applied to controller', function () {
500
+ const S = new ControllerRegistry();
501
+ @controller()
502
+ @after([POST_HANDLER_1, POST_HANDLER_2])
503
+ class MyController {
504
+ @get('/myAction')
505
+ myAction() {}
506
+ }
507
+ S.addController(MyController);
508
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
509
+ const req = createRequestMock({
510
+ method: HttpMethod.GET,
511
+ path: '/myAction',
512
+ });
513
+ const matching = routeReg.matchRouteByRequest(req);
514
+ expect(matching).to.be.not.empty;
515
+ const res = matching!.route.hookRegistry.getHooks(
516
+ HookName.POST_HANDLER,
517
+ );
518
+ expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
519
+ });
520
+
521
+ it('uses pre-handlers of @after metadata applied to action', function () {
522
+ const S = new ControllerRegistry();
523
+ @controller()
524
+ class MyController {
525
+ @get('/myAction')
526
+ @after([POST_HANDLER_1, POST_HANDLER_2])
527
+ myAction() {}
528
+ }
529
+ S.addController(MyController);
530
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
293
531
  const req = createRequestMock({
294
532
  method: HttpMethod.GET,
295
533
  path: '/myAction',
@@ -302,8 +540,8 @@ describe('ControllerRegistry', function () {
302
540
  expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
303
541
  });
304
542
 
305
- it('passes post-handlers of a given controller to the route', function () {
306
- const s = new ControllerRegistry();
543
+ it('uses post-handlers of @controller metadata', function () {
544
+ const S = new ControllerRegistry();
307
545
  @controller({
308
546
  after: [POST_HANDLER_1, POST_HANDLER_2],
309
547
  })
@@ -311,8 +549,8 @@ describe('ControllerRegistry', function () {
311
549
  @get('/myAction')
312
550
  myAction() {}
313
551
  }
314
- s.addController(MyController);
315
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
552
+ S.addController(MyController);
553
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
316
554
  const req = createRequestMock({
317
555
  method: HttpMethod.GET,
318
556
  path: '/myAction',
@@ -325,8 +563,8 @@ describe('ControllerRegistry', function () {
325
563
  expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
326
564
  });
327
565
 
328
- it('passes action post-handlers of a given controller to the route', function () {
329
- const s = new ControllerRegistry();
566
+ it('uses post-handlers of @action metadata', function () {
567
+ const S = new ControllerRegistry();
330
568
  @controller()
331
569
  class MyController {
332
570
  @get('/myAction', {
@@ -334,8 +572,8 @@ describe('ControllerRegistry', function () {
334
572
  })
335
573
  myAction() {}
336
574
  }
337
- s.addController(MyController);
338
- const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
575
+ S.addController(MyController);
576
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
339
577
  const req = createRequestMock({
340
578
  method: HttpMethod.GET,
341
579
  path: '/myAction',
@@ -348,245 +586,333 @@ describe('ControllerRegistry', function () {
348
586
  expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
349
587
  });
350
588
  });
351
- });
352
589
 
353
- describe('hasController', function () {
354
- it('returns false if a given controller is not registered', function () {
355
- const s = new ControllerRegistry();
590
+ it('injects parsed parameters', async function () {
591
+ let checked = false;
592
+ const S = new ControllerRegistry();
356
593
  @controller()
357
- class MyController {}
358
- expect(s.hasController(MyController)).to.be.false;
594
+ class MyController {
595
+ @get('/myAction/:id')
596
+ myAction(
597
+ @params()
598
+ params: ParsedParams,
599
+ ) {
600
+ expect(params).to.be.eql({id: '123'});
601
+ checked = true;
602
+ }
603
+ }
604
+ S.addController(MyController);
605
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
606
+ const req = createRequestMock({
607
+ method: HttpMethod.GET,
608
+ path: '/myAction/123',
609
+ });
610
+ const matching = routeReg.matchRouteByRequest(req);
611
+ expect(matching).to.be.not.empty;
612
+ const res = createResponseMock();
613
+ const ctx = new RequestContext(S.container, req, res);
614
+ ctx.params = matching!.params;
615
+ await matching!.route.handler(ctx);
616
+ expect(checked).to.be.true;
359
617
  });
360
618
 
361
- it('returns true if a given controller is registered', function () {
362
- const s = new ControllerRegistry();
619
+ it('injects parsed parameter', async function () {
620
+ let checked = false;
621
+ const S = new ControllerRegistry();
363
622
  @controller()
364
- class MyController {}
365
- expect(s.hasController(MyController)).to.be.false;
366
- s.addController(MyController);
367
- expect(s.hasController(MyController)).to.be.true;
623
+ class MyController {
624
+ @get('/myAction/:id')
625
+ myAction(
626
+ @param('id')
627
+ param: string,
628
+ ) {
629
+ expect(param).to.be.eq('123');
630
+ checked = true;
631
+ }
632
+ }
633
+ S.addController(MyController);
634
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
635
+ const req = createRequestMock({
636
+ method: HttpMethod.GET,
637
+ path: '/myAction/123',
638
+ });
639
+ const matching = routeReg.matchRouteByRequest(req);
640
+ expect(matching).to.be.not.empty;
641
+ const res = createResponseMock();
642
+ const ctx = new RequestContext(S.container, req, res);
643
+ ctx.params = matching!.params;
644
+ await matching!.route.handler(ctx);
645
+ expect(checked).to.be.true;
368
646
  });
369
- });
370
647
 
371
- describe('getPathPrefixByControllerMetadata', function () {
372
- it('returns an empty string if no prefix is specified', function () {
373
- const s = new ControllerRegistry();
648
+ it('injects parsed queries', async function () {
649
+ let checked = false;
650
+ const S = new ControllerRegistry();
374
651
  @controller()
375
- class MyController {}
376
- const md = ControllerReflector.getMetadata(MyController)!;
377
- const res = s.getPathPrefixByControllerMetadata(md);
378
- expect(res).to.be.eq('');
379
- });
380
-
381
- it('returns controller path prefix that starts with slash', function () {
382
- const s = new ControllerRegistry();
383
- @controller({path: 'myPrefix'})
384
- class MyController {}
385
- const md = ControllerReflector.getMetadata(MyController)!;
386
- const res = s.getPathPrefixByControllerMetadata(md);
387
- expect(res).to.be.eq('/myPrefix');
652
+ class MyController {
653
+ @get('/myAction')
654
+ myAction(
655
+ @queries()
656
+ queries: ParsedQuery,
657
+ ) {
658
+ expect(queries).to.be.eql({foo: 'bar'});
659
+ checked = true;
660
+ }
661
+ }
662
+ S.addController(MyController);
663
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
664
+ const req = createRequestMock({
665
+ method: HttpMethod.GET,
666
+ path: '/myAction?foo=bar',
667
+ });
668
+ const matching = routeReg.matchRouteByRequest(req);
669
+ expect(matching).to.be.not.empty;
670
+ const res = createResponseMock();
671
+ const ctx = new RequestContext(S.container, req, res);
672
+ const reqData = await S.getService(RequestParser).parse(req);
673
+ Object.assign(ctx, reqData);
674
+ await matching!.route.handler(ctx);
675
+ expect(checked).to.be.true;
388
676
  });
389
677
 
390
- it('returns path prefix from options that starts with slash', function () {
391
- const s = new ControllerRegistry();
678
+ it('injects parsed query', async function () {
679
+ let checked = false;
680
+ const S = new ControllerRegistry();
392
681
  @controller()
393
- class MyController {}
394
- const md = ControllerReflector.getMetadata(MyController)!;
395
- const res = s.getPathPrefixByControllerMetadata(md, {
396
- pathPrefix: 'myPrefix',
682
+ class MyController {
683
+ @get('/myAction')
684
+ myAction(
685
+ @query('foo')
686
+ foo: string,
687
+ ) {
688
+ expect(foo).to.be.eql('bar');
689
+ checked = true;
690
+ }
691
+ }
692
+ S.addController(MyController);
693
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
694
+ const req = createRequestMock({
695
+ method: HttpMethod.GET,
696
+ path: '/myAction?foo=bar',
397
697
  });
398
- expect(res).to.be.eq('/myPrefix');
698
+ const matching = routeReg.matchRouteByRequest(req);
699
+ expect(matching).to.be.not.empty;
700
+ const res = createResponseMock();
701
+ const ctx = new RequestContext(S.container, req, res);
702
+ const reqData = await S.getService(RequestParser).parse(req);
703
+ Object.assign(ctx, reqData);
704
+ await matching!.route.handler(ctx);
705
+ expect(checked).to.be.true;
399
706
  });
400
707
 
401
- it('combines a path prefix from options and controller', function () {
402
- const s = new ControllerRegistry();
403
- @controller({path: 'controller'})
404
- class MyController {}
405
- const md = ControllerReflector.getMetadata(MyController)!;
406
- const res = s.getPathPrefixByControllerMetadata(md, {
407
- pathPrefix: 'root',
708
+ it('injects parsed headers', async function () {
709
+ let checked = false;
710
+ const S = new ControllerRegistry();
711
+ @controller()
712
+ class MyController {
713
+ @get('/myAction')
714
+ myAction(
715
+ @headers()
716
+ headers: ParsedHeaders,
717
+ ) {
718
+ expect(headers).to.be.eql({
719
+ host: 'localhost',
720
+ foo: 'bar',
721
+ });
722
+ checked = true;
723
+ }
724
+ }
725
+ S.addController(MyController);
726
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
727
+ const req = createRequestMock({
728
+ host: 'localhost',
729
+ method: HttpMethod.GET,
730
+ path: '/myAction',
731
+ headers: {foo: 'bar'},
408
732
  });
409
- expect(res).to.be.eq('/root/controller');
733
+ const matching = routeReg.matchRouteByRequest(req);
734
+ expect(matching).to.be.not.empty;
735
+ const res = createResponseMock();
736
+ const ctx = new RequestContext(S.container, req, res);
737
+ const reqData = await S.getService(RequestParser).parse(req);
738
+ Object.assign(ctx, reqData);
739
+ await matching!.route.handler(ctx);
740
+ expect(checked).to.be.true;
410
741
  });
411
- });
412
742
 
413
- describe('getPreHandlersByControllerMetadata', function () {
414
- it('returns an empty array if no handlers are specified', function () {
415
- const s = new ControllerRegistry();
743
+ it('injects parsed header', async function () {
744
+ let checked = false;
745
+ const S = new ControllerRegistry();
416
746
  @controller()
417
- class MyController {}
418
- const md = ControllerReflector.getMetadata(MyController)!;
419
- const res = s.getPreHandlersByControllerMetadata(md);
420
- expect(res).to.be.eql([]);
421
- });
422
-
423
- describe('single handler', function () {
424
- it('returns pre-handlers from a given controller', function () {
425
- const s = new ControllerRegistry();
426
- @controller({
427
- before: PRE_HANDLER_1,
428
- })
429
- class MyController {}
430
- const md = ControllerReflector.getMetadata(MyController)!;
431
- const res = s.getPreHandlersByControllerMetadata(md);
432
- expect(res).to.be.eql([PRE_HANDLER_1]);
433
- });
434
-
435
- it('returns pre-handlers from a given options', function () {
436
- const s = new ControllerRegistry();
437
- @controller()
438
- class MyController {}
439
- const md = ControllerReflector.getMetadata(MyController)!;
440
- const res = s.getPreHandlersByControllerMetadata(md, {
441
- before: PRE_HANDLER_1,
442
- });
443
- expect(res).to.be.eql([PRE_HANDLER_1]);
747
+ class MyController {
748
+ @get('/myAction')
749
+ myAction(
750
+ @header('foo')
751
+ foo: string,
752
+ ) {
753
+ expect(foo).to.be.eq('bar');
754
+ checked = true;
755
+ }
756
+ }
757
+ S.addController(MyController);
758
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
759
+ const req = createRequestMock({
760
+ method: HttpMethod.GET,
761
+ path: '/myAction',
762
+ headers: {foo: 'bar'},
444
763
  });
764
+ const matching = routeReg.matchRouteByRequest(req);
765
+ expect(matching).to.be.not.empty;
766
+ const res = createResponseMock();
767
+ const ctx = new RequestContext(S.container, req, res);
768
+ const reqData = await S.getService(RequestParser).parse(req);
769
+ Object.assign(ctx, reqData);
770
+ await matching!.route.handler(ctx);
771
+ expect(checked).to.be.true;
772
+ });
445
773
 
446
- it('combines pre-handlers from a given options and controller', function () {
447
- const s = new ControllerRegistry();
448
- @controller({
449
- before: PRE_HANDLER_2,
450
- })
451
- class MyController {}
452
- const md = ControllerReflector.getMetadata(MyController)!;
453
- const res = s.getPreHandlersByControllerMetadata(md, {
454
- before: PRE_HANDLER_1,
455
- });
456
- expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
774
+ it('injects parsed cookies', async function () {
775
+ let checked = false;
776
+ const S = new ControllerRegistry();
777
+ @controller()
778
+ class MyController {
779
+ @get('/myAction')
780
+ myAction(
781
+ @cookies()
782
+ cookies: ParsedCookie,
783
+ ) {
784
+ expect(cookies).to.be.eql({foo: 'bar'});
785
+ checked = true;
786
+ }
787
+ }
788
+ S.addController(MyController);
789
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
790
+ const req = createRequestMock({
791
+ method: HttpMethod.GET,
792
+ path: '/myAction',
793
+ headers: {cookie: 'foo=bar;'},
457
794
  });
795
+ const matching = routeReg.matchRouteByRequest(req);
796
+ expect(matching).to.be.not.empty;
797
+ const res = createResponseMock();
798
+ const ctx = new RequestContext(S.container, req, res);
799
+ const reqData = await S.getService(RequestParser).parse(req);
800
+ Object.assign(ctx, reqData);
801
+ await matching!.route.handler(ctx);
802
+ expect(checked).to.be.true;
458
803
  });
459
804
 
460
- describe('multiple handlers', function () {
461
- it('returns pre-handlers from a given controller', function () {
462
- const s = new ControllerRegistry();
463
- @controller({
464
- before: [PRE_HANDLER_1, PRE_HANDLER_2],
465
- })
466
- class MyController {}
467
- const md = ControllerReflector.getMetadata(MyController)!;
468
- const res = s.getPreHandlersByControllerMetadata(md);
469
- expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
805
+ it('injects parsed cookie', async function () {
806
+ let checked = false;
807
+ const S = new ControllerRegistry();
808
+ @controller()
809
+ class MyController {
810
+ @get('/myAction')
811
+ myAction(
812
+ @cookie('foo')
813
+ foo: string,
814
+ ) {
815
+ expect(foo).to.be.eq('bar');
816
+ checked = true;
817
+ }
818
+ }
819
+ S.addController(MyController);
820
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
821
+ const req = createRequestMock({
822
+ method: HttpMethod.GET,
823
+ path: '/myAction',
824
+ headers: {cookie: 'foo=bar;'},
470
825
  });
826
+ const matching = routeReg.matchRouteByRequest(req);
827
+ expect(matching).to.be.not.empty;
828
+ const res = createResponseMock();
829
+ const ctx = new RequestContext(S.container, req, res);
830
+ const reqData = await S.getService(RequestParser).parse(req);
831
+ Object.assign(ctx, reqData);
832
+ await matching!.route.handler(ctx);
833
+ expect(checked).to.be.true;
834
+ });
471
835
 
472
- it('returns pre-handlers from a given options', function () {
473
- const s = new ControllerRegistry();
474
- @controller()
475
- class MyController {}
476
- const md = ControllerReflector.getMetadata(MyController)!;
477
- const res = s.getPreHandlersByControllerMetadata(md, {
478
- before: [PRE_HANDLER_1, PRE_HANDLER_2],
479
- });
480
- expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
836
+ it('injects parsed body', async function () {
837
+ let checked = false;
838
+ const S = new ControllerRegistry();
839
+ @controller()
840
+ class MyController {
841
+ @post('/myAction')
842
+ myAction(
843
+ @body()
844
+ body: object,
845
+ ) {
846
+ expect(body).to.be.eql({foo: 'bar'});
847
+ checked = true;
848
+ }
849
+ }
850
+ S.addController(MyController);
851
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
852
+ const req = createRequestMock({
853
+ method: HttpMethod.POST,
854
+ path: '/myAction',
855
+ headers: {'content-type': 'application/json'},
856
+ body: '{"foo":"bar"}',
481
857
  });
858
+ const matching = routeReg.matchRouteByRequest(req);
859
+ expect(matching).to.be.not.empty;
860
+ const res = createResponseMock();
861
+ const ctx = new RequestContext(S.container, req, res);
862
+ const reqData = await S.getService(RequestParser).parse(req);
863
+ Object.assign(ctx, reqData);
864
+ await matching!.route.handler(ctx);
865
+ expect(checked).to.be.true;
866
+ });
482
867
 
483
- it('combines pre-handlers from a given options and controller', function () {
484
- const s = new ControllerRegistry();
485
- @controller({
486
- before: [PRE_HANDLER_3, PRE_HANDLER_4],
487
- })
488
- class MyController {}
489
- const md = ControllerReflector.getMetadata(MyController)!;
490
- const res = s.getPreHandlersByControllerMetadata(md, {
491
- before: [PRE_HANDLER_1, PRE_HANDLER_2],
492
- });
493
- expect(res).to.be.eql([
494
- PRE_HANDLER_1,
495
- PRE_HANDLER_2,
496
- PRE_HANDLER_3,
497
- PRE_HANDLER_4,
498
- ]);
868
+ it('injects parsed body parameter', async function () {
869
+ let checked = false;
870
+ const S = new ControllerRegistry();
871
+ @controller()
872
+ class MyController {
873
+ @post('/myAction')
874
+ myAction(
875
+ @bodyProp('foo')
876
+ foo: object,
877
+ ) {
878
+ expect(foo).to.be.eq('bar');
879
+ checked = true;
880
+ }
881
+ }
882
+ S.addController(MyController);
883
+ const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
884
+ const req = createRequestMock({
885
+ method: HttpMethod.POST,
886
+ path: '/myAction',
887
+ headers: {'content-type': 'application/json'},
888
+ body: '{"foo":"bar"}',
499
889
  });
890
+ const matching = routeReg.matchRouteByRequest(req);
891
+ expect(matching).to.be.not.empty;
892
+ const res = createResponseMock();
893
+ const ctx = new RequestContext(S.container, req, res);
894
+ const reqData = await S.getService(RequestParser).parse(req);
895
+ Object.assign(ctx, reqData);
896
+ await matching!.route.handler(ctx);
897
+ expect(checked).to.be.true;
500
898
  });
501
899
  });
502
900
 
503
- describe('getPostHandlersByControllerMetadata', function () {
504
- it('returns an empty array if no handlers are specified', function () {
505
- const s = new ControllerRegistry();
901
+ describe('hasController', function () {
902
+ it('returns false if a given controller is not registered', function () {
903
+ const S = new ControllerRegistry();
506
904
  @controller()
507
905
  class MyController {}
508
- const md = ControllerReflector.getMetadata(MyController)!;
509
- const res = s.getPostHandlersByControllerMetadata(md);
510
- expect(res).to.be.eql([]);
511
- });
512
-
513
- describe('single handler', function () {
514
- it('returns pre-handlers from a given controller', function () {
515
- const s = new ControllerRegistry();
516
- @controller({
517
- after: POST_HANDLER_1,
518
- })
519
- class MyController {}
520
- const md = ControllerReflector.getMetadata(MyController)!;
521
- const res = s.getPostHandlersByControllerMetadata(md);
522
- expect(res).to.be.eql([POST_HANDLER_1]);
523
- });
524
-
525
- it('returns pre-handlers from a given options', function () {
526
- const s = new ControllerRegistry();
527
- @controller()
528
- class MyController {}
529
- const md = ControllerReflector.getMetadata(MyController)!;
530
- const res = s.getPostHandlersByControllerMetadata(md, {
531
- after: POST_HANDLER_1,
532
- });
533
- expect(res).to.be.eql([POST_HANDLER_1]);
534
- });
535
-
536
- it('combines pre-handlers from a given options and controller', function () {
537
- const s = new ControllerRegistry();
538
- @controller({
539
- after: POST_HANDLER_2,
540
- })
541
- class MyController {}
542
- const md = ControllerReflector.getMetadata(MyController)!;
543
- const res = s.getPostHandlersByControllerMetadata(md, {
544
- after: POST_HANDLER_1,
545
- });
546
- expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
547
- });
906
+ expect(S.hasController(MyController)).to.be.false;
548
907
  });
549
908
 
550
- describe('multiple handlers', function () {
551
- it('returns pre-handlers from a given controller', function () {
552
- const s = new ControllerRegistry();
553
- @controller({
554
- after: [POST_HANDLER_1, POST_HANDLER_2],
555
- })
556
- class MyController {}
557
- const md = ControllerReflector.getMetadata(MyController)!;
558
- const res = s.getPostHandlersByControllerMetadata(md);
559
- expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
560
- });
561
-
562
- it('returns pre-handlers from a given options', function () {
563
- const s = new ControllerRegistry();
564
- @controller()
565
- class MyController {}
566
- const md = ControllerReflector.getMetadata(MyController)!;
567
- const res = s.getPostHandlersByControllerMetadata(md, {
568
- after: [POST_HANDLER_1, POST_HANDLER_2],
569
- });
570
- expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
571
- });
572
-
573
- it('combines pre-handlers from a given options and controller', function () {
574
- const s = new ControllerRegistry();
575
- @controller({
576
- after: [POST_HANDLER_3, POST_HANDLER_4],
577
- })
578
- class MyController {}
579
- const md = ControllerReflector.getMetadata(MyController)!;
580
- const res = s.getPostHandlersByControllerMetadata(md, {
581
- after: [POST_HANDLER_1, POST_HANDLER_2],
582
- });
583
- expect(res).to.be.eql([
584
- POST_HANDLER_1,
585
- POST_HANDLER_2,
586
- POST_HANDLER_3,
587
- POST_HANDLER_4,
588
- ]);
589
- });
909
+ it('returns true if a given controller is registered', function () {
910
+ const S = new ControllerRegistry();
911
+ @controller()
912
+ class MyController {}
913
+ expect(S.hasController(MyController)).to.be.false;
914
+ S.addController(MyController);
915
+ expect(S.hasController(MyController)).to.be.true;
590
916
  });
591
917
  });
592
918
  });