feat: allow synchronous method & string name in Returns

This commit is contained in:
p-sw 2024-01-10 01:20:27 +09:00
parent 91796820fe
commit 14a19c9871
2 changed files with 16 additions and 12 deletions

View File

@ -126,7 +126,7 @@ function overrideBuild<F extends Array<any>, R>(
baseLogger: Logger, baseLogger: Logger,
metadatas: FunctionMetadata, metadatas: FunctionMetadata,
key: string, key: string,
returnsData: ReturnsReflectData[] | true, returnsData: ReturnsReflectData[] | string | true,
route?: { route?: {
fullRoute: string; fullRoute: string;
} }
@ -196,11 +196,13 @@ function overrideBuild<F extends Array<any>, R>(
.filter((v) => v.length > 0) .filter((v) => v.length > 0)
.join(", ") .join(", ")
: "" : ""
: returnsData : typeof returnsData === 'string'
? typeof r === "object" ? "WITH " + returnsData + "=" + typeof r === "object" ? JSON.stringify(r) : r
? "WITH " + JSON.stringify(r) : returnsData
: "WITH " + r ? typeof r === "object"
: ""; ? "WITH " + JSON.stringify(r)
: "WITH " + r
: "";
injectedLogger.log( injectedLogger.log(
route route

View File

@ -230,19 +230,21 @@ export function LoggedHeaders(property?: string): LoggedParamReturns {
export function Returns<F extends Array<any>, R>(namePaths?: { export function Returns<F extends Array<any>, R>(namePaths?: {
[name: string]: string; [name: string]: string;
}) { } | string) {
return ( return (
_target: any, _target: any,
_key: string | symbol, _key: string | symbol,
descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R>> descriptor: TypedPropertyDescriptor<(...args: F) => Promise<R> | R>
) => { ) => {
Reflect.defineMetadata( Reflect.defineMetadata(
returns, returns,
namePaths namePaths
? Object.entries(namePaths).reduce<ReturnsReflectData[]>( ? typeof namePaths === 'string'
(prev, curr) => [...prev, { name: curr[0], path: curr[1] }], ? namePaths
[] : Object.entries(namePaths).reduce<ReturnsReflectData[]>(
) (prev, curr) => [...prev, { name: curr[0], path: curr[1] }],
[]
)
: true, : true,
descriptor.value descriptor.value
); );