feat: add LoggedMiddleware
This commit is contained in:
parent
88bdb9dfc2
commit
c74ad2b119
@ -161,19 +161,28 @@ class LoggedMetadata {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type BuildType = 'route' | 'function' | 'guard' | 'interceptor';
|
type BuildType = 'route' | 'function' | 'guard' | 'interceptor' | 'middleware';
|
||||||
|
|
||||||
const callLogIdentifyMessageDictionary: Record<BuildType, string> = {
|
const callLogIdentifyMessageDictionary: Record<BuildType, string> = {
|
||||||
route: 'HIT HTTP',
|
route: 'HIT HTTP',
|
||||||
function: 'CALL',
|
function: 'CALL',
|
||||||
guard: 'HIT GUARD',
|
guard: 'HIT GUARD',
|
||||||
interceptor: 'HIT INTERCEPTOR'
|
interceptor: 'HIT INTERCEPTOR',
|
||||||
|
middleware: 'HIT MIDDLEWARE',
|
||||||
}
|
}
|
||||||
|
|
||||||
function createCallLogIdentifyMessage(type: 'route' | 'guard' | 'interceptor', key: string, route: string)
|
function createCallLogIdentifyMessage(type: 'route', key: string, route: string)
|
||||||
|
function createCallLogIdentifyMessage(type: 'guard' | 'interceptor' | 'middleware', route: string)
|
||||||
function createCallLogIdentifyMessage(type: 'function', key: string)
|
function createCallLogIdentifyMessage(type: 'function', key: string)
|
||||||
function createCallLogIdentifyMessage(type: BuildType, key: string, route?: string) {
|
function createCallLogIdentifyMessage(type: BuildType, key?: string, route?: string) {
|
||||||
return `${callLogIdentifyMessageDictionary[type]} ${type === 'route' ? `${route} (${key})` : type === 'guard' ? `${route}` : key}`
|
if (type === 'route')
|
||||||
|
return `${callLogIdentifyMessageDictionary[type]} ${route} (${key})`
|
||||||
|
if (type === 'guard' || type === 'interceptor' || type === 'middleware')
|
||||||
|
return `${callLogIdentifyMessageDictionary[type]} ${route}`
|
||||||
|
if (type === 'function')
|
||||||
|
return `${callLogIdentifyMessageDictionary[type]} ${key}`;
|
||||||
|
|
||||||
|
return callLogIdentifyMessageDictionary[type];
|
||||||
}
|
}
|
||||||
|
|
||||||
function overrideBuild<F extends Array<any>, R>(
|
function overrideBuild<F extends Array<any>, R>(
|
||||||
@ -187,7 +196,7 @@ function overrideBuild<F extends Array<any>, R>(
|
|||||||
route: string,
|
route: string,
|
||||||
): (...args: F) => R;
|
): (...args: F) => R;
|
||||||
function overrideBuild<F extends Array<any>, R>(
|
function overrideBuild<F extends Array<any>, R>(
|
||||||
type: 'function' | 'guard' | 'interceptor',
|
type: 'function' | 'guard' | 'interceptor' | 'middleware',
|
||||||
originalFunction: (...args: F) => R,
|
originalFunction: (...args: F) => R,
|
||||||
baseLogger: Logger,
|
baseLogger: Logger,
|
||||||
metadatas: FunctionMetadata,
|
metadatas: FunctionMetadata,
|
||||||
@ -652,3 +661,79 @@ export function LoggedInterceptor<F extends Array<any>, R>(options?: Partial<Ove
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function LoggedMiddleware<F extends Array<any>, R>(options?: Partial<OverrideBuildOptions>) {
|
||||||
|
return (
|
||||||
|
_target: any,
|
||||||
|
key: string,
|
||||||
|
descriptor: TypedPropertyDescriptor<(context: ExecutionContext, ...args: F) => R>
|
||||||
|
) => {
|
||||||
|
loggerInit(_target);
|
||||||
|
|
||||||
|
const logger: Logger = _target.logger;
|
||||||
|
|
||||||
|
const fn = descriptor.value;
|
||||||
|
|
||||||
|
if (!fn || typeof fn!== "function") {
|
||||||
|
logger.warn(
|
||||||
|
`LoggedMiddleware decorator applied to non-function property: ${key}`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const logMetadata: LoggedMetadata | undefined = Reflect.getOwnMetadata(
|
||||||
|
nestLoggedMetadata,
|
||||||
|
_target,
|
||||||
|
key
|
||||||
|
)
|
||||||
|
if (logMetadata) {
|
||||||
|
// already applied, override instead
|
||||||
|
logMetadata.updateOption(options)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const newMetadata = new LoggedMetadata(options);
|
||||||
|
|
||||||
|
const all = Reflect.getMetadataKeys(fn).map((k) => [
|
||||||
|
k,
|
||||||
|
Reflect.getMetadata(k, fn),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
|
||||||
|
scopedLogger,
|
||||||
|
_target,
|
||||||
|
key
|
||||||
|
);
|
||||||
|
|
||||||
|
const returnsData: ReturnsReflectData[] | true = Reflect.getOwnMetadata(
|
||||||
|
returns,
|
||||||
|
fn
|
||||||
|
);
|
||||||
|
|
||||||
|
const overrideFunction = overrideBuild(
|
||||||
|
'middleware',
|
||||||
|
fn,
|
||||||
|
logger,
|
||||||
|
{
|
||||||
|
scopedLoggerInjectableParam,
|
||||||
|
loggedParams: [],
|
||||||
|
},
|
||||||
|
key,
|
||||||
|
returnsData,
|
||||||
|
newMetadata,
|
||||||
|
);
|
||||||
|
|
||||||
|
_target[key] = overrideFunction;
|
||||||
|
descriptor.value = overrideFunction;
|
||||||
|
|
||||||
|
Reflect.defineMetadata(
|
||||||
|
nestLoggedMetadata,
|
||||||
|
newMetadata,
|
||||||
|
_target,
|
||||||
|
key
|
||||||
|
)
|
||||||
|
all.forEach(([k, v]) => {
|
||||||
|
Reflect.defineMetadata(k, v, _target[key]);
|
||||||
|
Reflect.defineMetadata(k, v, descriptor.value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user