diff --git a/packages/sdk/src/common/decorators/cache.ts b/packages/sdk/src/common/decorators/cache.ts index 781235e2..ca441d71 100644 --- a/packages/sdk/src/common/decorators/cache.ts +++ b/packages/sdk/src/common/decorators/cache.ts @@ -1,12 +1,20 @@ +import { isBigint } from '../utils/index.js'; + import { callConsoleMessage } from './utils.js'; -const serializeArgs = (...args: any[]) => - args.map((arg: any) => JSON.stringify(arg)).join(':'); +const serializeArgs = (args: any[]) => + args + .map((arg: any) => + JSON.stringify(arg, (_key, value) => { + return isBigint(value) ? value.toString() : value; + }), + ) + .join(':'); const getDecoratorArgsString = function (this: This, args?: string[]) { if (!args) return ''; - const argsString = args.map((arg) => { + const argsStringArr = args.map((arg) => { const field = arg .split('.') .reduce((a, b) => (a as { [key: string]: any })[b], this); @@ -14,7 +22,7 @@ const getDecoratorArgsString = function (this: This, args?: string[]) { return arg && typeof field === 'function' ? field.call(this) : field; }); - return serializeArgs(argsString); + return serializeArgs(argsStringArr); }; export const Cache = function (timeMs = 0, cacheArgs?: string[]) { @@ -60,7 +68,12 @@ export const Cache = function (timeMs = 0, cacheArgs?: string[]) { `Cache for method '${methodName}' set.`, ); const result = originalMethod.call(this, ...args); - cache.set(cacheKey, { data: result, timestamp: Date.now() }); + if (result instanceof Promise) { + result.then((resolvedResult) => + cache.set(cacheKey, { data: resolvedResult, timestamp: Date.now() }), + ); + } else cache.set(cacheKey, { data: result, timestamp: Date.now() }); + return result; }; return replacementMethod;