@e22m4u/ts-rest-router 0.5.5 → 0.6.0

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.
Files changed (27) hide show
  1. package/dist/cjs/index.cjs +36 -22
  2. package/dist/esm/controller-registry.js +11 -3
  3. package/dist/esm/data-schema-types.d.ts +15 -0
  4. package/dist/esm/data-schema-types.js +1 -0
  5. package/dist/esm/decorators/before-action/before-action-reflector.spec.js +15 -15
  6. package/dist/esm/decorators/request-data/request-data-decorator.d.ts +11 -12
  7. package/dist/esm/decorators/request-data/request-data-decorator.js +20 -12
  8. package/dist/esm/decorators/request-data/request-data-decorator.spec.js +183 -1
  9. package/dist/esm/decorators/request-data/request-data-metadata.d.ts +2 -2
  10. package/dist/esm/decorators/response-body/response-body-decorator.d.ts +2 -3
  11. package/dist/esm/decorators/response-body/response-body-decorator.js +7 -7
  12. package/dist/esm/decorators/response-body/response-body-decorator.spec.js +21 -1
  13. package/dist/esm/decorators/response-body/response-body-metadata.d.ts +2 -2
  14. package/dist/esm/index.d.ts +1 -0
  15. package/dist/esm/index.js +1 -0
  16. package/package.json +1 -1
  17. package/src/controller-registry.spec.ts +174 -1
  18. package/src/controller-registry.ts +11 -7
  19. package/src/data-schema-types.ts +18 -0
  20. package/src/decorators/before-action/before-action-reflector.spec.ts +15 -15
  21. package/src/decorators/request-data/request-data-decorator.spec.ts +174 -1
  22. package/src/decorators/request-data/request-data-decorator.ts +22 -13
  23. package/src/decorators/request-data/request-data-metadata.ts +2 -2
  24. package/src/decorators/response-body/response-body-decorator.spec.ts +17 -1
  25. package/src/decorators/response-body/response-body-decorator.ts +9 -11
  26. package/src/decorators/response-body/response-body-metadata.ts +2 -2
  27. package/src/index.ts +1 -0
@@ -2,4 +2,5 @@ export * from './utils/index.js';
2
2
  export * from './rest-router.js';
3
3
  export * from './errors/index.js';
4
4
  export * from './decorators/index.js';
5
+ export * from './data-schema-types.js';
5
6
  export * from './controller-registry.js';
package/dist/esm/index.js CHANGED
@@ -2,4 +2,5 @@ export * from './utils/index.js';
2
2
  export * from './rest-router.js';
3
3
  export * from './errors/index.js';
4
4
  export * from './decorators/index.js';
5
+ export * from './data-schema-types.js';
5
6
  export * from './controller-registry.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/ts-rest-router",
3
- "version": "0.5.5",
3
+ "version": "0.6.0",
4
4
  "description": "Декларативный REST-маршрутизатор на основе контроллеров для TypeScript",
5
5
  "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
6
6
  "license": "MIT",
@@ -33,9 +33,11 @@ import {
33
33
  } from './decorators/index.js';
34
34
 
35
35
  import {expect} from 'chai';
36
+ import {Service} from '@e22m4u/js-service';
36
37
  import {DataType} from '@e22m4u/ts-data-schema';
38
+ import {ServiceContainer} from '@e22m4u/js-service';
39
+ import {DataSchemaFactory} from './data-schema-types.js';
37
40
  import {ControllerRegistry} from './controller-registry.js';
38
- import {Service, ServiceContainer} from '@e22m4u/js-service';
39
41
 
40
42
  const PRE_HANDLER_1 = () => undefined;
41
43
  const PRE_HANDLER_2 = () => undefined;
@@ -1039,5 +1041,176 @@ describe('ControllerRegistry', function () {
1039
1041
  await handler(ctx1);
1040
1042
  expect(counter).to.be.eq(1);
1041
1043
  });
