@e22m4u/ts-rest-router 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +171 -74
- package/dist/cjs/index.cjs +352 -362
- package/dist/esm/controller-registry.js +37 -32
- package/dist/esm/debuggable-service.d.ts +10 -1
- package/dist/esm/debuggable-service.js +14 -3
- package/dist/esm/decorators/after-action/after-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{before/before-decorator.js → after-action/after-action-decorator.js} +7 -7
- package/dist/esm/decorators/{before/before-decorator.spec.js → after-action/after-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{after/after-metadata.d.ts → after-action/after-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/after-action/after-action-metadata.js +5 -0
- package/dist/esm/decorators/{after/after-reflector.d.ts → after-action/after-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{after/after-reflector.js → after-action/after-action-reflector.js} +6 -6
- package/dist/esm/decorators/{before/before-reflector.spec.js → after-action/after-action-reflector.spec.js} +23 -24
- package/dist/esm/decorators/after-action/index.d.ts +3 -0
- package/dist/esm/decorators/after-action/index.js +3 -0
- package/dist/esm/decorators/before-action/before-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{after/after-decorator.js → before-action/before-action-decorator.js} +7 -7
- package/dist/esm/decorators/{after/after-decorator.spec.js → before-action/before-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{before/before-metadata.d.ts → before-action/before-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/before-action/before-action-metadata.js +5 -0
- package/dist/esm/decorators/{before/before-reflector.d.ts → before-action/before-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{before/before-reflector.js → before-action/before-action-reflector.js} +6 -6
- package/dist/esm/decorators/{after/after-reflector.spec.js → before-action/before-action-reflector.spec.js} +23 -24
- package/dist/esm/decorators/before-action/index.d.ts +3 -0
- package/dist/esm/decorators/before-action/index.js +3 -0
- package/dist/esm/decorators/index.d.ts +4 -4
- package/dist/esm/decorators/index.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +6 -0
- package/dist/esm/decorators/request-context/request-context-reflector.spec.js +0 -1
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.js +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +44 -32
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +1 -1
- package/dist/esm/decorators/request-data/request-data-reflector.spec.js +0 -1
- package/dist/esm/decorators/rest-action/index.d.ts +3 -0
- package/dist/esm/decorators/rest-action/index.js +3 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.d.ts +57 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.js +52 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.spec.js +401 -0
- package/dist/esm/decorators/{action/action-metadata.d.ts → rest-action/rest-action-metadata.d.ts} +6 -6
- package/dist/esm/decorators/rest-action/rest-action-metadata.js +5 -0
- package/dist/esm/decorators/rest-action/rest-action-reflector.d.ts +22 -0
- package/dist/esm/decorators/{action/action-reflector.js → rest-action/rest-action-reflector.js} +6 -6
- package/dist/esm/decorators/{action/action-reflector.spec.js → rest-action/rest-action-reflector.spec.js} +13 -14
- package/dist/esm/decorators/rest-controller/index.d.ts +3 -0
- package/dist/esm/decorators/rest-controller/index.js +3 -0
- package/dist/esm/decorators/rest-controller/rest-controller-decorator.d.ts +14 -0
- package/dist/esm/decorators/{controller/controller-decorator.js → rest-controller/rest-controller-decorator.js} +5 -5
- package/dist/esm/decorators/{controller/controller-decorator.spec.js → rest-controller/rest-controller-decorator.spec.js} +15 -15
- package/dist/esm/decorators/{controller/controller-metadata.d.ts → rest-controller/rest-controller-metadata.d.ts} +5 -5
- package/dist/esm/decorators/rest-controller/rest-controller-metadata.js +5 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.d.ts +20 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.js +24 -0
- package/dist/esm/decorators/{controller/controller-reflector.spec.js → rest-controller/rest-controller-reflector.spec.js} +12 -13
- package/dist/esm/errors/not-a-controller-error.js +1 -1
- package/dist/esm/types.d.ts +0 -10
- package/dist/esm/utils/index.d.ts +0 -1
- package/dist/esm/utils/index.js +0 -1
- package/eslint.config.js +1 -1
- package/package.json +15 -15
- package/src/controller-registry.spec.ts +192 -134
- package/src/controller-registry.ts +46 -39
- package/src/debuggable-service.spec.ts +0 -1
- package/src/debuggable-service.ts +17 -4
- package/src/decorators/{before/before-decorator.spec.ts → after-action/after-action-decorator.spec.ts} +19 -19
- package/src/decorators/{before/before-decorator.ts → after-action/after-action-decorator.ts} +9 -9
- package/src/decorators/{after/after-metadata.ts → after-action/after-action-metadata.ts} +5 -5
- package/src/decorators/{before/before-reflector.spec.ts → after-action/after-action-reflector.spec.ts} +33 -24
- package/src/decorators/{before/before-reflector.ts → after-action/after-action-reflector.ts} +13 -9
- package/src/decorators/after-action/index.ts +3 -0
- package/src/decorators/{after/after-decorator.spec.ts → before-action/before-action-decorator.spec.ts} +19 -19
- package/src/decorators/{after/after-decorator.ts → before-action/before-action-decorator.ts} +9 -9
- package/src/decorators/before-action/before-action-metadata.ts +17 -0
- package/src/decorators/{after/after-reflector.spec.ts → before-action/before-action-reflector.spec.ts} +40 -24
- package/src/decorators/{after/after-reflector.ts → before-action/before-action-reflector.ts} +18 -9
- package/src/decorators/before-action/index.ts +3 -0
- package/src/decorators/index.ts +4 -4
- package/src/decorators/request-context/request-context-decorator.spec.ts +7 -0
- package/src/decorators/request-context/request-context-decorator.ts +4 -4
- package/src/decorators/request-context/request-context-reflector.spec.ts +0 -1
- package/src/decorators/request-data/request-data-decorator.spec.ts +45 -32
- package/src/decorators/request-data/request-data-decorator.ts +10 -10
- package/src/decorators/request-data/request-data-metadata.ts +1 -1
- package/src/decorators/request-data/request-data-reflector.spec.ts +0 -1
- package/src/decorators/rest-action/index.ts +3 -0
- package/src/decorators/rest-action/rest-action-decorator.spec.ts +325 -0
- package/src/decorators/rest-action/rest-action-decorator.ts +166 -0
- package/src/decorators/{action/action-metadata.ts → rest-action/rest-action-metadata.ts} +7 -7
- package/src/decorators/{action/action-reflector.spec.ts → rest-action/rest-action-reflector.spec.ts} +13 -14
- package/src/decorators/rest-action/rest-action-reflector.ts +41 -0
- package/src/decorators/rest-controller/index.ts +3 -0
- package/src/decorators/{controller/controller-decorator.spec.ts → rest-controller/rest-controller-decorator.spec.ts} +16 -16
- package/src/decorators/{controller/controller-decorator.ts → rest-controller/rest-controller-decorator.ts} +14 -10
- package/src/decorators/{controller/controller-metadata.ts → rest-controller/rest-controller-metadata.ts} +6 -7
- package/src/decorators/{controller/controller-reflector.spec.ts → rest-controller/rest-controller-reflector.spec.ts} +21 -13
- package/src/decorators/rest-controller/rest-controller-reflector.ts +32 -0
- package/src/errors/not-a-controller-error.ts +4 -1
- package/src/types.ts +0 -10
- package/src/utils/index.ts +0 -1
- package/dist/esm/decorators/action/action-decorator.d.ts +0 -52
- package/dist/esm/decorators/action/action-decorator.js +0 -62
- package/dist/esm/decorators/action/action-decorator.spec.js +0 -59
- package/dist/esm/decorators/action/action-metadata.js +0 -5
- package/dist/esm/decorators/action/action-reflector.d.ts +0 -22
- package/dist/esm/decorators/action/index.d.ts +0 -3
- package/dist/esm/decorators/action/index.js +0 -3
- package/dist/esm/decorators/after/after-decorator.d.ts +0 -9
- package/dist/esm/decorators/after/after-metadata.js +0 -5
- package/dist/esm/decorators/after/index.d.ts +0 -3
- package/dist/esm/decorators/after/index.js +0 -3
- package/dist/esm/decorators/before/before-decorator.d.ts +0 -9
- package/dist/esm/decorators/before/before-metadata.js +0 -5
- package/dist/esm/decorators/before/index.d.ts +0 -3
- package/dist/esm/decorators/before/index.js +0 -3
- package/dist/esm/decorators/controller/controller-decorator.d.ts +0 -14
- package/dist/esm/decorators/controller/controller-metadata.js +0 -5
- package/dist/esm/decorators/controller/controller-reflector.d.ts +0 -20
- package/dist/esm/decorators/controller/controller-reflector.js +0 -24
- package/dist/esm/decorators/controller/index.d.ts +0 -3
- package/dist/esm/decorators/controller/index.js +0 -3
- package/dist/esm/utils/create-debugger.d.ts +0 -44
- package/dist/esm/utils/create-debugger.js +0 -81
- package/dist/esm/utils/create-debugger.spec.d.ts +0 -1
- package/dist/esm/utils/create-debugger.spec.js +0 -8
- package/src/decorators/action/action-decorator.spec.ts +0 -42
- package/src/decorators/action/action-decorator.ts +0 -98
- package/src/decorators/action/action-reflector.ts +0 -38
- package/src/decorators/action/index.ts +0 -3
- package/src/decorators/after/index.ts +0 -3
- package/src/decorators/before/before-metadata.ts +0 -17
- package/src/decorators/before/index.ts +0 -3
- package/src/decorators/controller/controller-reflector.ts +0 -28
- package/src/decorators/controller/index.ts +0 -3
- package/src/utils/create-debugger.spec.ts +0 -9
- package/src/utils/create-debugger.ts +0 -98
- /package/dist/esm/decorators/{action/action-decorator.spec.d.ts → after-action/after-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{action/action-reflector.spec.d.ts → after-action/after-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-decorator.spec.d.ts → before-action/before-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-reflector.spec.d.ts → before-action/before-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-decorator.spec.d.ts → rest-action/rest-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-reflector.spec.d.ts → rest-action/rest-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-decorator.spec.d.ts → rest-controller/rest-controller-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-reflector.spec.d.ts → rest-controller/rest-controller-reflector.spec.d.ts} +0 -0
@@ -1,32 +1,38 @@
|
|
1
1
|
/* eslint mocha/no-sibling-hooks: 0 */
|
2
|
+
import {
|
3
|
+
createRequestMock,
|
4
|
+
createResponseMock,
|
5
|
+
HookName,
|
6
|
+
HttpMethod,
|
7
|
+
ParsedCookie,
|
8
|
+
ParsedHeaders,
|
9
|
+
ParsedParams,
|
10
|
+
ParsedQuery,
|
11
|
+
RequestContext,
|
12
|
+
RequestParser,
|
13
|
+
RouteRegistry,
|
14
|
+
TrieRouter,
|
15
|
+
} from '@e22m4u/js-trie-router';
|
16
|
+
import {
|
17
|
+
afterAction,
|
18
|
+
beforeAction,
|
19
|
+
getAction,
|
20
|
+
postAction,
|
21
|
+
requestBody,
|
22
|
+
requestCookie,
|
23
|
+
requestCookies,
|
24
|
+
requestField,
|
25
|
+
requestHeader,
|
26
|
+
requestHeaders,
|
27
|
+
requestParam,
|
28
|
+
requestParams,
|
29
|
+
requestQueries,
|
30
|
+
requestQuery,
|
31
|
+
restController,
|
32
|
+
} from './decorators/index.js';
|
33
|
+
|
2
34
|
import {expect} from 'chai';
|
3
|
-
import {
|
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 {field} from './decorators/index.js';
|
10
|
-
import {cookie} from './decorators/index.js';
|
11
|
-
import {params} from './decorators/index.js';
|
12
|
-
import {before} from './decorators/index.js';
|
13
|
-
import {header} from './decorators/index.js';
|
14
|
-
import {cookies} from './decorators/index.js';
|
15
|
-
import {queries} from './decorators/index.js';
|
16
|
-
import {headers} from './decorators/index.js';
|
17
|
-
import {HookName} from '@e22m4u/js-trie-router';
|
18
|
-
import {controller} from './decorators/index.js';
|
19
|
-
import {TrieRouter} from '@e22m4u/js-trie-router';
|
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';
|
25
|
-
import {RouteRegistry} from '@e22m4u/js-trie-router';
|
26
|
-
import {RequestParser} from '@e22m4u/js-trie-router';
|
27
|
-
import {RequestContext} from '@e22m4u/js-trie-router';
|
28
|
-
import {createRequestMock} from '@e22m4u/js-trie-router';
|
29
|
-
import {createResponseMock} from '@e22m4u/js-trie-router';
|
35
|
+
import {DataType} from '@e22m4u/ts-data-schema';
|
30
36
|
import {ControllerRegistry} from './controller-registry.js';
|
31
37
|
|
32
38
|
const PRE_HANDLER_1 = () => undefined;
|
@@ -45,7 +51,7 @@ describe('ControllerRegistry', function () {
|
|
45
51
|
describe('addController', function () {
|
46
52
|
it('returns itself', function () {
|
47
53
|
const S = new ControllerRegistry();
|
48
|
-
@
|
54
|
+
@restController()
|
49
55
|
class MyController {}
|
50
56
|
const res = S.addController(MyController);
|
51
57
|
expect(res).to.be.eq(S);
|
@@ -53,7 +59,7 @@ describe('ControllerRegistry', function () {
|
|
53
59
|
|
54
60
|
it('adds a given controller to controllers set', function () {
|
55
61
|
const S = new ControllerRegistry();
|
56
|
-
@
|
62
|
+
@restController()
|
57
63
|
class MyController {}
|
58
64
|
expect(S.hasController(MyController)).to.be.false;
|
59
65
|
S.addController(MyController);
|
@@ -62,9 +68,9 @@ describe('ControllerRegistry', function () {
|
|
62
68
|
|
63
69
|
it('uses http method and action path for a new route', function () {
|
64
70
|
const S = new ControllerRegistry();
|
65
|
-
@
|
71
|
+
@restController()
|
66
72
|
class MyController {
|
67
|
-
@
|
73
|
+
@getAction('/myAction')
|
68
74
|
foo() {}
|
69
75
|
}
|
70
76
|
S.addController(MyController);
|
@@ -81,11 +87,11 @@ describe('ControllerRegistry', function () {
|
|
81
87
|
|
82
88
|
it('adds multiple routes by the given controller', function () {
|
83
89
|
const S = new ControllerRegistry();
|
84
|
-
@
|
90
|
+
@restController()
|
85
91
|
class MyController {
|
86
|
-
@
|
92
|
+
@getAction('/foo')
|
87
93
|
foo() {}
|
88
|
-
@
|
94
|
+
@postAction('/bar')
|
89
95
|
bar() {}
|
90
96
|
}
|
91
97
|
S.addController(MyController);
|
@@ -106,9 +112,9 @@ describe('ControllerRegistry', function () {
|
|
106
112
|
|
107
113
|
it('uses path prefix of controller root options', function () {
|
108
114
|
const S = new ControllerRegistry();
|
109
|
-
@
|
115
|
+
@restController()
|
110
116
|
class MyController {
|
111
|
-
@
|
117
|
+
@getAction('/myAction')
|
112
118
|
myAction() {}
|
113
119
|
}
|
114
120
|
S.addController(MyController, {pathPrefix: '/myPrefix'});
|
@@ -121,11 +127,11 @@ describe('ControllerRegistry', function () {
|
|
121
127
|
expect(matching).to.be.not.empty;
|
122
128
|
});
|
123
129
|
|
124
|
-
it('uses path prefix of @
|
130
|
+
it('uses path prefix of @restController metadata', function () {
|
125
131
|
const S = new ControllerRegistry();
|
126
|
-
@
|
132
|
+
@restController('/myController')
|
127
133
|
class MyController {
|
128
|
-
@
|
134
|
+
@getAction('/myAction')
|
129
135
|
myAction() {}
|
130
136
|
}
|
131
137
|
S.addController(MyController);
|
@@ -138,11 +144,11 @@ describe('ControllerRegistry', function () {
|
|
138
144
|
expect(matching).to.be.not.empty;
|
139
145
|
});
|
140
146
|
|
141
|
-
it('uses path prefix of controller root options and @
|
147
|
+
it('uses path prefix of controller root options and @restController metadata', function () {
|
142
148
|
const S = new ControllerRegistry();
|
143
|
-
@
|
149
|
+
@restController('/myController')
|
144
150
|
class MyController {
|
145
|
-
@
|
151
|
+
@getAction('/myAction')
|
146
152
|
myAction() {}
|
147
153
|
}
|
148
154
|
S.addController(MyController, {pathPrefix: '/myPrefix'});
|
@@ -158,9 +164,9 @@ describe('ControllerRegistry', function () {
|
|
158
164
|
describe('single pre-handler', function () {
|
159
165
|
it('uses pre-handler of controller root options', function () {
|
160
166
|
const S = new ControllerRegistry();
|
161
|
-
@
|
167
|
+
@restController()
|
162
168
|
class MyController {
|
163
|
-
@
|
169
|
+
@getAction('/myAction')
|
164
170
|
myAction() {}
|
165
171
|
}
|
166
172
|
S.addController(MyController, {before: PRE_HANDLER_1});
|
@@ -175,12 +181,12 @@ describe('ControllerRegistry', function () {
|
|
175
181
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
176
182
|
});
|
177
183
|
|
178
|
-
it('uses pre-handler of @
|
184
|
+
it('uses pre-handler of @beforeAction metadata applied to controller', function () {
|
179
185
|
const S = new ControllerRegistry();
|
180
|
-
@
|
181
|
-
@
|
186
|
+
@restController()
|
187
|
+
@beforeAction(PRE_HANDLER_1)
|
182
188
|
class MyController {
|
183
|
-
@
|
189
|
+
@getAction('/myAction')
|
184
190
|
myAction() {}
|
185
191
|
}
|
186
192
|
S.addController(MyController);
|
@@ -195,12 +201,12 @@ describe('ControllerRegistry', function () {
|
|
195
201
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
196
202
|
});
|
197
203
|
|
198
|
-
it('uses pre-handler of @
|
204
|
+
it('uses pre-handler of @beforeAction metadata applied to action', function () {
|
199
205
|
const S = new ControllerRegistry();
|
200
|
-
@
|
206
|
+
@restController()
|
201
207
|
class MyController {
|
202
|
-
@
|
203
|
-
@
|
208
|
+
@getAction('/myAction')
|
209
|
+
@beforeAction(PRE_HANDLER_1)
|
204
210
|
myAction() {}
|
205
211
|
}
|
206
212
|
S.addController(MyController);
|
@@ -215,11 +221,11 @@ describe('ControllerRegistry', function () {
|
|
215
221
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
216
222
|
});
|
217
223
|
|
218
|
-
it('uses pre-handler of @
|
224
|
+
it('uses pre-handler of @restController metadata', function () {
|
219
225
|
const S = new ControllerRegistry();
|
220
|
-
@
|
226
|
+
@restController({before: PRE_HANDLER_1})
|
221
227
|
class MyController {
|
222
|
-
@
|
228
|
+
@getAction('/myAction')
|
223
229
|
myAction() {}
|
224
230
|
}
|
225
231
|
S.addController(MyController);
|
@@ -236,9 +242,9 @@ describe('ControllerRegistry', function () {
|
|
236
242
|
|
237
243
|
it('uses pre-handler of @action metadata', function () {
|
238
244
|
const S = new ControllerRegistry();
|
239
|
-
@
|
245
|
+
@restController()
|
240
246
|
class MyController {
|
241
|
-
@
|
247
|
+
@getAction('/myAction', {before: PRE_HANDLER_1})
|
242
248
|
myAction() {}
|
243
249
|
}
|
244
250
|
S.addController(MyController);
|
@@ -257,9 +263,9 @@ describe('ControllerRegistry', function () {
|
|
257
263
|
describe('multiple pre-handlers', function () {
|
258
264
|
it('uses pre-handlers of controller root options', function () {
|
259
265
|
const S = new ControllerRegistry();
|
260
|
-
@
|
266
|
+
@restController()
|
261
267
|
class MyController {
|
262
|
-
@
|
268
|
+
@getAction('/myAction')
|
263
269
|
myAction() {}
|
264
270
|
}
|
265
271
|
S.addController(MyController, {
|
@@ -276,12 +282,12 @@ describe('ControllerRegistry', function () {
|
|
276
282
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
277
283
|
});
|
278
284
|
|
279
|
-
it('uses pre-handlers of @
|
285
|
+
it('uses pre-handlers of @beforeAction metadata applied to controller', function () {
|
280
286
|
const S = new ControllerRegistry();
|
281
|
-
@
|
282
|
-
@
|
287
|
+
@restController()
|
288
|
+
@beforeAction([PRE_HANDLER_1, PRE_HANDLER_2])
|
283
289
|
class MyController {
|
284
|
-
@
|
290
|
+
@getAction('/myAction')
|
285
291
|
myAction() {}
|
286
292
|
}
|
287
293
|
S.addController(MyController);
|
@@ -296,12 +302,12 @@ describe('ControllerRegistry', function () {
|
|
296
302
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
297
303
|
});
|
298
304
|
|
299
|
-
it('uses pre-handlers of @
|
305
|
+
it('uses pre-handlers of @beforeAction metadata applied to action', function () {
|
300
306
|
const S = new ControllerRegistry();
|
301
|
-
@
|
307
|
+
@restController()
|
302
308
|
class MyController {
|
303
|
-
@
|
304
|
-
@
|
309
|
+
@getAction('/myAction')
|
310
|
+
@beforeAction([PRE_HANDLER_1, PRE_HANDLER_2])
|
305
311
|
myAction() {}
|
306
312
|
}
|
307
313
|
S.addController(MyController);
|
@@ -316,13 +322,13 @@ describe('ControllerRegistry', function () {
|
|
316
322
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
317
323
|
});
|
318
324
|
|
319
|
-
it('uses pre-handlers of @
|
325
|
+
it('uses pre-handlers of @restController metadata', function () {
|
320
326
|
const S = new ControllerRegistry();
|
321
|
-
@
|
327
|
+
@restController({
|
322
328
|
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
323
329
|
})
|
324
330
|
class MyController {
|
325
|
-
@
|
331
|
+
@getAction('/myAction')
|
326
332
|
myAction() {}
|
327
333
|
}
|
328
334
|
S.addController(MyController);
|
@@ -339,9 +345,9 @@ describe('ControllerRegistry', function () {
|
|
339
345
|
|
340
346
|
it('uses pre-handlers of @action metadata', function () {
|
341
347
|
const S = new ControllerRegistry();
|
342
|
-
@
|
348
|
+
@restController()
|
343
349
|
class MyController {
|
344
|
-
@
|
350
|
+
@getAction('/myAction', {
|
345
351
|
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
346
352
|
})
|
347
353
|
myAction() {}
|
@@ -362,9 +368,9 @@ describe('ControllerRegistry', function () {
|
|
362
368
|
describe('single post-handler', function () {
|
363
369
|
it('uses post-handler of controller root options', function () {
|
364
370
|
const S = new ControllerRegistry();
|
365
|
-
@
|
371
|
+
@restController()
|
366
372
|
class MyController {
|
367
|
-
@
|
373
|
+
@getAction('/myAction')
|
368
374
|
myAction() {}
|
369
375
|
}
|
370
376
|
S.addController(MyController, {after: POST_HANDLER_1});
|
@@ -381,12 +387,12 @@ describe('ControllerRegistry', function () {
|
|
381
387
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
382
388
|
});
|
383
389
|
|
384
|
-
it('uses pre-handler of @
|
390
|
+
it('uses pre-handler of @afterAction metadata applied to controller', function () {
|
385
391
|
const S = new ControllerRegistry();
|
386
|
-
@
|
387
|
-
@
|
392
|
+
@restController()
|
393
|
+
@afterAction(POST_HANDLER_1)
|
388
394
|
class MyController {
|
389
|
-
@
|
395
|
+
@getAction('/myAction')
|
390
396
|
myAction() {}
|
391
397
|
}
|
392
398
|
S.addController(MyController);
|
@@ -403,12 +409,12 @@ describe('ControllerRegistry', function () {
|
|
403
409
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
404
410
|
});
|
405
411
|
|
406
|
-
it('uses pre-handler of @
|
412
|
+
it('uses pre-handler of @afterAction metadata applied to action', function () {
|
407
413
|
const S = new ControllerRegistry();
|
408
|
-
@
|
414
|
+
@restController()
|
409
415
|
class MyController {
|
410
|
-
@
|
411
|
-
@
|
416
|
+
@getAction('/myAction')
|
417
|
+
@afterAction(POST_HANDLER_1)
|
412
418
|
myAction() {}
|
413
419
|
}
|
414
420
|
S.addController(MyController);
|
@@ -425,11 +431,11 @@ describe('ControllerRegistry', function () {
|
|
425
431
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
426
432
|
});
|
427
433
|
|
428
|
-
it('uses post-handler of @
|
434
|
+
it('uses post-handler of @restController metadata', function () {
|
429
435
|
const S = new ControllerRegistry();
|
430
|
-
@
|
436
|
+
@restController({after: POST_HANDLER_1})
|
431
437
|
class MyController {
|
432
|
-
@
|
438
|
+
@getAction('/myAction')
|
433
439
|
myAction() {}
|
434
440
|
}
|
435
441
|
S.addController(MyController);
|
@@ -448,9 +454,9 @@ describe('ControllerRegistry', function () {
|
|
448
454
|
|
449
455
|
it('uses post-handler of @action metadata', function () {
|
450
456
|
const S = new ControllerRegistry();
|
451
|
-
@
|
457
|
+
@restController()
|
452
458
|
class MyController {
|
453
|
-
@
|
459
|
+
@getAction('/myAction', {after: POST_HANDLER_1})
|
454
460
|
myAction() {}
|
455
461
|
}
|
456
462
|
S.addController(MyController);
|
@@ -471,9 +477,9 @@ describe('ControllerRegistry', function () {
|
|
471
477
|
describe('multiple post-handlers', function () {
|
472
478
|
it('uses post-handlers of controller root options', function () {
|
473
479
|
const S = new ControllerRegistry();
|
474
|
-
@
|
480
|
+
@restController()
|
475
481
|
class MyController {
|
476
|
-
@
|
482
|
+
@getAction('/myAction')
|
477
483
|
myAction() {}
|
478
484
|
}
|
479
485
|
S.addController(MyController, {
|
@@ -492,12 +498,12 @@ describe('ControllerRegistry', function () {
|
|
492
498
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
493
499
|
});
|
494
500
|
|
495
|
-
it('uses pre-handlers of @
|
501
|
+
it('uses pre-handlers of @afterAction metadata applied to controller', function () {
|
496
502
|
const S = new ControllerRegistry();
|
497
|
-
@
|
498
|
-
@
|
503
|
+
@restController()
|
504
|
+
@afterAction([POST_HANDLER_1, POST_HANDLER_2])
|
499
505
|
class MyController {
|
500
|
-
@
|
506
|
+
@getAction('/myAction')
|
501
507
|
myAction() {}
|
502
508
|
}
|
503
509
|
S.addController(MyController);
|
@@ -514,12 +520,12 @@ describe('ControllerRegistry', function () {
|
|
514
520
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
515
521
|
});
|
516
522
|
|
517
|
-
it('uses pre-handlers of @
|
523
|
+
it('uses pre-handlers of @afterAction metadata applied to action', function () {
|
518
524
|
const S = new ControllerRegistry();
|
519
|
-
@
|
525
|
+
@restController()
|
520
526
|
class MyController {
|
521
|
-
@
|
522
|
-
@
|
527
|
+
@getAction('/myAction')
|
528
|
+
@afterAction([POST_HANDLER_1, POST_HANDLER_2])
|
523
529
|
myAction() {}
|
524
530
|
}
|
525
531
|
S.addController(MyController);
|
@@ -536,13 +542,13 @@ describe('ControllerRegistry', function () {
|
|
536
542
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
537
543
|
});
|
538
544
|
|
539
|
-
it('uses post-handlers of @
|
545
|
+
it('uses post-handlers of @restController metadata', function () {
|
540
546
|
const S = new ControllerRegistry();
|
541
|
-
@
|
547
|
+
@restController({
|
542
548
|
after: [POST_HANDLER_1, POST_HANDLER_2],
|
543
549
|
})
|
544
550
|
class MyController {
|
545
|
-
@
|
551
|
+
@getAction('/myAction')
|
546
552
|
myAction() {}
|
547
553
|
}
|
548
554
|
S.addController(MyController);
|
@@ -561,9 +567,9 @@ describe('ControllerRegistry', function () {
|
|
561
567
|
|
562
568
|
it('uses post-handlers of @action metadata', function () {
|
563
569
|
const S = new ControllerRegistry();
|
564
|
-
@
|
570
|
+
@restController()
|
565
571
|
class MyController {
|
566
|
-
@
|
572
|
+
@getAction('/myAction', {
|
567
573
|
after: [POST_HANDLER_1, POST_HANDLER_2],
|
568
574
|
})
|
569
575
|
myAction() {}
|
@@ -586,11 +592,11 @@ describe('ControllerRegistry', function () {
|
|
586
592
|
it('injects parsed parameters', async function () {
|
587
593
|
let checked = false;
|
588
594
|
const S = new ControllerRegistry();
|
589
|
-
@
|
595
|
+
@restController()
|
590
596
|
class MyController {
|
591
|
-
@
|
597
|
+
@getAction('/myAction/:id')
|
592
598
|
myAction(
|
593
|
-
@
|
599
|
+
@requestParams()
|
594
600
|
params: ParsedParams,
|
595
601
|
) {
|
596
602
|
expect(params).to.be.eql({id: '123'});
|
@@ -615,11 +621,11 @@ describe('ControllerRegistry', function () {
|
|
615
621
|
it('injects parsed parameter', async function () {
|
616
622
|
let checked = false;
|
617
623
|
const S = new ControllerRegistry();
|
618
|
-
@
|
624
|
+
@restController()
|
619
625
|
class MyController {
|
620
|
-
@
|
626
|
+
@getAction('/myAction/:id')
|
621
627
|
myAction(
|
622
|
-
@
|
628
|
+
@requestParam('id')
|
623
629
|
param: string,
|
624
630
|
) {
|
625
631
|
expect(param).to.be.eq('123');
|
@@ -644,11 +650,11 @@ describe('ControllerRegistry', function () {
|
|
644
650
|
it('injects parsed queries', async function () {
|
645
651
|
let checked = false;
|
646
652
|
const S = new ControllerRegistry();
|
647
|
-
@
|
653
|
+
@restController()
|
648
654
|
class MyController {
|
649
|
-
@
|
655
|
+
@getAction('/myAction')
|
650
656
|
myAction(
|
651
|
-
@
|
657
|
+
@requestQueries()
|
652
658
|
queries: ParsedQuery,
|
653
659
|
) {
|
654
660
|
expect(queries).to.be.eql({foo: 'bar'});
|
@@ -674,11 +680,11 @@ describe('ControllerRegistry', function () {
|
|
674
680
|
it('injects parsed query', async function () {
|
675
681
|
let checked = false;
|
676
682
|
const S = new ControllerRegistry();
|
677
|
-
@
|
683
|
+
@restController()
|
678
684
|
class MyController {
|
679
|
-
@
|
685
|
+
@getAction('/myAction')
|
680
686
|
myAction(
|
681
|
-
@
|
687
|
+
@requestQuery('foo')
|
682
688
|
foo: string,
|
683
689
|
) {
|
684
690
|
expect(foo).to.be.eql('bar');
|
@@ -704,11 +710,11 @@ describe('ControllerRegistry', function () {
|
|
704
710
|
it('injects parsed headers', async function () {
|
705
711
|
let checked = false;
|
706
712
|
const S = new ControllerRegistry();
|
707
|
-
@
|
713
|
+
@restController()
|
708
714
|
class MyController {
|
709
|
-
@
|
715
|
+
@getAction('/myAction')
|
710
716
|
myAction(
|
711
|
-
@
|
717
|
+
@requestHeaders()
|
712
718
|
headers: ParsedHeaders,
|
713
719
|
) {
|
714
720
|
expect(headers).to.be.eql({
|
@@ -739,11 +745,11 @@ describe('ControllerRegistry', function () {
|
|
739
745
|
it('injects parsed header', async function () {
|
740
746
|
let checked = false;
|
741
747
|
const S = new ControllerRegistry();
|
742
|
-
@
|
748
|
+
@restController()
|
743
749
|
class MyController {
|
744
|
-
@
|
750
|
+
@getAction('/myAction')
|
745
751
|
myAction(
|
746
|
-
@
|
752
|
+
@requestHeader('foo')
|
747
753
|
foo: string,
|
748
754
|
) {
|
749
755
|
expect(foo).to.be.eq('bar');
|
@@ -770,11 +776,11 @@ describe('ControllerRegistry', function () {
|
|
770
776
|
it('injects parsed cookies', async function () {
|
771
777
|
let checked = false;
|
772
778
|
const S = new ControllerRegistry();
|
773
|
-
@
|
779
|
+
@restController()
|
774
780
|
class MyController {
|
775
|
-
@
|
781
|
+
@getAction('/myAction')
|
776
782
|
myAction(
|
777
|
-
@
|
783
|
+
@requestCookies()
|
778
784
|
cookies: ParsedCookie,
|
779
785
|
) {
|
780
786
|
expect(cookies).to.be.eql({foo: 'bar'});
|
@@ -801,11 +807,11 @@ describe('ControllerRegistry', function () {
|
|
801
807
|
it('injects parsed cookie', async function () {
|
802
808
|
let checked = false;
|
803
809
|
const S = new ControllerRegistry();
|
804
|
-
@
|
810
|
+
@restController()
|
805
811
|
class MyController {
|
806
|
-
@
|
812
|
+
@getAction('/myAction')
|
807
813
|
myAction(
|
808
|
-
@
|
814
|
+
@requestCookie('foo')
|
809
815
|
foo: string,
|
810
816
|
) {
|
811
817
|
expect(foo).to.be.eq('bar');
|
@@ -832,11 +838,11 @@ describe('ControllerRegistry', function () {
|
|
832
838
|
it('injects parsed body', async function () {
|
833
839
|
let checked = false;
|
834
840
|
const S = new ControllerRegistry();
|
835
|
-
@
|
841
|
+
@restController()
|
836
842
|
class MyController {
|
837
|
-
@
|
843
|
+
@postAction('/myAction')
|
838
844
|
myAction(
|
839
|
-
@
|
845
|
+
@requestBody()
|
840
846
|
body: object,
|
841
847
|
) {
|
842
848
|
expect(body).to.be.eql({foo: 'bar'});
|
@@ -864,11 +870,11 @@ describe('ControllerRegistry', function () {
|
|
864
870
|
it('injects parsed body parameter', async function () {
|
865
871
|
let checked = false;
|
866
872
|
const S = new ControllerRegistry();
|
867
|
-
@
|
873
|
+
@restController()
|
868
874
|
class MyController {
|
869
|
-
@
|
875
|
+
@postAction('/myAction')
|
870
876
|
myAction(
|
871
|
-
@
|
877
|
+
@requestField('foo')
|
872
878
|
foo: object,
|
873
879
|
) {
|
874
880
|
expect(foo).to.be.eq('bar');
|
@@ -897,18 +903,70 @@ describe('ControllerRegistry', function () {
|
|
897
903
|
describe('hasController', function () {
|
898
904
|
it('returns false if a given controller is not registered', function () {
|
899
905
|
const S = new ControllerRegistry();
|
900
|
-
@
|
906
|
+
@restController()
|
901
907
|
class MyController {}
|
902
908
|
expect(S.hasController(MyController)).to.be.false;
|
903
909
|
});
|
904
910
|
|
905
911
|
it('returns true if a given controller is registered', function () {
|
906
912
|
const S = new ControllerRegistry();
|
907
|
-
@
|
913
|
+
@restController()
|
908
914
|
class MyController {}
|
909
915
|
expect(S.hasController(MyController)).to.be.false;
|
910
916
|
S.addController(MyController);
|
911
917
|
expect(S.hasController(MyController)).to.be.true;
|
912
918
|
});
|
913
919
|
});
|
920
|
+
|
921
|
+
describe('createRouteHandler', function () {
|
922
|
+
it('uses default values from schema as copy', async function () {
|
923
|
+
let invoked = false;
|
924
|
+
const defaultValue = {foo: 'bar'};
|
925
|
+
class MyController {
|
926
|
+
myAction(
|
927
|
+
@requestBody({
|
928
|
+
type: DataType.OBJECT,
|
929
|
+
default: defaultValue,
|
930
|
+
})
|
931
|
+
body: object,
|
932
|
+
) {
|
933
|
+
expect(body).to.be.not.eq(defaultValue);
|
934
|
+
expect(body).to.be.eql(defaultValue);
|
935
|
+
invoked = true;
|
936
|
+
}
|
937
|
+
}
|
938
|
+
const S = new ControllerRegistry();
|
939
|
+
const req = createRequestMock();
|
940
|
+
const res = createResponseMock();
|
941
|
+
const ctx = new RequestContext(S.container, req, res);
|
942
|
+
const handler = S['createRouteHandler'](MyController, 'myAction');
|
943
|
+
await handler(ctx);
|
944
|
+
expect(invoked).to.be.true;
|
945
|
+
});
|
946
|
+
|
947
|
+
it('uses default values from factory function that defined in schema as copy', async function () {
|
948
|
+
let invoked = false;
|
949
|
+
const defaultValue = {foo: 'bar'};
|
950
|
+
class MyController {
|
951
|
+
myAction(
|
952
|
+
@requestBody({
|
953
|
+
type: DataType.OBJECT,
|
954
|
+
default: () => defaultValue,
|
955
|
+
})
|
956
|
+
body: object,
|
957
|
+
) {
|
958
|
+
expect(body).to.be.not.eq(defaultValue);
|
959
|
+
expect(body).to.be.eql(defaultValue);
|
960
|
+
invoked = true;
|
961
|
+
}
|
962
|
+
}
|
963
|
+
const S = new ControllerRegistry();
|
964
|
+
const req = createRequestMock();
|
965
|
+
const res = createResponseMock();
|
966
|
+
const ctx = new RequestContext(S.container, req, res);
|
967
|
+
const handler = S['createRouteHandler'](MyController, 'myAction');
|
968
|
+
await handler(ctx);
|
969
|
+
expect(invoked).to.be.true;
|
970
|
+
});
|
971
|
+
});
|
914
972
|
});
|