From f232300aedc9f622770873b728f654622d5034e1 Mon Sep 17 00:00:00 2001 From: p-sw Date: Wed, 10 Jan 2024 02:02:20 +0900 Subject: [PATCH] v2.2.3 --- dist/lib/functions.d.ts | 6 ++++ dist/lib/functions.js | 51 +++++++++++++++++++++++++-- dist/lib/logged.js | 78 ++++++++++++++++++++++++++++------------- 3 files changed, 108 insertions(+), 27 deletions(-) diff --git a/dist/lib/functions.d.ts b/dist/lib/functions.d.ts index 6e48a05..52e313d 100644 --- a/dist/lib/functions.d.ts +++ b/dist/lib/functions.d.ts @@ -1,7 +1,13 @@ export declare const notIncludedSymbol: unique symbol; export declare function includeOrExcludeObject(ocv: any, paths: string[], currentPath: string[], include: boolean): any; +export declare function includeOrExcludeObjectSync(ocv: any, paths: string[], currentPath: string[], include: boolean): any; export default function objectContainedLogged(ocv: any, options?: { include?: string[]; exclude: string[]; }): Promise; +export declare function objectContainedLoggedSync(ocv: any, options?: { + include?: string[]; + exclude: string[]; +}): string; export declare function getItemByPath(obj: object, path: string | string[]): any; +export declare function getItemByPathSync(obj: object, path: string | string[]): any; diff --git a/dist/lib/functions.js b/dist/lib/functions.js index 2d5ab48..51845d2 100644 --- a/dist/lib/functions.js +++ b/dist/lib/functions.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.getItemByPath = exports.includeOrExcludeObject = exports.notIncludedSymbol = void 0; +exports.getItemByPathSync = exports.getItemByPath = exports.objectContainedLoggedSync = exports.includeOrExcludeObjectSync = exports.includeOrExcludeObject = exports.notIncludedSymbol = void 0; exports.notIncludedSymbol = Symbol("notIncluded"); async function includeOrExcludeObject(ocv, paths, currentPath = [], include // or exclude ) { @@ -23,6 +23,27 @@ async function includeOrExcludeObject(ocv, paths, currentPath = [], include // o : ocv; } exports.includeOrExcludeObject = includeOrExcludeObject; +function includeOrExcludeObjectSync(ocv, paths, currentPath = [], include // or exclude +) { + if (Array.isArray(ocv)) { + return (ocv.map((v, i) => includeOrExcludeObjectSync(v, paths, [...currentPath, i.toString()], include))).filter((e) => e !== exports.notIncludedSymbol); + } + if (typeof ocv === "object") { + return Object.fromEntries(Object.entries(ocv).map(([key, value]) => [ + key, + includeOrExcludeObject(value, paths, [...currentPath, key], include), + ]).filter((e) => e[1] !== exports.notIncludedSymbol)); + } + const isIncluded = paths.includes(currentPath.join(".")); + return include + ? isIncluded // include mode, path is in list + ? ocv + : exports.notIncludedSymbol + : isIncluded // exclude mode, path is in list + ? exports.notIncludedSymbol + : ocv; +} +exports.includeOrExcludeObjectSync = includeOrExcludeObjectSync; async function objectContainedLogged(ocv, options) { if (options && typeof ocv === "object") { if (options.include && options.include.length > 0) { @@ -40,7 +61,33 @@ async function objectContainedLogged(ocv, options) { } } exports.default = objectContainedLogged; +function objectContainedLoggedSync(ocv, options) { + if (options && typeof ocv === "object") { + if (options.include && options.include.length > 0) { + return JSON.stringify(includeOrExcludeObjectSync(ocv, options.include, [], true)); + } + if (options.exclude && options.exclude.length > 0) { + return JSON.stringify(includeOrExcludeObjectSync(ocv, options.exclude, [], false)); + } + } + if (typeof ocv === "object") { + return JSON.stringify(ocv); + } + else { + return `${ocv}`; + } +} +exports.objectContainedLoggedSync = objectContainedLoggedSync; async function getItemByPath(obj, path) { + const paths = Array.isArray(path) ? path : path.split("."); + return Object.keys(obj).includes(paths[0]) + ? typeof obj[paths[0]] === "object" + ? await getItemByPath(obj[paths[0]], paths.slice(1)) + : obj[paths[0]] + : undefined; +} +exports.getItemByPath = getItemByPath; +function getItemByPathSync(obj, path) { const paths = Array.isArray(path) ? path : path.split("."); return Object.keys(obj).includes(paths[0]) ? typeof obj[paths[0]] === "object" @@ -48,4 +95,4 @@ async function getItemByPath(obj, path) { : obj[paths[0]] : undefined; } -exports.getItemByPath = getItemByPath; +exports.getItemByPathSync = getItemByPathSync; diff --git a/dist/lib/logged.js b/dist/lib/logged.js index af28533..0ebaec4 100644 --- a/dist/lib/logged.js +++ b/dist/lib/logged.js @@ -74,7 +74,7 @@ function LoggedController(param) { } exports.LoggedController = LoggedController; function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData, route) { - return async function (...args) { + return function (...args) { let injectedLogger = baseLogger; if (typeof metadatas.scopedLoggerInjectableParam !== "undefined") { if (args.length <= metadatas.scopedLoggerInjectableParam || @@ -88,36 +88,64 @@ function overrideBuild(originalFunction, baseLogger, metadatas, key, returnsData } injectedLogger.log(`${route ? "HIT HTTP" : "CALL"} ${route ? `${route.fullRoute} (${key})` : key} ${metadatas.loggedParams && metadatas.loggedParams.length > 0 ? "WITH " + - (await Promise.all(metadatas.loggedParams.map(async ({ name, index, include, exclude }) => name + + metadatas.loggedParams.map(({ name, index, include, exclude }) => name + "=" + - (await (0, functions_1.default)(args[index], { + (0, functions_1.objectContainedLoggedSync)(args[index], { include, exclude, - }))))).join(", ") + })).join(", ") : ""}`); try { - const r = await originalFunction.call(this, ...args); - const resultLogged = Array.isArray(returnsData) - ? typeof r === "object" - ? "WITH " + - (await Promise.all(returnsData.map(async ({ name, path }) => { - const value = await (0, functions_1.getItemByPath)(r, path); - return value !== undefined ? `${name}=${value}` : ""; - }))) - .filter((v) => v.length > 0) - .join(", ") - : "" - : typeof returnsData === 'string' - ? "WITH " + returnsData + "=" + typeof r === "object" ? JSON.stringify(r) : r - : returnsData + const r = originalFunction.call(this, ...args); + if (originalFunction.constructor.name === 'AsyncFunction' || + (typeof r === 'object' && typeof r['then'] === 'function')) { + return r['then']((r) => { + const resultLogged = Array.isArray(returnsData) ? typeof r === "object" - ? "WITH " + JSON.stringify(r) - : "WITH " + r - : ""; - injectedLogger.log(route - ? `RETURNED HTTP ${route.fullRoute} (${key}) ${resultLogged}` - : `RETURNED ${key} ${resultLogged}`); - return r; + ? "WITH " + + returnsData.map(({ name, path }) => { + const value = (0, functions_1.getItemByPathSync)(r, path); + return value !== undefined ? `${name}=${value}` : ""; + }) + .filter((v) => v.length > 0) + .join(", ") + : "" + : typeof returnsData === 'string' + ? "WITH " + returnsData + "=" + typeof r === "object" ? JSON.stringify(r) : r + : returnsData + ? typeof r === "object" + ? "WITH " + JSON.stringify(r) + : "WITH " + r + : ""; + injectedLogger.log(route + ? `RETURNED HTTP ${route.fullRoute} (${key}) ${resultLogged}` + : `RETURNED ${key} ${resultLogged}`); + return r; + }); + } + else { + const resultLogged = Array.isArray(returnsData) + ? typeof r === "object" + ? "WITH " + + returnsData.map(({ name, path }) => { + const value = (0, functions_1.getItemByPathSync)(r, path); + return value !== undefined ? `${name}=${value}` : ""; + }) + .filter((v) => v.length > 0) + .join(", ") + : "" + : typeof returnsData === 'string' + ? "WITH " + returnsData + "=" + typeof r === "object" ? JSON.stringify(r) : r + : returnsData + ? typeof r === "object" + ? "WITH " + JSON.stringify(r) + : "WITH " + r + : ""; + injectedLogger.log(route + ? `RETURNED HTTP ${route.fullRoute} (${key}) ${resultLogged}` + : `RETURNED ${key} ${resultLogged}`); + return r; + } } catch (e) { injectedLogger.error(`WHILE ${route ? `HTTP ${route.fullRoute} (${key})` : key} ERROR ${e}`);