@e22m4u/ts-rest-router 0.5.5 → 0.6.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/dist/cjs/index.cjs +55 -80
- package/dist/esm/controller-registry.js +11 -3
- package/dist/esm/data-schema-types.d.ts +15 -0
- package/dist/esm/debuggable-service.d.ts +3 -17
- package/dist/esm/debuggable-service.js +7 -24
- package/dist/esm/decorators/before-action/before-action-reflector.spec.js +15 -15
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +11 -12
- package/dist/esm/decorators/request-data/request-data-decorator.js +20 -12
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +183 -1
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +2 -2
- package/dist/esm/decorators/response-body/response-body-decorator.d.ts +2 -3
- package/dist/esm/decorators/response-body/response-body-decorator.js +7 -7
- package/dist/esm/decorators/response-body/response-body-decorator.spec.js +21 -1
- package/dist/esm/decorators/response-body/response-body-metadata.d.ts +2 -2
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/utils/index.d.ts +0 -2
- package/dist/esm/utils/index.js +0 -2
- package/package.json +10 -10
- package/src/controller-registry.spec.ts +174 -1
- package/src/controller-registry.ts +11 -7
- package/src/data-schema-types.ts +18 -0
- package/src/debuggable-service.spec.ts +0 -8
- package/src/debuggable-service.ts +7 -28
- package/src/decorators/before-action/before-action-reflector.spec.ts +15 -15
- package/src/decorators/request-data/request-data-decorator.spec.ts +174 -1
- package/src/decorators/request-data/request-data-decorator.ts +22 -13
- package/src/decorators/request-data/request-data-metadata.ts +2 -2
- package/src/decorators/response-body/response-body-decorator.spec.ts +17 -1
- package/src/decorators/response-body/response-body-decorator.ts +9 -11
- package/src/decorators/response-body/response-body-metadata.ts +2 -2
- package/src/index.ts +3 -1
- package/src/utils/index.ts +0 -2
- package/dist/esm/utils/create-error.d.ts +0 -10
- package/dist/esm/utils/create-error.js +0 -13
- package/dist/esm/utils/create-error.spec.js +0 -8
- package/dist/esm/utils/to-camel-case.d.ts +0 -6
- package/dist/esm/utils/to-camel-case.js +0 -11
- package/dist/esm/utils/to-camel-case.spec.d.ts +0 -1
- package/dist/esm/utils/to-camel-case.spec.js +0 -10
- package/src/utils/create-error.spec.ts +0 -9
- package/src/utils/create-error.ts +0 -19
- package/src/utils/to-camel-case.spec.ts +0 -11
- package/src/utils/to-camel-case.ts +0 -11
- /package/dist/esm/{utils/create-error.spec.d.ts → data-schema-types.js} +0 -0
package/dist/cjs/index.cjs
CHANGED
@@ -16,6 +16,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
16
16
|
}
|
17
17
|
return to;
|
18
18
|
};
|
19
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
19
20
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
20
21
|
|
21
22
|
// dist/esm/index.js
|
@@ -41,8 +42,6 @@ __export(index_exports, {
|
|
41
42
|
RestRouter: () => RestRouter,
|
42
43
|
afterAction: () => afterAction,
|
43
44
|
beforeAction: () => beforeAction,
|
44
|
-
capitalize: () => capitalize,
|
45
|
-
createError: () => createError,
|
46
45
|
deleteAction: () => deleteAction,
|
47
46
|
getAction: () => getAction,
|
48
47
|
httpRequest: () => httpRequest,
|
@@ -65,77 +64,41 @@ __export(index_exports, {
|
|
65
64
|
requestQuery: () => requestQuery,
|
66
65
|
responseBody: () => responseBody,
|
67
66
|
restAction: () => restAction,
|
68
|
-
restController: () => restController
|
69
|
-
toCamelCase: () => toCamelCase
|
67
|
+
restController: () => restController
|
70
68
|
});
|
71
69
|
module.exports = __toCommonJS(index_exports);
|
72
|
-
|
73
|
-
// dist/esm/utils/capitalize.js
|
74
|
-
function capitalize(input) {
|
75
|
-
return input.charAt(0).toUpperCase() + input.slice(1);
|
76
|
-
}
|
77
|
-
__name(capitalize, "capitalize");
|
78
|
-
|
79
|
-
// dist/esm/utils/create-error.js
|
80
|
-
var import_js_format = require("@e22m4u/js-format");
|
81
|
-
function createError(errorCtor, message, ...args) {
|
82
|
-
const interpolatedMessage = (0, import_js_format.format)(message, ...args);
|
83
|
-
return new errorCtor(interpolatedMessage);
|
84
|
-
}
|
85
|
-
__name(createError, "createError");
|
86
|
-
|
87
|
-
// dist/esm/utils/to-camel-case.js
|
88
|
-
function toCamelCase(input) {
|
89
|
-
return input.replace(/(^\w|[A-Z]|\b\w)/g, (c) => c.toUpperCase()).replace(/\W+/g, "").replace(/(^\w)/g, (c) => c.toLowerCase());
|
90
|
-
}
|
91
|
-
__name(toCamelCase, "toCamelCase");
|
70
|
+
__reExport(index_exports, require("@e22m4u/js-trie-router"), module.exports);
|
92
71
|
|
93
72
|
// dist/esm/rest-router.js
|
94
73
|
var import_js_trie_router3 = require("@e22m4u/js-trie-router");
|
95
74
|
|
96
75
|
// dist/esm/debuggable-service.js
|
97
76
|
var import_js_service = require("@e22m4u/js-service");
|
98
|
-
var
|
99
|
-
var _DebuggableService = class _DebuggableService extends import_js_service.Service {
|
100
|
-
/**
|
101
|
-
* Debug.
|
102
|
-
*/
|
103
|
-
debug;
|
104
|
-
/**
|
105
|
-
* Возвращает функцию-отладчик с сегментом пространства имен
|
106
|
-
* указанного в параметре метода.
|
107
|
-
*
|
108
|
-
* @param method
|
109
|
-
* @protected
|
110
|
-
*/
|
111
|
-
getDebuggerFor(method) {
|
112
|
-
return this.debug.withHash().withNs(method.name);
|
113
|
-
}
|
77
|
+
var _DebuggableService = class _DebuggableService extends import_js_service.DebuggableService {
|
114
78
|
/**
|
115
79
|
* Constructor.
|
116
80
|
*
|
117
81
|
* @param container
|
118
82
|
*/
|
119
83
|
constructor(container) {
|
120
|
-
super(container
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
debug("Service created.");
|
84
|
+
super(container, {
|
85
|
+
namespace: "tsRestRouter",
|
86
|
+
noEnvironmentNamespace: true
|
87
|
+
});
|
125
88
|
}
|
126
89
|
};
|
127
90
|
__name(_DebuggableService, "DebuggableService");
|
128
91
|
var DebuggableService = _DebuggableService;
|
129
92
|
|
130
93
|
// dist/esm/controller-registry.js
|
131
|
-
var
|
94
|
+
var import_js_format2 = require("@e22m4u/js-format");
|
132
95
|
var import_js_trie_router2 = require("@e22m4u/js-trie-router");
|
133
96
|
var import_ts_data_schema2 = require("@e22m4u/ts-data-schema");
|
134
97
|
var import_ts_data_schema3 = require("@e22m4u/ts-data-schema");
|
135
98
|
|
136
99
|
// dist/esm/errors/not-a-controller-error.js
|
137
|
-
var
|
138
|
-
var _NotAControllerError = class _NotAControllerError extends
|
100
|
+
var import_js_format = require("@e22m4u/js-format");
|
101
|
+
var _NotAControllerError = class _NotAControllerError extends import_js_format.Errorf {
|
139
102
|
/**
|
140
103
|
* Constructor.
|
141
104
|
*
|
@@ -294,12 +257,12 @@ function requestData(options) {
|
|
294
257
|
}
|
295
258
|
__name(requestData, "requestData");
|
296
259
|
function createRequestDataDecoratorWithSource(source) {
|
297
|
-
return function(
|
260
|
+
return function(schemaInput) {
|
298
261
|
let schema;
|
299
|
-
if (typeof
|
300
|
-
schema =
|
301
|
-
} else if (typeof
|
302
|
-
schema = { type:
|
262
|
+
if (typeof schemaInput === "function" || typeof schemaInput === "object") {
|
263
|
+
schema = schemaInput;
|
264
|
+
} else if (typeof schemaInput === "string") {
|
265
|
+
schema = { type: schemaInput };
|
303
266
|
} else {
|
304
267
|
schema = { type: import_ts_data_schema.DataType.ANY };
|
305
268
|
}
|
@@ -308,14 +271,21 @@ function createRequestDataDecoratorWithSource(source) {
|
|
308
271
|
}
|
309
272
|
__name(createRequestDataDecoratorWithSource, "createRequestDataDecoratorWithSource");
|
310
273
|
function createRequestDataPropertyDecoratorWithSource(source) {
|
311
|
-
return function(propertyKey,
|
312
|
-
const properties = {};
|
274
|
+
return function(propertyKey, schemaInput) {
|
313
275
|
const rootSchema = { type: import_ts_data_schema.DataType.OBJECT };
|
314
|
-
|
315
|
-
|
276
|
+
const properties = {};
|
277
|
+
let schemaOrFactory = rootSchema;
|
278
|
+
if (typeof schemaInput === "function") {
|
279
|
+
schemaOrFactory = /* @__PURE__ */ __name((container) => {
|
280
|
+
properties[propertyKey] = schemaInput(container);
|
281
|
+
rootSchema.properties = properties;
|
282
|
+
return rootSchema;
|
283
|
+
}, "schemaOrFactory");
|
284
|
+
} else if (typeof schemaInput === "object") {
|
285
|
+
properties[propertyKey] = schemaInput;
|
316
286
|
rootSchema.properties = properties;
|
317
|
-
} else if (typeof
|
318
|
-
properties[propertyKey] = { type:
|
287
|
+
} else if (typeof schemaInput === "string") {
|
288
|
+
properties[propertyKey] = { type: schemaInput };
|
319
289
|
rootSchema.properties = properties;
|
320
290
|
} else {
|
321
291
|
properties[propertyKey] = { type: import_ts_data_schema.DataType.ANY };
|
@@ -323,7 +293,7 @@ function createRequestDataPropertyDecoratorWithSource(source) {
|
|
323
293
|
}
|
324
294
|
return requestData({
|
325
295
|
source,
|
326
|
-
schema:
|
296
|
+
schema: schemaOrFactory,
|
327
297
|
property: propertyKey
|
328
298
|
});
|
329
299
|
};
|
@@ -478,18 +448,18 @@ __name(_ResponseBodyReflector, "ResponseBodyReflector");
|
|
478
448
|
var ResponseBodyReflector = _ResponseBodyReflector;
|
479
449
|
|
480
450
|
// dist/esm/decorators/response-body/response-body-decorator.js
|
481
|
-
function responseBody(
|
451
|
+
function responseBody(schemaInput) {
|
482
452
|
return function(target, propertyKey, descriptor) {
|
483
453
|
const decoratorType = (0, import_ts_reflector20.getDecoratorTargetType)(target, propertyKey, descriptor);
|
484
454
|
if (decoratorType !== import_ts_reflector19.DecoratorTargetType.INSTANCE_METHOD)
|
485
455
|
throw new Error("@responseBody decorator is only supported on an instance method.");
|
486
|
-
let
|
487
|
-
if (typeof
|
488
|
-
|
489
|
-
} else if (typeof
|
490
|
-
|
456
|
+
let schemaOrFactory;
|
457
|
+
if (typeof schemaInput === "function" || typeof schemaInput === "object") {
|
458
|
+
schemaOrFactory = schemaInput;
|
459
|
+
} else if (typeof schemaInput === "string") {
|
460
|
+
schemaOrFactory = { type: schemaInput };
|
491
461
|
}
|
492
|
-
ResponseBodyReflector.setMetadata(
|
462
|
+
ResponseBodyReflector.setMetadata(schemaOrFactory ? { schema: schemaOrFactory } : {}, target.constructor, propertyKey);
|
493
463
|
};
|
494
464
|
}
|
495
465
|
__name(responseBody, "responseBody");
|
@@ -624,7 +594,7 @@ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
624
594
|
addController(ctor, options) {
|
625
595
|
const debug = this.getDebuggerFor(this.addController);
|
626
596
|
if (this.hasController(ctor))
|
627
|
-
throw new
|
597
|
+
throw new import_js_format2.Errorf("The controller %v is already registered.", ctor.name);
|
628
598
|
const controllerMd = RestControllerReflector.getMetadata(ctor);
|
629
599
|
if (!controllerMd)
|
630
600
|
throw new NotAControllerError(ctor);
|
@@ -711,7 +681,7 @@ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
711
681
|
debug("Metadata target is %s.", ctor.name);
|
712
682
|
const md = RestControllerReflector.getMetadata(ctor);
|
713
683
|
if (!md)
|
714
|
-
throw new
|
684
|
+
throw new import_js_format2.Errorf("Controller %v has no metadata.", ctor);
|
715
685
|
const res = md.path || "";
|
716
686
|
debug("Controller path prefix is %v.", res);
|
717
687
|
return md.path || "";
|
@@ -815,7 +785,7 @@ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
815
785
|
debug("Target is %s.", ctor.name);
|
816
786
|
const md = RestControllerReflector.getMetadata(ctor);
|
817
787
|
if (!md)
|
818
|
-
throw new
|
788
|
+
throw new import_js_format2.Errorf("Controller %v has no metadata.", ctor);
|
819
789
|
let res = [];
|
820
790
|
if (md.before)
|
821
791
|
res = Array.isArray(md.before) ? md.before : [md.before];
|
@@ -832,7 +802,7 @@ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
832
802
|
debug("Getting post-handlers from @restController metadata.");
|
833
803
|
const md = RestControllerReflector.getMetadata(ctor);
|
834
804
|
if (!md)
|
835
|
-
throw new
|
805
|
+
throw new import_js_format2.Errorf("Controller %v has no metadata.", ctor);
|
836
806
|
let res = [];
|
837
807
|
if (md.after)
|
838
808
|
res = Array.isArray(md.after) ? md.after : [md.after];
|
@@ -851,7 +821,7 @@ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
851
821
|
const actionsMd = RestActionReflector.getMetadata(ctor);
|
852
822
|
const actionMd = actionsMd.get(actionName);
|
853
823
|
if (!actionMd)
|
854
|
-
throw new
|
824
|
+
throw new import_js_format2.Errorf("Action %s.%s has no metadata.", ctor.name, actionName);
|
855
825
|
let res = [];
|
856
826
|
if (actionMd.before)
|
857
827
|
res = Array.isArray(actionMd.before) ? actionMd.before : [actionMd.before];
|
@@ -870,7 +840,7 @@ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
870
840
|
const actionsMd = RestActionReflector.getMetadata(ctor);
|
871
841
|
const actionMd = actionsMd.get(actionName);
|
872
842
|
if (!actionMd)
|
873
|
-
throw new
|
843
|
+
throw new import_js_format2.Errorf("Action %s.%s has no metadata.", ctor.name, actionName);
|
874
844
|
let res = [];
|
875
845
|
if (actionMd.after)
|
876
846
|
res = Array.isArray(actionMd.after) ? actionMd.after : [actionMd.after];
|
@@ -935,14 +905,21 @@ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
935
905
|
}
|
936
906
|
debug("Request data source is %v.", requestDataMd.source);
|
937
907
|
if (requestDataMd.schema) {
|
938
|
-
|
908
|
+
let dataSchema;
|
909
|
+
if (typeof requestDataMd.schema === "function") {
|
910
|
+
dataSchema = requestDataMd.schema(this.container);
|
911
|
+
debug("Data schema extracted from factory function.");
|
912
|
+
} else {
|
913
|
+
dataSchema = requestDataMd.schema;
|
914
|
+
}
|
915
|
+
data = defaultsApplier.applyDefaultValuesIfNeeded(data, dataSchema, requestDataMd.source);
|
939
916
|
debug("Default values applied.");
|
940
|
-
data = dataTypeCaster.cast(data,
|
917
|
+
data = dataTypeCaster.cast(data, dataSchema, {
|
941
918
|
noTypeCastError: true,
|
942
919
|
sourcePath: requestDataMd.source
|
943
920
|
});
|
944
921
|
debug("Data type casting applied.");
|
945
|
-
dataValidator.validate(data,
|
922
|
+
dataValidator.validate(data, dataSchema, requestDataMd.source);
|
946
923
|
debug("Data validation passed.");
|
947
924
|
}
|
948
925
|
if (requestDataMd.property == null) {
|
@@ -961,7 +938,7 @@ var _ControllerRegistry = class _ControllerRegistry extends DebuggableService {
|
|
961
938
|
}
|
962
939
|
});
|
963
940
|
if (requestContext2.container.has(controllerCtor))
|
964
|
-
throw new
|
941
|
+
throw new import_js_format2.Errorf("The controller %v is already registered, which breaks controller isolation per request.", controllerCtor.name);
|
965
942
|
const controller = requestContext2.container.get(controllerCtor);
|
966
943
|
return controller[actionName](...args);
|
967
944
|
};
|
@@ -1013,8 +990,6 @@ var RestRouter = _RestRouter;
|
|
1013
990
|
RestRouter,
|
1014
991
|
afterAction,
|
1015
992
|
beforeAction,
|
1016
|
-
capitalize,
|
1017
|
-
createError,
|
1018
993
|
deleteAction,
|
1019
994
|
getAction,
|
1020
995
|
httpRequest,
|
@@ -1038,5 +1013,5 @@ var RestRouter = _RestRouter;
|
|
1038
1013
|
responseBody,
|
1039
1014
|
restAction,
|
1040
1015
|
restController,
|
1041
|
-
|
1016
|
+
...require("@e22m4u/js-trie-router")
|
1042
1017
|
});
|
@@ -384,14 +384,22 @@ export class ControllerRegistry extends DebuggableService {
|
|
384
384
|
// по умолчанию, выполняется конвертация входящего
|
385
385
|
// значения и валидация согласно схеме
|
386
386
|
if (requestDataMd.schema) {
|
387
|
-
|
387
|
+
let dataSchema;
|
388
|
+
if (typeof requestDataMd.schema === 'function') {
|
389
|
+
dataSchema = requestDataMd.schema(this.container);
|
390
|
+
debug('Data schema extracted from factory function.');
|
391
|
+
}
|
392
|
+
else {
|
393
|
+
dataSchema = requestDataMd.schema;
|
394
|
+
}
|
395
|
+
data = defaultsApplier.applyDefaultValuesIfNeeded(data, dataSchema, requestDataMd.source);
|
388
396
|
debug('Default values applied.');
|
389
|
-
data = dataTypeCaster.cast(data,
|
397
|
+
data = dataTypeCaster.cast(data, dataSchema, {
|
390
398
|
noTypeCastError: true,
|
391
399
|
sourcePath: requestDataMd.source,
|
392
400
|
});
|
393
401
|
debug('Data type casting applied.');
|
394
|
-
dataValidator.validate(data,
|
402
|
+
dataValidator.validate(data, dataSchema, requestDataMd.source);
|
395
403
|
debug('Data validation passed.');
|
396
404
|
}
|
397
405
|
// если свойство данных не определено,
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { DataType } from '@e22m4u/ts-data-schema';
|
2
|
+
import { DataSchema } from '@e22m4u/ts-data-schema';
|
3
|
+
import { ServiceContainer } from '@e22m4u/js-service';
|
4
|
+
/**
|
5
|
+
* Data schema factory.
|
6
|
+
*/
|
7
|
+
export type DataSchemaFactory = (container: ServiceContainer) => DataSchema;
|
8
|
+
/**
|
9
|
+
* Data schema or factory.
|
10
|
+
*/
|
11
|
+
export type DataSchemaOrFactory = DataSchema | DataSchemaFactory;
|
12
|
+
/**
|
13
|
+
* Data schema input.
|
14
|
+
*/
|
15
|
+
export type DataSchemaInput = DataSchemaOrFactory | DataType;
|
@@ -1,23 +1,9 @@
|
|
1
|
-
import { Callable } from './types.js';
|
2
|
-
import { Debugger } from '@e22m4u/js-debug';
|
3
|
-
import { Service } from '@e22m4u/js-service';
|
4
1
|
import { ServiceContainer } from '@e22m4u/js-service';
|
2
|
+
import { DebuggableService as BaseDebuggableService } from '@e22m4u/js-service';
|
5
3
|
/**
|
6
|
-
*
|
4
|
+
* Base debuggable service.
|
7
5
|
*/
|
8
|
-
export declare class DebuggableService extends
|
9
|
-
/**
|
10
|
-
* Debug.
|
11
|
-
*/
|
12
|
-
debug: Debugger;
|
13
|
-
/**
|
14
|
-
* Возвращает функцию-отладчик с сегментом пространства имен
|
15
|
-
* указанного в параметре метода.
|
16
|
-
*
|
17
|
-
* @param method
|
18
|
-
* @protected
|
19
|
-
*/
|
20
|
-
protected getDebuggerFor(method: Callable): Debugger;
|
6
|
+
export declare class DebuggableService extends BaseDebuggableService {
|
21
7
|
/**
|
22
8
|
* Constructor.
|
23
9
|
*
|
@@ -1,34 +1,17 @@
|
|
1
|
-
import {
|
2
|
-
import { toCamelCase } from './utils/index.js';
|
3
|
-
import { createDebugger } from '@e22m4u/js-debug';
|
1
|
+
import { DebuggableService as BaseDebuggableService } from '@e22m4u/js-service';
|
4
2
|
/**
|
5
|
-
*
|
3
|
+
* Base debuggable service.
|
6
4
|
*/
|
7
|
-
export class DebuggableService extends
|
8
|
-
/**
|
9
|
-
* Debug.
|
10
|
-
*/
|
11
|
-
debug;
|
12
|
-
/**
|
13
|
-
* Возвращает функцию-отладчик с сегментом пространства имен
|
14
|
-
* указанного в параметре метода.
|
15
|
-
*
|
16
|
-
* @param method
|
17
|
-
* @protected
|
18
|
-
*/
|
19
|
-
getDebuggerFor(method) {
|
20
|
-
return this.debug.withHash().withNs(method.name);
|
21
|
-
}
|
5
|
+
export class DebuggableService extends BaseDebuggableService {
|
22
6
|
/**
|
23
7
|
* Constructor.
|
24
8
|
*
|
25
9
|
* @param container
|
26
10
|
*/
|
27
11
|
constructor(container) {
|
28
|
-
super(container
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
debug('Service created.');
|
12
|
+
super(container, {
|
13
|
+
namespace: 'tsRestRouter',
|
14
|
+
noEnvironmentNamespace: true,
|
15
|
+
});
|
33
16
|
}
|
34
17
|
}
|
@@ -2,17 +2,17 @@ import { expect } from 'chai';
|
|
2
2
|
import { Reflector } from '@e22m4u/ts-reflector';
|
3
3
|
import { BeforeActionReflector } from './before-action-reflector.js';
|
4
4
|
import { BEFORE_ACTION_METADATA_KEY } from './before-action-metadata.js';
|
5
|
-
const
|
6
|
-
const
|
7
|
-
const
|
5
|
+
const HOOK_1 = () => undefined;
|
6
|
+
const HOOK_2 = () => undefined;
|
7
|
+
const HOOK_3 = () => undefined;
|
8
8
|
describe('BeforeActionReflector', function () {
|
9
9
|
describe('class target', function () {
|
10
10
|
describe('addMetadata', function () {
|
11
11
|
it('adds a given value to the target metadata', function () {
|
12
12
|
class Target {
|
13
13
|
}
|
14
|
-
const md1 = { hook:
|
15
|
-
const md2 = { hook: [
|
14
|
+
const md1 = { hook: HOOK_1 };
|
15
|
+
const md2 = { hook: [HOOK_2, HOOK_3] };
|
16
16
|
BeforeActionReflector.addMetadata(md1, Target);
|
17
17
|
BeforeActionReflector.addMetadata(md2, Target);
|
18
18
|
const res = Reflector.getOwnMetadata(BEFORE_ACTION_METADATA_KEY, Target);
|
@@ -29,8 +29,8 @@ describe('BeforeActionReflector', function () {
|
|
29
29
|
it('returns existing metadata by the target', function () {
|
30
30
|
class Target {
|
31
31
|
}
|
32
|
-
const md1 = { hook:
|
33
|
-
const md2 = { hook: [
|
32
|
+
const md1 = { hook: HOOK_1 };
|
33
|
+
const md2 = { hook: [HOOK_2, HOOK_3] };
|
34
34
|
const mdArray = [md1, md2];
|
35
35
|
Reflector.defineMetadata(BEFORE_ACTION_METADATA_KEY, mdArray, Target);
|
36
36
|
const res = BeforeActionReflector.getMetadata(Target);
|
@@ -43,8 +43,8 @@ describe('BeforeActionReflector', function () {
|
|
43
43
|
it('adds a given value to the target metadata', function () {
|
44
44
|
class Target {
|
45
45
|
}
|
46
|
-
const md1 = { hook:
|
47
|
-
const md2 = { hook: [
|
46
|
+
const md1 = { hook: HOOK_1 };
|
47
|
+
const md2 = { hook: [HOOK_2, HOOK_3] };
|
48
48
|
BeforeActionReflector.addMetadata(md1, Target, 'prop');
|
49
49
|
BeforeActionReflector.addMetadata(md2, Target, 'prop');
|
50
50
|
const res = Reflector.getOwnMetadata(BEFORE_ACTION_METADATA_KEY, Target, 'prop');
|
@@ -61,8 +61,8 @@ describe('BeforeActionReflector', function () {
|
|
61
61
|
it('returns existing metadata by the target', function () {
|
62
62
|
class Target {
|
63
63
|
}
|
64
|
-
const md1 = { hook:
|
65
|
-
const md2 = { hook: [
|
64
|
+
const md1 = { hook: HOOK_1 };
|
65
|
+
const md2 = { hook: [HOOK_2, HOOK_3] };
|
66
66
|
const mdArray = [md1, md2];
|
67
67
|
Reflector.defineMetadata(BEFORE_ACTION_METADATA_KEY, mdArray, Target, 'prop');
|
68
68
|
const res = BeforeActionReflector.getMetadata(Target, 'prop');
|
@@ -74,8 +74,8 @@ describe('BeforeActionReflector', function () {
|
|
74
74
|
it('can distinguish class and method metadata', function () {
|
75
75
|
class Target {
|
76
76
|
}
|
77
|
-
const md1 = { hook:
|
78
|
-
const md2 = { hook:
|
77
|
+
const md1 = { hook: HOOK_1 };
|
78
|
+
const md2 = { hook: HOOK_2 };
|
79
79
|
BeforeActionReflector.addMetadata(md1, Target);
|
80
80
|
BeforeActionReflector.addMetadata(md2, Target, 'prop');
|
81
81
|
const res1 = Reflector.getOwnMetadata(BEFORE_ACTION_METADATA_KEY, Target);
|
@@ -88,8 +88,8 @@ describe('BeforeActionReflector', function () {
|
|
88
88
|
it('can distinguish class and method metadata', function () {
|
89
89
|
class Target {
|
90
90
|
}
|
91
|
-
const md1 = { hook:
|
92
|
-
const md2 = { hook:
|
91
|
+
const md1 = { hook: HOOK_1 };
|
92
|
+
const md2 = { hook: HOOK_2 };
|
93
93
|
Reflector.defineMetadata(BEFORE_ACTION_METADATA_KEY, [md1], Target);
|
94
94
|
Reflector.defineMetadata(BEFORE_ACTION_METADATA_KEY, [md2], Target, 'prop');
|
95
95
|
const res1 = BeforeActionReflector.getMetadata(Target);
|
@@ -1,6 +1,5 @@
|
|
1
1
|
import { Prototype } from '../../types.js';
|
2
|
-
import {
|
3
|
-
import { DataSchema } from '@e22m4u/ts-data-schema';
|
2
|
+
import { DataSchemaInput } from '../../data-schema-types.js';
|
4
3
|
import { RequestDataMetadata } from './request-data-metadata.js';
|
5
4
|
/**
|
6
5
|
* Request data options.
|
@@ -15,13 +14,13 @@ export declare function requestData<T extends object>(options: RequestDataOption
|
|
15
14
|
/**
|
16
15
|
* Decorator aliases.
|
17
16
|
*/
|
18
|
-
export declare const requestParams: (
|
19
|
-
export declare const requestParam: (propertyKey: string,
|
20
|
-
export declare const requestQueries: (
|
21
|
-
export declare const requestQuery: (propertyKey: string,
|
22
|
-
export declare const requestHeaders: (
|
23
|
-
export declare const requestHeader: (propertyKey: string,
|
24
|
-
export declare const requestCookies: (
|
25
|
-
export declare const requestCookie: (propertyKey: string,
|
26
|
-
export declare const requestBody: (
|
27
|
-
export declare const requestField: (propertyKey: string,
|
17
|
+
export declare const requestParams: (schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
18
|
+
export declare const requestParam: (propertyKey: string, schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
19
|
+
export declare const requestQueries: (schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
20
|
+
export declare const requestQuery: (propertyKey: string, schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
21
|
+
export declare const requestHeaders: (schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
22
|
+
export declare const requestHeader: (propertyKey: string, schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
23
|
+
export declare const requestCookies: (schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
24
|
+
export declare const requestCookie: (propertyKey: string, schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
25
|
+
export declare const requestBody: (schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
26
|
+
export declare const requestField: (propertyKey: string, schemaInput?: DataSchemaInput) => (target: Prototype<object>, propertyKey: string, index: number) => void;
|
@@ -23,13 +23,13 @@ export function requestData(options) {
|
|
23
23
|
* @param source
|
24
24
|
*/
|
25
25
|
function createRequestDataDecoratorWithSource(source) {
|
26
|
-
return function (
|
26
|
+
return function (schemaInput) {
|
27
27
|
let schema;
|
28
|
-
if (typeof
|
29
|
-
schema =
|
28
|
+
if (typeof schemaInput === 'function' || typeof schemaInput === 'object') {
|
29
|
+
schema = schemaInput;
|
30
30
|
}
|
31
|
-
else if (typeof
|
32
|
-
schema = { type:
|
31
|
+
else if (typeof schemaInput === 'string') {
|
32
|
+
schema = { type: schemaInput };
|
33
33
|
}
|
34
34
|
else {
|
35
35
|
schema = { type: DataType.ANY };
|
@@ -43,15 +43,23 @@ function createRequestDataDecoratorWithSource(source) {
|
|
43
43
|
* @param source
|
44
44
|
*/
|
45
45
|
function createRequestDataPropertyDecoratorWithSource(source) {
|
46
|
-
return function (propertyKey,
|
47
|
-
const properties = {};
|
46
|
+
return function (propertyKey, schemaInput) {
|
48
47
|
const rootSchema = { type: DataType.OBJECT };
|
49
|
-
|
50
|
-
|
48
|
+
const properties = {};
|
49
|
+
let schemaOrFactory = rootSchema;
|
50
|
+
if (typeof schemaInput === 'function') {
|
51
|
+
schemaOrFactory = container => {
|
52
|
+
properties[propertyKey] = schemaInput(container);
|
53
|
+
rootSchema.properties = properties;
|
54
|
+
return rootSchema;
|
55
|
+
};
|
56
|
+
}
|
57
|
+
else if (typeof schemaInput === 'object') {
|
58
|
+
properties[propertyKey] = schemaInput;
|
51
59
|
rootSchema.properties = properties;
|
52
60
|
}
|
53
|
-
else if (typeof
|
54
|
-
properties[propertyKey] = { type:
|
61
|
+
else if (typeof schemaInput === 'string') {
|
62
|
+
properties[propertyKey] = { type: schemaInput };
|
55
63
|
rootSchema.properties = properties;
|
56
64
|
}
|
57
65
|
else {
|
@@ -60,7 +68,7 @@ function createRequestDataPropertyDecoratorWithSource(source) {
|
|
60
68
|
}
|
61
69
|
return requestData({
|
62
70
|
source: source,
|
63
|
-
schema:
|
71
|
+
schema: schemaOrFactory,
|
64
72
|
property: propertyKey,
|
65
73
|
});
|
66
74
|
};
|