@e22m4u/ts-rest-router 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/.c8rc +9 -0
- package/.commitlintrc +5 -0
- package/.editorconfig +13 -0
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +6 -0
- package/.mocharc.json +5 -0
- package/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/README-ru.md +41 -0
- package/README.md +41 -0
- package/build-cjs.js +16 -0
- package/dist/cjs/index.cjs +692 -0
- package/dist/esm/controller-registry.d.ts +65 -0
- package/dist/esm/controller-registry.js +281 -0
- package/dist/esm/controller-registry.spec.d.ts +1 -0
- package/dist/esm/controller-registry.spec.js +719 -0
- package/dist/esm/debuggable-service.d.ts +18 -0
- package/dist/esm/debuggable-service.js +23 -0
- package/dist/esm/debuggable-service.spec.d.ts +1 -0
- package/dist/esm/debuggable-service.spec.js +16 -0
- package/dist/esm/decorators/action/action-decorator.d.ts +53 -0
- package/dist/esm/decorators/action/action-decorator.js +66 -0
- package/dist/esm/decorators/action/action-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/action/action-decorator.spec.js +59 -0
- package/dist/esm/decorators/action/action-metadata.d.ts +23 -0
- package/dist/esm/decorators/action/action-metadata.js +5 -0
- package/dist/esm/decorators/action/action-reflector.d.ts +22 -0
- package/dist/esm/decorators/action/action-reflector.js +29 -0
- package/dist/esm/decorators/action/action-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/action/action-reflector.spec.js +84 -0
- package/dist/esm/decorators/action/index.d.ts +3 -0
- package/dist/esm/decorators/action/index.js +3 -0
- package/dist/esm/decorators/controller/controller-decorator.d.ts +13 -0
- package/dist/esm/decorators/controller/controller-decorator.js +20 -0
- package/dist/esm/decorators/controller/controller-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/controller/controller-decorator.spec.js +53 -0
- package/dist/esm/decorators/controller/controller-metadata.d.ts +17 -0
- package/dist/esm/decorators/controller/controller-metadata.js +5 -0
- package/dist/esm/decorators/controller/controller-reflector.d.ts +20 -0
- package/dist/esm/decorators/controller/controller-reflector.js +24 -0
- package/dist/esm/decorators/controller/controller-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/controller/controller-reflector.spec.js +45 -0
- package/dist/esm/decorators/controller/index.d.ts +3 -0
- package/dist/esm/decorators/controller/index.js +3 -0
- package/dist/esm/decorators/index.d.ts +4 -0
- package/dist/esm/decorators/index.js +4 -0
- package/dist/esm/decorators/request-context/index.d.ts +3 -0
- package/dist/esm/decorators/request-context/index.js +3 -0
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +17 -0
- package/dist/esm/decorators/request-context/request-context-decorator.js +32 -0
- package/dist/esm/decorators/request-context/request-context-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +59 -0
- package/dist/esm/decorators/request-context/request-context-metadata.d.ts +17 -0
- package/dist/esm/decorators/request-context/request-context-metadata.js +5 -0
- package/dist/esm/decorators/request-context/request-context-reflector.d.ts +24 -0
- package/dist/esm/decorators/request-context/request-context-reflector.js +31 -0
- package/dist/esm/decorators/request-context/request-context-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/request-context/request-context-reflector.spec.js +59 -0
- package/dist/esm/decorators/request-data/index.d.ts +3 -0
- package/dist/esm/decorators/request-data/index.js +3 -0
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +28 -0
- package/dist/esm/decorators/request-data/request-data-decorator.js +84 -0
- package/dist/esm/decorators/request-data/request-data-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +534 -0
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +29 -0
- package/dist/esm/decorators/request-data/request-data-metadata.js +16 -0
- package/dist/esm/decorators/request-data/request-data-reflector.d.ts +24 -0
- package/dist/esm/decorators/request-data/request-data-reflector.js +31 -0
- package/dist/esm/decorators/request-data/request-data-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/request-data/request-data-reflector.spec.js +60 -0
- package/dist/esm/errors/index.d.ts +1 -0
- package/dist/esm/errors/index.js +1 -0
- package/dist/esm/errors/not-a-controller-error.d.ts +12 -0
- package/dist/esm/errors/not-a-controller-error.js +14 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/rest-router.d.ts +19 -0
- package/dist/esm/rest-router.js +24 -0
- package/dist/esm/types.d.ts +57 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/utils/capitalize.d.ts +6 -0
- package/dist/esm/utils/capitalize.js +8 -0
- package/dist/esm/utils/capitalize.spec.d.ts +1 -0
- package/dist/esm/utils/capitalize.spec.js +8 -0
- package/dist/esm/utils/create-debugger.d.ts +11 -0
- package/dist/esm/utils/create-debugger.js +15 -0
- package/dist/esm/utils/create-debugger.spec.d.ts +1 -0
- package/dist/esm/utils/create-debugger.spec.js +8 -0
- package/dist/esm/utils/create-error.d.ts +10 -0
- package/dist/esm/utils/create-error.js +13 -0
- package/dist/esm/utils/create-error.spec.d.ts +1 -0
- package/dist/esm/utils/create-error.spec.js +8 -0
- package/dist/esm/utils/index.d.ts +4 -0
- package/dist/esm/utils/index.js +4 -0
- package/dist/esm/utils/to-camel-case.d.ts +6 -0
- package/dist/esm/utils/to-camel-case.js +11 -0
- package/dist/esm/utils/to-camel-case.spec.d.ts +1 -0
- package/dist/esm/utils/to-camel-case.spec.js +10 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/eslint.config.js +43 -0
- package/package.json +74 -0
- package/src/controller-registry.spec.ts +592 -0
- package/src/controller-registry.ts +355 -0
- package/src/debuggable-service.spec.ts +18 -0
- package/src/debuggable-service.ts +27 -0
- package/src/decorators/action/action-decorator.spec.ts +42 -0
- package/src/decorators/action/action-decorator.ts +100 -0
- package/src/decorators/action/action-metadata.ts +28 -0
- package/src/decorators/action/action-reflector.spec.ts +84 -0
- package/src/decorators/action/action-reflector.ts +38 -0
- package/src/decorators/action/index.ts +3 -0
- package/src/decorators/controller/controller-decorator.spec.ts +41 -0
- package/src/decorators/controller/controller-decorator.ts +29 -0
- package/src/decorators/controller/controller-metadata.ts +21 -0
- package/src/decorators/controller/controller-reflector.spec.ts +45 -0
- package/src/decorators/controller/controller-reflector.ts +28 -0
- package/src/decorators/controller/index.ts +3 -0
- package/src/decorators/index.ts +4 -0
- package/src/decorators/request-context/index.ts +3 -0
- package/src/decorators/request-context/request-context-decorator.spec.ts +41 -0
- package/src/decorators/request-context/request-context-decorator.ts +57 -0
- package/src/decorators/request-context/request-context-metadata.ts +21 -0
- package/src/decorators/request-context/request-context-reflector.spec.ts +77 -0
- package/src/decorators/request-context/request-context-reflector.ts +57 -0
- package/src/decorators/request-data/index.ts +3 -0
- package/src/decorators/request-data/request-data-decorator.spec.ts +477 -0
- package/src/decorators/request-data/request-data-decorator.ts +106 -0
- package/src/decorators/request-data/request-data-metadata.ts +34 -0
- package/src/decorators/request-data/request-data-reflector.spec.ts +78 -0
- package/src/decorators/request-data/request-data-reflector.ts +57 -0
- package/src/errors/index.ts +1 -0
- package/src/errors/not-a-controller-error.ts +15 -0
- package/src/index.ts +5 -0
- package/src/rest-router.ts +31 -0
- package/src/types.ts +59 -0
- package/src/utils/capitalize.spec.ts +9 -0
- package/src/utils/capitalize.ts +8 -0
- package/src/utils/create-debugger.spec.ts +9 -0
- package/src/utils/create-debugger.ts +21 -0
- package/src/utils/create-error.spec.ts +9 -0
- package/src/utils/create-error.ts +19 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/to-camel-case.spec.ts +11 -0
- package/src/utils/to-camel-case.ts +11 -0
- package/tsconfig.json +17 -0
@@ -0,0 +1,592 @@
|
|
1
|
+
import {expect} from 'chai';
|
2
|
+
import {get} from './decorators/index.js';
|
3
|
+
import {post} from './decorators/index.js';
|
4
|
+
import {HookName} from '@e22m4u/js-trie-router';
|
5
|
+
import {controller} from './decorators/index.js';
|
6
|
+
import {TrieRouter} from '@e22m4u/js-trie-router';
|
7
|
+
import {HttpMethod} from '@e22m4u/js-trie-router';
|
8
|
+
import {RouteRegistry} from '@e22m4u/js-trie-router';
|
9
|
+
import {createRequestMock} from '@e22m4u/js-trie-router';
|
10
|
+
import {ControllerReflector} from './decorators/index.js';
|
11
|
+
import {ControllerRegistry} from './controller-registry.js';
|
12
|
+
|
13
|
+
const PRE_HANDLER_1 = () => undefined;
|
14
|
+
const PRE_HANDLER_2 = () => undefined;
|
15
|
+
const PRE_HANDLER_3 = () => undefined;
|
16
|
+
const PRE_HANDLER_4 = () => undefined;
|
17
|
+
|
18
|
+
const POST_HANDLER_1 = () => undefined;
|
19
|
+
const POST_HANDLER_2 = () => undefined;
|
20
|
+
const POST_HANDLER_3 = () => undefined;
|
21
|
+
const POST_HANDLER_4 = () => undefined;
|
22
|
+
|
23
|
+
describe('ControllerRegistry', function () {
|
24
|
+
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);
|
28
|
+
});
|
29
|
+
|
30
|
+
describe('addController', function () {
|
31
|
+
it('returns itself', function () {
|
32
|
+
const s = new ControllerRegistry();
|
33
|
+
@controller()
|
34
|
+
class MyController {}
|
35
|
+
const res = s.addController(MyController);
|
36
|
+
expect(res).to.be.eq(s);
|
37
|
+
});
|
38
|
+
|
39
|
+
it('adds a given controller to controllers set', function () {
|
40
|
+
const s = new ControllerRegistry();
|
41
|
+
@controller()
|
42
|
+
class MyController {}
|
43
|
+
expect(s.hasController(MyController)).to.be.false;
|
44
|
+
s.addController(MyController);
|
45
|
+
expect(s.hasController(MyController)).to.be.true;
|
46
|
+
});
|
47
|
+
|
48
|
+
it('passes action method and path of a given controller to the route', function () {
|
49
|
+
const s = new ControllerRegistry();
|
50
|
+
@controller()
|
51
|
+
class MyController {
|
52
|
+
@get('/myAction')
|
53
|
+
foo() {}
|
54
|
+
}
|
55
|
+
s.addController(MyController);
|
56
|
+
const reg = s.getService(TrieRouter).getService(RouteRegistry);
|
57
|
+
const req = createRequestMock({
|
58
|
+
method: HttpMethod.GET,
|
59
|
+
path: '/myAction',
|
60
|
+
});
|
61
|
+
const matching = reg.matchRouteByRequest(req);
|
62
|
+
expect(matching).to.be.not.empty;
|
63
|
+
expect(matching!.route.method).to.be.eq(HttpMethod.GET);
|
64
|
+
expect(matching!.route.path).to.be.eq('/myAction');
|
65
|
+
});
|
66
|
+
|
67
|
+
it('creates routes for multiple actions of a given controller', function () {
|
68
|
+
const s = new ControllerRegistry();
|
69
|
+
@controller()
|
70
|
+
class MyController {
|
71
|
+
@get('/foo')
|
72
|
+
foo() {}
|
73
|
+
@post('/bar')
|
74
|
+
bar() {}
|
75
|
+
}
|
76
|
+
s.addController(MyController);
|
77
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
78
|
+
const fooReq = createRequestMock({
|
79
|
+
method: HttpMethod.GET,
|
80
|
+
path: '/foo',
|
81
|
+
});
|
82
|
+
const fooMatching = routeReg.matchRouteByRequest(fooReq);
|
83
|
+
expect(fooMatching).to.be.not.empty;
|
84
|
+
const barReq = createRequestMock({
|
85
|
+
method: HttpMethod.POST,
|
86
|
+
path: '/bar',
|
87
|
+
});
|
88
|
+
const barMatching = routeReg.matchRouteByRequest(barReq);
|
89
|
+
expect(barMatching).to.be.not.empty;
|
90
|
+
});
|
91
|
+
|
92
|
+
describe('single pre-handler', function () {
|
93
|
+
it('passes pre-handler of a given options to the route', function () {
|
94
|
+
const s = new ControllerRegistry();
|
95
|
+
@controller()
|
96
|
+
class MyController {
|
97
|
+
@get('/myAction')
|
98
|
+
myAction() {}
|
99
|
+
}
|
100
|
+
s.addController(MyController, {before: PRE_HANDLER_1});
|
101
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
102
|
+
const req = createRequestMock({
|
103
|
+
method: HttpMethod.GET,
|
104
|
+
path: '/myAction',
|
105
|
+
});
|
106
|
+
const matching = routeReg.matchRouteByRequest(req);
|
107
|
+
expect(matching).to.be.not.empty;
|
108
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
109
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
110
|
+
});
|
111
|
+
|
112
|
+
it('passes pre-handler of a given controller to the route', function () {
|
113
|
+
const s = new ControllerRegistry();
|
114
|
+
@controller({before: PRE_HANDLER_1})
|
115
|
+
class MyController {
|
116
|
+
@get('/myAction')
|
117
|
+
myAction() {}
|
118
|
+
}
|
119
|
+
s.addController(MyController);
|
120
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
121
|
+
const req = createRequestMock({
|
122
|
+
method: HttpMethod.GET,
|
123
|
+
path: '/myAction',
|
124
|
+
});
|
125
|
+
const matching = routeReg.matchRouteByRequest(req);
|
126
|
+
expect(matching).to.be.not.empty;
|
127
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
128
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
129
|
+
});
|
130
|
+
|
131
|
+
it('passes action pre-handler of a given controller to the route', function () {
|
132
|
+
const s = new ControllerRegistry();
|
133
|
+
@controller()
|
134
|
+
class MyController {
|
135
|
+
@get('/myAction', {before: PRE_HANDLER_1})
|
136
|
+
myAction() {}
|
137
|
+
}
|
138
|
+
s.addController(MyController);
|
139
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
140
|
+
const req = createRequestMock({
|
141
|
+
method: HttpMethod.GET,
|
142
|
+
path: '/myAction',
|
143
|
+
});
|
144
|
+
const matching = routeReg.matchRouteByRequest(req);
|
145
|
+
expect(matching).to.be.not.empty;
|
146
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
147
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
148
|
+
});
|
149
|
+
});
|
150
|
+
|
151
|
+
describe('multiple pre-handlers', function () {
|
152
|
+
it('passes pre-handlers of a given options to the route', function () {
|
153
|
+
const s = new ControllerRegistry();
|
154
|
+
@controller()
|
155
|
+
class MyController {
|
156
|
+
@get('/myAction')
|
157
|
+
myAction() {}
|
158
|
+
}
|
159
|
+
s.addController(MyController, {
|
160
|
+
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
161
|
+
});
|
162
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
163
|
+
const req = createRequestMock({
|
164
|
+
method: HttpMethod.GET,
|
165
|
+
path: '/myAction',
|
166
|
+
});
|
167
|
+
const matching = routeReg.matchRouteByRequest(req);
|
168
|
+
expect(matching).to.be.not.empty;
|
169
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
170
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
171
|
+
});
|
172
|
+
|
173
|
+
it('passes pre-handlers of a given controller to the route', function () {
|
174
|
+
const s = new ControllerRegistry();
|
175
|
+
@controller({
|
176
|
+
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
177
|
+
})
|
178
|
+
class MyController {
|
179
|
+
@get('/myAction')
|
180
|
+
myAction() {}
|
181
|
+
}
|
182
|
+
s.addController(MyController);
|
183
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
184
|
+
const req = createRequestMock({
|
185
|
+
method: HttpMethod.GET,
|
186
|
+
path: '/myAction',
|
187
|
+
});
|
188
|
+
const matching = routeReg.matchRouteByRequest(req);
|
189
|
+
expect(matching).to.be.not.empty;
|
190
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
191
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
192
|
+
});
|
193
|
+
|
194
|
+
it('passes action pre-handlers of a given controller to the route', function () {
|
195
|
+
const s = new ControllerRegistry();
|
196
|
+
@controller()
|
197
|
+
class MyController {
|
198
|
+
@get('/myAction', {
|
199
|
+
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
200
|
+
})
|
201
|
+
myAction() {}
|
202
|
+
}
|
203
|
+
s.addController(MyController);
|
204
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
205
|
+
const req = createRequestMock({
|
206
|
+
method: HttpMethod.GET,
|
207
|
+
path: '/myAction',
|
208
|
+
});
|
209
|
+
const matching = routeReg.matchRouteByRequest(req);
|
210
|
+
expect(matching).to.be.not.empty;
|
211
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
212
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
213
|
+
});
|
214
|
+
});
|
215
|
+
|
216
|
+
describe('single post-handler', function () {
|
217
|
+
it('passes post-handler of a given options to the route', function () {
|
218
|
+
const s = new ControllerRegistry();
|
219
|
+
@controller()
|
220
|
+
class MyController {
|
221
|
+
@get('/myAction')
|
222
|
+
myAction() {}
|
223
|
+
}
|
224
|
+
s.addController(MyController, {after: POST_HANDLER_1});
|
225
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
226
|
+
const req = createRequestMock({
|
227
|
+
method: HttpMethod.GET,
|
228
|
+
path: '/myAction',
|
229
|
+
});
|
230
|
+
const matching = routeReg.matchRouteByRequest(req);
|
231
|
+
expect(matching).to.be.not.empty;
|
232
|
+
const res = matching!.route.hookRegistry.getHooks(
|
233
|
+
HookName.POST_HANDLER,
|
234
|
+
);
|
235
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
236
|
+
});
|
237
|
+
|
238
|
+
it('passes post-handler of a given controller to the route', function () {
|
239
|
+
const s = new ControllerRegistry();
|
240
|
+
@controller({after: POST_HANDLER_1})
|
241
|
+
class MyController {
|
242
|
+
@get('/myAction')
|
243
|
+
myAction() {}
|
244
|
+
}
|
245
|
+
s.addController(MyController);
|
246
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
247
|
+
const req = createRequestMock({
|
248
|
+
method: HttpMethod.GET,
|
249
|
+
path: '/myAction',
|
250
|
+
});
|
251
|
+
const matching = routeReg.matchRouteByRequest(req);
|
252
|
+
expect(matching).to.be.not.empty;
|
253
|
+
const res = matching!.route.hookRegistry.getHooks(
|
254
|
+
HookName.POST_HANDLER,
|
255
|
+
);
|
256
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
257
|
+
});
|
258
|
+
|
259
|
+
it('passes action post-handler of a given controller to the route', function () {
|
260
|
+
const s = new ControllerRegistry();
|
261
|
+
@controller()
|
262
|
+
class MyController {
|
263
|
+
@get('/myAction', {after: POST_HANDLER_1})
|
264
|
+
myAction() {}
|
265
|
+
}
|
266
|
+
s.addController(MyController);
|
267
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
268
|
+
const req = createRequestMock({
|
269
|
+
method: HttpMethod.GET,
|
270
|
+
path: '/myAction',
|
271
|
+
});
|
272
|
+
const matching = routeReg.matchRouteByRequest(req);
|
273
|
+
expect(matching).to.be.not.empty;
|
274
|
+
const res = matching!.route.hookRegistry.getHooks(
|
275
|
+
HookName.POST_HANDLER,
|
276
|
+
);
|
277
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
278
|
+
});
|
279
|
+
});
|
280
|
+
|
281
|
+
describe('multiple post-handlers', function () {
|
282
|
+
it('passes post-handlers of a given options to the route', function () {
|
283
|
+
const s = new ControllerRegistry();
|
284
|
+
@controller()
|
285
|
+
class MyController {
|
286
|
+
@get('/myAction')
|
287
|
+
myAction() {}
|
288
|
+
}
|
289
|
+
s.addController(MyController, {
|
290
|
+
after: [POST_HANDLER_1, POST_HANDLER_2],
|
291
|
+
});
|
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(
|
300
|
+
HookName.POST_HANDLER,
|
301
|
+
);
|
302
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
303
|
+
});
|
304
|
+
|
305
|
+
it('passes post-handlers of a given controller to the route', function () {
|
306
|
+
const s = new ControllerRegistry();
|
307
|
+
@controller({
|
308
|
+
after: [POST_HANDLER_1, POST_HANDLER_2],
|
309
|
+
})
|
310
|
+
class MyController {
|
311
|
+
@get('/myAction')
|
312
|
+
myAction() {}
|
313
|
+
}
|
314
|
+
s.addController(MyController);
|
315
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
316
|
+
const req = createRequestMock({
|
317
|
+
method: HttpMethod.GET,
|
318
|
+
path: '/myAction',
|
319
|
+
});
|
320
|
+
const matching = routeReg.matchRouteByRequest(req);
|
321
|
+
expect(matching).to.be.not.empty;
|
322
|
+
const res = matching!.route.hookRegistry.getHooks(
|
323
|
+
HookName.POST_HANDLER,
|
324
|
+
);
|
325
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
326
|
+
});
|
327
|
+
|
328
|
+
it('passes action post-handlers of a given controller to the route', function () {
|
329
|
+
const s = new ControllerRegistry();
|
330
|
+
@controller()
|
331
|
+
class MyController {
|
332
|
+
@get('/myAction', {
|
333
|
+
after: [POST_HANDLER_1, POST_HANDLER_2],
|
334
|
+
})
|
335
|
+
myAction() {}
|
336
|
+
}
|
337
|
+
s.addController(MyController);
|
338
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
339
|
+
const req = createRequestMock({
|
340
|
+
method: HttpMethod.GET,
|
341
|
+
path: '/myAction',
|
342
|
+
});
|
343
|
+
const matching = routeReg.matchRouteByRequest(req);
|
344
|
+
expect(matching).to.be.not.empty;
|
345
|
+
const res = matching!.route.hookRegistry.getHooks(
|
346
|
+
HookName.POST_HANDLER,
|
347
|
+
);
|
348
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
349
|
+
});
|
350
|
+
});
|
351
|
+
});
|
352
|
+
|
353
|
+
describe('hasController', function () {
|
354
|
+
it('returns false if a given controller is not registered', function () {
|
355
|
+
const s = new ControllerRegistry();
|
356
|
+
@controller()
|
357
|
+
class MyController {}
|
358
|
+
expect(s.hasController(MyController)).to.be.false;
|
359
|
+
});
|
360
|
+
|
361
|
+
it('returns true if a given controller is registered', function () {
|
362
|
+
const s = new ControllerRegistry();
|
363
|
+
@controller()
|
364
|
+
class MyController {}
|
365
|
+
expect(s.hasController(MyController)).to.be.false;
|
366
|
+
s.addController(MyController);
|
367
|
+
expect(s.hasController(MyController)).to.be.true;
|
368
|
+
});
|
369
|
+
});
|
370
|
+
|
371
|
+
describe('getPathPrefixByControllerMetadata', function () {
|
372
|
+
it('returns an empty string if no prefix is specified', function () {
|
373
|
+
const s = new ControllerRegistry();
|
374
|
+
@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');
|
388
|
+
});
|
389
|
+
|
390
|
+
it('returns path prefix from options that starts with slash', function () {
|
391
|
+
const s = new ControllerRegistry();
|
392
|
+
@controller()
|
393
|
+
class MyController {}
|
394
|
+
const md = ControllerReflector.getMetadata(MyController)!;
|
395
|
+
const res = s.getPathPrefixByControllerMetadata(md, {
|
396
|
+
pathPrefix: 'myPrefix',
|
397
|
+
});
|
398
|
+
expect(res).to.be.eq('/myPrefix');
|
399
|
+
});
|
400
|
+
|
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',
|
408
|
+
});
|
409
|
+
expect(res).to.be.eq('/root/controller');
|
410
|
+
});
|
411
|
+
});
|
412
|
+
|
413
|
+
describe('getPreHandlersByControllerMetadata', function () {
|
414
|
+
it('returns an empty array if no handlers are specified', function () {
|
415
|
+
const s = new ControllerRegistry();
|
416
|
+
@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]);
|
444
|
+
});
|
445
|
+
|
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]);
|
457
|
+
});
|
458
|
+
});
|
459
|
+
|
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]);
|
470
|
+
});
|
471
|
+
|
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]);
|
481
|
+
});
|
482
|
+
|
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
|
+
]);
|
499
|
+
});
|
500
|
+
});
|
501
|
+
});
|
502
|
+
|
503
|
+
describe('getPostHandlersByControllerMetadata', function () {
|
504
|
+
it('returns an empty array if no handlers are specified', function () {
|
505
|
+
const s = new ControllerRegistry();
|
506
|
+
@controller()
|
507
|
+
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
|
+
});
|
548
|
+
});
|
549
|
+
|
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
|
+
});
|
590
|
+
});
|
591
|
+
});
|
592
|
+
});
|