feat: 2.2.0

This commit is contained in:
p-sw 2024-01-10 00:54:02 +09:00
parent 2229475dbf
commit 4ccafe0a16
7 changed files with 86 additions and 82 deletions

2
dist/lib/index.d.ts vendored
View File

@ -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";

7
dist/lib/index.js vendored
View File

@ -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; } });

43
dist/lib/logged.js vendored
View File

@ -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,
});

View File

@ -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;

10
dist/lib/logger.js vendored
View File

@ -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) => {

View File

@ -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> | 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<F extends Array<any>, R>(namePaths?: {
[name: string]: string;
}): (_target: any, _key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>>) => void;
export declare function ShouldScoped(_target: any, _key: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>): void;
export {};

77
dist/lib/reflected.js vendored
View File

@ -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;