fix: add metadata proxy to each logged decorators

This commit is contained in:
Shinwoo PARK 2023-12-10 18:48:14 +09:00
parent d46c49fa35
commit a380613d0b
3 changed files with 48 additions and 26 deletions

27
dist/lib/logged.js vendored
View File

@ -37,13 +37,11 @@ function LoggedInjectable(options) {
methods.forEach((method) => { methods.forEach((method) => {
if (method !== "constructor" && if (method !== "constructor" &&
typeof target.prototype[method] === "function") { typeof target.prototype[method] === "function") {
const all = Reflect.getMetadataKeys(target.prototype[method]).map((k) => [k, Reflect.getMetadata(k, target.prototype[method])]);
if (options && options.verbose) if (options && options.verbose)
logger.log(`LoggedFunction applied to ${method}`); logger.log(`LoggedFunction applied to ${method}`);
LoggedFunction(target.prototype, method, { LoggedFunction(target.prototype, method, {
value: target.prototype[method], value: target.prototype[method],
}); });
all.forEach(([k, v]) => Reflect.defineMetadata(k, v, target.prototype[method]));
} }
}); });
(0, common_1.Injectable)(options)(target); (0, common_1.Injectable)(options)(target);
@ -61,15 +59,14 @@ function LoggedController(param) {
methods.forEach((method) => { methods.forEach((method) => {
if (method !== "constructor" && if (method !== "constructor" &&
typeof target.prototype[method] === "function") { typeof target.prototype[method] === "function") {
const path = Reflect.getMetadata("path", target.prototype[method]); if (verbose) {
const httpMethod = Reflect.getMetadata("method", target.prototype[method]); const path = Reflect.getMetadata("path", target.prototype[method]);
const all = Reflect.getMetadataKeys(target.prototype[method]).map((k) => [k, Reflect.getMetadata(k, target.prototype[method])]); const httpMethod = Reflect.getMetadata("method", target.prototype[method]);
if (verbose)
logger.log(`LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})`); logger.log(`LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})`);
}
LoggedRoute()(target.prototype, method, { LoggedRoute()(target.prototype, method, {
value: target.prototype[method], value: target.prototype[method],
}); });
all.forEach(([k, v]) => Reflect.defineMetadata(k, v, target.prototype[method]));
} }
}); });
(0, common_1.Controller)(param)(target); (0, common_1.Controller)(param)(target);
@ -156,6 +153,10 @@ function LoggedFunction(_target, key, descriptor) {
logger.warn(`LoggedFunction decorator applied to non-function property: ${key}`); logger.warn(`LoggedFunction decorator applied to non-function property: ${key}`);
return; return;
} }
const all = Reflect.getMetadataKeys(fn).map((k) => [
k,
Reflect.getMetadata(k, fn),
]);
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_2.scopedLogger, _target, key); const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_2.scopedLogger, _target, key);
const loggedParams = Reflect.getOwnMetadata(reflected_2.loggedParam, _target, key); const loggedParams = Reflect.getOwnMetadata(reflected_2.loggedParam, _target, key);
const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key); const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key);
@ -168,6 +169,10 @@ function LoggedFunction(_target, key, descriptor) {
}, key); }, key);
_target[key] = overrideFunction; _target[key] = overrideFunction;
descriptor.value = overrideFunction; descriptor.value = overrideFunction;
all.forEach(([k, v]) => {
Reflect.defineMetadata(k, v, _target[key]);
Reflect.defineMetadata(k, v, descriptor.value);
});
} }
exports.LoggedFunction = LoggedFunction; exports.LoggedFunction = LoggedFunction;
function LoggedRoute(route) { function LoggedRoute(route) {
@ -179,6 +184,10 @@ function LoggedRoute(route) {
logger.warn(`LoggedRoute decorator applied to non-function property: ${key}`); logger.warn(`LoggedRoute decorator applied to non-function property: ${key}`);
return; return;
} }
const all = Reflect.getMetadataKeys(fn).map((k) => [
k,
Reflect.getMetadata(k, fn),
]);
const httpPath = Reflect.getMetadata("path", fn); const httpPath = Reflect.getMetadata("path", fn);
const httpMethod = Reflect.getMetadata("method", fn); const httpMethod = Reflect.getMetadata("method", fn);
const fullRoute = `${_target.constructor.name}::${route ?? httpPath}[${RevRequestMethod[httpMethod]}]`; const fullRoute = `${_target.constructor.name}::${route ?? httpPath}[${RevRequestMethod[httpMethod]}]`;
@ -196,6 +205,10 @@ function LoggedRoute(route) {
}); });
_target[key] = overrideFunction; _target[key] = overrideFunction;
descriptor.value = overrideFunction; descriptor.value = overrideFunction;
all.forEach(([k, v]) => {
Reflect.defineMetadata(k, v, _target[key]);
Reflect.defineMetadata(k, v, descriptor.value);
});
}; };
} }
exports.LoggedRoute = LoggedRoute; exports.LoggedRoute = LoggedRoute;

