v3.0.0 build

This commit is contained in:
p-sw 2024-11-29 22:15:10 +09:00
parent 8cd91db037
commit 04e5b82c7d
7 changed files with 124 additions and 74 deletions

View File

@ -17,4 +17,4 @@ yarn add nestlogged
## More Info
[Github Wiki](https://github.com/Worplo/nestlogged/wiki)
[Wiki](https://nestlogged.worplo.com)

2
dist/README.md vendored
View File

@ -17,4 +17,4 @@ yarn add nestlogged
## More Info
[Github Wiki](https://github.com/Worplo/nestlogged/wiki)
[Wiki](https://nestlogged.worplo.com)

10
dist/lib/logged.d.ts vendored
View File

@ -7,5 +7,11 @@ export declare function LoggedController(prefix: string | string[]): (target: an
export declare function LoggedController(options: ControllerOptions & {
verbose?: boolean;
}): (target: any) => void;
export declare function LoggedFunction<F extends Array<any>, R>(_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R | Promise<R>>): void;
export declare function LoggedRoute<F extends Array<any>, R>(route?: string): (_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R>) => void;
interface OverrideBuildOptions {
skipCallLog: boolean;
skipReturnLog: boolean;
skipErrorLog: boolean;
}
export declare function LoggedFunction<F extends Array<any>, R>(options?: Partial<OverrideBuildOptions>): (_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R | Promise<R>>) => void;
export declare function LoggedRoute<F extends Array<any>, R>(route?: string, options?: Partial<OverrideBuildOptions>): (_target: any, key: string, descriptor: TypedPropertyDescriptor<(...args: F) => R>) => void;
export {};

64
dist/lib/logged.js vendored
View File

@ -4,7 +4,6 @@ exports.LoggedRoute = exports.LoggedFunction = exports.LoggedController = export
const common_1 = require("@nestjs/common");
const logger_1 = require("./logger");
const reflected_1 = require("./reflected");
const reflected_2 = require("./reflected");
const functions_1 = require("./functions");
const RevRequestMethod = [
"GET",
@ -39,7 +38,7 @@ function LoggedInjectable(options) {
typeof target.prototype[method] === "function") {
if (options && options.verbose)
logger.log(`LoggedFunction applied to ${method}`);
LoggedFunction(target.prototype, method, {
LoggedFunction()(target.prototype, method, {
value: target.prototype[method],
});
}
@ -73,7 +72,23 @@ function LoggedController(param) {
};
}
exports.LoggedController = LoggedController;
function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData, route) {
const defaultOverrideBuildOptions = {
skipCallLog: false,
skipReturnLog: false,
skipErrorLog: false,
};
class LoggedMetadata {
constructor(options) {
this.options = options ?? defaultOverrideBuildOptions;
}
updateOption(options) {
this.options = {
...this.options,
...options
};
}
}
function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData, logged, route) {
return function (...args) {
let injectedLogger = baseLogger;
if (typeof metadatas.scopedLoggerInjectableParam !== "undefined") {
@ -86,6 +101,7 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData
}
injectedLogger = args[metadatas.scopedLoggerInjectableParam];
}
if (!logged.options.skipCallLog) {
injectedLogger.log(`${route ? "HIT HTTP" : "CALL"} ${route ? `${route.fullRoute} (${key})` : key} ${metadatas.loggedParams && metadatas.loggedParams.length > 0
? "WITH " +
metadatas.loggedParams.map(({ name, index, include, exclude }) => name +
@ -95,8 +111,10 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData
exclude,
})).join(", ")
: ""}`);
}
try {
const r = originalFunction.call(this, ...args);
if (!logged.options.skipReturnLog) {
if (originalFunction.constructor.name === 'AsyncFunction' ||
(r && typeof r === 'object' && typeof r['then'] === 'function')) {
return r['then']((r) => {
@ -147,13 +165,20 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData
return r;
}
}
else {
return r;
}
}
catch (e) {
if (!logged.options.skipErrorLog) {
injectedLogger.error(`WHILE ${route ? `HTTP ${route.fullRoute} (${key})` : key} ERROR ${e}`);
}
throw e;
}
};
}
function LoggedFunction(_target, key, descriptor) {
function LoggedFunction(options) {
return (_target, key, descriptor) => {
loggerInit(_target);
const logger = _target.logger;
const fn = descriptor.value;
@ -161,28 +186,37 @@ function LoggedFunction(_target, key, descriptor) {
logger.warn(`LoggedFunction decorator applied to non-function property: ${key}`);
return;
}
const logMetadata = Reflect.getOwnMetadata(reflected_1.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 = Reflect.getOwnMetadata(reflected_2.scopedLogger, _target, key);
const loggedParams = Reflect.getOwnMetadata(reflected_2.loggedParam, _target, key);
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_1.scopedLogger, _target, key);
const loggedParams = Reflect.getOwnMetadata(reflected_1.loggedParam, _target, key);
const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key);
const returnsData = Reflect.getOwnMetadata(reflected_1.returns, fn);
const overrideFunction = overrideBuild(fn, logger, {
scopedLoggerInjectableParam,
loggedParams,
scopeKeys,
}, key, returnsData);
}, key, returnsData, newMetadata, undefined);
_target[key] = overrideFunction;
descriptor.value = overrideFunction;
Reflect.defineMetadata(reflected_1.nestLoggedMetadata, newMetadata, _target, key);
all.forEach(([k, v]) => {
Reflect.defineMetadata(k, v, _target[key]);
Reflect.defineMetadata(k, v, descriptor.value);
});
};
}
exports.LoggedFunction = LoggedFunction;
function LoggedRoute(route) {
function LoggedRoute(route, options) {
return (_target, key, descriptor) => {
loggerInit(_target);
const logger = _target.logger;
@ -191,6 +225,13 @@ function LoggedRoute(route) {
logger.warn(`LoggedRoute decorator applied to non-function property: ${key}`);
return;
}
const logMetadata = Reflect.getOwnMetadata(reflected_1.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),
@ -198,19 +239,20 @@ function LoggedRoute(route) {
const httpPath = Reflect.getMetadata("path", fn);
const httpMethod = Reflect.getMetadata("method", fn);
const fullRoute = `${_target.constructor.name}::${route ?? httpPath}[${RevRequestMethod[httpMethod]}]`;
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_2.scopedLogger, _target, key);
const loggedParams = Reflect.getOwnMetadata(reflected_2.loggedParam, _target, key);
const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_1.scopedLogger, _target, key);
const loggedParams = Reflect.getOwnMetadata(reflected_1.loggedParam, _target, key);
const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key);
const returnsData = Reflect.getOwnMetadata(reflected_1.returns, fn);
const overrideFunction = overrideBuild(fn, logger, {
scopedLoggerInjectableParam,
loggedParams,
scopeKeys,
}, key, returnsData, {
}, key, returnsData, newMetadata, {
fullRoute,
});
_target[key] = overrideFunction;
descriptor.value = overrideFunction;
Reflect.defineMetadata(reflected_1.nestLoggedMetadata, newMetadata, _target, key);
all.forEach(([k, v]) => {
Reflect.defineMetadata(k, v, _target[key]);
Reflect.defineMetadata(k, v, descriptor.value);

View File

@ -25,6 +25,7 @@ export declare const scopedLogger: unique symbol;
export declare const loggedParam: unique symbol;
export declare const scopeKey: unique symbol;
export declare const returns: unique symbol;
export declare const nestLoggedMetadata: unique symbol;
export declare function InjectLogger(target: any, propertyKey: string | symbol, parameterIndex: number): void;
type ParameterDecoratorType = (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
type LoggedParamReturns = (name: string, options?: IncludeExcludePath) => ParameterDecoratorType;

View File

@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Returns = exports.LoggedHeaders = exports.LoggedBody = exports.LoggedQuery = exports.LoggedParam = exports.Logged = exports.InjectLogger = exports.returns = exports.scopeKey = exports.loggedParam = exports.scopedLogger = void 0;
exports.Returns = exports.LoggedHeaders = exports.LoggedBody = exports.LoggedQuery = exports.LoggedParam = exports.Logged = exports.InjectLogger = exports.nestLoggedMetadata = exports.returns = exports.scopeKey = exports.loggedParam = exports.scopedLogger = void 0;
const route_paramtypes_enum_1 = require("@nestjs/common/enums/route-paramtypes.enum");
const common_1 = require("@nestjs/common");
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
@ -22,6 +22,7 @@ exports.scopedLogger = Symbol("nlogdec-scopedLogger");
exports.loggedParam = Symbol("nlogdec-loggedParam");
exports.scopeKey = Symbol("nlogdec-scopeKey");
exports.returns = Symbol("nlogdec-returns");
exports.nestLoggedMetadata = Symbol("nlogdec-metadata");
function InjectLogger(target, propertyKey, parameterIndex) {
Reflect.defineMetadata(exports.scopedLogger, parameterIndex, target, propertyKey);
}

2
dist/package.json vendored
View File

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