@e22m4u/ts-rest-router 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/.c8rc +9 -0
- package/.commitlintrc +5 -0
- package/.editorconfig +13 -0
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +6 -0
- package/.mocharc.json +5 -0
- package/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/README-ru.md +41 -0
- package/README.md +41 -0
- package/build-cjs.js +16 -0
- package/dist/cjs/index.cjs +692 -0
- package/dist/esm/controller-registry.d.ts +65 -0
- package/dist/esm/controller-registry.js +281 -0
- package/dist/esm/controller-registry.spec.d.ts +1 -0
- package/dist/esm/controller-registry.spec.js +719 -0
- package/dist/esm/debuggable-service.d.ts +18 -0
- package/dist/esm/debuggable-service.js +23 -0
- package/dist/esm/debuggable-service.spec.d.ts +1 -0
- package/dist/esm/debuggable-service.spec.js +16 -0
- package/dist/esm/decorators/action/action-decorator.d.ts +53 -0
- package/dist/esm/decorators/action/action-decorator.js +66 -0
- package/dist/esm/decorators/action/action-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/action/action-decorator.spec.js +59 -0
- package/dist/esm/decorators/action/action-metadata.d.ts +23 -0
- package/dist/esm/decorators/action/action-metadata.js +5 -0
- package/dist/esm/decorators/action/action-reflector.d.ts +22 -0
- package/dist/esm/decorators/action/action-reflector.js +29 -0
- package/dist/esm/decorators/action/action-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/action/action-reflector.spec.js +84 -0
- package/dist/esm/decorators/action/index.d.ts +3 -0
- package/dist/esm/decorators/action/index.js +3 -0
- package/dist/esm/decorators/controller/controller-decorator.d.ts +13 -0
- package/dist/esm/decorators/controller/controller-decorator.js +20 -0
- package/dist/esm/decorators/controller/controller-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/controller/controller-decorator.spec.js +53 -0
- package/dist/esm/decorators/controller/controller-metadata.d.ts +17 -0
- package/dist/esm/decorators/controller/controller-metadata.js +5 -0
- package/dist/esm/decorators/controller/controller-reflector.d.ts +20 -0
- package/dist/esm/decorators/controller/controller-reflector.js +24 -0
- package/dist/esm/decorators/controller/controller-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/controller/controller-reflector.spec.js +45 -0
- package/dist/esm/decorators/controller/index.d.ts +3 -0
- package/dist/esm/decorators/controller/index.js +3 -0
- package/dist/esm/decorators/index.d.ts +4 -0
- package/dist/esm/decorators/index.js +4 -0
- package/dist/esm/decorators/request-context/index.d.ts +3 -0
- package/dist/esm/decorators/request-context/index.js +3 -0
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +17 -0
- package/dist/esm/decorators/request-context/request-context-decorator.js +32 -0
- package/dist/esm/decorators/request-context/request-context-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +59 -0
- package/dist/esm/decorators/request-context/request-context-metadata.d.ts +17 -0
- package/dist/esm/decorators/request-context/request-context-metadata.js +5 -0
- package/dist/esm/decorators/request-context/request-context-reflector.d.ts +24 -0
- package/dist/esm/decorators/request-context/request-context-reflector.js +31 -0
- package/dist/esm/decorators/request-context/request-context-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/request-context/request-context-reflector.spec.js +59 -0
- package/dist/esm/decorators/request-data/index.d.ts +3 -0
- package/dist/esm/decorators/request-data/index.js +3 -0
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +28 -0
- package/dist/esm/decorators/request-data/request-data-decorator.js +84 -0
- package/dist/esm/decorators/request-data/request-data-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +534 -0
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +29 -0
- package/dist/esm/decorators/request-data/request-data-metadata.js +16 -0
- package/dist/esm/decorators/request-data/request-data-reflector.d.ts +24 -0
- package/dist/esm/decorators/request-data/request-data-reflector.js +31 -0
- package/dist/esm/decorators/request-data/request-data-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/request-data/request-data-reflector.spec.js +60 -0
- package/dist/esm/errors/index.d.ts +1 -0
- package/dist/esm/errors/index.js +1 -0
- package/dist/esm/errors/not-a-controller-error.d.ts +12 -0
- package/dist/esm/errors/not-a-controller-error.js +14 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/rest-router.d.ts +19 -0
- package/dist/esm/rest-router.js +24 -0
- package/dist/esm/types.d.ts +57 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/utils/capitalize.d.ts +6 -0
- package/dist/esm/utils/capitalize.js +8 -0
- package/dist/esm/utils/capitalize.spec.d.ts +1 -0
- package/dist/esm/utils/capitalize.spec.js +8 -0
- package/dist/esm/utils/create-debugger.d.ts +11 -0
- package/dist/esm/utils/create-debugger.js +15 -0
- package/dist/esm/utils/create-debugger.spec.d.ts +1 -0
- package/dist/esm/utils/create-debugger.spec.js +8 -0
- package/dist/esm/utils/create-error.d.ts +10 -0
- package/dist/esm/utils/create-error.js +13 -0
- package/dist/esm/utils/create-error.spec.d.ts +1 -0
- package/dist/esm/utils/create-error.spec.js +8 -0
- package/dist/esm/utils/index.d.ts +4 -0
- package/dist/esm/utils/index.js +4 -0
- package/dist/esm/utils/to-camel-case.d.ts +6 -0
- package/dist/esm/utils/to-camel-case.js +11 -0
- package/dist/esm/utils/to-camel-case.spec.d.ts +1 -0
- package/dist/esm/utils/to-camel-case.spec.js +10 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/eslint.config.js +43 -0
- package/package.json +74 -0
- package/src/controller-registry.spec.ts +592 -0
- package/src/controller-registry.ts +355 -0
- package/src/debuggable-service.spec.ts +18 -0
- package/src/debuggable-service.ts +27 -0
- package/src/decorators/action/action-decorator.spec.ts +42 -0
- package/src/decorators/action/action-decorator.ts +100 -0
- package/src/decorators/action/action-metadata.ts +28 -0
- package/src/decorators/action/action-reflector.spec.ts +84 -0
- package/src/decorators/action/action-reflector.ts +38 -0
- package/src/decorators/action/index.ts +3 -0
- package/src/decorators/controller/controller-decorator.spec.ts +41 -0
- package/src/decorators/controller/controller-decorator.ts +29 -0
- package/src/decorators/controller/controller-metadata.ts +21 -0
- package/src/decorators/controller/controller-reflector.spec.ts +45 -0
- package/src/decorators/controller/controller-reflector.ts +28 -0
- package/src/decorators/controller/index.ts +3 -0
- package/src/decorators/index.ts +4 -0
- package/src/decorators/request-context/index.ts +3 -0
- package/src/decorators/request-context/request-context-decorator.spec.ts +41 -0
- package/src/decorators/request-context/request-context-decorator.ts +57 -0
- package/src/decorators/request-context/request-context-metadata.ts +21 -0
- package/src/decorators/request-context/request-context-reflector.spec.ts +77 -0
- package/src/decorators/request-context/request-context-reflector.ts +57 -0
- package/src/decorators/request-data/index.ts +3 -0
- package/src/decorators/request-data/request-data-decorator.spec.ts +477 -0
- package/src/decorators/request-data/request-data-decorator.ts +106 -0
- package/src/decorators/request-data/request-data-metadata.ts +34 -0
- package/src/decorators/request-data/request-data-reflector.spec.ts +78 -0
- package/src/decorators/request-data/request-data-reflector.ts +57 -0
- package/src/errors/index.ts +1 -0
- package/src/errors/not-a-controller-error.ts +15 -0
- package/src/index.ts +5 -0
- package/src/rest-router.ts +31 -0
- package/src/types.ts +59 -0
- package/src/utils/capitalize.spec.ts +9 -0
- package/src/utils/capitalize.ts +8 -0
- package/src/utils/create-debugger.spec.ts +9 -0
- package/src/utils/create-debugger.ts +21 -0
- package/src/utils/create-error.spec.ts +9 -0
- package/src/utils/create-error.ts +19 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/to-camel-case.spec.ts +11 -0
- package/src/utils/to-camel-case.ts +11 -0
- package/tsconfig.json +17 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
import { Prototype } from '../../types.js';
|
2
|
+
import { RequestContext } from '@e22m4u/js-trie-router';
|
3
|
+
import { RequestContextMetadata } from './request-context-metadata.js';
|
4
|
+
/**
|
5
|
+
* Request context decorator.
|
6
|
+
*
|
7
|
+
* @param propertyOrMetadata
|
8
|
+
*/
|
9
|
+
export declare function requestContext<T extends object>(propertyOrMetadata?: keyof RequestContext | RequestContextMetadata): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
|
10
|
+
/**
|
11
|
+
* Request decorator.
|
12
|
+
*/
|
13
|
+
export declare function request(): (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
14
|
+
/**
|
15
|
+
* Response decorator.
|
16
|
+
*/
|
17
|
+
export declare function response(): (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import { DecoratorTargetType } from '@e22m4u/ts-reflector';
|
2
|
+
import { getDecoratorTargetType } from '@e22m4u/ts-reflector';
|
3
|
+
import { RequestContextReflector } from './request-context-reflector.js';
|
4
|
+
/**
|
5
|
+
* Request context decorator.
|
6
|
+
*
|
7
|
+
* @param propertyOrMetadata
|
8
|
+
*/
|
9
|
+
export function requestContext(propertyOrMetadata) {
|
10
|
+
return function (target, propertyKey, indexOrDescriptor) {
|
11
|
+
const decoratorType = getDecoratorTargetType(target, propertyKey, indexOrDescriptor);
|
12
|
+
if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
|
13
|
+
throw new Error('@requestContext decorator is only supported ' +
|
14
|
+
'on an instance method parameter.');
|
15
|
+
const metadata = typeof propertyOrMetadata !== 'object'
|
16
|
+
? { property: propertyOrMetadata }
|
17
|
+
: propertyOrMetadata;
|
18
|
+
RequestContextReflector.setMetadata(metadata, target.constructor, indexOrDescriptor, propertyKey);
|
19
|
+
};
|
20
|
+
}
|
21
|
+
/**
|
22
|
+
* Request decorator.
|
23
|
+
*/
|
24
|
+
export function request() {
|
25
|
+
return requestContext('req');
|
26
|
+
}
|
27
|
+
/**
|
28
|
+
* Response decorator.
|
29
|
+
*/
|
30
|
+
export function response() {
|
31
|
+
return requestContext('res');
|
32
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,59 @@
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
6
|
+
};
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
9
|
+
};
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
12
|
+
};
|
13
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
14
|
+
import { expect } from 'chai';
|
15
|
+
import { requestContext } from './request-context-decorator.js';
|
16
|
+
import { RequestContextReflector } from './request-context-reflector.js';
|
17
|
+
describe('requestContext', function () {
|
18
|
+
it('does not requires options', function () {
|
19
|
+
class Target {
|
20
|
+
method(prop) { }
|
21
|
+
}
|
22
|
+
__decorate([
|
23
|
+
__param(0, requestContext()),
|
24
|
+
__metadata("design:type", Function),
|
25
|
+
__metadata("design:paramtypes", [Object]),
|
26
|
+
__metadata("design:returntype", void 0)
|
27
|
+
], Target.prototype, "method", null);
|
28
|
+
const res = RequestContextReflector.getMetadata(Target, 'method');
|
29
|
+
expect(res.get(0)).to.be.eql({ property: undefined });
|
30
|
+
});
|
31
|
+
it('sets a given key of RequestContext as "propertyKey" of the target metadata', function () {
|
32
|
+
class Target {
|
33
|
+
method(prop) { }
|
34
|
+
}
|
35
|
+
__decorate([
|
36
|
+
__param(0, requestContext('res')),
|
37
|
+
__metadata("design:type", Function),
|
38
|
+
__metadata("design:paramtypes", [Object]),
|
39
|
+
__metadata("design:returntype", void 0)
|
40
|
+
], Target.prototype, "method", null);
|
41
|
+
const res = RequestContextReflector.getMetadata(Target, 'method');
|
42
|
+
expect(res.get(0)).to.be.eql({ property: 'res' });
|
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
|
+
});
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { MetadataKey } from '@e22m4u/ts-reflector';
|
2
|
+
import { RequestContext } from '@e22m4u/js-trie-router';
|
3
|
+
/**
|
4
|
+
* Request context metadata.
|
5
|
+
*/
|
6
|
+
export type RequestContextMetadata = {
|
7
|
+
property?: keyof RequestContext;
|
8
|
+
[option: string]: unknown | undefined;
|
9
|
+
};
|
10
|
+
/**
|
11
|
+
* Request context metadata map.
|
12
|
+
*/
|
13
|
+
export type RequestContextMetadataMap = Map<number, RequestContextMetadata>;
|
14
|
+
/**
|
15
|
+
* Request context metadata key.
|
16
|
+
*/
|
17
|
+
export declare const REQUEST_CONTEXT_METADATA_KEY: MetadataKey<RequestContextMetadataMap>;
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { Constructor } from '../../types.js';
|
2
|
+
import { RequestContextMetadata } from './request-context-metadata.js';
|
3
|
+
import { RequestContextMetadataMap } from './request-context-metadata.js';
|
4
|
+
/**
|
5
|
+
* Request context reflector.
|
6
|
+
*/
|
7
|
+
export declare class RequestContextReflector {
|
8
|
+
/**
|
9
|
+
* Set metadata.
|
10
|
+
*
|
11
|
+
* @param metadata
|
12
|
+
* @param target
|
13
|
+
* @param index
|
14
|
+
* @param propertyKey
|
15
|
+
*/
|
16
|
+
static setMetadata(metadata: RequestContextMetadata, target: Constructor, index: number, propertyKey: string): void;
|
17
|
+
/**
|
18
|
+
* Get metadata.
|
19
|
+
*
|
20
|
+
* @param target
|
21
|
+
* @param propertyKey
|
22
|
+
*/
|
23
|
+
static getMetadata(target: Constructor, propertyKey: string): RequestContextMetadataMap;
|
24
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { Reflector } from '@e22m4u/ts-reflector';
|
2
|
+
import { REQUEST_CONTEXT_METADATA_KEY } from './request-context-metadata.js';
|
3
|
+
/**
|
4
|
+
* Request context reflector.
|
5
|
+
*/
|
6
|
+
export class RequestContextReflector {
|
7
|
+
/**
|
8
|
+
* Set metadata.
|
9
|
+
*
|
10
|
+
* @param metadata
|
11
|
+
* @param target
|
12
|
+
* @param index
|
13
|
+
* @param propertyKey
|
14
|
+
*/
|
15
|
+
static setMetadata(metadata, target, index, propertyKey) {
|
16
|
+
const oldMap = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, target, propertyKey);
|
17
|
+
const newMap = new Map(oldMap);
|
18
|
+
newMap.set(index, metadata);
|
19
|
+
Reflector.defineMetadata(REQUEST_CONTEXT_METADATA_KEY, newMap, target, propertyKey);
|
20
|
+
}
|
21
|
+
/**
|
22
|
+
* Get metadata.
|
23
|
+
*
|
24
|
+
* @param target
|
25
|
+
* @param propertyKey
|
26
|
+
*/
|
27
|
+
static getMetadata(target, propertyKey) {
|
28
|
+
const metadata = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, target, propertyKey);
|
29
|
+
return metadata ?? new Map();
|
30
|
+
}
|
31
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,59 @@
|
|
1
|
+
import { expect } from 'chai';
|
2
|
+
import { describe } from 'mocha';
|
3
|
+
import { Reflector } from '@e22m4u/ts-reflector';
|
4
|
+
import { RequestContextReflector } from './request-context-reflector.js';
|
5
|
+
import { REQUEST_CONTEXT_METADATA_KEY } from './request-context-metadata.js';
|
6
|
+
describe('RequestContextReflector', function () {
|
7
|
+
describe('setMetadata', function () {
|
8
|
+
it('sets a given value as target metadata', function () {
|
9
|
+
class Target {
|
10
|
+
}
|
11
|
+
const md1 = { property: 'req' };
|
12
|
+
const md2 = { property: 'res' };
|
13
|
+
RequestContextReflector.setMetadata(md1, Target, 0, 'propertyKey');
|
14
|
+
RequestContextReflector.setMetadata(md2, Target, 1, 'propertyKey');
|
15
|
+
const res = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, Target, 'propertyKey');
|
16
|
+
expect(res).to.be.instanceof(Map);
|
17
|
+
expect(res.get(0)).to.be.eq(md1);
|
18
|
+
expect(res.get(1)).to.be.eq(md2);
|
19
|
+
});
|
20
|
+
it('overrides existing metadata', function () {
|
21
|
+
class Target {
|
22
|
+
}
|
23
|
+
const md1 = { property: 'req' };
|
24
|
+
const md2 = { property: 'res' };
|
25
|
+
RequestContextReflector.setMetadata(md1, Target, 0, 'propertyKey');
|
26
|
+
const res1 = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, Target, 'propertyKey');
|
27
|
+
expect(res1).to.be.instanceof(Map);
|
28
|
+
expect(res1.get(0)).to.be.eq(md1);
|
29
|
+
RequestContextReflector.setMetadata(md2, Target, 0, 'propertyKey');
|
30
|
+
const res2 = Reflector.getOwnMetadata(REQUEST_CONTEXT_METADATA_KEY, Target, 'propertyKey');
|
31
|
+
expect(res2).to.be.instanceof(Map);
|
32
|
+
expect(res2.get(0)).to.be.eq(md2);
|
33
|
+
});
|
34
|
+
});
|
35
|
+
describe('getMetadata', function () {
|
36
|
+
it('returns an existing metadata of the target', function () {
|
37
|
+
class Target {
|
38
|
+
}
|
39
|
+
const md1 = { property: 'req' };
|
40
|
+
const md2 = { property: 'res' };
|
41
|
+
const mdMap = new Map([
|
42
|
+
[0, md1],
|
43
|
+
[1, md2],
|
44
|
+
]);
|
45
|
+
Reflector.defineMetadata(REQUEST_CONTEXT_METADATA_KEY, mdMap, Target, 'propertyKey');
|
46
|
+
const res = RequestContextReflector.getMetadata(Target, 'propertyKey');
|
47
|
+
expect(res).to.be.instanceof(Map);
|
48
|
+
expect(res.get(0)).to.be.eq(md1);
|
49
|
+
expect(res.get(1)).to.be.eq(md2);
|
50
|
+
});
|
51
|
+
it('returns an empty map if no metadata', function () {
|
52
|
+
class Target {
|
53
|
+
}
|
54
|
+
const res = RequestContextReflector.getMetadata(Target, 'propertyKey');
|
55
|
+
expect(res).to.be.instanceof(Map);
|
56
|
+
expect(res).to.be.empty;
|
57
|
+
});
|
58
|
+
});
|
59
|
+
});
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import { Prototype } from '../../types.js';
|
2
|
+
import { DataType } from '@e22m4u/ts-data-schema';
|
3
|
+
import { DataSchema } from '@e22m4u/ts-data-schema';
|
4
|
+
import { RequestDataMetadata } from './request-data-metadata.js';
|
5
|
+
/**
|
6
|
+
* Request data decorator.
|
7
|
+
*
|
8
|
+
* @param metadata
|
9
|
+
*/
|
10
|
+
export declare function requestData<T extends object>(metadata: RequestDataMetadata): (target: Prototype<T>, propertyKey: string, indexOrDescriptor: number) => void;
|
11
|
+
/**
|
12
|
+
* Decorator aliases.
|
13
|
+
*/
|
14
|
+
export declare const params: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
15
|
+
export declare const param: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
16
|
+
export declare const queries: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
17
|
+
export declare const query: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
18
|
+
export declare const headers: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
19
|
+
export declare const header: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
20
|
+
export declare const cookies: () => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
21
|
+
export declare const cookie: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
22
|
+
export declare const bodyParam: (propertyKey: string, schemaOrType?: DataSchema | DataType) => (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
23
|
+
/**
|
24
|
+
* Request body decorator.
|
25
|
+
*
|
26
|
+
* @param schemaOrType
|
27
|
+
*/
|
28
|
+
export declare function body(schemaOrType?: DataSchema | DataType): (target: Prototype<object>, propertyKey: string, indexOrDescriptor: number) => void;
|
@@ -0,0 +1,84 @@
|
|
1
|
+
import { DataType } from '@e22m4u/ts-data-schema';
|
2
|
+
import { DecoratorTargetType } from '@e22m4u/ts-reflector';
|
3
|
+
import { getDecoratorTargetType } from '@e22m4u/ts-reflector';
|
4
|
+
import { RequestDataSource } from './request-data-metadata.js';
|
5
|
+
import { RequestDataReflector } from './request-data-reflector.js';
|
6
|
+
/**
|
7
|
+
* Request data decorator.
|
8
|
+
*
|
9
|
+
* @param metadata
|
10
|
+
*/
|
11
|
+
export function requestData(metadata) {
|
12
|
+
return function (target, propertyKey, indexOrDescriptor) {
|
13
|
+
const decoratorType = getDecoratorTargetType(target, propertyKey, indexOrDescriptor);
|
14
|
+
if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD_PARAMETER)
|
15
|
+
throw new Error('@requestData decorator is only supported ' +
|
16
|
+
'on an instance method parameter.');
|
17
|
+
RequestDataReflector.setMetadata(metadata, target.constructor, indexOrDescriptor, propertyKey);
|
18
|
+
};
|
19
|
+
}
|
20
|
+
/**
|
21
|
+
* Create data decorator.
|
22
|
+
*
|
23
|
+
* @param source
|
24
|
+
*/
|
25
|
+
function createDataDecorator(source) {
|
26
|
+
return function () {
|
27
|
+
const schema = { type: DataType.OBJECT };
|
28
|
+
return requestData({ schema, source });
|
29
|
+
};
|
30
|
+
}
|
31
|
+
/**
|
32
|
+
* Create property decorator.
|
33
|
+
*
|
34
|
+
* @param source
|
35
|
+
*/
|
36
|
+
function createPropertyDecorator(source) {
|
37
|
+
return function (propertyKey, schemaOrType) {
|
38
|
+
const properties = {};
|
39
|
+
const rootSchema = { type: DataType.OBJECT };
|
40
|
+
if (typeof schemaOrType === 'object') {
|
41
|
+
properties[propertyKey] = schemaOrType;
|
42
|
+
rootSchema.properties = properties;
|
43
|
+
}
|
44
|
+
else if (typeof schemaOrType === 'string') {
|
45
|
+
properties[propertyKey] = { type: schemaOrType };
|
46
|
+
rootSchema.properties = properties;
|
47
|
+
}
|
48
|
+
return requestData({
|
49
|
+
source: source,
|
50
|
+
schema: rootSchema,
|
51
|
+
property: propertyKey,
|
52
|
+
});
|
53
|
+
};
|
54
|
+
}
|
55
|
+
/**
|
56
|
+
* Decorator aliases.
|
57
|
+
*/
|
58
|
+
export const params = createDataDecorator(RequestDataSource.PARAMS);
|
59
|
+
export const param = createPropertyDecorator(RequestDataSource.PARAMS);
|
60
|
+
export const queries = createDataDecorator(RequestDataSource.QUERY);
|
61
|
+
export const query = createPropertyDecorator(RequestDataSource.QUERY);
|
62
|
+
export const headers = createDataDecorator(RequestDataSource.HEADERS);
|
63
|
+
export const header = createPropertyDecorator(RequestDataSource.HEADERS);
|
64
|
+
export const cookies = createDataDecorator(RequestDataSource.COOKIE);
|
65
|
+
export const cookie = createPropertyDecorator(RequestDataSource.COOKIE);
|
66
|
+
export const bodyParam = createPropertyDecorator(RequestDataSource.BODY);
|
67
|
+
/**
|
68
|
+
* Request body decorator.
|
69
|
+
*
|
70
|
+
* @param schemaOrType
|
71
|
+
*/
|
72
|
+
export function body(schemaOrType) {
|
73
|
+
let schema;
|
74
|
+
if (typeof schemaOrType === 'object') {
|
75
|
+
schema = schemaOrType;
|
76
|
+
}
|
77
|
+
else if (typeof schemaOrType === 'string') {
|
78
|
+
schema = { type: schemaOrType };
|
79
|
+
}
|
80
|
+
else {
|
81
|
+
schema = { type: DataType.ANY };
|
82
|
+
}
|
83
|
+
return requestData({ schema, source: RequestDataSource.BODY });
|
84
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|