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,
metadatas: FunctionMetadata,
key: string,
returnsData: ReturnsReflectData[] | true,
returnsData: ReturnsReflectData[] | string | true,
route?: {
fullRoute: string;
}
@ -196,6 +196,8 @@ function overrideBuild<F extends Array<any>, R>(
.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)

View File

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