From 14a19c98715431afc6c2c26a427ec4ce09fd8f3b Mon Sep 17 00:00:00 2001 From: p-sw Date: Wed, 10 Jan 2024 01:20:27 +0900 Subject: [PATCH] feat: allow synchronous method & string name in Returns --- src/logged.ts | 14 ++++++++------ src/reflected.ts | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/logged.ts b/src/logged.ts index 3f9f541..e4fc79b 100644 --- a/src/logged.ts +++ b/src/logged.ts @@ -126,7 +126,7 @@ function overrideBuild, R>( baseLogger: Logger, metadatas: FunctionMetadata, key: string, - returnsData: ReturnsReflectData[] | true, + returnsData: ReturnsReflectData[] | string | true, route?: { fullRoute: string; } @@ -196,11 +196,13 @@ function overrideBuild, R>( .filter((v) => v.length > 0) .join(", ") : "" - : returnsData - ? typeof r === "object" - ? "WITH " + JSON.stringify(r) - : "WITH " + r - : ""; + : 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 diff --git a/src/reflected.ts b/src/reflected.ts index 9daa727..8b3216d 100644 --- a/src/reflected.ts +++ b/src/reflected.ts @@ -230,19 +230,21 @@ export function LoggedHeaders(property?: string): LoggedParamReturns { export function Returns, R>(namePaths?: { [name: string]: string; -}) { +} | string) { return ( _target: any, _key: string | symbol, - descriptor: TypedPropertyDescriptor<(...args: F) => Promise> + descriptor: TypedPropertyDescriptor<(...args: F) => Promise | R> ) => { Reflect.defineMetadata( returns, namePaths - ? Object.entries(namePaths).reduce( - (prev, curr) => [...prev, { name: curr[0], path: curr[1] }], - [] - ) + ? typeof namePaths === 'string' + ? namePaths + : Object.entries(namePaths).reduce( + (prev, curr) => [...prev, { name: curr[0], path: curr[1] }], + [] + ) : true, descriptor.value );