@e22m4u/ts-rest-router 0.1.1 → 0.2.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 +118 -118
- package/dist/cjs/index.cjs +346 -360
- package/dist/esm/controller-registry.js +28 -28
- package/dist/esm/debuggable-service.d.ts +10 -1
- package/dist/esm/debuggable-service.js +14 -3
- package/dist/esm/decorators/after-action/after-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{after/after-decorator.js → after-action/after-action-decorator.js} +7 -7
- package/dist/esm/decorators/{before/before-decorator.spec.js → after-action/after-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{after/after-metadata.d.ts → after-action/after-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/after-action/after-action-metadata.js +5 -0
- package/dist/esm/decorators/{after/after-reflector.d.ts → after-action/after-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{before/before-reflector.js → after-action/after-action-reflector.js} +6 -6
- package/dist/esm/decorators/{before/before-reflector.spec.js → after-action/after-action-reflector.spec.js} +23 -23
- package/dist/esm/decorators/after-action/index.d.ts +3 -0
- package/dist/esm/decorators/after-action/index.js +3 -0
- package/dist/esm/decorators/before-action/before-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{before/before-decorator.js → before-action/before-action-decorator.js} +7 -7
- package/dist/esm/decorators/{after/after-decorator.spec.js → before-action/before-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{before/before-metadata.d.ts → before-action/before-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/before-action/before-action-metadata.js +5 -0
- package/dist/esm/decorators/{before/before-reflector.d.ts → before-action/before-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{after/after-reflector.js → before-action/before-action-reflector.js} +6 -6
- package/dist/esm/decorators/{after/after-reflector.spec.js → before-action/before-action-reflector.spec.js} +23 -23
- package/dist/esm/decorators/before-action/index.d.ts +3 -0
- package/dist/esm/decorators/before-action/index.js +3 -0
- package/dist/esm/decorators/index.d.ts +4 -4
- package/dist/esm/decorators/index.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +6 -0
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.js +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +44 -32
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +1 -1
- package/dist/esm/decorators/rest-action/index.d.ts +3 -0
- package/dist/esm/decorators/rest-action/index.js +3 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.d.ts +57 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.js +52 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.spec.js +401 -0
- package/dist/esm/decorators/{action/action-metadata.d.ts → rest-action/rest-action-metadata.d.ts} +6 -6
- package/dist/esm/decorators/rest-action/rest-action-metadata.js +5 -0
- package/dist/esm/decorators/rest-action/rest-action-reflector.d.ts +22 -0
- package/dist/esm/decorators/{action/action-reflector.js → rest-action/rest-action-reflector.js} +6 -6
- package/dist/esm/decorators/{action/action-reflector.spec.js → rest-action/rest-action-reflector.spec.js} +13 -13
- package/dist/esm/decorators/rest-controller/index.d.ts +3 -0
- package/dist/esm/decorators/rest-controller/index.js +3 -0
- package/dist/esm/decorators/rest-controller/rest-controller-decorator.d.ts +14 -0
- package/dist/esm/decorators/{controller/controller-decorator.js → rest-controller/rest-controller-decorator.js} +5 -5
- package/dist/esm/decorators/{controller/controller-decorator.spec.js → rest-controller/rest-controller-decorator.spec.js} +15 -15
- package/dist/esm/decorators/{controller/controller-metadata.d.ts → rest-controller/rest-controller-metadata.d.ts} +5 -5
- package/dist/esm/decorators/rest-controller/rest-controller-metadata.js +5 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.d.ts +20 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.js +24 -0
- package/dist/esm/decorators/{controller/controller-reflector.spec.js → rest-controller/rest-controller-reflector.spec.js} +12 -12
- package/dist/esm/errors/not-a-controller-error.js +1 -1
- package/dist/esm/types.d.ts +0 -10
- package/dist/esm/utils/index.d.ts +0 -1
- package/dist/esm/utils/index.js +0 -1
- package/package.json +19 -19
- package/src/controller-registry.spec.ts +122 -122
- package/src/controller-registry.ts +33 -35
- package/src/debuggable-service.ts +17 -4
- package/src/decorators/{after/after-decorator.spec.ts → after-action/after-action-decorator.spec.ts} +19 -19
- package/src/decorators/{before/before-decorator.ts → after-action/after-action-decorator.ts} +9 -9
- package/src/decorators/{after/after-metadata.ts → after-action/after-action-metadata.ts} +5 -5
- package/src/decorators/{before/before-reflector.spec.ts → after-action/after-action-reflector.spec.ts} +33 -23
- package/src/decorators/{before/before-reflector.ts → after-action/after-action-reflector.ts} +13 -9
- package/src/decorators/after-action/index.ts +3 -0
- package/src/decorators/{before/before-decorator.spec.ts → before-action/before-action-decorator.spec.ts} +19 -19
- package/src/decorators/{after/after-decorator.ts → before-action/before-action-decorator.ts} +9 -9
- package/src/decorators/before-action/before-action-metadata.ts +17 -0
- package/src/decorators/{after/after-reflector.spec.ts → before-action/before-action-reflector.spec.ts} +40 -23
- package/src/decorators/{after/after-reflector.ts → before-action/before-action-reflector.ts} +18 -9
- package/src/decorators/before-action/index.ts +3 -0
- package/src/decorators/index.ts +4 -4
- package/src/decorators/request-context/request-context-decorator.spec.ts +7 -0
- package/src/decorators/request-context/request-context-decorator.ts +4 -4
- package/src/decorators/request-data/request-data-decorator.spec.ts +45 -32
- package/src/decorators/request-data/request-data-decorator.ts +10 -10
- package/src/decorators/request-data/request-data-metadata.ts +1 -1
- package/src/decorators/rest-action/index.ts +3 -0
- package/src/decorators/rest-action/rest-action-decorator.spec.ts +325 -0
- package/src/decorators/rest-action/rest-action-decorator.ts +166 -0
- package/src/decorators/{action/action-metadata.ts → rest-action/rest-action-metadata.ts} +7 -7
- package/src/decorators/{action/action-reflector.spec.ts → rest-action/rest-action-reflector.spec.ts} +13 -13
- package/src/decorators/rest-action/rest-action-reflector.ts +41 -0
- package/src/decorators/rest-controller/index.ts +3 -0
- package/src/decorators/{controller/controller-decorator.spec.ts → rest-controller/rest-controller-decorator.spec.ts} +16 -16
- package/src/decorators/{controller/controller-decorator.ts → rest-controller/rest-controller-decorator.ts} +14 -10
- package/src/decorators/{controller/controller-metadata.ts → rest-controller/rest-controller-metadata.ts} +6 -7
- package/src/decorators/{controller/controller-reflector.spec.ts → rest-controller/rest-controller-reflector.spec.ts} +21 -12
- package/src/decorators/rest-controller/rest-controller-reflector.ts +32 -0
- package/src/errors/not-a-controller-error.ts +4 -1
- package/src/types.ts +0 -10
- package/src/utils/index.ts +0 -1
- package/README-ru.md +0 -268
- package/dist/esm/decorators/action/action-decorator.d.ts +0 -52
- package/dist/esm/decorators/action/action-decorator.js +0 -62
- package/dist/esm/decorators/action/action-decorator.spec.js +0 -59
- package/dist/esm/decorators/action/action-metadata.js +0 -5
- package/dist/esm/decorators/action/action-reflector.d.ts +0 -22
- package/dist/esm/decorators/action/index.d.ts +0 -3
- package/dist/esm/decorators/action/index.js +0 -3
- package/dist/esm/decorators/after/after-decorator.d.ts +0 -9
- package/dist/esm/decorators/after/after-metadata.js +0 -5
- package/dist/esm/decorators/after/index.d.ts +0 -3
- package/dist/esm/decorators/after/index.js +0 -3
- package/dist/esm/decorators/before/before-decorator.d.ts +0 -9
- package/dist/esm/decorators/before/before-metadata.js +0 -5
- package/dist/esm/decorators/before/index.d.ts +0 -3
- package/dist/esm/decorators/before/index.js +0 -3
- package/dist/esm/decorators/controller/controller-decorator.d.ts +0 -14
- package/dist/esm/decorators/controller/controller-metadata.js +0 -5
- package/dist/esm/decorators/controller/controller-reflector.d.ts +0 -20
- package/dist/esm/decorators/controller/controller-reflector.js +0 -24
- package/dist/esm/decorators/controller/index.d.ts +0 -3
- package/dist/esm/decorators/controller/index.js +0 -3
- package/dist/esm/utils/create-debugger.d.ts +0 -44
- package/dist/esm/utils/create-debugger.js +0 -81
- package/dist/esm/utils/create-debugger.spec.d.ts +0 -1
- package/dist/esm/utils/create-debugger.spec.js +0 -8
- package/src/decorators/action/action-decorator.spec.ts +0 -42
- package/src/decorators/action/action-decorator.ts +0 -98
- package/src/decorators/action/action-reflector.ts +0 -38
- package/src/decorators/action/index.ts +0 -3
- package/src/decorators/after/index.ts +0 -3
- package/src/decorators/before/before-metadata.ts +0 -17
- package/src/decorators/before/index.ts +0 -3
- package/src/decorators/controller/controller-reflector.ts +0 -28
- package/src/decorators/controller/index.ts +0 -3
- package/src/utils/create-debugger.spec.ts +0 -9
- package/src/utils/create-debugger.ts +0 -98
- /package/dist/esm/decorators/{action/action-decorator.spec.d.ts → after-action/after-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{action/action-reflector.spec.d.ts → after-action/after-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-decorator.spec.d.ts → before-action/before-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-reflector.spec.d.ts → before-action/before-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-decorator.spec.d.ts → rest-action/rest-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-reflector.spec.d.ts → rest-action/rest-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-decorator.spec.d.ts → rest-controller/rest-controller-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-reflector.spec.d.ts → rest-controller/rest-controller-reflector.spec.d.ts} +0 -0
@@ -1,59 +0,0 @@
|
|
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
|
-
import { expect } from 'chai';
|
11
|
-
import { action } from './action-decorator.js';
|
12
|
-
import { HttpMethod } from '@e22m4u/js-trie-router';
|
13
|
-
import { ActionReflector } from './action-reflector.js';
|
14
|
-
describe('action', function () {
|
15
|
-
it('sets given options to the target metadata', function () {
|
16
|
-
const options = {
|
17
|
-
method: HttpMethod.GET,
|
18
|
-
path: 'myPath',
|
19
|
-
before: () => undefined,
|
20
|
-
after: () => undefined,
|
21
|
-
customOption: 'customOption',
|
22
|
-
};
|
23
|
-
class Target {
|
24
|
-
method() { }
|
25
|
-
}
|
26
|
-
__decorate([
|
27
|
-
action(options),
|
28
|
-
__metadata("design:type", Function),
|
29
|
-
__metadata("design:paramtypes", []),
|
30
|
-
__metadata("design:returntype", void 0)
|
31
|
-
], Target.prototype, "method", null);
|
32
|
-
const res = ActionReflector.getMetadata(Target);
|
33
|
-
expect(res.get('method')).to.be.eql({
|
34
|
-
...options,
|
35
|
-
propertyKey: 'method',
|
36
|
-
});
|
37
|
-
});
|
38
|
-
it('overrides a given "propertyKey" option by the target method name', function () {
|
39
|
-
const options = {
|
40
|
-
propertyKey: 'myMethod',
|
41
|
-
method: HttpMethod.GET,
|
42
|
-
path: 'myPath',
|
43
|
-
};
|
44
|
-
class Target {
|
45
|
-
method() { }
|
46
|
-
}
|
47
|
-
__decorate([
|
48
|
-
action(options),
|
49
|
-
__metadata("design:type", Function),
|
50
|
-
__metadata("design:paramtypes", []),
|
51
|
-
__metadata("design:returntype", void 0)
|
52
|
-
], Target.prototype, "method", null);
|
53
|
-
const res = ActionReflector.getMetadata(Target);
|
54
|
-
expect(res.get('method')).to.be.eql({
|
55
|
-
...options,
|
56
|
-
propertyKey: 'method',
|
57
|
-
});
|
58
|
-
});
|
59
|
-
});
|
@@ -1,22 +0,0 @@
|
|
1
|
-
import { Constructor } from '../../types.js';
|
2
|
-
import { ActionMetadata } from './action-metadata.js';
|
3
|
-
import { ActionMetadataMap } from './action-metadata.js';
|
4
|
-
/**
|
5
|
-
* Action reflector.
|
6
|
-
*/
|
7
|
-
export declare class ActionReflector {
|
8
|
-
/**
|
9
|
-
* Set metadata.
|
10
|
-
*
|
11
|
-
* @param metadata
|
12
|
-
* @param target
|
13
|
-
* @param propertyKey
|
14
|
-
*/
|
15
|
-
static setMetadata(metadata: ActionMetadata, target: Constructor, propertyKey: string): void;
|
16
|
-
/**
|
17
|
-
* Get metadata.
|
18
|
-
*
|
19
|
-
* @param target
|
20
|
-
*/
|
21
|
-
static getMetadata(target: Constructor): ActionMetadataMap;
|
22
|
-
}
|
@@ -1,9 +0,0 @@
|
|
1
|
-
import { Prototype } from '../../types.js';
|
2
|
-
import { Constructor } from '../../types.js';
|
3
|
-
import { AfterMetadata } from './after-metadata.js';
|
4
|
-
/**
|
5
|
-
* After decorator.
|
6
|
-
*
|
7
|
-
* @param options
|
8
|
-
*/
|
9
|
-
export declare function after<T extends object>(middleware: AfterMetadata['middleware']): (target: Constructor<T> | Prototype<T>, propertyKey?: string, descriptor?: PropertyDescriptor) => void;
|
@@ -1,9 +0,0 @@
|
|
1
|
-
import { Prototype } from '../../types.js';
|
2
|
-
import { Constructor } from '../../types.js';
|
3
|
-
import { BeforeMetadata } from './before-metadata.js';
|
4
|
-
/**
|
5
|
-
* Before decorator.
|
6
|
-
*
|
7
|
-
* @param options
|
8
|
-
*/
|
9
|
-
export declare function before<T extends object>(middleware: BeforeMetadata['middleware']): (target: Constructor<T> | Prototype<T>, propertyKey?: string, descriptor?: PropertyDescriptor) => void;
|
@@ -1,14 +0,0 @@
|
|
1
|
-
import { Flatten } from '../../types.js';
|
2
|
-
import { Constructor } from '../../types.js';
|
3
|
-
import { ControllerMetadata } from './controller-metadata.js';
|
4
|
-
/**
|
5
|
-
* Controller options.
|
6
|
-
*/
|
7
|
-
export type ControllerOptions = Flatten<Omit<ControllerMetadata, 'className'>>;
|
8
|
-
/**
|
9
|
-
* Controller decorator.
|
10
|
-
*
|
11
|
-
* @param pathOrOptions
|
12
|
-
* @param options
|
13
|
-
*/
|
14
|
-
export declare function controller<T extends object>(pathOrOptions?: string | ControllerOptions, options?: ControllerOptions): (target: Constructor<T>) => void;
|
@@ -1,20 +0,0 @@
|
|
1
|
-
import { Constructor } from '../../types.js';
|
2
|
-
import { ControllerMetadata } from './controller-metadata.js';
|
3
|
-
/**
|
4
|
-
* Controller reflector.
|
5
|
-
*/
|
6
|
-
export declare class ControllerReflector {
|
7
|
-
/**
|
8
|
-
* Set metadata.
|
9
|
-
*
|
10
|
-
* @param metadata
|
11
|
-
* @param target
|
12
|
-
*/
|
13
|
-
static setMetadata(metadata: ControllerMetadata, target: Constructor): void;
|
14
|
-
/**
|
15
|
-
* Get metadata.
|
16
|
-
*
|
17
|
-
* @param target
|
18
|
-
*/
|
19
|
-
static getMetadata(target: Constructor): ControllerMetadata | undefined;
|
20
|
-
}
|
@@ -1,24 +0,0 @@
|
|
1
|
-
import { Reflector } from '@e22m4u/ts-reflector';
|
2
|
-
import { CONTROLLER_METADATA_KEY } from './controller-metadata.js';
|
3
|
-
/**
|
4
|
-
* Controller reflector.
|
5
|
-
*/
|
6
|
-
export class ControllerReflector {
|
7
|
-
/**
|
8
|
-
* Set metadata.
|
9
|
-
*
|
10
|
-
* @param metadata
|
11
|
-
* @param target
|
12
|
-
*/
|
13
|
-
static setMetadata(metadata, target) {
|
14
|
-
return Reflector.defineMetadata(CONTROLLER_METADATA_KEY, metadata, target);
|
15
|
-
}
|
16
|
-
/**
|
17
|
-
* Get metadata.
|
18
|
-
*
|
19
|
-
* @param target
|
20
|
-
*/
|
21
|
-
static getMetadata(target) {
|
22
|
-
return Reflector.getOwnMetadata(CONTROLLER_METADATA_KEY, target);
|
23
|
-
}
|
24
|
-
}
|
@@ -1,44 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Debugger.
|
3
|
-
*/
|
4
|
-
export type Debugger = (messageOrData: string | unknown, ...args: any[]) => void;
|
5
|
-
/**
|
6
|
-
* Create debugger.
|
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
|
-
*
|
42
|
-
* @param name
|
43
|
-
*/
|
44
|
-
export declare function createDebugger(name: string): Debugger;
|
@@ -1,81 +0,0 @@
|
|
1
|
-
import { inspect } from 'util';
|
2
|
-
import DebugModule from 'debug';
|
3
|
-
import { format } from '@e22m4u/js-format';
|
4
|
-
/**
|
5
|
-
* Colorize string.
|
6
|
-
*
|
7
|
-
* @param input
|
8
|
-
*/
|
9
|
-
function colorizeString(input) {
|
10
|
-
const c = Number(DebugModule['selectColor'](input));
|
11
|
-
const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
|
12
|
-
return `${colorCode};1m${input}\u001B[0m`;
|
13
|
-
}
|
14
|
-
/**
|
15
|
-
* Create debugger.
|
16
|
-
*
|
17
|
-
* Base usage:
|
18
|
-
* ```ts
|
19
|
-
* const debug = createDebugger('myService');
|
20
|
-
* debug('Service created.');
|
21
|
-
* // ujut:myService Service created.
|
22
|
-
* ```
|
23
|
-
*
|
24
|
-
* Nested namespaces:
|
25
|
-
* ```ts
|
26
|
-
* const debug1 = debug.bind('namespace1');
|
27
|
-
* const debug2 = debug.bind('namespace2');
|
28
|
-
* debug1('Application started'); // ujut:myService [namespace1] Application started
|
29
|
-
* debug2('Connection established'); // ujut:myService [namespace2] Connection established
|
30
|
-
* ```
|
31
|
-
*
|
32
|
-
* Value inspection:
|
33
|
-
* ```ts
|
34
|
-
* debug({foo: 'lorem', bar: 'ipsum'})
|
35
|
-
* // ujut:myService {
|
36
|
-
* // ujut:myService "foo": "lorem",
|
37
|
-
* // ujut:myService "bar": "ipsum"
|
38
|
-
* // ujut:myService }
|
39
|
-
* ```
|
40
|
-
*
|
41
|
-
* Titled inspection output:
|
42
|
-
* ```ts
|
43
|
-
* debug({foo: 'lorem', bar: 'ipsum'}, 'My awesome output:')
|
44
|
-
* // ujut:myService My awesome output:
|
45
|
-
* // ujut:myService {
|
46
|
-
* // ujut:myService "foo": "lorem",
|
47
|
-
* // ujut:myService "bar": "ipsum"
|
48
|
-
* // ujut:myService }
|
49
|
-
* ```
|
50
|
-
*
|
51
|
-
* @param name
|
52
|
-
*/
|
53
|
-
export function createDebugger(name) {
|
54
|
-
const debuggerName = `tsRestRouter:${name}`;
|
55
|
-
// включить вывод логов можно принудительно
|
56
|
-
// if (!process.env.DEBUG) DebugModule.enable('ujut*');
|
57
|
-
const debug = DebugModule(debuggerName);
|
58
|
-
return function (messageOrData,
|
59
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
60
|
-
...args) {
|
61
|
-
let prefix = '';
|
62
|
-
if (typeof this === 'string') {
|
63
|
-
const isDebugUsesColors = debug.useColors;
|
64
|
-
prefix = isDebugUsesColors ? colorizeString(`[${this}] `) : `[${this}] `;
|
65
|
-
}
|
66
|
-
if (typeof messageOrData === 'string') {
|
67
|
-
const interpolatedMessage = format(messageOrData, ...args);
|
68
|
-
return debug(prefix + interpolatedMessage);
|
69
|
-
}
|
70
|
-
const inspectOptions = {
|
71
|
-
showHidden: false,
|
72
|
-
depth: null,
|
73
|
-
colors: true,
|
74
|
-
compact: false,
|
75
|
-
};
|
76
|
-
const multiString = inspect(messageOrData, inspectOptions);
|
77
|
-
const rows = multiString.split('\n');
|
78
|
-
const colorizedDebuggerName = colorizeString(debuggerName);
|
79
|
-
[...args, ...rows].forEach(v => console.log(` ${colorizedDebuggerName} ${prefix}${v}`));
|
80
|
-
};
|
81
|
-
}
|
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
@@ -1,42 +0,0 @@
|
|
1
|
-
import {expect} from 'chai';
|
2
|
-
import {action} from './action-decorator.js';
|
3
|
-
import {HttpMethod} from '@e22m4u/js-trie-router';
|
4
|
-
import {ActionReflector} from './action-reflector.js';
|
5
|
-
|
6
|
-
describe('action', function () {
|
7
|
-
it('sets given options to the target metadata', function () {
|
8
|
-
const options = {
|
9
|
-
method: HttpMethod.GET,
|
10
|
-
path: 'myPath',
|
11
|
-
before: () => undefined,
|
12
|
-
after: () => undefined,
|
13
|
-
customOption: 'customOption',
|
14
|
-
};
|
15
|
-
class Target {
|
16
|
-
@action(options)
|
17
|
-
method() {}
|
18
|
-
}
|
19
|
-
const res = ActionReflector.getMetadata(Target);
|
20
|
-
expect(res.get('method')).to.be.eql({
|
21
|
-
...options,
|
22
|
-
propertyKey: 'method',
|
23
|
-
});
|
24
|
-
});
|
25
|
-
|
26
|
-
it('overrides a given "propertyKey" option by the target method name', function () {
|
27
|
-
const options = {
|
28
|
-
propertyKey: 'myMethod',
|
29
|
-
method: HttpMethod.GET,
|
30
|
-
path: 'myPath',
|
31
|
-
};
|
32
|
-
class Target {
|
33
|
-
@action(options)
|
34
|
-
method() {}
|
35
|
-
}
|
36
|
-
const res = ActionReflector.getMetadata(Target);
|
37
|
-
expect(res.get('method')).to.be.eql({
|
38
|
-
...options,
|
39
|
-
propertyKey: 'method',
|
40
|
-
});
|
41
|
-
});
|
42
|
-
});
|
@@ -1,98 +0,0 @@
|
|
1
|
-
import {Flatten} from '../../types.js';
|
2
|
-
import {Prototype} from '../../types.js';
|
3
|
-
import {Constructor} from '../../types.js';
|
4
|
-
import {HttpMethod} from '@e22m4u/js-trie-router';
|
5
|
-
import {ActionMetadata} from './action-metadata.js';
|
6
|
-
import {ActionReflector} from './action-reflector.js';
|
7
|
-
import {DecoratorTargetType} from '@e22m4u/ts-reflector';
|
8
|
-
import {getDecoratorTargetType} from '@e22m4u/ts-reflector';
|
9
|
-
|
10
|
-
/**
|
11
|
-
* Action options.
|
12
|
-
*/
|
13
|
-
export type ActionOptions = Flatten<Omit<ActionMetadata, 'propertyKey'>>;
|
14
|
-
|
15
|
-
/**
|
16
|
-
* Action decorator.
|
17
|
-
*
|
18
|
-
* @param options
|
19
|
-
*/
|
20
|
-
export function action<T extends object>(options: ActionOptions) {
|
21
|
-
return function (
|
22
|
-
target: Prototype<T>,
|
23
|
-
propertyKey: string,
|
24
|
-
descriptor: PropertyDescriptor,
|
25
|
-
) {
|
26
|
-
const decoratorType = getDecoratorTargetType(
|
27
|
-
target,
|
28
|
-
propertyKey,
|
29
|
-
descriptor,
|
30
|
-
);
|
31
|
-
if (decoratorType !== DecoratorTargetType.INSTANCE_METHOD)
|
32
|
-
throw new Error(
|
33
|
-
'@action decorator is only supported on an instance method.',
|
34
|
-
);
|
35
|
-
ActionReflector.setMetadata(
|
36
|
-
{...options, propertyKey},
|
37
|
-
target.constructor as Constructor<T>,
|
38
|
-
propertyKey,
|
39
|
-
);
|
40
|
-
};
|
41
|
-
}
|
42
|
-
|
43
|
-
/**
|
44
|
-
* Action method options.
|
45
|
-
*/
|
46
|
-
export type ActionMethodOptions = Flatten<
|
47
|
-
Omit<ActionOptions, 'method' | 'path'>
|
48
|
-
>;
|
49
|
-
|
50
|
-
/**
|
51
|
-
* Get decorator.
|
52
|
-
*
|
53
|
-
* @param path
|
54
|
-
* @param options
|
55
|
-
*/
|
56
|
-
export const get = (path: string, options?: ActionMethodOptions) => {
|
57
|
-
return action({...options, path, method: HttpMethod.GET});
|
58
|
-
};
|
59
|
-
|
60
|
-
/**
|
61
|
-
* Post decorator.
|
62
|
-
*
|
63
|
-
* @param path
|
64
|
-
* @param options
|
65
|
-
*/
|
66
|
-
export const post = (path: string, options?: ActionMethodOptions) => {
|
67
|
-
return action({...options, path, method: HttpMethod.POST});
|
68
|
-
};
|
69
|
-
|
70
|
-
/**
|
71
|
-
* Put decorator.
|
72
|
-
*
|
73
|
-
* @param path
|
74
|
-
* @param options
|
75
|
-
*/
|
76
|
-
export const put = (path: string, options?: ActionMethodOptions) => {
|
77
|
-
return action({...options, path, method: HttpMethod.PUT});
|
78
|
-
};
|
79
|
-
|
80
|
-
/**
|
81
|
-
* Patch decorator.
|
82
|
-
*
|
83
|
-
* @param path
|
84
|
-
* @param options
|
85
|
-
*/
|
86
|
-
export const patch = (path: string, options?: ActionMethodOptions) => {
|
87
|
-
return action({...options, path, method: HttpMethod.PATCH});
|
88
|
-
};
|
89
|
-
|
90
|
-
/**
|
91
|
-
* Del decorator.
|
92
|
-
*
|
93
|
-
* @param path
|
94
|
-
* @param options
|
95
|
-
*/
|
96
|
-
export const del = (path: string, options?: ActionMethodOptions) => {
|
97
|
-
return action({...options, path, method: HttpMethod.DELETE});
|
98
|
-
};
|
@@ -1,38 +0,0 @@
|
|
1
|
-
import {Constructor} from '../../types.js';
|
2
|
-
import {Reflector} from '@e22m4u/ts-reflector';
|
3
|
-
import {ActionMetadata} from './action-metadata.js';
|
4
|
-
import {ActionMetadataMap} from './action-metadata.js';
|
5
|
-
import {ACTIONS_METADATA_KEY} from './action-metadata.js';
|
6
|
-
|
7
|
-
/**
|
8
|
-
* Action reflector.
|
9
|
-
*/
|
10
|
-
export class ActionReflector {
|
11
|
-
/**
|
12
|
-
* Set metadata.
|
13
|
-
*
|
14
|
-
* @param metadata
|
15
|
-
* @param target
|
16
|
-
* @param propertyKey
|
17
|
-
*/
|
18
|
-
static setMetadata(
|
19
|
-
metadata: ActionMetadata,
|
20
|
-
target: Constructor,
|
21
|
-
propertyKey: string,
|
22
|
-
) {
|
23
|
-
const oldMap = Reflector.getOwnMetadata(ACTIONS_METADATA_KEY, target);
|
24
|
-
const newMap = new Map(oldMap);
|
25
|
-
newMap.set(propertyKey, metadata);
|
26
|
-
Reflector.defineMetadata(ACTIONS_METADATA_KEY, newMap, target);
|
27
|
-
}
|
28
|
-
|
29
|
-
/**
|
30
|
-
* Get metadata.
|
31
|
-
*
|
32
|
-
* @param target
|
33
|
-
*/
|
34
|
-
static getMetadata(target: Constructor): ActionMetadataMap {
|
35
|
-
const metadata = Reflector.getOwnMetadata(ACTIONS_METADATA_KEY, target);
|
36
|
-
return metadata ?? new Map();
|
37
|
-
}
|
38
|
-
}
|
@@ -1,17 +0,0 @@
|
|
1
|
-
import {MetadataKey} from '@e22m4u/ts-reflector';
|
2
|
-
import {RoutePreHandler} from '@e22m4u/js-trie-router';
|
3
|
-
|
4
|
-
/**
|
5
|
-
* Before metadata.
|
6
|
-
*/
|
7
|
-
export type BeforeMetadata = {
|
8
|
-
propertyKey?: string;
|
9
|
-
middleware: RoutePreHandler | RoutePreHandler[];
|
10
|
-
};
|
11
|
-
|
12
|
-
/**
|
13
|
-
* Before metadata key.
|
14
|
-
*/
|
15
|
-
export const BEFORE_METADATA_KEY = new MetadataKey<BeforeMetadata[]>(
|
16
|
-
'beforeMetadataKey',
|
17
|
-
);
|
@@ -1,28 +0,0 @@
|
|
1
|
-
import {Constructor} from '../../types.js';
|
2
|
-
import {Reflector} from '@e22m4u/ts-reflector';
|
3
|
-
import {ControllerMetadata} from './controller-metadata.js';
|
4
|
-
import {CONTROLLER_METADATA_KEY} from './controller-metadata.js';
|
5
|
-
|
6
|
-
/**
|
7
|
-
* Controller reflector.
|
8
|
-
*/
|
9
|
-
export class ControllerReflector {
|
10
|
-
/**
|
11
|
-
* Set metadata.
|
12
|
-
*
|
13
|
-
* @param metadata
|
14
|
-
* @param target
|
15
|
-
*/
|
16
|
-
static setMetadata(metadata: ControllerMetadata, target: Constructor) {
|
17
|
-
return Reflector.defineMetadata(CONTROLLER_METADATA_KEY, metadata, target);
|
18
|
-
}
|
19
|
-
|
20
|
-
/**
|
21
|
-
* Get metadata.
|
22
|
-
*
|
23
|
-
* @param target
|
24
|
-
*/
|
25
|
-
static getMetadata(target: Constructor): ControllerMetadata | undefined {
|
26
|
-
return Reflector.getOwnMetadata(CONTROLLER_METADATA_KEY, target);
|
27
|
-
}
|
28
|
-
}
|