@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
@@ -1,5 +1,4 @@
|
|
1
1
|
import {expect} from 'chai';
|
2
|
-
import {Service} from '@e22m4u/js-service';
|
3
2
|
import {DebuggableService} from './debuggable-service.js';
|
4
3
|
|
5
4
|
describe('DebuggableService', function () {
|
@@ -7,11 +6,4 @@ describe('DebuggableService', function () {
|
|
7
6
|
const res = new DebuggableService();
|
8
7
|
expect(typeof res.debug).to.be.eq('function');
|
9
8
|
});
|
10
|
-
|
11
|
-
describe('constructor', function () {
|
12
|
-
it('extends the Service class', function () {
|
13
|
-
const res = new DebuggableService();
|
14
|
-
expect(res).to.be.instanceof(Service);
|
15
|
-
});
|
16
|
-
});
|
17
9
|
});
|
@@ -1,40 +1,19 @@
|
|
1
|
-
import {Callable} from './types.js';
|
2
|
-
import {Debugger} from '@e22m4u/js-debug';
|
3
|
-
import {Service} from '@e22m4u/js-service';
|
4
|
-
import {toCamelCase} from './utils/index.js';
|
5
|
-
import {createDebugger} from '@e22m4u/js-debug';
|
6
1
|
import {ServiceContainer} from '@e22m4u/js-service';
|
2
|
+
import {DebuggableService as BaseDebuggableService} from '@e22m4u/js-service';
|
7
3
|
|
8
4
|
/**
|
9
|
-
*
|
5
|
+
* Base debuggable service.
|
10
6
|
*/
|
11
|
-
export class DebuggableService extends
|
12
|
-
/**
|
13
|
-
* Debug.
|
14
|
-
*/
|
15
|
-
debug: Debugger;
|
16
|
-
|
17
|
-
/**
|
18
|
-
* Возвращает функцию-отладчик с сегментом пространства имен
|
19
|
-
* указанного в параметре метода.
|
20
|
-
*
|
21
|
-
* @param method
|
22
|
-
* @protected
|
23
|
-
*/
|
24
|
-
protected getDebuggerFor(method: Callable) {
|
25
|
-
return this.debug.withHash().withNs(method.name);
|
26
|
-
}
|
27
|
-
|
7
|
+
export class DebuggableService extends BaseDebuggableService {
|
28
8
|
/**
|
29
9
|
* Constructor.
|
30
10
|
*
|
31
11
|
* @param container
|
32
12
|
*/
|
33
13
|
constructor(container?: ServiceContainer) {
|
34
|
-
super(container
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
debug('Service created.');
|
14
|
+
super(container, {
|
15
|
+
namespace: 'tsRestRouter',
|
16
|
+
noEnvironmentNamespace: true,
|
17
|
+
});
|
39
18
|
}
|
40
19
|
}
|
@@ -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
|
7
|
-
const
|
8
|
-
const
|
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:
|
16
|
-
const md2 = {hook: [
|
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:
|
37
|
-
const md2 = {hook: [
|
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:
|
51
|
-
const md2 = {hook: [
|
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:
|
73
|
-
const md2 = {hook: [
|
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:
|
91
|
-
const md2 = {hook:
|
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:
|
109
|
-
const md2 = {hook:
|
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('
|
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 (
|
46
|
-
let schema:
|
47
|
-
if (typeof
|
48
|
-
schema =
|
49
|
-
} else if (typeof
|
50
|
-
schema = {type:
|
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,
|
67
|
-
const properties = {} as NoUndef<DataSchema['properties']>;
|
68
|
+
return function (propertyKey: string, schemaInput?: DataSchemaInput) {
|
68
69
|
const rootSchema: DataSchema = {type: DataType.OBJECT};
|
69
|
-
|
70
|
-
|
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
|
73
|
-
properties[propertyKey] = {type:
|
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:
|
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 {
|
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?:
|
20
|
+
schema?: DataSchemaOrFactory;
|
21
21
|
property?: string;
|
22
22
|
};
|
23
23
|
|
@@ -22,7 +22,7 @@ describe('responseBody', function () {
|
|
22
22
|
expect(res.get('myMethod')).to.be.eql({schema: {type: DataType.STRING}});
|
23
23
|
});
|
24
24
|
|
25
|
-
it('sets the given
|
25
|
+
it('sets the given DataSchema to the target metadata', function () {
|
26
26
|
const schema = {
|
27
27
|
type: DataType.OBJECT,
|
28
28
|
properties: {
|
@@ -37,4 +37,20 @@ describe('responseBody', function () {
|
|
37
37
|
const res = ResponseBodyReflector.getMetadata(Target);
|
38
38
|
expect(res.get('myMethod')).to.be.eql({schema});
|
39
39
|
});
|
40
|
+
|
41
|
+
it('sets the given DataSchemaFactory to the target metadata', function () {
|
42
|
+
const factory = () => ({
|
43
|
+
type: DataType.OBJECT,
|
44
|
+
properties: {
|
45
|
+
foo: {type: DataType.STRING},
|
46
|
+
bar: {type: DataType.NUMBER},
|
47
|
+
},
|
48
|
+
});
|
49
|
+
class Target {
|
50
|
+
@responseBody(factory)
|
51
|
+
myMethod() {}
|
52
|
+
}
|
53
|
+
const res = ResponseBodyReflector.getMetadata(Target);
|
54
|
+
expect(res.get('myMethod')).to.be.eql({schema: factory});
|
55
|
+
});
|
40
56
|
});
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import {Prototype} from '../../types.js';
|
2
2
|
import {Constructor} from '../../types.js';
|
3
|
-
import {DataType} from '@e22m4u/ts-data-schema';
|
4
|
-
import {DataSchema} from '@e22m4u/ts-data-schema';
|
5
3
|
import {DecoratorTargetType} from '@e22m4u/ts-reflector';
|
4
|
+
import {DataSchemaInput} from '../../data-schema-types.js';
|
6
5
|
import {getDecoratorTargetType} from '@e22m4u/ts-reflector';
|
6
|
+
import {DataSchemaOrFactory} from '../../data-schema-types.js';
|
7
7
|
import {ResponseBodyReflector} from './response-body-reflector.js';
|
8
8
|
|
9
9
|
/**
|
@@ -11,9 +11,7 @@ import {ResponseBodyReflector} from './response-body-reflector.js';
|
|
11
11
|
*
|
12
12
|
* @param schemaOrType
|
13
13
|
*/
|
14
|
-
export function responseBody<T extends object>(
|
15
|
-
schemaOrType?: DataSchema | DataType,
|
16
|
-
) {
|
14
|
+
export function responseBody<T extends object>(schemaInput?: DataSchemaInput) {
|
17
15
|
return function (
|
18
16
|
target: Prototype<T>,
|
19
17
|
propertyKey: string,
|
@@ -28,14 +26,14 @@ export function responseBody<T extends object>(
|
|
28
26
|
throw new Error(
|
29
27
|
'@responseBody decorator is only supported on an instance method.',
|
30
28
|
);
|
31
|
-
let
|
32
|
-
if (typeof
|
33
|
-
|
34
|
-
} else if (typeof
|
35
|
-
|
29
|
+
let schemaOrFactory: DataSchemaOrFactory | undefined;
|
30
|
+
if (typeof schemaInput === 'function' || typeof schemaInput === 'object') {
|
31
|
+
schemaOrFactory = schemaInput;
|
32
|
+
} else if (typeof schemaInput === 'string') {
|
33
|
+
schemaOrFactory = {type: schemaInput};
|
36
34
|
}
|
37
35
|
ResponseBodyReflector.setMetadata(
|
38
|
-
|
36
|
+
schemaOrFactory ? {schema: schemaOrFactory} : {},
|
39
37
|
target.constructor as Constructor<T>,
|
40
38
|
propertyKey,
|
41
39
|
);
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import {MetadataKey} from '@e22m4u/ts-reflector';
|
2
|
-
import {
|
2
|
+
import {DataSchemaOrFactory} from '../../data-schema-types.js';
|
3
3
|
|
4
4
|
/**
|
5
5
|
* Response body metadata.
|
6
6
|
*/
|
7
7
|
export type ResponseBodyMetadata = {
|
8
|
-
schema?:
|
8
|
+
schema?: DataSchemaOrFactory;
|
9
9
|
};
|
10
10
|
|
11
11
|
/**
|
package/src/index.ts
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
export * from '
|
1
|
+
export * from '@e22m4u/js-trie-router';
|
2
|
+
|
2
3
|
export * from './rest-router.js';
|
3
4
|
export * from './errors/index.js';
|
4
5
|
export * from './decorators/index.js';
|
6
|
+
export * from './data-schema-types.js';
|
5
7
|
export * from './controller-registry.js';
|
package/src/utils/index.ts
CHANGED
@@ -1,10 +0,0 @@
|
|
1
|
-
import { Constructor } from '../types.js';
|
2
|
-
/**
|
3
|
-
* Create error.
|
4
|
-
*
|
5
|
-
* @param {Function} errorCtor
|
6
|
-
* @param {string} message
|
7
|
-
* @param {*[]|undefined} args
|
8
|
-
* @returns {object}
|
9
|
-
*/
|
10
|
-
export declare function createError<T>(errorCtor: Constructor<T>, message: string, ...args: unknown[]): T;
|
@@ -1,13 +0,0 @@
|
|
1
|
-
import { format } from '@e22m4u/js-format';
|
2
|
-
/**
|
3
|
-
* Create error.
|
4
|
-
*
|
5
|
-
* @param {Function} errorCtor
|
6
|
-
* @param {string} message
|
7
|
-
* @param {*[]|undefined} args
|
8
|
-
* @returns {object}
|
9
|
-
*/
|
10
|
-
export function createError(errorCtor, message, ...args) {
|
11
|
-
const interpolatedMessage = format(message, ...args);
|
12
|
-
return new errorCtor(interpolatedMessage);
|
13
|
-
}
|
@@ -1,8 +0,0 @@
|
|
1
|
-
import { expect } from 'chai';
|
2
|
-
import { createError } from './create-error.js';
|
3
|
-
describe('createError', function () {
|
4
|
-
it('interpolates the given message with arguments', function () {
|
5
|
-
const res = createError(Error, 'My %s', 'message');
|
6
|
-
expect(res.message).to.be.eq('My message');
|
7
|
-
});
|
8
|
-
});
|
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
@@ -1,10 +0,0 @@
|
|
1
|
-
import { expect } from 'chai';
|
2
|
-
import { toCamelCase } from './to-camel-case.js';
|
3
|
-
describe('toCamelCase', function () {
|
4
|
-
it('returns a camelCase string', function () {
|
5
|
-
expect(toCamelCase('TestString')).to.be.eq('testString');
|
6
|
-
expect(toCamelCase('test-string')).to.be.eq('testString');
|
7
|
-
expect(toCamelCase('test string')).to.be.eq('testString');
|
8
|
-
expect(toCamelCase('Test string')).to.be.eq('testString');
|
9
|
-
});
|
10
|
-
});
|
@@ -1,9 +0,0 @@
|
|
1
|
-
import {expect} from 'chai';
|
2
|
-
import {createError} from './create-error.js';
|
3
|
-
|
4
|
-
describe('createError', function () {
|
5
|
-
it('interpolates the given message with arguments', function () {
|
6
|
-
const res = createError(Error, 'My %s', 'message');
|
7
|
-
expect(res.message).to.be.eq('My message');
|
8
|
-
});
|
9
|
-
});
|
@@ -1,19 +0,0 @@
|
|
1
|
-
import {Constructor} from '../types.js';
|
2
|
-
import {format} from '@e22m4u/js-format';
|
3
|
-
|
4
|
-
/**
|
5
|
-
* Create error.
|
6
|
-
*
|
7
|
-
* @param {Function} errorCtor
|
8
|
-
* @param {string} message
|
9
|
-
* @param {*[]|undefined} args
|
10
|
-
* @returns {object}
|
11
|
-
*/
|
12
|
-
export function createError<T>(
|
13
|
-
errorCtor: Constructor<T>,
|
14
|
-
message: string,
|
15
|
-
...args: unknown[]
|
16
|
-
): T {
|
17
|
-
const interpolatedMessage = format(message, ...args);
|
18
|
-
return new errorCtor(interpolatedMessage);
|
19
|
-
}
|