style: prettified

This commit is contained in:
Shinwoo PARK 2023-12-03 18:46:49 +09:00
parent a8f7cb4f95
commit 9f29fb28c2
4 changed files with 131 additions and 134 deletions

View File

@ -1,76 +1,76 @@
export const notIncludedSymbol = Symbol('notIncluded') export const notIncludedSymbol = Symbol("notIncluded");
export async function includeOrExcludeObject( export async function includeOrExcludeObject(
ocv: any, ocv: any,
paths: string[], paths: string[],
currentPath: string[] = [], currentPath: string[] = [],
include: boolean, // or exclude include: boolean // or exclude
) { ) {
if (Array.isArray(ocv)) { if (Array.isArray(ocv)) {
return ( return (
await Promise.all( await Promise.all(
ocv.map( ocv.map(
async (v, i) => async (v, i) =>
await includeOrExcludeObject( await includeOrExcludeObject(
v, v,
paths, paths,
[...currentPath, i.toString()], [...currentPath, i.toString()],
include, include
),
),
) )
).filter((e) => e !== notIncludedSymbol); )
} )
).filter((e) => e !== notIncludedSymbol);
}
if (typeof ocv === 'object') { if (typeof ocv === "object") {
return Object.fromEntries( return Object.fromEntries(
( (
await Promise.all( await Promise.all(
Object.entries(ocv).map(async ([key, value]) => [ Object.entries(ocv).map(async ([key, value]) => [
key, key,
await includeOrExcludeObject( await includeOrExcludeObject(
value, value,
paths, paths,
[...currentPath, key], [...currentPath, key],
include, include
), ),
]), ])
) )
).filter((e) => e[1] !== notIncludedSymbol), ).filter((e) => e[1] !== notIncludedSymbol)
); );
} }
const isIncluded = paths.includes(currentPath.join('.')); const isIncluded = paths.includes(currentPath.join("."));
return include return include
? isIncluded // include mode, path is in list ? isIncluded // include mode, path is in list
? ocv ? ocv
: notIncludedSymbol : notIncludedSymbol
: isIncluded // exclude mode, path is in list : isIncluded // exclude mode, path is in list
? notIncludedSymbol ? notIncludedSymbol
: ocv : ocv;
} }
export default async function objectContainedLogged( export default async function objectContainedLogged(
ocv: any, ocv: any,
options?: {include?: string[]; exclude: string[]}, options?: { include?: string[]; exclude: string[] }
): Promise<string> { ): Promise<string> {
if (options && typeof ocv === 'object') { if (options && typeof ocv === "object") {
if (options.include && options.include.length > 0) { if (options.include && options.include.length > 0) {
return JSON.stringify( return JSON.stringify(
await includeOrExcludeObject(ocv, options.include, [], true), await includeOrExcludeObject(ocv, options.include, [], true)
); );
}
if (options.exclude && options.exclude.length > 0) {
return JSON.stringify(
await includeOrExcludeObject(ocv, options.exclude, [], false),
);
}
} }
if (options.exclude && options.exclude.length > 0) {
return JSON.stringify(
await includeOrExcludeObject(ocv, options.exclude, [], false)
);
}
}
if (typeof ocv === 'object') { if (typeof ocv === "object") {
return JSON.stringify(ocv); return JSON.stringify(ocv);
} else { } else {
return `${ocv}`; return `${ocv}`;
} }
} }

View File

@ -1,3 +1,3 @@
export {LoggedRoute, LoggedFunction} from './logged'; export { LoggedRoute, LoggedFunction } from "./logged";
export {ScopedLogger} from './logger'; export { ScopedLogger } from "./logger";
export {LoggedParamReflectData, InjectLogger, LoggedParam} from './reflected'; export { LoggedParamReflectData, InjectLogger, LoggedParam } from "./reflected";

View File

@ -1,30 +1,28 @@
import {Logger} from "@nestjs/common"; import { Logger } from "@nestjs/common";
type LogLevel = 'debug' | 'log' | 'warn' | 'verbose' | 'error' | 'fatal'; type LogLevel = "debug" | "log" | "warn" | "verbose" | "error" | "fatal";
export class ScopedLogger extends Logger { export class ScopedLogger extends Logger {
constructor( constructor(
private logger: Logger, private logger: Logger,
private scope: string, private scope: string,
private scopeId?: string, private scopeId?: string
) { ) {
super(); super();
} }
private scopedLog(method: LogLevel) { private scopedLog(method: LogLevel) {
return (message: string) => { return (message: string) => {
this.logger[method]( this.logger[method](
`-> ${this.scope}${ `-> ${this.scope}${this.scopeId ? `(${this.scopeId})` : ""}: ${message}`
this.scopeId ? `(${this.scopeId})` : '' );
}: ${message}`, };
); }
};
}
debug = this.scopedLog('debug'); debug = this.scopedLog("debug");
log = this.scopedLog('log'); log = this.scopedLog("log");
warn = this.scopedLog('warn'); warn = this.scopedLog("warn");
verbose = this.scopedLog('verbose'); verbose = this.scopedLog("verbose");
error = this.scopedLog('error'); error = this.scopedLog("error");
fatal = this.scopedLog('fatal'); fatal = this.scopedLog("fatal");
} }

View File

@ -1,55 +1,54 @@
export interface LoggedParamReflectData { export interface LoggedParamReflectData {
name: string; name: string;
index: number; index: number;
include?: string[]; include?: string[];
exclude?: string[]; exclude?: string[];
} }
export const scopedLogger = Symbol('scopedLogger'); export const scopedLogger = Symbol("scopedLogger");
export const loggedParam = Symbol('loggedParam'); export const loggedParam = Symbol("loggedParam");
export function InjectLogger( export function InjectLogger(
target: any, target: any,
propertyKey: string | symbol, propertyKey: string | symbol,
parameterIndex: number, parameterIndex: number
) { ) {
Reflect.defineMetadata(scopedLogger, parameterIndex, target, propertyKey); Reflect.defineMetadata(scopedLogger, parameterIndex, target, propertyKey);
} }
export function LoggedParam( export function LoggedParam(
name: string, name: string,
options?: { options?: {
includePath?: (string | string[])[]; includePath?: (string | string[])[];
excludePath?: (string | string[])[]; excludePath?: (string | string[])[];
}, }
) { ) {
return ( return (
target: any, target: any,
propertyKey: string | symbol, propertyKey: string | symbol,
parameterIndex: number, parameterIndex: number
) => { ) => {
const existingLoggedParams: LoggedParamReflectData[] = const existingLoggedParams: LoggedParamReflectData[] =
Reflect.getOwnMetadata(loggedParam, target, propertyKey) || []; Reflect.getOwnMetadata(loggedParam, target, propertyKey) || [];
existingLoggedParams.push({ existingLoggedParams.push({
name, name,
index: parameterIndex, index: parameterIndex,
include: include:
options && options &&
options.includePath && options.includePath &&
options.includePath.map((v) => (Array.isArray(v) ? v.join('.') : v)), options.includePath.map((v) => (Array.isArray(v) ? v.join(".") : v)),
exclude: exclude:
options && options &&
options.excludePath && options.excludePath &&
options.excludePath.map((v) => (Array.isArray(v) ? v.join('.') : v)), options.excludePath.map((v) => (Array.isArray(v) ? v.join(".") : v)),
}); });
Reflect.defineMetadata( Reflect.defineMetadata(
loggedParam, loggedParam,
existingLoggedParams, existingLoggedParams,
target, target,
propertyKey propertyKey
); );
}; };
} }