我必须在此实现中实现箭头功能,并且需要将来自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;
}
答案 0 :(得分:0)
它必须是箭头功能吗?
class Foo {
async login(event: Input): Promise<APIGatewayProxyResult> { ... }
}
在您的情况下,箭头函数不是方法(对象拥有的函数)。您拥有一个拥有由他人拥有的功能(箭头功能)的属性。