diff --git a/dist/lib/index.d.ts b/dist/lib/index.d.ts index f37fdf8..9163e61 100644 --- a/dist/lib/index.d.ts +++ b/dist/lib/index.d.ts @@ -1,3 +1,3 @@ export { LoggedRoute, LoggedFunction, LoggedController, LoggedInjectable, } from "./logged"; export { ScopedLogger } from "./logger"; -export { InjectLogger, LoggedParam, ScopeKey, ShouldScoped, Returns } from "./reflected"; +export { InjectLogger, LoggedParam, LoggedHeaders, LoggedBody, LoggedQuery, Returns } from "./reflected"; diff --git a/dist/lib/index.js b/dist/lib/index.js index 9bb8e1e..8395f47 100644 --- a/dist/lib/index.js +++ b/dist/lib/index.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.Returns = exports.ShouldScoped = exports.ScopeKey = exports.LoggedParam = exports.InjectLogger = exports.ScopedLogger = exports.LoggedInjectable = exports.LoggedController = exports.LoggedFunction = exports.LoggedRoute = void 0; +exports.Returns = exports.LoggedQuery = exports.LoggedBody = exports.LoggedHeaders = exports.LoggedParam = exports.InjectLogger = exports.ScopedLogger = exports.LoggedInjectable = exports.LoggedController = exports.LoggedFunction = exports.LoggedRoute = void 0; var logged_1 = require("./logged"); Object.defineProperty(exports, "LoggedRoute", { enumerable: true, get: function () { return logged_1.LoggedRoute; } }); Object.defineProperty(exports, "LoggedFunction", { enumerable: true, get: function () { return logged_1.LoggedFunction; } }); @@ -11,6 +11,7 @@ Object.defineProperty(exports, "ScopedLogger", { enumerable: true, get: function var reflected_1 = require("./reflected"); Object.defineProperty(exports, "InjectLogger", { enumerable: true, get: function () { return reflected_1.InjectLogger; } }); Object.defineProperty(exports, "LoggedParam", { enumerable: true, get: function () { return reflected_1.LoggedParam; } }); -Object.defineProperty(exports, "ScopeKey", { enumerable: true, get: function () { return reflected_1.ScopeKey; } }); -Object.defineProperty(exports, "ShouldScoped", { enumerable: true, get: function () { return reflected_1.ShouldScoped; } }); +Object.defineProperty(exports, "LoggedHeaders", { enumerable: true, get: function () { return reflected_1.LoggedHeaders; } }); +Object.defineProperty(exports, "LoggedBody", { enumerable: true, get: function () { return reflected_1.LoggedBody; } }); +Object.defineProperty(exports, "LoggedQuery", { enumerable: true, get: function () { return reflected_1.LoggedQuery; } }); Object.defineProperty(exports, "Returns", { enumerable: true, get: function () { return reflected_1.Returns; } }); diff --git a/dist/lib/logged.js b/dist/lib/logged.js index 6df15b8..2486ae7 100644 --- a/dist/lib/logged.js +++ b/dist/lib/logged.js @@ -79,49 +79,12 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData if (typeof metadatas.scopedLoggerInjectableParam !== "undefined") { if (args.length <= metadatas.scopedLoggerInjectableParam || !(args[metadatas.scopedLoggerInjectableParam] instanceof logger_1.ScopedLogger)) { - args[metadatas.scopedLoggerInjectableParam] = new logger_1.ScopedLogger(baseLogger, key, true); + args[metadatas.scopedLoggerInjectableParam] = new logger_1.ScopedLogger(baseLogger, key, true, true); } else { args[metadatas.scopedLoggerInjectableParam] = new logger_1.ScopedLogger(args[metadatas.scopedLoggerInjectableParam], key, false); } injectedLogger = args[metadatas.scopedLoggerInjectableParam]; - if (Array.isArray(metadatas.scopeKeys)) { - const scopeKeyResults = metadatas.scopeKeys.map((key) => { - const argsValue = args[key.index]; - if (!key.path) { - if (!metadatas.shouldScoped || argsValue) { - return { error: false, value: `${key.name}=${argsValue}` }; - } - else { - return { - error: true, - value: `ScopeKey in ShouldScope cannot be falsy value (${argsValue})`, - }; - } - } - try { - const reduceResult = key.path.reduce((base, keyPath) => { - if (typeof base !== "object" || - !Object.keys(base).includes(keyPath)) - throw new Error(`Cannot find key ${keyPath} in ${typeof base === "object" ? JSON.stringify(base) : base}`); - return base[keyPath]; - }, argsValue); - return { error: false, value: `${key.name}=${reduceResult}` }; - } - catch (e) { - return { error: true, value: e.message }; - } - }); - const successResults = scopeKeyResults.filter((v) => v.error === false); - if (successResults.length === 0) { - if (metadatas.shouldScoped) { - scopeKeyResults.forEach((v) => injectedLogger.warn(v.value)); - } - } - else { - injectedLogger.addScope(successResults[0].value); - } - } } injectedLogger.log(`${route ? "HIT HTTP" : "CALL"} ${route ? `${route.fullRoute} (${key})` : key} ${metadatas.loggedParams && metadatas.loggedParams.length > 0 ? "WITH " + @@ -175,13 +138,11 @@ function LoggedFunction(_target, key, descriptor) { const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_2.scopedLogger, _target, key); const loggedParams = Reflect.getOwnMetadata(reflected_2.loggedParam, _target, key); const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key); - const shouldScoped = Reflect.getOwnMetadata(reflected_1.forceScopeKey, fn); const returnsData = Reflect.getOwnMetadata(reflected_1.returns, fn); const overrideFunction = overrideBuild(fn, logger, { scopedLoggerInjectableParam, loggedParams, scopeKeys, - shouldScoped, }, key, returnsData); _target[key] = overrideFunction; descriptor.value = overrideFunction; @@ -210,13 +171,11 @@ function LoggedRoute(route) { const scopedLoggerInjectableParam = Reflect.getOwnMetadata(reflected_2.scopedLogger, _target, key); const loggedParams = Reflect.getOwnMetadata(reflected_2.loggedParam, _target, key); const scopeKeys = Reflect.getOwnMetadata(reflected_1.scopeKey, _target, key); - const shouldScoped = Reflect.getOwnMetadata(reflected_1.forceScopeKey, fn); const returnsData = Reflect.getOwnMetadata(reflected_1.returns, fn); const overrideFunction = overrideBuild(fn, logger, { scopedLoggerInjectableParam, loggedParams, scopeKeys, - shouldScoped, }, key, returnsData, { fullRoute, }); diff --git a/dist/lib/logger.d.ts b/dist/lib/logger.d.ts index 9c1048d..9b7dad3 100644 --- a/dist/lib/logger.d.ts +++ b/dist/lib/logger.d.ts @@ -3,9 +3,9 @@ export declare class ScopedLogger extends Logger { private logger; private scope; private root; - scopeId?: string; - constructor(logger: Logger, scope: string, root?: boolean); - addScope(scopeId: string): void; + private createScopeId; + private readonly scopeId?; + constructor(logger: Logger, scope: string, root?: boolean, createScopeId?: boolean); private scopedLog; debug: (message: string) => void; log: (message: string) => void; diff --git a/dist/lib/logger.js b/dist/lib/logger.js index ae45aee..dab56e3 100644 --- a/dist/lib/logger.js +++ b/dist/lib/logger.js @@ -2,21 +2,23 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.ScopedLogger = void 0; const common_1 = require("@nestjs/common"); +const hyperid = require("hyperid"); +const createId = hyperid({ fixedLength: true }); class ScopedLogger extends common_1.Logger { - constructor(logger, scope, root = false) { + constructor(logger, scope, root = false, createScopeId = false) { super(); this.logger = logger; this.scope = scope; this.root = root; + this.createScopeId = createScopeId; this.debug = this.scopedLog("debug"); this.log = this.scopedLog("log"); this.warn = this.scopedLog("warn"); this.verbose = this.scopedLog("verbose"); this.error = this.scopedLog("error"); this.fatal = this.scopedLog("fatal"); - } - addScope(scopeId) { - this.scopeId = scopeId; + if (this.createScopeId) + this.scopeId = createId(); } scopedLog(method) { return (message) => { diff --git a/dist/lib/reflected.d.ts b/dist/lib/reflected.d.ts index 779a1a7..4fece1a 100644 --- a/dist/lib/reflected.d.ts +++ b/dist/lib/reflected.d.ts @@ -1,3 +1,4 @@ +import { PipeTransform, Type } from "@nestjs/common"; export type Path = string | string[]; export type Paths = Path[]; export interface IncludeExcludePath { @@ -23,15 +24,23 @@ export interface ReturnsReflectData { export declare const scopedLogger: unique symbol; export declare const loggedParam: unique symbol; export declare const scopeKey: unique symbol; -export declare const forceScopeKey: unique symbol; export declare const returns: unique symbol; export declare function InjectLogger(target: any, propertyKey: string | symbol, parameterIndex: number): void; -export declare function LoggedParam(name: string, options?: IncludeExcludePath): (target: any, propertyKey: string | symbol, parameterIndex: number) => void; -export declare function ScopeKey(name: string, options?: { - path?: Path; - priority?: number; -}): (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; +export declare const Logged: LoggedParamReturns; +type Pipe = Type | PipeTransform; +export declare function LoggedParam(): LoggedParamReturns; +export declare function LoggedParam(...pipes: Pipe[]): LoggedParamReturns; +export declare function LoggedParam(property: string, ...pipes: Pipe[]): LoggedParamReturns; +export declare function LoggedQuery(): LoggedParamReturns; +export declare function LoggedQuery(...pipes: Pipe[]): LoggedParamReturns; +export declare function LoggedQuery(property: string, ...pipes: Pipe[]): LoggedParamReturns; +export declare function LoggedBody(): LoggedParamReturns; +export declare function LoggedBody(...pipes: Pipe[]): LoggedParamReturns; +export declare function LoggedBody(property: string, ...pipes: Pipe[]): LoggedParamReturns; +export declare function LoggedHeaders(property?: string): LoggedParamReturns; export declare function Returns, R>(namePaths?: { [name: string]: string; }): (_target: any, _key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: F) => Promise>) => void; -export declare function ShouldScoped(_target: any, _key: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise>): void; +export {}; diff --git a/dist/lib/reflected.js b/dist/lib/reflected.js index 59601ff..6d768fa 100644 --- a/dist/lib/reflected.js +++ b/dist/lib/reflected.js @@ -1,16 +1,32 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.ShouldScoped = exports.Returns = exports.ScopeKey = exports.LoggedParam = exports.InjectLogger = exports.returns = exports.forceScopeKey = exports.scopeKey = exports.loggedParam = exports.scopedLogger = void 0; +exports.Returns = exports.LoggedHeaders = exports.LoggedBody = exports.LoggedQuery = exports.LoggedParam = exports.Logged = exports.InjectLogger = 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"); +const ROUTE_ARGS_METADATA = '__routeArguments__'; +function createRouteParamDecorator(paramtype) { + return (data) => (target, key, index) => { + const args = Reflect.getMetadata(ROUTE_ARGS_METADATA, target.constructor, key) || {}; + Reflect.defineMetadata(ROUTE_ARGS_METADATA, (0, common_1.assignMetadata)(args, paramtype, index, data), target.constructor, key); + }; +} +const createPipesRouteParamDecorator = (paramtype) => (data, ...pipes) => (target, key, index) => { + const args = Reflect.getMetadata(ROUTE_ARGS_METADATA, target.constructor, key) || {}; + const hasParamData = (0, shared_utils_1.isNil)(data) || (0, shared_utils_1.isString)(data); + const paramData = hasParamData ? data : undefined; + const paramPipes = hasParamData ? pipes : [data, ...pipes]; + Reflect.defineMetadata(ROUTE_ARGS_METADATA, (0, common_1.assignMetadata)(args, paramtype, index, paramData, ...paramPipes), target.constructor, key); +}; exports.scopedLogger = Symbol("nlogdec-scopedLogger"); exports.loggedParam = Symbol("nlogdec-loggedParam"); exports.scopeKey = Symbol("nlogdec-scopeKey"); -exports.forceScopeKey = Symbol("nlogdec-forceScopeKey"); exports.returns = Symbol("nlogdec-returns"); function InjectLogger(target, propertyKey, parameterIndex) { Reflect.defineMetadata(exports.scopedLogger, parameterIndex, target, propertyKey); } exports.InjectLogger = InjectLogger; -function LoggedParam(name, options) { +function createLoggedFunctionParam(name, options) { return (target, propertyKey, parameterIndex) => { const existingLoggedParams = Reflect.getOwnMetadata(exports.loggedParam, target, propertyKey) || []; existingLoggedParams.push({ @@ -27,23 +43,44 @@ function LoggedParam(name, options) { Reflect.defineMetadata(exports.loggedParam, existingLoggedParams, target, propertyKey); }; } -exports.LoggedParam = LoggedParam; -function ScopeKey(name, options) { - return (target, propertyKey, parameterIndex) => { - const existingScopeKeys = Reflect.getOwnMetadata(exports.scopeKey, target, propertyKey) || []; - existingScopeKeys.push({ - name, - index: parameterIndex, - path: Array.isArray(options?.path) - ? options.path - : options?.path?.split("."), - priority: options?.priority, - }); - existingScopeKeys.sort((a, b) => (b.priority ?? 1) - (a.priority ?? 1)); - Reflect.defineMetadata(exports.scopeKey, existingScopeKeys, target, propertyKey); +const Logged = (name, options) => createLoggedFunctionParam(name, options); +exports.Logged = Logged; +function LoggedParam(property, ...pipes) { + return (name, options) => { + return (target, propertyKey, parameterIndex) => { + createPipesRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.PARAM)(property, ...pipes)(target, propertyKey, parameterIndex); + createLoggedFunctionParam(name, options)(target, propertyKey, parameterIndex); + }; }; } -exports.ScopeKey = ScopeKey; +exports.LoggedParam = LoggedParam; +function LoggedQuery(property, ...pipes) { + return (name, options) => { + return (target, propertyKey, parameterIndex) => { + createPipesRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.QUERY)(property, ...pipes)(target, propertyKey, parameterIndex); + createLoggedFunctionParam(name, options)(target, propertyKey, parameterIndex); + }; + }; +} +exports.LoggedQuery = LoggedQuery; +function LoggedBody(property, ...pipes) { + return (name, options) => { + return (target, propertyKey, parameterIndex) => { + createPipesRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.BODY)(property, ...pipes)(target, propertyKey, parameterIndex); + createLoggedFunctionParam(name, options)(target, propertyKey, parameterIndex); + }; + }; +} +exports.LoggedBody = LoggedBody; +function LoggedHeaders(property) { + return (name, options) => { + return (target, propertyKey, parameterIndex) => { + createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.HEADERS)(property)(target, propertyKey, parameterIndex); + createLoggedFunctionParam(name, options)(target, propertyKey, parameterIndex); + }; + }; +} +exports.LoggedHeaders = LoggedHeaders; function Returns(namePaths) { return (_target, _key, descriptor) => { Reflect.defineMetadata(exports.returns, namePaths @@ -52,7 +89,3 @@ function Returns(namePaths) { }; } exports.Returns = Returns; -function ShouldScoped(_target, _key, descriptor) { - Reflect.defineMetadata(exports.forceScopeKey, true, descriptor.value); -} -exports.ShouldScoped = ShouldScoped;