feat: add Returns decorator

This commit is contained in:
Shinwoo PARK 2023-12-15 01:37:35 +09:00
parent 0dbd7c4103
commit 36da94a623

View File

@ -1,3 +1,11 @@
export type Path = string | string[];
export type Paths = Path[];
export interface IncludeExcludePath {
includePath?: Paths;
excludePath?: Paths;
}
export interface LoggedParamReflectData { export interface LoggedParamReflectData {
name: string; name: string;
index: number; index: number;
@ -12,10 +20,16 @@ export interface ScopeKeyReflectData {
priority?: number; priority?: number;
} }
export interface ReturnsReflectData {
name: string;
path: string;
}
export const scopedLogger = Symbol("nlogdec-scopedLogger"); export const scopedLogger = Symbol("nlogdec-scopedLogger");
export const loggedParam = Symbol("nlogdec-loggedParam"); export const loggedParam = Symbol("nlogdec-loggedParam");
export const scopeKey = Symbol("nlogdec-scopeKey"); export const scopeKey = Symbol("nlogdec-scopeKey");
export const forceScopeKey = Symbol("nlogdec-forceScopeKey"); export const forceScopeKey = Symbol("nlogdec-forceScopeKey");
export const returns = Symbol("nlogdec-returns");
export function InjectLogger( export function InjectLogger(
target: any, target: any,
@ -25,13 +39,7 @@ export function InjectLogger(
Reflect.defineMetadata(scopedLogger, parameterIndex, target, propertyKey); Reflect.defineMetadata(scopedLogger, parameterIndex, target, propertyKey);
} }
export function LoggedParam( export function LoggedParam(name: string, options?: IncludeExcludePath) {
name: string,
options?: {
includePath?: (string | string[])[];
excludePath?: (string | string[])[];
}
) {
return ( return (
target: any, target: any,
propertyKey: string | symbol, propertyKey: string | symbol,
@ -65,7 +73,7 @@ export function LoggedParam(
export function ScopeKey( export function ScopeKey(
name: string, name: string,
options?: { path?: string | string[]; priority?: number } options?: { path?: Path; priority?: number }
) { ) {
return ( return (
target: any, target: any,
@ -90,6 +98,27 @@ export function ScopeKey(
}; };
} }
export function Returns<F extends Array<any>, R>(namePaths?: {
[name: string]: string;
}) {
return (
_target: any,
_key: string | symbol,
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>
) => {
Reflect.defineMetadata(
returns,
namePaths
? Object.entries(namePaths).reduce<ReturnsReflectData[]>(
(prev, curr) => [...prev, { name: curr[0], path: curr[1] }],
[]
)
: true,
descriptor.value
);
};
}
export function ShouldScoped( export function ShouldScoped(
_target: any, _target: any,
_key: string, _key: string,