@e22m4u/ts-rest-router 0.0.6 → 0.1.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-ru.md +227 -0
- package/README.md +227 -0
- package/dist/cjs/index.cjs +448 -153
- package/dist/esm/controller-registry.d.ts +53 -11
- package/dist/esm/controller-registry.js +242 -104
- package/dist/esm/debuggable-service.js +1 -1
- package/dist/esm/decorators/action/action-decorator.d.ts +7 -8
- package/dist/esm/decorators/action/action-decorator.js +1 -5
- package/dist/esm/decorators/action/action-decorator.spec.js +16 -16
- package/dist/esm/decorators/action/action-metadata.d.ts +0 -1
- package/dist/esm/decorators/after/after-decorator.d.ts +9 -0
- package/dist/esm/decorators/after/after-decorator.js +22 -0
- package/dist/esm/decorators/after/after-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/after/after-decorator.spec.js +115 -0
- package/dist/esm/decorators/after/after-metadata.d.ts +13 -0
- package/dist/esm/decorators/after/after-metadata.js +5 -0
- package/dist/esm/decorators/after/after-reflector.d.ts +22 -0
- package/dist/esm/decorators/after/after-reflector.js +29 -0
- package/dist/esm/decorators/after/after-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/after/after-reflector.spec.js +102 -0
- package/dist/esm/decorators/after/index.d.ts +3 -0
- package/dist/esm/decorators/after/index.js +3 -0
- package/dist/esm/decorators/before/before-decorator.d.ts +9 -0
- package/dist/esm/decorators/before/before-decorator.js +22 -0
- package/dist/esm/decorators/before/before-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/before/before-decorator.spec.js +115 -0
- package/dist/esm/decorators/before/before-metadata.d.ts +13 -0
- package/dist/esm/decorators/before/before-metadata.js +5 -0
- package/dist/esm/decorators/before/before-reflector.d.ts +22 -0
- package/dist/esm/decorators/before/before-reflector.js +29 -0
- package/dist/esm/decorators/before/before-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/before/before-reflector.spec.js +102 -0
- package/dist/esm/decorators/before/index.d.ts +3 -0
- package/dist/esm/decorators/before/index.js +3 -0
- package/dist/esm/decorators/controller/controller-decorator.d.ts +2 -1
- package/dist/esm/decorators/controller/controller-decorator.js +27 -6
- package/dist/esm/decorators/controller/controller-decorator.spec.js +37 -15
- package/dist/esm/decorators/controller/controller-metadata.d.ts +0 -1
- package/dist/esm/decorators/index.d.ts +2 -0
- package/dist/esm/decorators/index.js +2 -0
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +2 -3
- package/dist/esm/decorators/request-context/request-context-decorator.js +3 -6
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +2 -17
- package/dist/esm/decorators/request-context/request-context-metadata.d.ts +0 -1
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +7 -3
- package/dist/esm/decorators/request-data/request-data-decorator.js +16 -16
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +12 -10
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +0 -1
- package/dist/esm/utils/create-debugger.d.ts +35 -2
- package/dist/esm/utils/create-debugger.js +71 -5
- package/package.json +17 -17
- package/src/controller-registry.spec.ts +601 -275
- package/src/controller-registry.ts +263 -128
- package/src/debuggable-service.ts +1 -1
- package/src/decorators/action/action-decorator.spec.ts +16 -16
- package/src/decorators/action/action-decorator.ts +10 -12
- package/src/decorators/action/action-metadata.ts +0 -1
- package/src/decorators/after/after-decorator.spec.ts +92 -0
- package/src/decorators/after/after-decorator.ts +40 -0
- package/src/decorators/after/after-metadata.ts +17 -0
- package/src/decorators/after/after-reflector.spec.ts +107 -0
- package/src/decorators/after/after-reflector.ts +45 -0
- package/src/decorators/after/index.ts +3 -0
- package/src/decorators/before/before-decorator.spec.ts +92 -0
- package/src/decorators/before/before-decorator.ts +40 -0
- package/src/decorators/before/before-metadata.ts +17 -0
- package/src/decorators/before/before-reflector.spec.ts +111 -0
- package/src/decorators/before/before-reflector.ts +50 -0
- package/src/decorators/before/index.ts +3 -0
- package/src/decorators/controller/controller-decorator.spec.ts +33 -16
- package/src/decorators/controller/controller-decorator.ts +33 -6
- package/src/decorators/controller/controller-metadata.ts +0 -1
- package/src/decorators/index.ts +2 -0
- package/src/decorators/request-context/request-context-decorator.spec.ts +2 -15
- package/src/decorators/request-context/request-context-decorator.ts +3 -8
- package/src/decorators/request-context/request-context-metadata.ts +0 -1
- package/src/decorators/request-data/request-data-decorator.spec.ts +12 -11
- package/src/decorators/request-data/request-data-decorator.ts +41 -16
- package/src/decorators/request-data/request-data-metadata.ts +0 -1
- package/src/utils/create-debugger.ts +84 -7
@@ -0,0 +1,102 @@
|
|
1
|
+
import { expect } from 'chai';
|
2
|
+
import { describe } from 'mocha';
|
3
|
+
import { Reflector } from '@e22m4u/ts-reflector';
|
4
|
+
import { BeforeReflector } from './before-reflector.js';
|
5
|
+
import { BEFORE_METADATA_KEY } from './before-metadata.js';
|
6
|
+
const MIDDLEWARE_1 = () => undefined;
|
7
|
+
const MIDDLEWARE_2 = () => undefined;
|
8
|
+
const MIDDLEWARE_3 = () => undefined;
|
9
|
+
describe('BeforeReflector', function () {
|
10
|
+
describe('class target', function () {
|
11
|
+
describe('addMetadata', function () {
|
12
|
+
it('adds a given value to the target metadata', function () {
|
13
|
+
class Target {
|
14
|
+
}
|
15
|
+
const md1 = { middleware: MIDDLEWARE_1 };
|
16
|
+
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
17
|
+
BeforeReflector.addMetadata(md1, Target);
|
18
|
+
BeforeReflector.addMetadata(md2, Target);
|
19
|
+
const res = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target);
|
20
|
+
expect(res).to.be.eql([md2, md1]);
|
21
|
+
});
|
22
|
+
});
|
23
|
+
describe('getMetadata', function () {
|
24
|
+
it('returns an empty array if no metadata', function () {
|
25
|
+
class Target {
|
26
|
+
}
|
27
|
+
const res = BeforeReflector.getMetadata(Target);
|
28
|
+
expect(res).to.be.eql([]);
|
29
|
+
});
|
30
|
+
it('returns existing metadata by the target', function () {
|
31
|
+
class Target {
|
32
|
+
}
|
33
|
+
const md1 = { middleware: MIDDLEWARE_1 };
|
34
|
+
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
35
|
+
const mdArray = [md1, md2];
|
36
|
+
Reflector.defineMetadata(BEFORE_METADATA_KEY, mdArray, Target);
|
37
|
+
const res = BeforeReflector.getMetadata(Target);
|
38
|
+
expect(res).to.be.eql(mdArray);
|
39
|
+
});
|
40
|
+
});
|
41
|
+
});
|
42
|
+
describe('method target', function () {
|
43
|
+
describe('addMetadata', function () {
|
44
|
+
it('adds a given value to the target metadata', function () {
|
45
|
+
class Target {
|
46
|
+
}
|
47
|
+
const md1 = { middleware: MIDDLEWARE_1 };
|
48
|
+
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
49
|
+
BeforeReflector.addMetadata(md1, Target, 'prop');
|
50
|
+
BeforeReflector.addMetadata(md2, Target, 'prop');
|
51
|
+
const res = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target, 'prop');
|
52
|
+
expect(res).to.be.eql([md2, md1]);
|
53
|
+
});
|
54
|
+
});
|
55
|
+
describe('getMetadata', function () {
|
56
|
+
it('returns an empty array if no metadata', function () {
|
57
|
+
class Target {
|
58
|
+
}
|
59
|
+
const res = BeforeReflector.getMetadata(Target, 'prop');
|
60
|
+
expect(res).to.be.eql([]);
|
61
|
+
});
|
62
|
+
it('returns existing metadata by the target', function () {
|
63
|
+
class Target {
|
64
|
+
}
|
65
|
+
const md1 = { middleware: MIDDLEWARE_1 };
|
66
|
+
const md2 = { middleware: [MIDDLEWARE_2, MIDDLEWARE_3] };
|
67
|
+
const mdArray = [md1, md2];
|
68
|
+
Reflector.defineMetadata(BEFORE_METADATA_KEY, mdArray, Target, 'prop');
|
69
|
+
const res = BeforeReflector.getMetadata(Target, 'prop');
|
70
|
+
expect(res).to.be.eql(mdArray);
|
71
|
+
});
|
72
|
+
});
|
73
|
+
});
|
74
|
+
describe('addMetadata', function () {
|
75
|
+
it('can distinguish class and method metadata', function () {
|
76
|
+
class Target {
|
77
|
+
}
|
78
|
+
const md1 = { middleware: MIDDLEWARE_1 };
|
79
|
+
const md2 = { middleware: MIDDLEWARE_2 };
|
80
|
+
BeforeReflector.addMetadata(md1, Target);
|
81
|
+
BeforeReflector.addMetadata(md2, Target, 'prop');
|
82
|
+
const res1 = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target);
|
83
|
+
const res2 = Reflector.getOwnMetadata(BEFORE_METADATA_KEY, Target, 'prop');
|
84
|
+
expect(res1).to.be.eql([md1]);
|
85
|
+
expect(res2).to.be.eql([md2]);
|
86
|
+
});
|
87
|
+
});
|
88
|
+
describe('getMetadata', function () {
|
89
|
+
it('can distinguish class and method metadata', function () {
|
90
|
+
class Target {
|
91
|
+
}
|
92
|
+
const md1 = { middleware: MIDDLEWARE_1 };
|
93
|
+
const md2 = { middleware: MIDDLEWARE_2 };
|
94
|
+
Reflector.defineMetadata(BEFORE_METADATA_KEY, [md1], Target);
|
95
|
+
Reflector.defineMetadata(BEFORE_METADATA_KEY, [md2], Target, 'prop');
|
96
|
+
const res1 = BeforeReflector.getMetadata(Target);
|
97
|
+
const res2 = BeforeReflector.getMetadata(Target, 'prop');
|
98
|
+
expect(res1).to.be.eql([md1]);
|
99
|
+
expect(res2).to.be.eql([md2]);
|
100
|
+
});
|
101
|
+
});
|
102
|
+
});
|
@@ -8,6 +8,7 @@ export type ControllerOptions = Flatten<Omit<ControllerMetadata, 'className'>>;
|
|
8
8
|
/**
|
9
9
|
* Controller decorator.
|
10
10
|
*
|
11
|
+
* @param pathOrOptions
|
11
12
|
* @param options
|
12
13
|
*/
|
13
|
-
export declare function controller<T extends object>(options?: ControllerOptions): (target: Constructor<T>) => void;
|
14
|
+
export declare function controller<T extends object>(pathOrOptions?: string | ControllerOptions, options?: ControllerOptions): (target: Constructor<T>) => void;
|
@@ -4,17 +4,38 @@ import { ControllerReflector } from './controller-reflector.js';
|
|
4
4
|
/**
|
5
5
|
* Controller decorator.
|
6
6
|
*
|
7
|
+
* @param pathOrOptions
|
7
8
|
* @param options
|
8
9
|
*/
|
9
|
-
export function controller(options) {
|
10
|
+
export function controller(pathOrOptions, options) {
|
10
11
|
return function (target) {
|
11
12
|
const decoratorType = getDecoratorTargetType(target);
|
12
13
|
if (decoratorType !== DecoratorTargetType.CONSTRUCTOR)
|
13
14
|
throw new Error('@controller decorator is only supported on a class.');
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
// если первый аргумент является строкой,
|
16
|
+
// то значение используется в качестве
|
17
|
+
// базового пути контроллера
|
18
|
+
if (typeof pathOrOptions === 'string') {
|
19
|
+
// если второй аргумент не определен,
|
20
|
+
// то создается новый объект опций,
|
21
|
+
// который включает базовый путь
|
22
|
+
if (!options) {
|
23
|
+
options = { path: pathOrOptions };
|
24
|
+
}
|
25
|
+
// если второй аргумент определен,
|
26
|
+
// то базовый путь из первого аргумента
|
27
|
+
// передается в объект опций декоратора
|
28
|
+
else {
|
29
|
+
options.path = pathOrOptions;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
// если первый аргумент является объектом,
|
33
|
+
// то его значение используется в качестве
|
34
|
+
// объекта опций декоратора, а второй
|
35
|
+
// аргумент игнорируется
|
36
|
+
else if (typeof pathOrOptions === 'object') {
|
37
|
+
options = pathOrOptions;
|
38
|
+
}
|
39
|
+
ControllerReflector.setMetadata({ ...options, className: target.name }, target);
|
19
40
|
};
|
20
41
|
}
|
@@ -8,7 +8,7 @@ import { expect } from 'chai';
|
|
8
8
|
import { controller } from './controller-decorator.js';
|
9
9
|
import { ControllerReflector } from './controller-reflector.js';
|
10
10
|
describe('controller', function () {
|
11
|
-
it('does not
|
11
|
+
it('does not require options', function () {
|
12
12
|
let Target = class Target {
|
13
13
|
};
|
14
14
|
Target = __decorate([
|
@@ -18,27 +18,21 @@ describe('controller', function () {
|
|
18
18
|
expect(res).to.be.eql({ className: 'Target' });
|
19
19
|
});
|
20
20
|
it('sets given options to the target metadata', function () {
|
21
|
-
const
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
const options = {
|
22
|
+
path: 'myPath',
|
23
|
+
before: () => undefined,
|
24
|
+
after: () => undefined,
|
25
|
+
extraOption: 'extraOption',
|
26
|
+
};
|
25
27
|
let Target = class Target {
|
26
28
|
};
|
27
29
|
Target = __decorate([
|
28
|
-
controller(
|
29
|
-
path,
|
30
|
-
before,
|
31
|
-
after,
|
32
|
-
extraOption,
|
33
|
-
})
|
30
|
+
controller(options)
|
34
31
|
], Target);
|
35
32
|
const res = ControllerReflector.getMetadata(Target);
|
36
33
|
expect(res).to.be.eql({
|
34
|
+
...options,
|
37
35
|
className: 'Target',
|
38
|
-
path,
|
39
|
-
before,
|
40
|
-
after,
|
41
|
-
extraOption,
|
42
36
|
});
|
43
37
|
});
|
44
38
|
it('overrides given "className" option by the target class name', function () {
|
@@ -50,4 +44,32 @@ describe('controller', function () {
|
|
50
44
|
const res = ControllerReflector.getMetadata(Target);
|
51
45
|
expect(res).to.be.eql({ className: 'Target' });
|
52
46
|
});
|
47
|
+
it('allows to pass the path option to the first parameter', function () {
|
48
|
+
let Target = class Target {
|
49
|
+
};
|
50
|
+
Target = __decorate([
|
51
|
+
controller('myPath')
|
52
|
+
], Target);
|
53
|
+
const res = ControllerReflector.getMetadata(Target);
|
54
|
+
expect(res).to.be.eql({ className: 'Target', path: 'myPath' });
|
55
|
+
});
|
56
|
+
it('merges two given arguments in the target metadata', function () {
|
57
|
+
const before = () => undefined;
|
58
|
+
let Target = class Target {
|
59
|
+
};
|
60
|
+
Target = __decorate([
|
61
|
+
controller('myPath', { before })
|
62
|
+
], Target);
|
63
|
+
const res = ControllerReflector.getMetadata(Target);
|
64
|
+
expect(res).to.be.eql({ className: 'Target', path: 'myPath', before });
|
65
|
+
});
|
66
|
+
it('overrides the path option by the first argument', function () {
|
67
|
+
let Target = class Target {
|
68
|
+
};
|
69
|
+
Target = __decorate([
|
70
|
+
controller('myPath1', { path: 'myPath2' })
|
71
|
+
], Target);
|
72
|
+
const res = ControllerReflector.getMetadata(Target);
|
73
|
+
expect(res).to.be.eql({ className: 'Target', path: 'myPath1' });
|
74
|
+
});
|
53
75
|
});
|
@@ -1,12 +1,11 @@
|
|
1
1
|
import { Prototype } from '../../types.js';
|
2
2
|
import { RequestContext } from '@e22m4u/js-trie-router';
|
3
|
-
import { RequestContextMetadata } from './request-context-metadata.js';
|
4
3
|
/**
|
5
4
|
* Request context decorator.
|
6
5
|
*
|
7
|
-
* @param
|
6
|
+
* @param propertyName
|
8
7
|
*/
|
9
|
-
export declare function requestContext<T extends object>(
|
8
|
+
export declare function requestContext<T extends object>(propertyName?: keyof RequestContext): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
|
10
9
|
/**
|
11
10
|
* Request decorator.
|
12
11
|
*/
|
@@ -4,18 +4,15 @@ import { RequestContextReflector } from './request-context-reflector.js';
|
|
4
4
|
/**
|
5
5
|
* Request context decorator.
|
6
6
|
*
|
7
|
-
* @param
|
7
|
+
* @param propertyName
|
8
8
|
*/
|
9
|
-
export function requestContext(
|
9
|
+
export function requestContext(propertyName) {
|
10
10
|
return function (target, propertyKey, indexOrDescriptor) {
|
11
11
|
const decoratorType = getDecoratorTargetType(target, propertyKey, indexOrDescriptor);
|
12
12
|
if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
|
13
13
|
throw new Error('@requestContext decorator is only supported ' +
|
14
14
|
'on an instance method parameter.');
|
15
|
-
|
16
|
-
? { property: propertyOrMetadata }
|
17
|
-
: propertyOrMetadata;
|
18
|
-
RequestContextReflector.setMetadata(metadata, target.constructor, indexOrDescriptor, propertyKey);
|
15
|
+
RequestContextReflector.setMetadata({ property: propertyName }, target.constructor, indexOrDescriptor, propertyKey);
|
19
16
|
};
|
20
17
|
}
|
21
18
|
/**
|
@@ -15,7 +15,7 @@ import { expect } from 'chai';
|
|
15
15
|
import { requestContext } from './request-context-decorator.js';
|
16
16
|
import { RequestContextReflector } from './request-context-reflector.js';
|
17
17
|
describe('requestContext', function () {
|
18
|
-
it('does not
|
18
|
+
it('does not require options', function () {
|
19
19
|
class Target {
|
20
20
|
method(prop) { }
|
21
21
|
}
|
@@ -28,7 +28,7 @@ describe('requestContext', function () {
|
|
28
28
|
const res = RequestContextReflector.getMetadata(Target, 'method');
|
29
29
|
expect(res.get(0)).to.be.eql({ property: undefined });
|
30
30
|
});
|
31
|
-
it('sets a given
|
31
|
+
it('sets a given property to the target metadata', function () {
|
32
32
|
class Target {
|
33
33
|
method(prop) { }
|
34
34
|
}
|
@@ -41,19 +41,4 @@ describe('requestContext', function () {
|
|
41
41
|
const res = RequestContextReflector.getMetadata(Target, 'method');
|
42
42
|
expect(res.get(0)).to.be.eql({ property: 'res' });
|
43
43
|
});
|
44
|
-
it('sets a given RequestContextMetadata as the target metadata', function () {
|
45
|
-
const property = 'res';
|
46
|
-
const customOption = 'customOption';
|
47
|
-
class Target {
|
48
|
-
method(prop) { }
|
49
|
-
}
|
50
|
-
__decorate([
|
51
|
-
__param(0, requestContext({ property, customOption })),
|
52
|
-
__metadata("design:type", Function),
|
53
|
-
__metadata("design:paramtypes", [Object]),
|
54
|
-
__metadata("design:returntype", void 0)
|
55
|
-
], Target.prototype, "method", null);
|
56
|
-
const res = RequestContextReflector.getMetadata(Target, 'method');
|
57
|
-
expect(res.get(0)).to.be.eql({ property, customOption });
|
58
|
-
});
|
59
44
|
});
|
@@ -2,12 +2,16 @@ import { Prototype } from '../../types.js';
|
|
2
2
|
import { DataType } from '@e22m4u/ts-data-schema';
|
3
3
|
import { DataSchema } from '@e22m4u/ts-data-schema';
|
4
4
|
import { RequestDataMetadata } from './request-data-metadata.js';
|
5
|
+
/**
|
6
|
+
* Request data options.
|
7
|
+
*/
|
8
|
+
export type RequestDataOptions = RequestDataMetadata;
|
5
9
|
/**
|
6
10
|
* Request data decorator.
|
7
11
|
*
|
8
|
-
* @param
|
12
|
+
* @param options
|
9
13
|
*/
|
10
|
-
export declare function requestData<T extends object>(
|
14
|
+
export declare function requestData<T extends object>(options: RequestDataOptions): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
|
11
15
|
/**
|
12
16
|
* Decorator aliases.
|
13
17
|
*/
|
@@ -19,7 +23,7 @@ export declare const headers: () => (target: Prototype<object>, propertyKey: str
|
|
19
23
|
export declare const header: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
20
24
|
export declare const cookies: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
21
25
|
export declare const cookie: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
22
|
-
export declare const
|
26
|
+
export declare const bodyProp: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
23
27
|
/**
|
24
28
|
* Request body decorator.
|
25
29
|
*
|
@@ -6,34 +6,34 @@ import { RequestDataReflector } from './request-data-reflector.js';
|
|
6
6
|
/**
|
7
7
|
* Request data decorator.
|
8
8
|
*
|
9
|
-
* @param
|
9
|
+
* @param options
|
10
10
|
*/
|
11
|
-
export function requestData(
|
11
|
+
export function requestData(options) {
|
12
12
|
return function (target, propertyKey, indexOrDescriptor) {
|
13
13
|
const decoratorType = getDecoratorTargetType(target, propertyKey, indexOrDescriptor);
|
14
14
|
if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
|
15
15
|
throw new Error('@requestData decorator is only supported ' +
|
16
16
|
'on an instance method parameter.');
|
17
|
-
RequestDataReflector.setMetadata(
|
17
|
+
RequestDataReflector.setMetadata(options, target.constructor, indexOrDescriptor, propertyKey);
|
18
18
|
};
|
19
19
|
}
|
20
20
|
/**
|
21
|
-
* Create data decorator.
|
21
|
+
* Create request data decorator with source.
|
22
22
|
*
|
23
23
|
* @param source
|
24
24
|
*/
|
25
|
-
function
|
25
|
+
function createRequestDataDecoratorWithSource(source) {
|
26
26
|
return function () {
|
27
27
|
const schema = { type: DataType.OBJECT };
|
28
28
|
return requestData({ schema, source });
|
29
29
|
};
|
30
30
|
}
|
31
31
|
/**
|
32
|
-
* Create property decorator.
|
32
|
+
* Create request data property decorator with source.
|
33
33
|
*
|
34
34
|
* @param source
|
35
35
|
*/
|
36
|
-
function
|
36
|
+
function createRequestDataPropertyDecoratorWithSource(source) {
|
37
37
|
return function (propertyKey, schemaOrType) {
|
38
38
|
const properties = {};
|
39
39
|
const rootSchema = { type: DataType.OBJECT };
|
@@ -55,15 +55,15 @@ function createPropertyDecorator(source) {
|
|
55
55
|
/**
|
56
56
|
* Decorator aliases.
|
57
57
|
*/
|
58
|
-
export const params =
|
59
|
-
export const param =
|
60
|
-
export const queries =
|
61
|
-
export const query =
|
62
|
-
export const headers =
|
63
|
-
export const header =
|
64
|
-
export const cookies =
|
65
|
-
export const cookie =
|
66
|
-
export const
|
58
|
+
export const params = createRequestDataDecoratorWithSource(RequestDataSource.PARAMS);
|
59
|
+
export const param = createRequestDataPropertyDecoratorWithSource(RequestDataSource.PARAMS);
|
60
|
+
export const queries = createRequestDataDecoratorWithSource(RequestDataSource.QUERY);
|
61
|
+
export const query = createRequestDataPropertyDecoratorWithSource(RequestDataSource.QUERY);
|
62
|
+
export const headers = createRequestDataDecoratorWithSource(RequestDataSource.HEADERS);
|
63
|
+
export const header = createRequestDataPropertyDecoratorWithSource(RequestDataSource.HEADERS);
|
64
|
+
export const cookies = createRequestDataDecoratorWithSource(RequestDataSource.COOKIE);
|
65
|
+
export const cookie = createRequestDataPropertyDecoratorWithSource(RequestDataSource.COOKIE);
|
66
|
+
export const bodyProp = createRequestDataPropertyDecoratorWithSource(RequestDataSource.BODY);
|
67
67
|
/**
|
68
68
|
* Request body decorator.
|
69
69
|
*
|
@@ -22,27 +22,29 @@ import { params } from './request-data-decorator.js';
|
|
22
22
|
import { queries } from './request-data-decorator.js';
|
23
23
|
import { cookies } from './request-data-decorator.js';
|
24
24
|
import { headers } from './request-data-decorator.js';
|
25
|
-
import {
|
25
|
+
import { bodyProp } from './request-data-decorator.js';
|
26
26
|
import { requestData } from './request-data-decorator.js';
|
27
27
|
import { RequestDataSource } from './request-data-metadata.js';
|
28
28
|
import { RequestDataReflector } from './request-data-reflector.js';
|
29
29
|
describe('requestData', function () {
|
30
|
-
it('sets
|
31
|
-
const
|
32
|
-
source: RequestDataSource.
|
30
|
+
it('sets given options to the target metadata', function () {
|
31
|
+
const options = {
|
32
|
+
source: RequestDataSource.BODY,
|
33
|
+
schema: { type: DataType.STRING },
|
34
|
+
property: 'prop',
|
33
35
|
customOption: 'myOption',
|
34
36
|
};
|
35
37
|
class Target {
|
36
38
|
myMethod(prop) { }
|
37
39
|
}
|
38
40
|
__decorate([
|
39
|
-
__param(0, requestData(
|
41
|
+
__param(0, requestData(options)),
|
40
42
|
__metadata("design:type", Function),
|
41
43
|
__metadata("design:paramtypes", [Object]),
|
42
44
|
__metadata("design:returntype", void 0)
|
43
45
|
], Target.prototype, "myMethod", null);
|
44
46
|
const res = RequestDataReflector.getMetadata(Target, 'myMethod');
|
45
|
-
expect(res.get(0)).to.be.eql(
|
47
|
+
expect(res.get(0)).to.be.eql(options);
|
46
48
|
});
|
47
49
|
describe('request data by a given source', function () {
|
48
50
|
describe('params', function () {
|
@@ -458,13 +460,13 @@ describe('requestData', function () {
|
|
458
460
|
});
|
459
461
|
});
|
460
462
|
});
|
461
|
-
describe('
|
463
|
+
describe('bodyProp', function () {
|
462
464
|
it('sets a given "propertyKey" to the target metadata', function () {
|
463
465
|
class Target {
|
464
466
|
myMethod(prop) { }
|
465
467
|
}
|
466
468
|
__decorate([
|
467
|
-
__param(0,
|
469
|
+
__param(0, bodyProp('myPropertyKey')),
|
468
470
|
__metadata("design:type", Function),
|
469
471
|
__metadata("design:paramtypes", [Object]),
|
470
472
|
__metadata("design:returntype", void 0)
|
@@ -483,7 +485,7 @@ describe('requestData', function () {
|
|
483
485
|
myMethod(prop) { }
|
484
486
|
}
|
485
487
|
__decorate([
|
486
|
-
__param(0,
|
488
|
+
__param(0, bodyProp(propertyKey, propertyType)),
|
487
489
|
__metadata("design:type", Function),
|
488
490
|
__metadata("design:paramtypes", [Object]),
|
489
491
|
__metadata("design:returntype", void 0)
|
@@ -512,7 +514,7 @@ describe('requestData', function () {
|
|
512
514
|
myMethod(prop) { }
|
513
515
|
}
|
514
516
|
__decorate([
|
515
|
-
__param(0,
|
517
|
+
__param(0, bodyProp(propertyKey, schema)),
|
516
518
|
__metadata("design:type", Function),
|
517
519
|
__metadata("design:paramtypes", [Object]),
|
518
520
|
__metadata("design:returntype", void 0)
|
@@ -1,11 +1,44 @@
|
|
1
|
-
import { format } from '@e22m4u/js-format';
|
2
1
|
/**
|
3
2
|
* Debugger.
|
4
3
|
*/
|
5
|
-
export type Debugger = (...args:
|
4
|
+
export type Debugger = (messageOrData: string | unknown, ...args: any[]) => void;
|
6
5
|
/**
|
7
6
|
* Create debugger.
|
8
7
|
*
|
8
|
+
* Base usage:
|
9
|
+
* ```ts
|
10
|
+
* const debug = createDebugger('myService');
|
11
|
+
* debug('Service created.');
|
12
|
+
* // ujut:myService Service created.
|
13
|
+
* ```
|
14
|
+
*
|
15
|
+
* Nested namespaces:
|
16
|
+
* ```ts
|
17
|
+
* const debug1 = debug.bind('namespace1');
|
18
|
+
* const debug2 = debug.bind('namespace2');
|
19
|
+
* debug1('Application started'); // ujut:myService [namespace1] Application started
|
20
|
+
* debug2('Connection established'); // ujut:myService [namespace2] Connection established
|
21
|
+
* ```
|
22
|
+
*
|
23
|
+
* Value inspection:
|
24
|
+
* ```ts
|
25
|
+
* debug({foo: 'lorem', bar: 'ipsum'})
|
26
|
+
* // ujut:myService {
|
27
|
+
* // ujut:myService "foo": "lorem",
|
28
|
+
* // ujut:myService "bar": "ipsum"
|
29
|
+
* // ujut:myService }
|
30
|
+
* ```
|
31
|
+
*
|
32
|
+
* Titled inspection output:
|
33
|
+
* ```ts
|
34
|
+
* debug({foo: 'lorem', bar: 'ipsum'}, 'My awesome output:')
|
35
|
+
* // ujut:myService My awesome output:
|
36
|
+
* // ujut:myService {
|
37
|
+
* // ujut:myService "foo": "lorem",
|
38
|
+
* // ujut:myService "bar": "ipsum"
|
39
|
+
* // ujut:myService }
|
40
|
+
* ```
|
41
|
+
*
|
9
42
|
* @param name
|
10
43
|
*/
|
11
44
|
export declare function createDebugger(name: string): Debugger;
|