@e22m4u/ts-rest-router 0.6.9 → 0.6.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cjs/index.cjs +28 -10
- package/dist/esm/controller-registry.js +3 -3
- package/dist/esm/data-schema-types.d.ts +2 -2
- package/dist/esm/decorators/request-data/request-data-decorator.js +1 -1
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +1 -1
- package/dist/esm/decorators/response-body/response-body-decorator.spec.js +1 -1
- package/dist/esm/rest-router.d.ts +12 -0
- package/dist/esm/rest-router.js +18 -0
- package/package.json +3 -3
- package/src/controller-registry.spec.ts +39 -30
- package/src/controller-registry.ts +4 -4
- package/src/data-schema-types.ts +2 -2
- package/src/decorators/request-data/request-data-decorator.spec.ts +1 -1
- package/src/decorators/request-data/request-data-decorator.ts +2 -2
- package/src/decorators/response-body/response-body-decorator.spec.ts +1 -1
- package/src/rest-router.ts +20 -0
package/README.md
CHANGED
|
@@ -407,7 +407,7 @@ class SystemController {
|
|
|
407
407
|
## Валидация данных
|
|
408
408
|
|
|
409
409
|
Модуль интегрирован с
|
|
410
|
-
[@e22m4u/
|
|
410
|
+
[@e22m4u/ts-data-schema](https://www.npmjs.com/package/@e22m4u/ts-data-schema)
|
|
411
411
|
для гибкой проверки данных. Это дает возможность определять типы данных
|
|
412
412
|
и сложные правила.
|
|
413
413
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -93,8 +93,8 @@ var import_js_trie_router3 = require("@e22m4u/js-trie-router");
|
|
|
93
93
|
// dist/esm/controller-registry.js
|
|
94
94
|
var import_js_format2 = require("@e22m4u/js-format");
|
|
95
95
|
var import_js_trie_router2 = require("@e22m4u/js-trie-router");
|
|
96
|
-
var
|
|
97
|
-
var
|
|
96
|
+
var import_ts_data_schema2 = require("@e22m4u/ts-data-schema");
|
|
97
|
+
var import_ts_data_schema3 = require("@e22m4u/ts-data-schema");
|
|
98
98
|
|
|
99
99
|
// dist/esm/errors/not-a-controller-error.js
|
|
100
100
|
var import_js_format = require("@e22m4u/js-format");
|
|
@@ -211,7 +211,7 @@ var RequestDataSource;
|
|
|
211
211
|
var REQUEST_DATA_METADATA_KEY = new import_ts_reflector5.MetadataKey("requestDataMetadataKey");
|
|
212
212
|
|
|
213
213
|
// dist/esm/decorators/request-data/request-data-decorator.js
|
|
214
|
-
var
|
|
214
|
+
var import_ts_data_schema = require("@e22m4u/ts-data-schema");
|
|
215
215
|
var import_ts_reflector7 = require("@e22m4u/ts-reflector");
|
|
216
216
|
var import_ts_reflector8 = require("@e22m4u/ts-reflector");
|
|
217
217
|
|
|
@@ -264,7 +264,7 @@ function createRequestDataDecoratorWithSource(source) {
|
|
|
264
264
|
} else if (typeof schemaInput === "string") {
|
|
265
265
|
schema = { type: schemaInput };
|
|
266
266
|
} else {
|
|
267
|
-
schema = { type:
|
|
267
|
+
schema = { type: import_ts_data_schema.DataType.ANY };
|
|
268
268
|
}
|
|
269
269
|
return requestData({ schema, source });
|
|
270
270
|
};
|
|
@@ -272,7 +272,7 @@ function createRequestDataDecoratorWithSource(source) {
|
|
|
272
272
|
__name(createRequestDataDecoratorWithSource, "createRequestDataDecoratorWithSource");
|
|
273
273
|
function createRequestDataPropertyDecoratorWithSource(source) {
|
|
274
274
|
return function(propertyKey, schemaInput) {
|
|
275
|
-
const rootSchema = { type:
|
|
275
|
+
const rootSchema = { type: import_ts_data_schema.DataType.OBJECT };
|
|
276
276
|
const properties = {};
|
|
277
277
|
let schemaOrFactory = rootSchema;
|
|
278
278
|
if (typeof schemaInput === "function") {
|
|
@@ -288,7 +288,7 @@ function createRequestDataPropertyDecoratorWithSource(source) {
|
|
|
288
288
|
properties[propertyKey] = { type: schemaInput };
|
|
289
289
|
rootSchema.properties = properties;
|
|
290
290
|
} else {
|
|
291
|
-
properties[propertyKey] = { type:
|
|
291
|
+
properties[propertyKey] = { type: import_ts_data_schema.DataType.ANY };
|
|
292
292
|
rootSchema.properties = properties;
|
|
293
293
|
}
|
|
294
294
|
return requestData({
|
|
@@ -579,7 +579,7 @@ function requestContainer() {
|
|
|
579
579
|
__name(requestContainer, "requestContainer");
|
|
580
580
|
|
|
581
581
|
// dist/esm/controller-registry.js
|
|
582
|
-
var
|
|
582
|
+
var import_ts_data_schema4 = require("@e22m4u/ts-data-schema");
|
|
583
583
|
var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
584
584
|
/**
|
|
585
585
|
* Controller map.
|
|
@@ -860,9 +860,9 @@ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
|
860
860
|
const requestContextMetadataMap = RequestContextReflector.getMetadata(controllerCtor, actionName);
|
|
861
861
|
const requestDataMetadataMap = RequestDataReflector.getMetadata(controllerCtor, actionName);
|
|
862
862
|
const argsNumber = controllerCtor.prototype[actionName].length;
|
|
863
|
-
const defaultsApplier = this.getService(
|
|
864
|
-
const dataTypeCaster = this.getService(
|
|
865
|
-
const dataValidator = this.getService(
|
|
863
|
+
const defaultsApplier = this.getService(import_ts_data_schema4.DefaultValuesApplier);
|
|
864
|
+
const dataTypeCaster = this.getService(import_ts_data_schema3.DataTypeCaster);
|
|
865
|
+
const dataValidator = this.getService(import_ts_data_schema2.DataValidator);
|
|
866
866
|
return (requestContext2) => {
|
|
867
867
|
debug("Executing route handler for %s.%s.", controllerCtor.name, actionName);
|
|
868
868
|
const args = Array(argsNumber).fill(void 0).map((_, index) => {
|
|
@@ -975,6 +975,24 @@ var _RestRouter = class _RestRouter extends DebuggableService {
|
|
|
975
975
|
this.getService(import_js_trie_router3.TrieRouter).addHook(type, hook);
|
|
976
976
|
return this;
|
|
977
977
|
}
|
|
978
|
+
/**
|
|
979
|
+
* Add pre-handler hook.
|
|
980
|
+
*
|
|
981
|
+
* @param hook
|
|
982
|
+
*/
|
|
983
|
+
addPreHandler(hook) {
|
|
984
|
+
this.getService(import_js_trie_router3.TrieRouter).addPreHandler(hook);
|
|
985
|
+
return this;
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Add post-handler hook.
|
|
989
|
+
*
|
|
990
|
+
* @param hook
|
|
991
|
+
*/
|
|
992
|
+
addPostHandler(hook) {
|
|
993
|
+
this.getService(import_js_trie_router3.TrieRouter).addPostHandler(hook);
|
|
994
|
+
return this;
|
|
995
|
+
}
|
|
978
996
|
};
|
|
979
997
|
__name(_RestRouter, "RestRouter");
|
|
980
998
|
var RestRouter = _RestRouter;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { Errorf } from '@e22m4u/js-format';
|
|
2
2
|
import { TrieRouter } from '@e22m4u/js-trie-router';
|
|
3
|
-
import { DataValidator } from '@e22m4u/
|
|
4
|
-
import { DataTypeCaster } from '@e22m4u/
|
|
3
|
+
import { DataValidator } from '@e22m4u/ts-data-schema';
|
|
4
|
+
import { DataTypeCaster } from '@e22m4u/ts-data-schema';
|
|
5
5
|
import { NotAControllerError } from './errors/index.js';
|
|
6
6
|
import { RequestDataSource } from './decorators/index.js';
|
|
7
7
|
import { DebuggableService } from './debuggable-service.js';
|
|
8
8
|
import { RestActionReflector } from './decorators/index.js';
|
|
9
9
|
import { RequestDataReflector } from './decorators/index.js';
|
|
10
10
|
import { AfterActionReflector } from './decorators/index.js';
|
|
11
|
-
import { DefaultValuesApplier } from '@e22m4u/
|
|
11
|
+
import { DefaultValuesApplier } from '@e22m4u/ts-data-schema';
|
|
12
12
|
import { BeforeActionReflector } from './decorators/index.js';
|
|
13
13
|
import { RestControllerReflector } from './decorators/index.js';
|
|
14
14
|
import { RequestContextReflector } from './decorators/index.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DataType } from '@e22m4u/
|
|
2
|
-
import { DataSchema } from '@e22m4u/
|
|
1
|
+
import { DataType } from '@e22m4u/ts-data-schema';
|
|
2
|
+
import { DataSchema } from '@e22m4u/ts-data-schema';
|
|
3
3
|
import { ServiceContainer } from '@e22m4u/js-service';
|
|
4
4
|
/**
|
|
5
5
|
* Data schema factory.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DataType } from '@e22m4u/
|
|
1
|
+
import { DataType } from '@e22m4u/ts-data-schema';
|
|
2
2
|
import { DecoratorTargetType } from '@e22m4u/ts-reflector';
|
|
3
3
|
import { getDecoratorTargetType } from '@e22m4u/ts-reflector';
|
|
4
4
|
import { RequestDataSource } from './request-data-metadata.js';
|
|
@@ -12,7 +12,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
12
12
|
};
|
|
13
13
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
14
14
|
import { expect } from 'chai';
|
|
15
|
-
import { DataType } from '@e22m4u/
|
|
15
|
+
import { DataType } from '@e22m4u/ts-data-schema';
|
|
16
16
|
import { requestData } from './request-data-decorator.js';
|
|
17
17
|
import { requestBody } from './request-data-decorator.js';
|
|
18
18
|
import { requestField } from './request-data-decorator.js';
|
|
@@ -8,7 +8,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
import { expect } from 'chai';
|
|
11
|
-
import { DataType } from '@e22m4u/
|
|
11
|
+
import { DataType } from '@e22m4u/ts-data-schema';
|
|
12
12
|
import { responseBody } from './response-body-decorator.js';
|
|
13
13
|
import { ResponseBodyReflector } from './response-body-reflector.js';
|
|
14
14
|
describe('responseBody', function () {
|
|
@@ -31,4 +31,16 @@ export declare class RestRouter extends DebuggableService {
|
|
|
31
31
|
* @param hook
|
|
32
32
|
*/
|
|
33
33
|
addHook(type: typeof RouterHookType.POST_HANDLER, hook: PostHandlerHook): this;
|
|
34
|
+
/**
|
|
35
|
+
* Add pre-handler hook.
|
|
36
|
+
*
|
|
37
|
+
* @param hook
|
|
38
|
+
*/
|
|
39
|
+
addPreHandler(hook: PreHandlerHook): this;
|
|
40
|
+
/**
|
|
41
|
+
* Add post-handler hook.
|
|
42
|
+
*
|
|
43
|
+
* @param hook
|
|
44
|
+
*/
|
|
45
|
+
addPostHandler(hook: PostHandlerHook): this;
|
|
34
46
|
}
|
package/dist/esm/rest-router.js
CHANGED
|
@@ -31,4 +31,22 @@ export class RestRouter extends DebuggableService {
|
|
|
31
31
|
this.getService(TrieRouter).addHook(type, hook);
|
|
32
32
|
return this;
|
|
33
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Add pre-handler hook.
|
|
36
|
+
*
|
|
37
|
+
* @param hook
|
|
38
|
+
*/
|
|
39
|
+
addPreHandler(hook) {
|
|
40
|
+
this.getService(TrieRouter).addPreHandler(hook);
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Add post-handler hook.
|
|
45
|
+
*
|
|
46
|
+
* @param hook
|
|
47
|
+
*/
|
|
48
|
+
addPostHandler(hook) {
|
|
49
|
+
this.getService(TrieRouter).addPostHandler(hook);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
34
52
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e22m4u/ts-rest-router",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.11",
|
|
4
4
|
"description": "Декларативный REST-маршрутизатор на основе контроллеров для TypeScript",
|
|
5
5
|
"author": "Mikhail Evstropov <e22m4u@yandex.ru>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,11 +41,11 @@
|
|
|
41
41
|
"prepare": "husky"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@e22m4u/
|
|
44
|
+
"@e22m4u/ts-data-schema": "~0.4.9",
|
|
45
45
|
"@e22m4u/js-debug": "~0.3.3",
|
|
46
46
|
"@e22m4u/js-format": "~0.2.1",
|
|
47
47
|
"@e22m4u/js-service": "~0.4.6",
|
|
48
|
-
"@e22m4u/js-trie-router": "~0.4.
|
|
48
|
+
"@e22m4u/js-trie-router": "~0.4.3",
|
|
49
49
|
"@e22m4u/ts-reflector": "~0.1.8",
|
|
50
50
|
"http-errors": "~2.0.0"
|
|
51
51
|
},
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/* eslint mocha/no-sibling-hooks: 0 */
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
createResponseMock,
|
|
5
|
-
RouterHookType,
|
|
3
|
+
TrieRouter,
|
|
6
4
|
HttpMethod,
|
|
5
|
+
RouterHookType,
|
|
7
6
|
ParsedCookies,
|
|
8
7
|
ParsedHeaders,
|
|
9
8
|
ParsedParams,
|
|
@@ -11,14 +10,16 @@ import {
|
|
|
11
10
|
RequestContext,
|
|
12
11
|
RequestParser,
|
|
13
12
|
RouteRegistry,
|
|
14
|
-
|
|
13
|
+
createRouteMock,
|
|
14
|
+
createRequestMock,
|
|
15
|
+
createResponseMock,
|
|
15
16
|
} from '@e22m4u/js-trie-router';
|
|
16
17
|
|
|
17
18
|
import {
|
|
18
|
-
afterAction,
|
|
19
|
-
beforeAction,
|
|
20
19
|
getAction,
|
|
21
20
|
postAction,
|
|
21
|
+
afterAction,
|
|
22
|
+
beforeAction,
|
|
22
23
|
requestBody,
|
|
23
24
|
requestField,
|
|
24
25
|
requestCookies,
|
|
@@ -34,7 +35,7 @@ import {
|
|
|
34
35
|
|
|
35
36
|
import {expect} from 'chai';
|
|
36
37
|
import {Service} from '@e22m4u/js-service';
|
|
37
|
-
import {DataType} from '@e22m4u/
|
|
38
|
+
import {DataType} from '@e22m4u/ts-data-schema';
|
|
38
39
|
import {ServiceContainer} from '@e22m4u/js-service';
|
|
39
40
|
import {DataSchemaFactory} from './data-schema-types.js';
|
|
40
41
|
import {ControllerRegistry} from './controller-registry.js';
|
|
@@ -637,7 +638,7 @@ describe('ControllerRegistry', function () {
|
|
|
637
638
|
const matching = routeReg.matchRouteByRequest(req);
|
|
638
639
|
expect(matching).to.be.not.empty;
|
|
639
640
|
const res = createResponseMock();
|
|
640
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
641
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
641
642
|
ctx.params = matching!.params;
|
|
642
643
|
await matching!.route.handler(ctx);
|
|
643
644
|
expect(checked).to.be.true;
|
|
@@ -666,7 +667,7 @@ describe('ControllerRegistry', function () {
|
|
|
666
667
|
const matching = routeReg.matchRouteByRequest(req);
|
|
667
668
|
expect(matching).to.be.not.empty;
|
|
668
669
|
const res = createResponseMock();
|
|
669
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
670
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
670
671
|
ctx.params = matching!.params;
|
|
671
672
|
await matching!.route.handler(ctx);
|
|
672
673
|
expect(checked).to.be.true;
|
|
@@ -695,7 +696,7 @@ describe('ControllerRegistry', function () {
|
|
|
695
696
|
const matching = routeReg.matchRouteByRequest(req);
|
|
696
697
|
expect(matching).to.be.not.empty;
|
|
697
698
|
const res = createResponseMock();
|
|
698
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
699
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
699
700
|
const reqData = await S.getService(RequestParser).parse(req);
|
|
700
701
|
Object.assign(ctx, reqData);
|
|
701
702
|
await matching!.route.handler(ctx);
|
|
@@ -725,7 +726,7 @@ describe('ControllerRegistry', function () {
|
|
|
725
726
|
const matching = routeReg.matchRouteByRequest(req);
|
|
726
727
|
expect(matching).to.be.not.empty;
|
|
727
728
|
const res = createResponseMock();
|
|
728
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
729
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
729
730
|
const reqData = await S.getService(RequestParser).parse(req);
|
|
730
731
|
Object.assign(ctx, reqData);
|
|
731
732
|
await matching!.route.handler(ctx);
|
|
@@ -760,7 +761,7 @@ describe('ControllerRegistry', function () {
|
|
|
760
761
|
const matching = routeReg.matchRouteByRequest(req);
|
|
761
762
|
expect(matching).to.be.not.empty;
|
|
762
763
|
const res = createResponseMock();
|
|
763
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
764
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
764
765
|
const reqData = await S.getService(RequestParser).parse(req);
|
|
765
766
|
Object.assign(ctx, reqData);
|
|
766
767
|
await matching!.route.handler(ctx);
|
|
@@ -791,7 +792,7 @@ describe('ControllerRegistry', function () {
|
|
|
791
792
|
const matching = routeReg.matchRouteByRequest(req);
|
|
792
793
|
expect(matching).to.be.not.empty;
|
|
793
794
|
const res = createResponseMock();
|
|
794
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
795
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
795
796
|
const reqData = await S.getService(RequestParser).parse(req);
|
|
796
797
|
Object.assign(ctx, reqData);
|
|
797
798
|
await matching!.route.handler(ctx);
|
|
@@ -822,7 +823,7 @@ describe('ControllerRegistry', function () {
|
|
|
822
823
|
const matching = routeReg.matchRouteByRequest(req);
|
|
823
824
|
expect(matching).to.be.not.empty;
|
|
824
825
|
const res = createResponseMock();
|
|
825
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
826
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
826
827
|
const reqData = await S.getService(RequestParser).parse(req);
|
|
827
828
|
Object.assign(ctx, reqData);
|
|
828
829
|
await matching!.route.handler(ctx);
|
|
@@ -853,7 +854,7 @@ describe('ControllerRegistry', function () {
|
|
|
853
854
|
const matching = routeReg.matchRouteByRequest(req);
|
|
854
855
|
expect(matching).to.be.not.empty;
|
|
855
856
|
const res = createResponseMock();
|
|
856
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
857
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
857
858
|
const reqData = await S.getService(RequestParser).parse(req);
|
|
858
859
|
Object.assign(ctx, reqData);
|
|
859
860
|
await matching!.route.handler(ctx);
|
|
@@ -885,7 +886,7 @@ describe('ControllerRegistry', function () {
|
|
|
885
886
|
const matching = routeReg.matchRouteByRequest(req);
|
|
886
887
|
expect(matching).to.be.not.empty;
|
|
887
888
|
const res = createResponseMock();
|
|
888
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
889
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
889
890
|
const reqData = await S.getService(RequestParser).parse(req);
|
|
890
891
|
Object.assign(ctx, reqData);
|
|
891
892
|
await matching!.route.handler(ctx);
|
|
@@ -917,7 +918,7 @@ describe('ControllerRegistry', function () {
|
|
|
917
918
|
const matching = routeReg.matchRouteByRequest(req);
|
|
918
919
|
expect(matching).to.be.not.empty;
|
|
919
920
|
const res = createResponseMock();
|
|
920
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
921
|
+
const ctx = new RequestContext(S.container, req, res, matching!.route);
|
|
921
922
|
const reqData = await S.getService(RequestParser).parse(req);
|
|
922
923
|
Object.assign(ctx, reqData);
|
|
923
924
|
await matching!.route.handler(ctx);
|
|
@@ -963,7 +964,8 @@ describe('ControllerRegistry', function () {
|
|
|
963
964
|
const S = new ControllerRegistry();
|
|
964
965
|
const req = createRequestMock();
|
|
965
966
|
const res = createResponseMock();
|
|
966
|
-
const
|
|
967
|
+
const route = createRouteMock();
|
|
968
|
+
const ctx = new RequestContext(S.container, req, res, route);
|
|
967
969
|
const handler = S['createRouteHandler'](MyController, 'myAction');
|
|
968
970
|
await handler(ctx);
|
|
969
971
|
expect(invoked).to.be.true;
|
|
@@ -988,7 +990,8 @@ describe('ControllerRegistry', function () {
|
|
|
988
990
|
const S = new ControllerRegistry();
|
|
989
991
|
const req = createRequestMock();
|
|
990
992
|
const res = createResponseMock();
|
|
991
|
-
const
|
|
993
|
+
const route = createRouteMock();
|
|
994
|
+
const ctx = new RequestContext(S.container, req, res, route);
|
|
992
995
|
const handler = S['createRouteHandler'](MyController, 'myAction');
|
|
993
996
|
await handler(ctx);
|
|
994
997
|
expect(invoked).to.be.true;
|
|
@@ -1017,13 +1020,13 @@ describe('ControllerRegistry', function () {
|
|
|
1017
1020
|
const req1 = createRequestMock();
|
|
1018
1021
|
const res1 = createResponseMock();
|
|
1019
1022
|
const cont1 = new ServiceContainer(S.container);
|
|
1020
|
-
const ctx1 = new RequestContext(cont1, req1, res1);
|
|
1023
|
+
const ctx1 = new RequestContext(cont1, req1, res1, matching!.route);
|
|
1021
1024
|
const controllerInstance1 = (await handler(ctx1)) as MyStatefulController;
|
|
1022
1025
|
// симуляция второго запроса
|
|
1023
1026
|
const req2 = createRequestMock();
|
|
1024
1027
|
const res2 = createResponseMock();
|
|
1025
1028
|
const cont2 = new ServiceContainer(S.container);
|
|
1026
|
-
const ctx2 = new RequestContext(cont2, req2, res2);
|
|
1029
|
+
const ctx2 = new RequestContext(cont2, req2, res2, matching!.route);
|
|
1027
1030
|
const controllerInstance2 = (await handler(ctx2)) as MyStatefulController;
|
|
1028
1031
|
// проверка, что это два разных экземпляра
|
|
1029
1032
|
expect(controllerInstance1).to.be.instanceOf(MyStatefulController);
|
|
@@ -1057,7 +1060,7 @@ describe('ControllerRegistry', function () {
|
|
|
1057
1060
|
// симуляция запроса
|
|
1058
1061
|
const res = createResponseMock();
|
|
1059
1062
|
const cont1 = new ServiceContainer(S.container);
|
|
1060
|
-
const ctx1 = new RequestContext(cont1, req, res);
|
|
1063
|
+
const ctx1 = new RequestContext(cont1, req, res, matching!.route);
|
|
1061
1064
|
await handler(ctx1);
|
|
1062
1065
|
expect(counter).to.be.eq(1);
|
|
1063
1066
|
});
|
|
@@ -1078,8 +1081,9 @@ describe('ControllerRegistry', function () {
|
|
|
1078
1081
|
}
|
|
1079
1082
|
const req = createRequestMock();
|
|
1080
1083
|
const res = createResponseMock();
|
|
1084
|
+
const route = createRouteMock();
|
|
1081
1085
|
const S = new ControllerRegistry();
|
|
1082
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
1086
|
+
const ctx = new RequestContext(S.container, req, res, route);
|
|
1083
1087
|
ctx.body = '10';
|
|
1084
1088
|
const handler = S['createRouteHandler'](MyController, 'myAction');
|
|
1085
1089
|
await handler(ctx);
|
|
@@ -1104,8 +1108,9 @@ describe('ControllerRegistry', function () {
|
|
|
1104
1108
|
}
|
|
1105
1109
|
const req = createRequestMock();
|
|
1106
1110
|
const res = createResponseMock();
|
|
1111
|
+
const route = createRouteMock();
|
|
1107
1112
|
const S = new ControllerRegistry();
|
|
1108
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
1113
|
+
const ctx = new RequestContext(S.container, req, res, route);
|
|
1109
1114
|
const handler = S['createRouteHandler'](MyController, 'myAction');
|
|
1110
1115
|
await handler(ctx);
|
|
1111
1116
|
expect(invoked).to.be.eq(1);
|
|
@@ -1124,8 +1129,9 @@ describe('ControllerRegistry', function () {
|
|
|
1124
1129
|
}
|
|
1125
1130
|
const req = createRequestMock();
|
|
1126
1131
|
const res = createResponseMock();
|
|
1132
|
+
const route = createRouteMock();
|
|
1127
1133
|
const S = new ControllerRegistry();
|
|
1128
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
1134
|
+
const ctx = new RequestContext(S.container, req, res, route);
|
|
1129
1135
|
ctx.body = '10';
|
|
1130
1136
|
const handler = S['createRouteHandler'](MyController, 'myAction');
|
|
1131
1137
|
await handler(ctx);
|
|
@@ -1145,11 +1151,11 @@ describe('ControllerRegistry', function () {
|
|
|
1145
1151
|
return body;
|
|
1146
1152
|
}
|
|
1147
1153
|
}
|
|
1148
|
-
|
|
1149
1154
|
const req = createRequestMock();
|
|
1150
1155
|
const res = createResponseMock();
|
|
1156
|
+
const route = createRouteMock();
|
|
1151
1157
|
const S = new ControllerRegistry();
|
|
1152
|
-
const ctx = new RequestContext(S.container, req, res);
|
|
1158
|
+
const ctx = new RequestContext(S.container, req, res, route);
|
|
1153
1159
|
const handler = S['createRouteHandler'](MyController, 'myAction');
|
|
1154
1160
|
const throwable = () => handler(ctx);
|
|
1155
1161
|
expect(throwable).to.throw(/is required, but undefined was given/);
|
|
@@ -1175,7 +1181,8 @@ describe('ControllerRegistry', function () {
|
|
|
1175
1181
|
}
|
|
1176
1182
|
const req = createRequestMock();
|
|
1177
1183
|
const res = createResponseMock();
|
|
1178
|
-
const
|
|
1184
|
+
const route = createRouteMock();
|
|
1185
|
+
const ctx = new RequestContext(S.container, req, res, route);
|
|
1179
1186
|
const handler = S['createRouteHandler'](MyController, 'myAction');
|
|
1180
1187
|
await handler(ctx);
|
|
1181
1188
|
expect(invoked).to.be.eq(1);
|
|
@@ -1199,7 +1206,8 @@ describe('ControllerRegistry', function () {
|
|
|
1199
1206
|
}
|
|
1200
1207
|
const req = createRequestMock();
|
|
1201
1208
|
const res = createResponseMock();
|
|
1202
|
-
const
|
|
1209
|
+
const route = createRouteMock();
|
|
1210
|
+
const ctx = new RequestContext(S.container, req, res, route);
|
|
1203
1211
|
ctx.body = '10';
|
|
1204
1212
|
const handler = S['createRouteHandler'](MyController, 'myAction');
|
|
1205
1213
|
await handler(ctx);
|
|
@@ -1224,7 +1232,8 @@ describe('ControllerRegistry', function () {
|
|
|
1224
1232
|
|
|
1225
1233
|
const req = createRequestMock();
|
|
1226
1234
|
const res = createResponseMock();
|
|
1227
|
-
const
|
|
1235
|
+
const route = createRouteMock();
|
|
1236
|
+
const ctx = new RequestContext(S.container, req, res, route);
|
|
1228
1237
|
const handler = S['createRouteHandler'](MyController, 'myAction');
|
|
1229
1238
|
const throwable = () => handler(ctx);
|
|
1230
1239
|
expect(throwable).to.throw(/is required, but undefined was given/);
|
|
@@ -2,10 +2,10 @@ import {AnyObject} from './types.js';
|
|
|
2
2
|
import {Constructor} from './types.js';
|
|
3
3
|
import {Errorf} from '@e22m4u/js-format';
|
|
4
4
|
import {TrieRouter} from '@e22m4u/js-trie-router';
|
|
5
|
-
import {DataSchema} from '@e22m4u/
|
|
5
|
+
import {DataSchema} from '@e22m4u/ts-data-schema';
|
|
6
6
|
import {RouteHandler} from '@e22m4u/js-trie-router';
|
|
7
|
-
import {DataValidator} from '@e22m4u/
|
|
8
|
-
import {DataTypeCaster} from '@e22m4u/
|
|
7
|
+
import {DataValidator} from '@e22m4u/ts-data-schema';
|
|
8
|
+
import {DataTypeCaster} from '@e22m4u/ts-data-schema';
|
|
9
9
|
import {NotAControllerError} from './errors/index.js';
|
|
10
10
|
import {RequestContext} from '@e22m4u/js-trie-router';
|
|
11
11
|
import {RoutePreHandler} from '@e22m4u/js-trie-router';
|
|
@@ -15,7 +15,7 @@ import {DebuggableService} from './debuggable-service.js';
|
|
|
15
15
|
import {RestActionReflector} from './decorators/index.js';
|
|
16
16
|
import {RequestDataReflector} from './decorators/index.js';
|
|
17
17
|
import {AfterActionReflector} from './decorators/index.js';
|
|
18
|
-
import {DefaultValuesApplier} from '@e22m4u/
|
|
18
|
+
import {DefaultValuesApplier} from '@e22m4u/ts-data-schema';
|
|
19
19
|
import {BeforeActionReflector} from './decorators/index.js';
|
|
20
20
|
import {RestControllerReflector} from './decorators/index.js';
|
|
21
21
|
import {RequestContextReflector} from './decorators/index.js';
|
package/src/data-schema-types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {DataType} from '@e22m4u/
|
|
2
|
-
import {DataSchema} from '@e22m4u/
|
|
1
|
+
import {DataType} from '@e22m4u/ts-data-schema';
|
|
2
|
+
import {DataSchema} from '@e22m4u/ts-data-schema';
|
|
3
3
|
import {ServiceContainer} from '@e22m4u/js-service';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
2
|
import {expect} from 'chai';
|
|
3
|
-
import {DataType} from '@e22m4u/
|
|
3
|
+
import {DataType} from '@e22m4u/ts-data-schema';
|
|
4
4
|
import {ServiceContainer} from '@e22m4u/js-service';
|
|
5
5
|
import {requestData} from './request-data-decorator.js';
|
|
6
6
|
import {requestBody} from './request-data-decorator.js';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {NoUndef} from '../../types.js';
|
|
2
2
|
import {Prototype} from '../../types.js';
|
|
3
3
|
import {Constructor} from '../../types.js';
|
|
4
|
-
import {DataType} from '@e22m4u/
|
|
5
|
-
import {DataSchema} from '@e22m4u/
|
|
4
|
+
import {DataType} from '@e22m4u/ts-data-schema';
|
|
5
|
+
import {DataSchema} from '@e22m4u/ts-data-schema';
|
|
6
6
|
import {DecoratorTargetType} from '@e22m4u/ts-reflector';
|
|
7
7
|
import {getDecoratorTargetType} from '@e22m4u/ts-reflector';
|
|
8
8
|
import {RequestDataSource} from './request-data-metadata.js';
|
package/src/rest-router.ts
CHANGED
|
@@ -68,4 +68,24 @@ export class RestRouter extends DebuggableService {
|
|
|
68
68
|
this.getService(TrieRouter).addHook(type, hook);
|
|
69
69
|
return this;
|
|
70
70
|
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Add pre-handler hook.
|
|
74
|
+
*
|
|
75
|
+
* @param hook
|
|
76
|
+
*/
|
|
77
|
+
addPreHandler(hook: PreHandlerHook) {
|
|
78
|
+
this.getService(TrieRouter).addPreHandler(hook);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Add post-handler hook.
|
|
84
|
+
*
|
|
85
|
+
* @param hook
|
|
86
|
+
*/
|
|
87
|
+
addPostHandler(hook: PostHandlerHook) {
|
|
88
|
+
this.getService(TrieRouter).addPostHandler(hook);
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
71
91
|
}
|