@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,719 @@
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
6
|
+
};
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
9
|
+
};
|
10
|
+
import { expect } from 'chai';
|
11
|
+
import { get } from './decorators/index.js';
|
12
|
+
import { post } from './decorators/index.js';
|
13
|
+
import { HookName } from '@e22m4u/js-trie-router';
|
14
|
+
import { controller } from './decorators/index.js';
|
15
|
+
import { TrieRouter } from '@e22m4u/js-trie-router';
|
16
|
+
import { HttpMethod } from '@e22m4u/js-trie-router';
|
17
|
+
import { RouteRegistry } from '@e22m4u/js-trie-router';
|
18
|
+
import { createRequestMock } from '@e22m4u/js-trie-router';
|
19
|
+
import { ControllerReflector } from './decorators/index.js';
|
20
|
+
import { ControllerRegistry } from './controller-registry.js';
|
21
|
+
const PRE_HANDLER_1 = () => undefined;
|
22
|
+
const PRE_HANDLER_2 = () => undefined;
|
23
|
+
const PRE_HANDLER_3 = () => undefined;
|
24
|
+
const PRE_HANDLER_4 = () => undefined;
|
25
|
+
const POST_HANDLER_1 = () => undefined;
|
26
|
+
const POST_HANDLER_2 = () => undefined;
|
27
|
+
const POST_HANDLER_3 = () => undefined;
|
28
|
+
const POST_HANDLER_4 = () => undefined;
|
29
|
+
describe('ControllerRegistry', function () {
|
30
|
+
it('has a public property with set of controllers', function () {
|
31
|
+
const s = new ControllerRegistry();
|
32
|
+
expect(s).to.have.property('controllers');
|
33
|
+
expect(s.controllers).to.be.instanceof(Set);
|
34
|
+
});
|
35
|
+
describe('addController', function () {
|
36
|
+
it('returns itself', function () {
|
37
|
+
const s = new ControllerRegistry();
|
38
|
+
let MyController = class MyController {
|
39
|
+
};
|
40
|
+
MyController = __decorate([
|
41
|
+
controller()
|
42
|
+
], MyController);
|
43
|
+
const res = s.addController(MyController);
|
44
|
+
expect(res).to.be.eq(s);
|
45
|
+
});
|
46
|
+
it('adds a given controller to controllers set', function () {
|
47
|
+
const s = new ControllerRegistry();
|
48
|
+
let MyController = class MyController {
|
49
|
+
};
|
50
|
+
MyController = __decorate([
|
51
|
+
controller()
|
52
|
+
], MyController);
|
53
|
+
expect(s.hasController(MyController)).to.be.false;
|
54
|
+
s.addController(MyController);
|
55
|
+
expect(s.hasController(MyController)).to.be.true;
|
56
|
+
});
|
57
|
+
it('passes action method and path of a given controller to the route', function () {
|
58
|
+
const s = new ControllerRegistry();
|
59
|
+
let MyController = class MyController {
|
60
|
+
foo() { }
|
61
|
+
};
|
62
|
+
__decorate([
|
63
|
+
get('/myAction'),
|
64
|
+
__metadata("design:type", Function),
|
65
|
+
__metadata("design:paramtypes", []),
|
66
|
+
__metadata("design:returntype", void 0)
|
67
|
+
], MyController.prototype, "foo", null);
|
68
|
+
MyController = __decorate([
|
69
|
+
controller()
|
70
|
+
], MyController);
|
71
|
+
s.addController(MyController);
|
72
|
+
const reg = s.getService(TrieRouter).getService(RouteRegistry);
|
73
|
+
const req = createRequestMock({
|
74
|
+
method: HttpMethod.GET,
|
75
|
+
path: '/myAction',
|
76
|
+
});
|
77
|
+
const matching = reg.matchRouteByRequest(req);
|
78
|
+
expect(matching).to.be.not.empty;
|
79
|
+
expect(matching.route.method).to.be.eq(HttpMethod.GET);
|
80
|
+
expect(matching.route.path).to.be.eq('/myAction');
|
81
|
+
});
|
82
|
+
it('creates routes for multiple actions of a given controller', function () {
|
83
|
+
const s = new ControllerRegistry();
|
84
|
+
let MyController = class MyController {
|
85
|
+
foo() { }
|
86
|
+
bar() { }
|
87
|
+
};
|
88
|
+
__decorate([
|
89
|
+
get('/foo'),
|
90
|
+
__metadata("design:type", Function),
|
91
|
+
__metadata("design:paramtypes", []),
|
92
|
+
__metadata("design:returntype", void 0)
|
93
|
+
], MyController.prototype, "foo", null);
|
94
|
+
__decorate([
|
95
|
+
post('/bar'),
|
96
|
+
__metadata("design:type", Function),
|
97
|
+
__metadata("design:paramtypes", []),
|
98
|
+
__metadata("design:returntype", void 0)
|
99
|
+
], MyController.prototype, "bar", null);
|
100
|
+
MyController = __decorate([
|
101
|
+
controller()
|
102
|
+
], MyController);
|
103
|
+
s.addController(MyController);
|
104
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
105
|
+
const fooReq = createRequestMock({
|
106
|
+
method: HttpMethod.GET,
|
107
|
+
path: '/foo',
|
108
|
+
});
|
109
|
+
const fooMatching = routeReg.matchRouteByRequest(fooReq);
|
110
|
+
expect(fooMatching).to.be.not.empty;
|
111
|
+
const barReq = createRequestMock({
|
112
|
+
method: HttpMethod.POST,
|
113
|
+
path: '/bar',
|
114
|
+
});
|
115
|
+
const barMatching = routeReg.matchRouteByRequest(barReq);
|
116
|
+
expect(barMatching).to.be.not.empty;
|
117
|
+
});
|
118
|
+
describe('single pre-handler', function () {
|
119
|
+
it('passes pre-handler of a given options to the route', function () {
|
120
|
+
const s = new ControllerRegistry();
|
121
|
+
let MyController = class MyController {
|
122
|
+
myAction() { }
|
123
|
+
};
|
124
|
+
__decorate([
|
125
|
+
get('/myAction'),
|
126
|
+
__metadata("design:type", Function),
|
127
|
+
__metadata("design:paramtypes", []),
|
128
|
+
__metadata("design:returntype", void 0)
|
129
|
+
], MyController.prototype, "myAction", null);
|
130
|
+
MyController = __decorate([
|
131
|
+
controller()
|
132
|
+
], MyController);
|
133
|
+
s.addController(MyController, { before: PRE_HANDLER_1 });
|
134
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
135
|
+
const req = createRequestMock({
|
136
|
+
method: HttpMethod.GET,
|
137
|
+
path: '/myAction',
|
138
|
+
});
|
139
|
+
const matching = routeReg.matchRouteByRequest(req);
|
140
|
+
expect(matching).to.be.not.empty;
|
141
|
+
const res = matching.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
142
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
143
|
+
});
|
144
|
+
it('passes pre-handler of a given controller to the route', function () {
|
145
|
+
const s = new ControllerRegistry();
|
146
|
+
let MyController = class MyController {
|
147
|
+
myAction() { }
|
148
|
+
};
|
149
|
+
__decorate([
|
150
|
+
get('/myAction'),
|
151
|
+
__metadata("design:type", Function),
|
152
|
+
__metadata("design:paramtypes", []),
|
153
|
+
__metadata("design:returntype", void 0)
|
154
|
+
], MyController.prototype, "myAction", null);
|
155
|
+
MyController = __decorate([
|
156
|
+
controller({ before: PRE_HANDLER_1 })
|
157
|
+
], MyController);
|
158
|
+
s.addController(MyController);
|
159
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
160
|
+
const req = createRequestMock({
|
161
|
+
method: HttpMethod.GET,
|
162
|
+
path: '/myAction',
|
163
|
+
});
|
164
|
+
const matching = routeReg.matchRouteByRequest(req);
|
165
|
+
expect(matching).to.be.not.empty;
|
166
|
+
const res = matching.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
167
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
168
|
+
});
|
169
|
+
it('passes action pre-handler of a given controller to the route', function () {
|
170
|
+
const s = new ControllerRegistry();
|
171
|
+
let MyController = class MyController {
|
172
|
+
myAction() { }
|
173
|
+
};
|
174
|
+
__decorate([
|
175
|
+
get('/myAction', { before: PRE_HANDLER_1 }),
|
176
|
+
__metadata("design:type", Function),
|
177
|
+
__metadata("design:paramtypes", []),
|
178
|
+
__metadata("design:returntype", void 0)
|
179
|
+
], MyController.prototype, "myAction", null);
|
180
|
+
MyController = __decorate([
|
181
|
+
controller()
|
182
|
+
], MyController);
|
183
|
+
s.addController(MyController);
|
184
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
185
|
+
const req = createRequestMock({
|
186
|
+
method: HttpMethod.GET,
|
187
|
+
path: '/myAction',
|
188
|
+
});
|
189
|
+
const matching = routeReg.matchRouteByRequest(req);
|
190
|
+
expect(matching).to.be.not.empty;
|
191
|
+
const res = matching.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
192
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
193
|
+
});
|
194
|
+
});
|
195
|
+
describe('multiple pre-handlers', function () {
|
196
|
+
it('passes pre-handlers of a given options to the route', function () {
|
197
|
+
const s = new ControllerRegistry();
|
198
|
+
let MyController = class MyController {
|
199
|
+
myAction() { }
|
200
|
+
};
|
201
|
+
__decorate([
|
202
|
+
get('/myAction'),
|
203
|
+
__metadata("design:type", Function),
|
204
|
+
__metadata("design:paramtypes", []),
|
205
|
+
__metadata("design:returntype", void 0)
|
206
|
+
], MyController.prototype, "myAction", null);
|
207
|
+
MyController = __decorate([
|
208
|
+
controller()
|
209
|
+
], MyController);
|
210
|
+
s.addController(MyController, {
|
211
|
+
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
212
|
+
});
|
213
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
214
|
+
const req = createRequestMock({
|
215
|
+
method: HttpMethod.GET,
|
216
|
+
path: '/myAction',
|
217
|
+
});
|
218
|
+
const matching = routeReg.matchRouteByRequest(req);
|
219
|
+
expect(matching).to.be.not.empty;
|
220
|
+
const res = matching.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
221
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
222
|
+
});
|
223
|
+
it('passes pre-handlers of a given controller to the route', function () {
|
224
|
+
const s = new ControllerRegistry();
|
225
|
+
let MyController = class MyController {
|
226
|
+
myAction() { }
|
227
|
+
};
|
228
|
+
__decorate([
|
229
|
+
get('/myAction'),
|
230
|
+
__metadata("design:type", Function),
|
231
|
+
__metadata("design:paramtypes", []),
|
232
|
+
__metadata("design:returntype", void 0)
|
233
|
+
], MyController.prototype, "myAction", null);
|
234
|
+
MyController = __decorate([
|
235
|
+
controller({
|
236
|
+
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
237
|
+
})
|
238
|
+
], MyController);
|
239
|
+
s.addController(MyController);
|
240
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
241
|
+
const req = createRequestMock({
|
242
|
+
method: HttpMethod.GET,
|
243
|
+
path: '/myAction',
|
244
|
+
});
|
245
|
+
const matching = routeReg.matchRouteByRequest(req);
|
246
|
+
expect(matching).to.be.not.empty;
|
247
|
+
const res = matching.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
248
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
249
|
+
});
|
250
|
+
it('passes action pre-handlers of a given controller to the route', function () {
|
251
|
+
const s = new ControllerRegistry();
|
252
|
+
let MyController = class MyController {
|
253
|
+
myAction() { }
|
254
|
+
};
|
255
|
+
__decorate([
|
256
|
+
get('/myAction', {
|
257
|
+
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
258
|
+
}),
|
259
|
+
__metadata("design:type", Function),
|
260
|
+
__metadata("design:paramtypes", []),
|
261
|
+
__metadata("design:returntype", void 0)
|
262
|
+
], MyController.prototype, "myAction", null);
|
263
|
+
MyController = __decorate([
|
264
|
+
controller()
|
265
|
+
], MyController);
|
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(HookName.PRE_HANDLER);
|
275
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
276
|
+
});
|
277
|
+
});
|
278
|
+
describe('single post-handler', function () {
|
279
|
+
it('passes post-handler of a given options to the route', function () {
|
280
|
+
const s = new ControllerRegistry();
|
281
|
+
let MyController = class MyController {
|
282
|
+
myAction() { }
|
283
|
+
};
|
284
|
+
__decorate([
|
285
|
+
get('/myAction'),
|
286
|
+
__metadata("design:type", Function),
|
287
|
+
__metadata("design:paramtypes", []),
|
288
|
+
__metadata("design:returntype", void 0)
|
289
|
+
], MyController.prototype, "myAction", null);
|
290
|
+
MyController = __decorate([
|
291
|
+
controller()
|
292
|
+
], MyController);
|
293
|
+
s.addController(MyController, { after: POST_HANDLER_1 });
|
294
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
295
|
+
const req = createRequestMock({
|
296
|
+
method: HttpMethod.GET,
|
297
|
+
path: '/myAction',
|
298
|
+
});
|
299
|
+
const matching = routeReg.matchRouteByRequest(req);
|
300
|
+
expect(matching).to.be.not.empty;
|
301
|
+
const res = matching.route.hookRegistry.getHooks(HookName.POST_HANDLER);
|
302
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
303
|
+
});
|
304
|
+
it('passes post-handler of a given controller to the route', function () {
|
305
|
+
const s = new ControllerRegistry();
|
306
|
+
let MyController = class MyController {
|
307
|
+
myAction() { }
|
308
|
+
};
|
309
|
+
__decorate([
|
310
|
+
get('/myAction'),
|
311
|
+
__metadata("design:type", Function),
|
312
|
+
__metadata("design:paramtypes", []),
|
313
|
+
__metadata("design:returntype", void 0)
|
314
|
+
], MyController.prototype, "myAction", null);
|
315
|
+
MyController = __decorate([
|
316
|
+
controller({ after: POST_HANDLER_1 })
|
317
|
+
], MyController);
|
318
|
+
s.addController(MyController);
|
319
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
320
|
+
const req = createRequestMock({
|
321
|
+
method: HttpMethod.GET,
|
322
|
+
path: '/myAction',
|
323
|
+
});
|
324
|
+
const matching = routeReg.matchRouteByRequest(req);
|
325
|
+
expect(matching).to.be.not.empty;
|
326
|
+
const res = matching.route.hookRegistry.getHooks(HookName.POST_HANDLER);
|
327
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
328
|
+
});
|
329
|
+
it('passes action post-handler of a given controller to the route', function () {
|
330
|
+
const s = new ControllerRegistry();
|
331
|
+
let MyController = class MyController {
|
332
|
+
myAction() { }
|
333
|
+
};
|
334
|
+
__decorate([
|
335
|
+
get('/myAction', { after: POST_HANDLER_1 }),
|
336
|
+
__metadata("design:type", Function),
|
337
|
+
__metadata("design:paramtypes", []),
|
338
|
+
__metadata("design:returntype", void 0)
|
339
|
+
], MyController.prototype, "myAction", null);
|
340
|
+
MyController = __decorate([
|
341
|
+
controller()
|
342
|
+
], MyController);
|
343
|
+
s.addController(MyController);
|
344
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
345
|
+
const req = createRequestMock({
|
346
|
+
method: HttpMethod.GET,
|
347
|
+
path: '/myAction',
|
348
|
+
});
|
349
|
+
const matching = routeReg.matchRouteByRequest(req);
|
350
|
+
expect(matching).to.be.not.empty;
|
351
|
+
const res = matching.route.hookRegistry.getHooks(HookName.POST_HANDLER);
|
352
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
353
|
+
});
|
354
|
+
});
|
355
|
+
describe('multiple post-handlers', function () {
|
356
|
+
it('passes post-handlers of a given options to the route', function () {
|
357
|
+
const s = new ControllerRegistry();
|
358
|
+
let MyController = class MyController {
|
359
|
+
myAction() { }
|
360
|
+
};
|
361
|
+
__decorate([
|
362
|
+
get('/myAction'),
|
363
|
+
__metadata("design:type", Function),
|
364
|
+
__metadata("design:paramtypes", []),
|
365
|
+
__metadata("design:returntype", void 0)
|
366
|
+
], MyController.prototype, "myAction", null);
|
367
|
+
MyController = __decorate([
|
368
|
+
controller()
|
369
|
+
], MyController);
|
370
|
+
s.addController(MyController, {
|
371
|
+
after: [POST_HANDLER_1, POST_HANDLER_2],
|
372
|
+
});
|
373
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
374
|
+
const req = createRequestMock({
|
375
|
+
method: HttpMethod.GET,
|
376
|
+
path: '/myAction',
|
377
|
+
});
|
378
|
+
const matching = routeReg.matchRouteByRequest(req);
|
379
|
+
expect(matching).to.be.not.empty;
|
380
|
+
const res = matching.route.hookRegistry.getHooks(HookName.POST_HANDLER);
|
381
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
382
|
+
});
|
383
|
+
it('passes post-handlers of a given controller to the route', function () {
|
384
|
+
const s = new ControllerRegistry();
|
385
|
+
let MyController = class MyController {
|
386
|
+
myAction() { }
|
387
|
+
};
|
388
|
+
__decorate([
|
389
|
+
get('/myAction'),
|
390
|
+
__metadata("design:type", Function),
|
391
|
+
__metadata("design:paramtypes", []),
|
392
|
+
__metadata("design:returntype", void 0)
|
393
|
+
], MyController.prototype, "myAction", null);
|
394
|
+
MyController = __decorate([
|
395
|
+
controller({
|
396
|
+
after: [POST_HANDLER_1, POST_HANDLER_2],
|
397
|
+
})
|
398
|
+
], MyController);
|
399
|
+
s.addController(MyController);
|
400
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
401
|
+
const req = createRequestMock({
|
402
|
+
method: HttpMethod.GET,
|
403
|
+
path: '/myAction',
|
404
|
+
});
|
405
|
+
const matching = routeReg.matchRouteByRequest(req);
|
406
|
+
expect(matching).to.be.not.empty;
|
407
|
+
const res = matching.route.hookRegistry.getHooks(HookName.POST_HANDLER);
|
408
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
409
|
+
});
|
410
|
+
it('passes action post-handlers of a given controller to the route', function () {
|
411
|
+
const s = new ControllerRegistry();
|
412
|
+
let MyController = class MyController {
|
413
|
+
myAction() { }
|
414
|
+
};
|
415
|
+
__decorate([
|
416
|
+
get('/myAction', {
|
417
|
+
after: [POST_HANDLER_1, POST_HANDLER_2],
|
418
|
+
}),
|
419
|
+
__metadata("design:type", Function),
|
420
|
+
__metadata("design:paramtypes", []),
|
421
|
+
__metadata("design:returntype", void 0)
|
422
|
+
], MyController.prototype, "myAction", null);
|
423
|
+
MyController = __decorate([
|
424
|
+
controller()
|
425
|
+
], MyController);
|
426
|
+
s.addController(MyController);
|
427
|
+
const routeReg = s.getService(TrieRouter).getService(RouteRegistry);
|
428
|
+
const req = createRequestMock({
|
429
|
+
method: HttpMethod.GET,
|
430
|
+
path: '/myAction',
|
431
|
+
});
|
432
|
+
const matching = routeReg.matchRouteByRequest(req);
|
433
|
+
expect(matching).to.be.not.empty;
|
434
|
+
const res = matching.route.hookRegistry.getHooks(HookName.POST_HANDLER);
|
435
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
436
|
+
});
|
437
|
+
});
|
438
|
+
});
|
439
|
+
describe('hasController', function () {
|
440
|
+
it('returns false if a given controller is not registered', function () {
|
441
|
+
const s = new ControllerRegistry();
|
442
|
+
let MyController = class MyController {
|
443
|
+
};
|
444
|
+
MyController = __decorate([
|
445
|
+
controller()
|
446
|
+
], MyController);
|
447
|
+
expect(s.hasController(MyController)).to.be.false;
|
448
|
+
});
|
449
|
+
it('returns true if a given controller is registered', function () {
|
450
|
+
const s = new ControllerRegistry();
|
451
|
+
let MyController = class MyController {
|
452
|
+
};
|
453
|
+
MyController = __decorate([
|
454
|
+
controller()
|
455
|
+
], MyController);
|
456
|
+
expect(s.hasController(MyController)).to.be.false;
|
457
|
+
s.addController(MyController);
|
458
|
+
expect(s.hasController(MyController)).to.be.true;
|
459
|
+
});
|
460
|
+
});
|
461
|
+
describe('getPathPrefixByControllerMetadata', function () {
|
462
|
+
it('returns an empty string if no prefix is specified', function () {
|
463
|
+
const s = new ControllerRegistry();
|
464
|
+
let MyController = class MyController {
|
465
|
+
};
|
466
|
+
MyController = __decorate([
|
467
|
+
controller()
|
468
|
+
], MyController);
|
469
|
+
const md = ControllerReflector.getMetadata(MyController);
|
470
|
+
const res = s.getPathPrefixByControllerMetadata(md);
|
471
|
+
expect(res).to.be.eq('');
|
472
|
+
});
|
473
|
+
it('returns controller path prefix that starts with slash', function () {
|
474
|
+
const s = new ControllerRegistry();
|
475
|
+
let MyController = class MyController {
|
476
|
+
};
|
477
|
+
MyController = __decorate([
|
478
|
+
controller({ path: 'myPrefix' })
|
479
|
+
], MyController);
|
480
|
+
const md = ControllerReflector.getMetadata(MyController);
|
481
|
+
const res = s.getPathPrefixByControllerMetadata(md);
|
482
|
+
expect(res).to.be.eq('/myPrefix');
|
483
|
+
});
|
484
|
+
it('returns path prefix from options that starts with slash', function () {
|
485
|
+
const s = new ControllerRegistry();
|
486
|
+
let MyController = class MyController {
|
487
|
+
};
|
488
|
+
MyController = __decorate([
|
489
|
+
controller()
|
490
|
+
], MyController);
|
491
|
+
const md = ControllerReflector.getMetadata(MyController);
|
492
|
+
const res = s.getPathPrefixByControllerMetadata(md, {
|
493
|
+
pathPrefix: 'myPrefix',
|
494
|
+
});
|
495
|
+
expect(res).to.be.eq('/myPrefix');
|
496
|
+
});
|
497
|
+
it('combines a path prefix from options and controller', function () {
|
498
|
+
const s = new ControllerRegistry();
|
499
|
+
let MyController = class MyController {
|
500
|
+
};
|
501
|
+
MyController = __decorate([
|
502
|
+
controller({ path: 'controller' })
|
503
|
+
], MyController);
|
504
|
+
const md = ControllerReflector.getMetadata(MyController);
|
505
|
+
const res = s.getPathPrefixByControllerMetadata(md, {
|
506
|
+
pathPrefix: 'root',
|
507
|
+
});
|
508
|
+
expect(res).to.be.eq('/root/controller');
|
509
|
+
});
|
510
|
+
});
|
511
|
+
describe('getPreHandlersByControllerMetadata', function () {
|
512
|
+
it('returns an empty array if no handlers are specified', function () {
|
513
|
+
const s = new ControllerRegistry();
|
514
|
+
let MyController = class MyController {
|
515
|
+
};
|
516
|
+
MyController = __decorate([
|
517
|
+
controller()
|
518
|
+
], MyController);
|
519
|
+
const md = ControllerReflector.getMetadata(MyController);
|
520
|
+
const res = s.getPreHandlersByControllerMetadata(md);
|
521
|
+
expect(res).to.be.eql([]);
|
522
|
+
});
|
523
|
+
describe('single handler', function () {
|
524
|
+
it('returns pre-handlers from a given controller', function () {
|
525
|
+
const s = new ControllerRegistry();
|
526
|
+
let MyController = class MyController {
|
527
|
+
};
|
528
|
+
MyController = __decorate([
|
529
|
+
controller({
|
530
|
+
before: PRE_HANDLER_1,
|
531
|
+
})
|
532
|
+
], MyController);
|
533
|
+
const md = ControllerReflector.getMetadata(MyController);
|
534
|
+
const res = s.getPreHandlersByControllerMetadata(md);
|
535
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
536
|
+
});
|
537
|
+
it('returns pre-handlers from a given options', function () {
|
538
|
+
const s = new ControllerRegistry();
|
539
|
+
let MyController = class MyController {
|
540
|
+
};
|
541
|
+
MyController = __decorate([
|
542
|
+
controller()
|
543
|
+
], MyController);
|
544
|
+
const md = ControllerReflector.getMetadata(MyController);
|
545
|
+
const res = s.getPreHandlersByControllerMetadata(md, {
|
546
|
+
before: PRE_HANDLER_1,
|
547
|
+
});
|
548
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
549
|
+
});
|
550
|
+
it('combines pre-handlers from a given options and controller', function () {
|
551
|
+
const s = new ControllerRegistry();
|
552
|
+
let MyController = class MyController {
|
553
|
+
};
|
554
|
+
MyController = __decorate([
|
555
|
+
controller({
|
556
|
+
before: PRE_HANDLER_2,
|
557
|
+
})
|
558
|
+
], MyController);
|
559
|
+
const md = ControllerReflector.getMetadata(MyController);
|
560
|
+
const res = s.getPreHandlersByControllerMetadata(md, {
|
561
|
+
before: PRE_HANDLER_1,
|
562
|
+
});
|
563
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
564
|
+
});
|
565
|
+
});
|
566
|
+
describe('multiple handlers', function () {
|
567
|
+
it('returns pre-handlers from a given controller', function () {
|
568
|
+
const s = new ControllerRegistry();
|
569
|
+
let MyController = class MyController {
|
570
|
+
};
|
571
|
+
MyController = __decorate([
|
572
|
+
controller({
|
573
|
+
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
574
|
+
})
|
575
|
+
], MyController);
|
576
|
+
const md = ControllerReflector.getMetadata(MyController);
|
577
|
+
const res = s.getPreHandlersByControllerMetadata(md);
|
578
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
579
|
+
});
|
580
|
+
it('returns pre-handlers from a given options', function () {
|
581
|
+
const s = new ControllerRegistry();
|
582
|
+
let MyController = class MyController {
|
583
|
+
};
|
584
|
+
MyController = __decorate([
|
585
|
+
controller()
|
586
|
+
], MyController);
|
587
|
+
const md = ControllerReflector.getMetadata(MyController);
|
588
|
+
const res = s.getPreHandlersByControllerMetadata(md, {
|
589
|
+
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
590
|
+
});
|
591
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
592
|
+
});
|
593
|
+
it('combines pre-handlers from a given options and controller', function () {
|
594
|
+
const s = new ControllerRegistry();
|
595
|
+
let MyController = class MyController {
|
596
|
+
};
|
597
|
+
MyController = __decorate([
|
598
|
+
controller({
|
599
|
+
before: [PRE_HANDLER_3, PRE_HANDLER_4],
|
600
|
+
})
|
601
|
+
], MyController);
|
602
|
+
const md = ControllerReflector.getMetadata(MyController);
|
603
|
+
const res = s.getPreHandlersByControllerMetadata(md, {
|
604
|
+
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
605
|
+
});
|
606
|
+
expect(res).to.be.eql([
|
607
|
+
PRE_HANDLER_1,
|
608
|
+
PRE_HANDLER_2,
|
609
|
+
PRE_HANDLER_3,
|
610
|
+
PRE_HANDLER_4,
|
611
|
+
]);
|
612
|
+
});
|
613
|
+
});
|
614
|
+
});
|
615
|
+
describe('getPostHandlersByControllerMetadata', function () {
|
616
|
+
it('returns an empty array if no handlers are specified', function () {
|
617
|
+
const s = new ControllerRegistry();
|
618
|
+
let MyController = class MyController {
|
619
|
+
};
|
620
|
+
MyController = __decorate([
|
621
|
+
controller()
|
622
|
+
], MyController);
|
623
|
+
const md = ControllerReflector.getMetadata(MyController);
|
624
|
+
const res = s.getPostHandlersByControllerMetadata(md);
|
625
|
+
expect(res).to.be.eql([]);
|
626
|
+
});
|
627
|
+
describe('single handler', function () {
|
628
|
+
it('returns pre-handlers from a given controller', function () {
|
629
|
+
const s = new ControllerRegistry();
|
630
|
+
let MyController = class MyController {
|
631
|
+
};
|
632
|
+
MyController = __decorate([
|
633
|
+
controller({
|
634
|
+
after: POST_HANDLER_1,
|
635
|
+
})
|
636
|
+
], MyController);
|
637
|
+
const md = ControllerReflector.getMetadata(MyController);
|
638
|
+
const res = s.getPostHandlersByControllerMetadata(md);
|
639
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
640
|
+
});
|
641
|
+
it('returns pre-handlers from a given options', function () {
|
642
|
+
const s = new ControllerRegistry();
|
643
|
+
let MyController = class MyController {
|
644
|
+
};
|
645
|
+
MyController = __decorate([
|
646
|
+
controller()
|
647
|
+
], MyController);
|
648
|
+
const md = ControllerReflector.getMetadata(MyController);
|
649
|
+
const res = s.getPostHandlersByControllerMetadata(md, {
|
650
|
+
after: POST_HANDLER_1,
|
651
|
+
});
|
652
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
653
|
+
});
|
654
|
+
it('combines pre-handlers from a given options and controller', function () {
|
655
|
+
const s = new ControllerRegistry();
|
656
|
+
let MyController = class MyController {
|
657
|
+
};
|
658
|
+
MyController = __decorate([
|
659
|
+
controller({
|
660
|
+
after: POST_HANDLER_2,
|
661
|
+
})
|
662
|
+
], MyController);
|
663
|
+
const md = ControllerReflector.getMetadata(MyController);
|
664
|
+
const res = s.getPostHandlersByControllerMetadata(md, {
|
665
|
+
after: POST_HANDLER_1,
|
666
|
+
});
|
667
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
668
|
+
});
|
669
|
+
});
|
670
|
+
describe('multiple handlers', function () {
|
671
|
+
it('returns pre-handlers from a given controller', function () {
|
672
|
+
const s = new ControllerRegistry();
|
673
|
+
let MyController = class MyController {
|
674
|
+
};
|
675
|
+
MyController = __decorate([
|
676
|
+
controller({
|
677
|
+
after: [POST_HANDLER_1, POST_HANDLER_2],
|
678
|
+
})
|
679
|
+
], MyController);
|
680
|
+
const md = ControllerReflector.getMetadata(MyController);
|
681
|
+
const res = s.getPostHandlersByControllerMetadata(md);
|
682
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
683
|
+
});
|
684
|
+
it('returns pre-handlers from a given options', function () {
|
685
|
+
const s = new ControllerRegistry();
|
686
|
+
let MyController = class MyController {
|
687
|
+
};
|
688
|
+
MyController = __decorate([
|
689
|
+
controller()
|
690
|
+
], MyController);
|
691
|
+
const md = ControllerReflector.getMetadata(MyController);
|
692
|
+
const res = s.getPostHandlersByControllerMetadata(md, {
|
693
|
+
after: [POST_HANDLER_1, POST_HANDLER_2],
|
694
|
+
});
|
695
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
696
|
+
});
|
697
|
+
it('combines pre-handlers from a given options and controller', function () {
|
698
|
+
const s = new ControllerRegistry();
|
699
|
+
let MyController = class MyController {
|
700
|
+
};
|
701
|
+
MyController = __decorate([
|
702
|
+
controller({
|
703
|
+
after: [POST_HANDLER_3, POST_HANDLER_4],
|
704
|
+
})
|
705
|
+
], MyController);
|
706
|
+
const md = ControllerReflector.getMetadata(MyController);
|
707
|
+
const res = s.getPostHandlersByControllerMetadata(md, {
|
708
|
+
after: [POST_HANDLER_1, POST_HANDLER_2],
|
709
|
+
});
|
710
|
+
expect(res).to.be.eql([
|
711
|
+
POST_HANDLER_1,
|
712
|
+
POST_HANDLER_2,
|
713
|
+
POST_HANDLER_3,
|
714
|
+
POST_HANDLER_4,
|
715
|
+
]);
|
716
|
+
});
|
717
|
+
});
|
718
|
+
});
|
719
|
+
});
|