@e22m4u/ts-rest-router 0.5.4 → 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.
- package/README.md +9 -0
- package/dist/cjs/index.cjs +36 -22
- package/dist/esm/controller-registry.js +11 -3
- package/dist/esm/data-schema-types.d.ts +15 -0
- package/dist/esm/data-schema-types.js +1 -0
- 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 +1 -0
- package/dist/esm/index.js +1 -0
- package/package.json +5 -5
- package/src/controller-registry.spec.ts +178 -5
- package/src/controller-registry.ts +11 -7
- package/src/data-schema-types.ts +18 -0
- 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 +1 -0
@@ -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
|
/**
|