带有箭头功能的打字稿装饰器

问题描述 投票:0回答:1

我必须在此实现中实现箭头功能,并且需要将来自AWS的输入转换为自定义模型,以避免对每个API进行相同的逻辑。我想到了使用装饰器来完成每个功能。由于编译器将其视为属性,因此不会找到描述符属性并抛出异常。是否有解决方法,可以诱骗编译器将箭头函数识别为实际函数并传递描述符?

 @API(EndpointType.POST)
  public login = async (event: Input): Promise<APIGatewayProxyResult> => {
     ... logic
  }

   export function API(apiType:EndpointType): any {
      return (target: any, propertyKey: string, descriptor?: TypedPropertyDescriptor<any>) => {
         descriptor.value = function(request) {
             ... logic
             const bindedOriginalFunction = originalFunction.bind(this)
             const result = bindedOriginalFunction(finalResult);
             return result
         }
       return descriptor;
   }
typescript javascript-decorators
1个回答
0
投票

必须是箭头功能吗?

class Foo {
  async login(event: Input): Promise<APIGatewayProxyResult> { ... }
}

您的情况下的箭头函数不是方法(对象拥有的函数)。您拥有一个拥有由他人拥有的功能(箭头功能)的属性。

© www.soinside.com 2019 - 2024. All rights reserved.