-
Notifications
You must be signed in to change notification settings - Fork 13
/
LoggerDecorator.ts
57 lines (52 loc) · 1.64 KB
/
LoggerDecorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { Logger } from './Logger';
// /**
// * Represents a type that has a `logger` property
// * containing the Logger singleton instance
// */
// // export type Loggable<T> = T & { logger: Logger };
// /**
// * Class decorator that transforms a class to Loggable<T>.
// * Regrettably works but is not properly picked up by intellisense
// * at this point in time, meaning compiler errors when attempting
// * to access the `logger` property on the decorated class.
// * Maybe someday.
// *
// * Example:
// * ```
// * @loggable
// * class Foo { }
// * ```
// */
// // export function loggable<T extends Function>(target: T): Loggable<T>
// // {
// // Object.defineProperty(target.prototype, 'logger',
// // { value: Logger.instance });
// // return target as Loggable<T>;
// // }
export function logger(key: string): PropertyDecorator;
export function logger<T>(target: T, key: string): void;
/**
* Property decorator that will automatically assign
* the Logger singleton instance to the decorated
* class property
*
* Example:
* ```
* class Foo {
* @logger private readonly logger: Logger;
* ...
* ```
* >**Note:** This is a Typescript feature. If using the logger is desired
* in Javascript you should simply retrieve the singleton instance via
* `Logger.instance()`
* @returns {PropertyDecorator}
*/
export function logger(...args: any[]): PropertyDecorator | void
{
if (typeof args[0] === 'string')
return (<T>(target: T, key: string) =>
{
Object.defineProperty(target, key, { value: Logger.instance(args[0]) });
}) as PropertyDecorator;
Object.defineProperty(args[0], args[1], { value: Logger.instance() });
}