1044
+
1045
+ describe('data schema', function () {
1046
+ describe('@requestBody', function () {
1047
+ describe('DataType', function () {
1048
+ it('should cast request data by the specified type', async function () {
1049
+ let invoked = 0;
1050
+ class MyController {
1051
+ myAction(
1052
+ @requestBody(DataType.NUMBER)
1053
+ body: number,
1054
+ ) {
1055
+ expect(body).to.be.eq(10);
1056
+ invoked++;
1057
+ }
1058
+ }
1059
+ const req = createRequestMock();
1060
+ const res = createResponseMock();
1061
+ const S = new ControllerRegistry();
1062
+ const ctx = new RequestContext(S.container, req, res);
1063
+ ctx.body = '10';
1064
+ const handler = S['createRouteHandler'](MyController, 'myAction');
1065
+ await handler(ctx);
1066
+ expect(invoked).to.be.eq(1);
1067
+ });
1068
+ });
1069
+
1070
+ describe('DataSchema', function () {
1071
+ it('should apply default value to request data', async function () {
1072
+ let invoked = 0;
1073
+ class MyController {
1074
+ myAction(
1075
+ @requestBody({
1076
+ type: DataType.STRING,
1077
+ default: 'OK',
1078
+ })
1079
+ body: string,
1080
+ ) {
1081
+ expect(body).to.be.eq('OK');
1082
+ invoked++;
1083
+ }
1084
+ }
1085
+ const req = createRequestMock();
1086
+ const res = createResponseMock();
1087
+ const S = new ControllerRegistry();
1088
+ const ctx = new RequestContext(S.container, req, res);
1089
+ const handler = S['createRouteHandler'](MyController, 'myAction');
1090
+ await handler(ctx);
1091
+ expect(invoked).to.be.eq(1);
1092
+ });
1093
+
1094
+ it('should cast request data by the specified type', async function () {
1095
+ let invoked = 0;
1096
+ class MyController {
1097
+ myAction(
1098
+ @requestBody({type: DataType.NUMBER})
1099
+ body: number,
1100
+ ) {
1101
+ expect(body).to.be.eq(10);
1102
+ invoked++;
1103
+ }
1104
+ }
1105
+ const req = createRequestMock();
1106
+ const res = createResponseMock();
1107
+ const S = new ControllerRegistry();
1108
+ const ctx = new RequestContext(S.container, req, res);
1109
+ ctx.body = '10';
1110
+ const handler = S['createRouteHandler'](MyController, 'myAction');
1111
+ await handler(ctx);
1112
+ expect(invoked).to.be.eq(1);
1113
+ });
1114
+
1115
+ it('should validate request data by the given schema', async function () {
1116
+ class MyController {
1117
+ myAction(
1118
+ @requestBody({
1119
+ type: DataType.OBJECT,
1120
+ required: true,
1121
+ })
1122
+ body: object,
1123
+ ) {
1124
+ throw new Error('Must not to be invoked');
1125
+ return body;
1126
+ }
1127
+ }
1128
+
1129
+ const req = createRequestMock();
1130
+ const res = createResponseMock();
1131
+ const S = new ControllerRegistry();
1132
+ const ctx = new RequestContext(S.container, req, res);
1133
+ const handler = S['createRouteHandler'](MyController, 'myAction');
1134
+ const throwable = () => handler(ctx);
1135
+ expect(throwable).to.throw(/is required, but undefined was given/);
1136
+ });
1137
+ });
1138
+
1139
+ describe('DataSchemaFactory', function () {
1140
+ it('should apply default value to request data', async function () {
1141
+ let invoked = 0;
1142
+ const S = new ControllerRegistry();
1143
+ const factory: DataSchemaFactory = sc => {
1144
+ expect(sc).to.be.eq(S.container);
1145
+ return {type: DataType.STRING, default: 'OK'};
1146
+ };
1147
+ class MyController {
1148
+ myAction(
1149
+ @requestBody(factory)
1150
+ body: string,
1151
+ ) {
1152
+ expect(body).to.be.eq('OK');
1153
+ invoked++;
1154
+ }
1155
+ }
1156
+ const req = createRequestMock();
1157
+ const res = createResponseMock();
1158
+ const ctx = new RequestContext(S.container, req, res);
1159
+ const handler = S['createRouteHandler'](MyController, 'myAction');
1160
+ await handler(ctx);
1161
+ expect(invoked).to.be.eq(1);
1162
+ });
1163
+
1164
+ it('should cast request data by the specified type', async function () {
1165
+ let invoked = 0;
1166
+ const S = new ControllerRegistry();
1167
+ const factory: DataSchemaFactory = sc => {
1168
+ expect(sc).to.be.eq(S.container);
1169
+ return {type: DataType.NUMBER};
1170
+ };
1171
+ class MyController {
1172
+ myAction(
1173
+ @requestBody(factory)
1174
+ body: number,
1175
+ ) {
1176
+ expect(body).to.be.eq(10);
1177
+ invoked++;
1178
+ }
1179
+ }
1180
+ const req = createRequestMock();
1181
+ const res = createResponseMock();
1182
+ const ctx = new RequestContext(S.container, req, res);
1183
+ ctx.body = '10';
1184
+ const handler = S['createRouteHandler'](MyController, 'myAction');
1185
+ await handler(ctx);
1186
+ expect(invoked).to.be.eq(1);
1187
+ });
1188
+
1189
+ it('should validate request data by the given schema', async function () {
1190
+ const S = new ControllerRegistry();
1191
+ const factory: DataSchemaFactory = sc => {
1192
+ expect(sc).to.be.eq(S.container);
1193
+ return {type: DataType.OBJECT, required: true};
1194
+ };
1195
+ class MyController {
1196
+ myAction(
1197
+ @requestBody(factory)
1198
+ body: object,
1199
+ ) {
1200
+ throw new Error('Must not to be invoked');
1201
+ return body;
1202
+ }
1203
+ }
1204
+
1205
+ const req = createRequestMock();
1206
+ const res = createResponseMock();
1207
+ const ctx = new RequestContext(S.container, req, res);
1208
+ const handler = S['createRouteHandler'](MyController, 'myAction');
1209
+ const throwable = () => handler(ctx);
1210
+ expect(throwable).to.throw(/is required, but undefined was given/);
1211
+ });
1212
+ });
1213
+ });
1214
+ });
1042
1215
  });