2
dist/package.json vendored
View File

@ -1,6 +1,6 @@
{ {
"name": "nestlogged", "name": "nestlogged",
"version": "2.0.2", "version": "2.0.3",
"description": "A NestJS Logger Decorator Library", "description": "A NestJS Logger Decorator Library",
"main": "lib/index.js", "main": "lib/index.js",
"repository": "https://github.com/worplo/nestlogged", "repository": "https://github.com/worplo/nestlogged",

View File

@ -56,17 +56,11 @@ export function LoggedInjectable(
method !== "constructor" && method !== "constructor" &&
typeof target.prototype[method] === "function" typeof target.prototype[method] === "function"
) { ) {
const all = Reflect.getMetadataKeys(target.prototype[method]).map(
(k) => [k, Reflect.getMetadata(k, target.prototype[method])]
);
if (options && options.verbose) if (options && options.verbose)
logger.log(`LoggedFunction applied to ${method}`); logger.log(`LoggedFunction applied to ${method}`);
LoggedFunction(target.prototype, method, { LoggedFunction(target.prototype, method, {
value: target.prototype[method], value: target.prototype[method],
}); });
all.forEach(([k, v]) =>
Reflect.defineMetadata(k, v, target.prototype[method])
);
} }
}); });
@ -100,24 +94,19 @@ export function LoggedController(param?: any): (target: any) => void {
method !== "constructor" && method !== "constructor" &&
typeof target.prototype[method] === "function" typeof target.prototype[method] === "function"
) { ) {
const path = Reflect.getMetadata("path", target.prototype[method]); if (verbose) {
const httpMethod = Reflect.getMetadata( const path = Reflect.getMetadata("path", target.prototype[method]);
"method", const httpMethod = Reflect.getMetadata(
target.prototype[method] "method",
); target.prototype[method]
const all = Reflect.getMetadataKeys(target.prototype[method]).map( );
(k) => [k, Reflect.getMetadata(k, target.prototype[method])]
);
if (verbose)
logger.log( logger.log(
`LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})` `LoggedRoute applied to ${method} (${RevRequestMethod[httpMethod]} ${path})`
); );
}
LoggedRoute()(target.prototype, method, { LoggedRoute()(target.prototype, method, {
value: target.prototype[method], value: target.prototype[method],
}); });
all.forEach(([k, v]) =>
Reflect.defineMetadata(k, v, target.prototype[method])
);
} }
}); });
@ -263,6 +252,11 @@ export function LoggedFunction<F extends Array<any>, R>(
return; return;
} }
const all = Reflect.getMetadataKeys(fn).map((k) => [
k,
Reflect.getMetadata(k, fn),
]);
const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata( const scopedLoggerInjectableParam: number = Reflect.getOwnMetadata(
scopedLogger, scopedLogger,
_target, _target,
@ -297,6 +291,11 @@ export function LoggedFunction<F extends Array<any>, R>(
_target[key] = overrideFunction; _target[key] = overrideFunction;
descriptor.value = overrideFunction; descriptor.value = overrideFunction;
all.forEach(([k, v]) => {
Reflect.defineMetadata(k, v, _target[key]);
Reflect.defineMetadata(k, v, descriptor.value);
});
} }
export function LoggedRoute<F extends Array<any>, R>(route?: string) { export function LoggedRoute<F extends Array<any>, R>(route?: string) {
@ -318,6 +317,11 @@ export function LoggedRoute<F extends Array<any>, R>(route?: string) {
return; return;
} }
const all = Reflect.getMetadataKeys(fn).map((k) => [
k,
Reflect.getMetadata(k, fn),
]);
const httpPath: string = Reflect.getMetadata("path", fn); const httpPath: string = Reflect.getMetadata("path", fn);
const httpMethod: RequestMethod = Reflect.getMetadata("method", fn); const httpMethod: RequestMethod = Reflect.getMetadata("method", fn);
@ -362,5 +366,10 @@ export function LoggedRoute<F extends Array<any>, R>(route?: string) {
_target[key] = overrideFunction; _target[key] = overrideFunction;
descriptor.value = overrideFunction; descriptor.value = overrideFunction;
all.forEach(([k, v]) => {
Reflect.defineMetadata(k, v, _target[key]);
Reflect.defineMetadata(k, v, descriptor.value);
});
}; };
} }