1043
1216
  });
@@ -2,6 +2,7 @@ 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/ts-data-schema';
5
6
  import {RouteHandler} from '@e22m4u/js-trie-router';
6
7
  import {DataValidator} from '@e22m4u/ts-data-schema';
7
8
  import {DataTypeCaster} from '@e22m4u/ts-data-schema';
@@ -461,22 +462,25 @@ export class ControllerRegistry extends DebuggableService {
461
462
  // по умолчанию, выполняется конвертация входящего
462
463
  // значения и валидация согласно схеме
463
464
  if (requestDataMd.schema) {
465
+ let dataSchema: DataSchema;
466
+ if (typeof requestDataMd.schema === 'function') {
467
+ dataSchema = requestDataMd.schema(this.container);
468
+ debug('Data schema extracted from factory function.');
469
+ } else {
470
+ dataSchema = requestDataMd.schema;
471
+ }
464
472
  data = defaultsApplier.applyDefaultValuesIfNeeded(
465
473
  data,
466
- requestDataMd.schema,
474
+ dataSchema,
467
475
  requestDataMd.source,
468
476
  );
469
477
  debug('Default values applied.');
470
- data = dataTypeCaster.cast(data, requestDataMd.schema, {
478
+ data = dataTypeCaster.cast(data, dataSchema, {
471
479
  noTypeCastError: true,
472
480
  sourcePath: requestDataMd.source,
473
481
  });
474
482
  debug('Data type casting applied.');
475
- dataValidator.validate(
476
- data,
477
- requestDataMd.schema,
478
- requestDataMd.source,
479
- );
483
+ dataValidator.validate(data, dataSchema, requestDataMd.source);
480
484
  debug('Data validation passed.');
481
485
  }
482
486
  // если свойство данных не определено,
@@ -0,0 +1,18 @@
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
+ /**
6
+ * Data schema factory.
7
+ */
8
+ export type DataSchemaFactory = (container: ServiceContainer) => DataSchema;
9
+
10
+ /**
11
+ * Data schema or factory.
12
+ */
13
+ export type DataSchemaOrFactory = DataSchema | DataSchemaFactory;
14
+
15
+ /**
16
+ * Data schema input.
17
+ */
18
+ export type DataSchemaInput = DataSchemaOrFactory | DataType;
@@ -3,17 +3,17 @@ 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
5
 
6
- const hook_1 = () => undefined;
7
- const hook_2 = () => undefined;
8
- const hook_3 = () => undefined;
6
+ const HOOK_1 = () => undefined;
7
+ const HOOK_2 = () => undefined;
8
+ const HOOK_3 = () => undefined;
9
9
 
10
10
  describe('BeforeActionReflector', function () {
11
11
  describe('class target', function () {
12
12
  describe('addMetadata', function () {
13
13
  it('adds a given value to the target metadata', function () {
14
14
  class Target {}
15
- const md1 = {hook: hook_1};
16
- const md2 = {hook: [hook_2, hook_3]};
15
+ const md1 = {hook: HOOK_1};
16
+ const md2 = {hook: [HOOK_2, HOOK_3]};
17
17
  BeforeActionReflector.addMetadata(md1, Target);
18
18
  BeforeActionReflector.addMetadata(md2, Target);
19
19
  const res = Reflector.getOwnMetadata(
@@ -33,8 +33,8 @@ describe('BeforeActionReflector', function () {
33
33
 
34
34
  it('returns existing metadata by the target', function () {
35
35
  class Target {}
36
- const md1 = {hook: hook_1};
37
- const md2 = {hook: [hook_2, hook_3]};
36
+ const md1 = {hook: HOOK_1};
37
+ const md2 = {hook: [HOOK_2, HOOK_3]};
38
38
  const mdArray = [md1, md2];
39
39
  Reflector.defineMetadata(BEFORE_ACTION_METADATA_KEY, mdArray, Target);
40
40
  const res = BeforeActionReflector.getMetadata(Target);
@@ -47,8 +47,8 @@ describe('BeforeActionReflector', function () {
47
47
  describe('addMetadata', function () {
48
48
  it('adds a given value to the target metadata', function () {
49
49
  class Target {}
50
- const md1 = {hook: hook_1};
51
- const md2 = {hook: [hook_2, hook_3]};
50
+ const md1 = {hook: HOOK_1};
51
+ const md2 = {hook: [HOOK_2, HOOK_3]};
52
52
  BeforeActionReflector.addMetadata(md1, Target, 'prop');
53
53
  BeforeActionReflector.addMetadata(md2, Target, 'prop');
54
54
  const res = Reflector.getOwnMetadata(
@@ -69,8 +69,8 @@ describe('BeforeActionReflector', function () {
69
69
 
70
70
  it('returns existing metadata by the target', function () {
71
71
  class Target {}
72
- const md1 = {hook: hook_1};
73
- const md2 = {hook: [hook_2, hook_3]};
72
+ const md1 = {hook: HOOK_1};
73
+ const md2 = {hook: [HOOK_2, HOOK_3]};
74
74
  const mdArray = [md1, md2];
75
75
  Reflector.defineMetadata(
76
76
  BEFORE_ACTION_METADATA_KEY,
@@ -87,8 +87,8 @@ describe('BeforeActionReflector', function () {
87
87
  describe('addMetadata', function () {
88
88
  it('can distinguish class and method metadata', function () {
89
89
  class Target {}
90
- const md1 = {hook: hook_1};
91
- const md2 = {hook: hook_2};
90
+ const md1 = {hook: HOOK_1};
91
+ const md2 = {hook: HOOK_2};
92
92
  BeforeActionReflector.addMetadata(md1, Target);
93
93
  BeforeActionReflector.addMetadata(md2, Target, 'prop');
94
94
  const res1 = Reflector.getOwnMetadata(BEFORE_ACTION_METADATA_KEY, Target);
@@ -105,8 +105,8 @@ describe('BeforeActionReflector', function () {
105
105
  describe('getMetadata', function () {
106
106
  it('can distinguish class and method metadata', function () {
107
107
  class Target {}
108
- const md1 = {hook: hook_1};
109
- const md2 = {hook: hook_2};
108
+ const md1 = {hook: HOOK_1};
109
+ const md2 = {hook: HOOK_2};
110
110
  Reflector.defineMetadata(BEFORE_ACTION_METADATA_KEY, [md1], Target);
111
111
  Reflector.defineMetadata(
112
112
  BEFORE_ACTION_METADATA_KEY,
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
2
  import {expect} from 'chai';
3
3
  import {DataType} from '@e22m4u/ts-data-schema';
4
+ import {ServiceContainer} from '@e22m4u/js-service';
4
5
  import {requestData} from './request-data-decorator.js';
5
6
  import {requestBody} from './request-data-decorator.js';
6
7
  import {requestField} from './request-data-decorator.js';
@@ -13,6 +14,8 @@ import {requestQueries} from './request-data-decorator.js';
13
14
  import {requestHeaders} from './request-data-decorator.js';
14
15
  import {requestCookies} from './request-data-decorator.js';
15
16
  import {RequestDataSource} from './request-data-metadata.js';
17
+ import {DataSchemaFactory} from '../../data-schema-types.js';
18
+ import {RequestDataMetadata} from './request-data-metadata.js';
16
19
  import {RequestDataReflector} from './request-data-reflector.js';
17
20
 
18
21
  describe('requestData', function () {
@@ -140,7 +143,7 @@ describe('requestData', function () {
140
143
  });
141
144
  });
142
145
 
143
- it('set a given DataSchema to the target metadata', function () {
146
+ it('sets a given DataSchema to the target metadata', function () {
144
147
  const schema = {type: DataType.STRING, required: true};
145
148
  class Target {
146
149
  myMethod(
@@ -154,6 +157,21 @@ describe('requestData', function () {
154
157
  schema,
155
158
  });
156
159
  });
160
+
161
+ it('sets a given DataSchemaFactory to the target metadata', function () {
162
+ const factory = () => ({type: DataType.STRING, required: true});
163
+ class Target {
164
+ myMethod(
165
+ @requestBody(factory)
166
+ prop: unknown,
167
+ ) {}
168
+ }
169
+ const res = RequestDataReflector.getMetadata(Target, 'myMethod');
170
+ expect(res.get(0)).to.be.eql({
171
+ source: RequestDataSource.BODY,
172
+ schema: factory,
173
+ });
174
+ });
157
175
  });
158
176
  });
159
177
 
@@ -230,6 +248,37 @@ describe('requestData', function () {
230
248
  property: propertyKey,
231
249
  });
232
250
  });
251
+
252
+ it('sets a given DataSchemaFactory to the target metadata', function () {
253
+ const container = {} as ServiceContainer;
254
+ const factory: DataSchemaFactory = sc => {
255
+ expect(sc).to.be.eq(container);
256
+ return {type: DataType.STRING, required: true};
257
+ };
258
+ const propertyKey = 'myPropertyKey';
259
+ class Target {
260
+ myMethod(
261
+ @requestParam(propertyKey, factory)
262
+ prop: unknown,
263
+ ) {}
264
+ }
265
+ const mdMap = RequestDataReflector.getMetadata(Target, 'myMethod');
266
+ const md = mdMap.get(0) as RequestDataMetadata;
267
+ expect(md.source).to.be.eq(RequestDataSource.PARAMS);
268
+ expect(md.schema).to.be.a('function');
269
+ expect(md.property).to.be.eq(propertyKey);
270
+ const res1 = md.schema as DataSchemaFactory;
271
+ const res2 = res1(container);
272
+ expect(res2).to.be.eql({
273
+ type: DataType.OBJECT,
274
+ properties: {
275
+ [propertyKey]: {
276
+ type: DataType.STRING,
277
+ required: true,
278
+ },
279
+ },
280
+ });
281
+ });
233
282
  });
234
283
 
235
284
  describe('query', function () {
@@ -304,6 +353,37 @@ describe('requestData', function () {
304
353
  property: propertyKey,
305
354
  });
306
355
  });
356
+
357
+ it('sets a given DataSchemaFactory to the target metadata', function () {
358
+ const container = {} as ServiceContainer;
359
+ const factory: DataSchemaFactory = sc => {
360
+ expect(sc).to.be.eq(container);
361
+ return {type: DataType.STRING, required: true};
362
+ };
363
+ const propertyKey = 'myPropertyKey';
364
+ class Target {
365
+ myMethod(
366
+ @requestQuery(propertyKey, factory)
367
+ prop: unknown,
368
+ ) {}
369
+ }
370
+ const mdMap = RequestDataReflector.getMetadata(Target, 'myMethod');
371
+ const md = mdMap.get(0) as RequestDataMetadata;
372
+ expect(md.source).to.be.eq(RequestDataSource.QUERY);
373
+ expect(md.schema).to.be.a('function');
374
+ expect(md.property).to.be.eq(propertyKey);
375
+ const res1 = md.schema as DataSchemaFactory;
376
+ const res2 = res1(container);
377
+ expect(res2).to.be.eql({
378
+ type: DataType.OBJECT,
379
+ properties: {
380
+ [propertyKey]: {
381
+ type: DataType.STRING,
382
+ required: true,
383
+ },
384
+ },
385
+ });
386
+ });
307
387
  });
308
388
 
309
389
  describe('header', function () {
@@ -378,6 +458,37 @@ describe('requestData', function () {
378
458
  property: propertyKey,
379
459
  });
380
460
  });
461
+
462
+ it('sets a given DataSchemaFactory to the target metadata', function () {
463
+ const container = {} as ServiceContainer;
464
+ const factory: DataSchemaFactory = sc => {
465
+ expect(sc).to.be.eq(container);
466
+ return {type: DataType.STRING, required: true};
467
+ };
468
+ const propertyKey = 'myPropertyKey';
469
+ class Target {
470
+ myMethod(
471
+ @requestHeader(propertyKey, factory)
472
+ prop: unknown,
473
+ ) {}
474
+ }
475
+ const mdMap = RequestDataReflector.getMetadata(Target, 'myMethod');
476
+ const md = mdMap.get(0) as RequestDataMetadata;
477
+ expect(md.source).to.be.eq(RequestDataSource.HEADERS);
478
+ expect(md.schema).to.be.a('function');
479
+ expect(md.property).to.be.eq(propertyKey);
480
+ const res1 = md.schema as DataSchemaFactory;
481
+ const res2 = res1(container);
482
+ expect(res2).to.be.eql({
483
+ type: DataType.OBJECT,
484
+ properties: {
485
+ [propertyKey]: {
486
+ type: DataType.STRING,
487
+ required: true,
488
+ },
489
+ },
490
+ });
491
+ });
381
492
  });
382
493
 
383
494
  describe('cookie', function () {
@@ -452,6 +563,37 @@ describe('requestData', function () {
452
563
  property: propertyKey,
453
564
  });
454
565
  });
566
+
567
+ it('sets a given DataSchemaFactory to the target metadata', function () {
568
+ const container = {} as ServiceContainer;
569
+ const factory: DataSchemaFactory = sc => {
570
+ expect(sc).to.be.eq(container);
571
+ return {type: DataType.STRING, required: true};
572
+ };
573
+ const propertyKey = 'myPropertyKey';
574
+ class Target {
575
+ myMethod(
576
+ @requestCookie(propertyKey, factory)
577
+ prop: unknown,
578
+ ) {}
579
+ }
580
+ const mdMap = RequestDataReflector.getMetadata(Target, 'myMethod');
581
+ const md = mdMap.get(0) as RequestDataMetadata;
582
+ expect(md.source).to.be.eq(RequestDataSource.COOKIE);
583
+ expect(md.schema).to.be.a('function');
584
+ expect(md.property).to.be.eq(propertyKey);
585
+ const res1 = md.schema as DataSchemaFactory;
586
+ const res2 = res1(container);
587
+ expect(res2).to.be.eql({
588
+ type: DataType.OBJECT,
589
+ properties: {
590
+ [propertyKey]: {
591
+ type: DataType.STRING,
592
+ required: true,
593
+ },
594
+ },
595
+ });
596
+ });
455
597
  });
456
598
 
457
599
  describe('field', function () {
@@ -526,6 +668,37 @@ describe('requestData', function () {
526
668
  property: propertyKey,
527
669
  });
528
670
  });
671
+
672
+ it('sets a given DataSchemaFactory to the target metadata', function () {
673
+ const container = {} as ServiceContainer;
674
+ const factory: DataSchemaFactory = sc => {
675
+ expect(sc).to.be.eq(container);
676
+ return {type: DataType.STRING, required: true};
677
+ };
678
+ const propertyKey = 'myPropertyKey';
679
+ class Target {
680
+ myMethod(
681
+ @requestField(propertyKey, factory)
682
+ prop: unknown,
683
+ ) {}
684
+ }
685
+ const mdMap = RequestDataReflector.getMetadata(Target, 'myMethod');
686
+ const md = mdMap.get(0) as RequestDataMetadata;
687
+ expect(md.source).to.be.eq(RequestDataSource.BODY);
688
+ expect(md.schema).to.be.a('function');
689
+ expect(md.property).to.be.eq(propertyKey);
690
+ const res1 = md.schema as DataSchemaFactory;
691
+ const res2 = res1(container);
692
+ expect(res2).to.be.eql({
693
+ type: DataType.OBJECT,
694
+ properties: {
695
+ [propertyKey]: {
696
+ type: DataType.STRING,
697
+ required: true,
698
+ },
699
+ },
700
+ });
701
+ });
529
702
  });
530
703
  });
531
704
  });
@@ -4,9 +4,11 @@ import {Constructor} from '../../types.js';
4
4
  import {DataType} from '@e22m4u/ts-data-schema';
5
5
  import {DataSchema} from '@e22m4u/ts-data-schema';
6
6
  import {DecoratorTargetType} from '@e22m4u/ts-reflector';
7
+ import {DataSchemaInput} from '../../data-schema-types.js';
7
8
  import {getDecoratorTargetType} from '@e22m4u/ts-reflector';
8
9
  import {RequestDataSource} from './request-data-metadata.js';
9
10
  import {RequestDataMetadata} from './request-data-metadata.js';
11
+ import {DataSchemaOrFactory} from '../../data-schema-types.js';
10
12
  import {RequestDataReflector} from './request-data-reflector.js';
11
13
 
12
14
  /**
@@ -42,12 +44,12 @@ export function requestData<T extends object>(options: RequestDataOptions) {
42
44
  * @param source
43
45
  */
44
46
  function createRequestDataDecoratorWithSource(source: RequestDataSource) {
45
- return function (schemaOrType?: DataSchema | DataType) {
46
- let schema: DataSchema;
47
- if (typeof schemaOrType === 'object') {
48
- schema = schemaOrType;
49
- } else if (typeof schemaOrType === 'string') {
50
- schema = {type: schemaOrType};
47
+ return function (schemaInput?: DataSchemaInput) {
48
+ let schema: DataSchemaOrFactory;
49
+ if (typeof schemaInput === 'function' || typeof schemaInput === 'object') {
50
+ schema = schemaInput;
51
+ } else if (typeof schemaInput === 'string') {
52
+ schema = {type: schemaInput};
51
53
  } else {
52
54
  schema = {type: DataType.ANY};
53
55
  }
@@ -63,14 +65,21 @@ function createRequestDataDecoratorWithSource(source: RequestDataSource) {
63
65
  function createRequestDataPropertyDecoratorWithSource(
64
66
  source: RequestDataSource,
65
67
  ) {
66
- return function (propertyKey: string, schemaOrType?: DataSchema | DataType) {
67
- const properties = {} as NoUndef<DataSchema['properties']>;
68
+ return function (propertyKey: string, schemaInput?: DataSchemaInput) {
68
69
  const rootSchema: DataSchema = {type: DataType.OBJECT};
69
- if (typeof schemaOrType === 'object') {
70
- properties[propertyKey] = schemaOrType;
70
+ const properties = {} as NoUndef<DataSchema['properties']>;
71
+ let schemaOrFactory: DataSchemaOrFactory = rootSchema;
72
+ if (typeof schemaInput === 'function') {
73
+ schemaOrFactory = container => {
74
+ properties[propertyKey] = schemaInput(container);
75
+ rootSchema.properties = properties;
76
+ return rootSchema;
77
+ };
78
+ } else if (typeof schemaInput === 'object') {
79
+ properties[propertyKey] = schemaInput;
71
80
  rootSchema.properties = properties;
72
- } else if (typeof schemaOrType === 'string') {
73
- properties[propertyKey] = {type: schemaOrType};
81
+ } else if (typeof schemaInput === 'string') {
82
+ properties[propertyKey] = {type: schemaInput};
74
83
  rootSchema.properties = properties;
75
84
  } else {
76
85
  properties[propertyKey] = {type: DataType.ANY};
@@ -78,7 +87,7 @@ function createRequestDataPropertyDecoratorWithSource(
78
87
  }
79
88
  return requestData({
80
89
  source: source,
81
- schema: rootSchema,
90
+ schema: schemaOrFactory,
82
91
  property: propertyKey,
83
92
  });
84
93
  };
@@ -1,5 +1,5 @@
1
1
  import {MetadataKey} from '@e22m4u/ts-reflector';
2
- import {DataSchema} from '@e22m4u/ts-data-schema';
2
+ import {DataSchemaOrFactory} from '../../data-schema-types.js';
3
3
 
4
4
  /**
5
5
  * Request data source.
@@ -17,7 +17,7 @@ export enum RequestDataSource {
17
17
  */
18
18
  export type RequestDataMetadata = {
19
19
  source: RequestDataSource;
20
- schema?: DataSchema;
20
+ schema?: DataSchemaOrFactory;
21
21
  property?: string;
22
22
  };
23
23